drivers/google/compute_util.go
func (c *ComputeUtil) createInstance(d *Driver) error {
	log.Infof("Creating instance")

	var net string
	if strings.Contains(d.Network, "/networks/") {
		net = d.Network
	} else {
		net = c.globalURL + "/networks/" + d.Network
	}

	instance := &raw.Instance{
		Name:        c.instanceName,
		Description: "docker host vm",
		MachineType: c.zoneURL + "/machineTypes/" + d.MachineType,
		Disks: []*raw.AttachedDisk{
			{
				Boot:       true,
				AutoDelete: true,
				Type:       "PERSISTENT",
				Mode:       "READ_WRITE",
			},
		},
		NetworkInterfaces: []*raw.NetworkInterface{
			{
				Network: net,
			},
		},
		Tags: &raw.Tags{
			Items: parseTags(d),
		},
		ServiceAccounts: []*raw.ServiceAccount{
			{
				Email:  d.ServiceAccount,
				Scopes: strings.Split(d.Scopes, ","),
			},
		},
		Scheduling: &raw.Scheduling{
			Preemptible: c.preemptible,
		},
	}

	if strings.Contains(c.subnetwork, "/subnetworks/") {
		instance.NetworkInterfaces[0].Subnetwork = c.subnetwork
	} else if c.subnetwork != "" {
		instance.NetworkInterfaces[0].Subnetwork = "projects/" + c.project + "/regions/" + c.region() + "/subnetworks/" + c.subnetwork
	}

	if !c.useInternalIPOnly {
		cfg := &raw.AccessConfig{
			Type: "ONE_TO_ONE_NAT",
		}
		instance.NetworkInterfaces[0].AccessConfigs = append(instance.NetworkInterfaces[0].AccessConfigs, cfg)
	}

	if c.address != "" {
		staticAddress, err := c.staticAddress()
		if err != nil {
			return err
		}

		instance.NetworkInterfaces[0].AccessConfigs[0].NatIP = staticAddress
	}

	disk, err := c.disk()
	if disk == nil || err != nil {
		instance.Disks[0].InitializeParams = &raw.AttachedDiskInitializeParams{
			DiskName:    c.diskName(),
			SourceImage: "https://www.googleapis.com/compute/v1/projects/" + d.MachineImage,
			// The maximum supported disk size is 1000GB, the cast should be fine.
			DiskSizeGb: int64(d.DiskSize),
			DiskType:   c.diskType(),
		}
	} else {
		instance.Disks[0].Source = c.zoneURL + "/disks/" + c.instanceName + "-disk"
	}
	op, err := c.service.Instances.Insert(c.project, c.zone, instance).Do()

	if err != nil {
		return err
	}

	log.Infof("Waiting for Instance")
	if err = c.waitForRegionalOp(op.Name); err != nil {
		return err
	}

	instance, err = c.instance()
	if err != nil {
		return err
	}

	return c.uploadSSHKey(instance, d.GetSSHKeyPath())
}
func (c *ComputeUtil) createInstance(d *Driver) error {
	log.Infof("Creating instance")

	var net string
	if strings.Contains(d.Network, "/networks/") {
		net = d.Network
	} else {
		net = c.globalURL + "/networks/" + d.Network
	}

	instance := &raw.Instance{
		Name:        c.instanceName,
		Description: "docker host vm",
		MachineType: c.zoneURL + "/machineTypes/" + d.MachineType,
		Disks: []*raw.AttachedDisk{
			{
				Boot:       true,
				AutoDelete: true,
				Type:       "PERSISTENT",
				Mode:       "READ_WRITE",
			},
		},
		NetworkInterfaces: []*raw.NetworkInterface{
			{
				Network: net,
			},
		},
		Tags: &raw.Tags{
			Items: parseTags(d),
		},
		ServiceAccounts: []*raw.ServiceAccount{
			{
				Email:  d.ServiceAccount,
				Scopes: strings.Split(d.Scopes, ","),
			},
		},
		Scheduling: &raw.Scheduling{
			Preemptible: c.preemptible,
		},
	}

	if strings.Contains(c.subnetwork, "/subnetworks/") {
		instance.NetworkInterfaces[0].Subnetwork = c.subnetwork
	} else if c.subnetwork != "" {
		instance.NetworkInterfaces[0].Subnetwork = "projects/" + c.project + "/regions/" + c.region() + "/subnetworks/" + c.subnetwork
	}

	if !c.useInternalIPOnly {
		cfg := &raw.AccessConfig{
			Type: "ONE_TO_ONE_NAT",
		}
		instance.NetworkInterfaces[0].AccessConfigs = append(instance.NetworkInterfaces[0].AccessConfigs, cfg)
	}

	if c.address != "" {
		staticAddress, err := c.staticAddress()
		if err != nil {
			return err
		}

		instance.NetworkInterfaces[0].AccessConfigs[0].NatIP = staticAddress
	}

	disk, err := c.disk()
	if disk == nil || err != nil {
		instance.Disks[0].InitializeParams = &raw.AttachedDiskInitializeParams{
			DiskName:    c.diskName(),
			SourceImage: "https://www.googleapis.com/compute/v1/projects/" + d.MachineImage,
			// The maximum supported disk size is 1000GB, the cast should be fine.
			DiskSizeGb: int64(d.DiskSize),
			DiskType:   c.diskType(),
		}
	} else {
		instance.Disks[0].Source = c.zoneURL + "/disks/" + c.instanceName + "-disk"
	}
	op, err := c.service.Instances.Insert(c.project, c.zone, instance).Do()

	if err != nil {
		return err
	}

	log.Infof("Waiting for Instance")
	if err = c.waitForRegionalOp(op.Name); err != nil {
		return err
	}

	instance, err = c.instance()
	if err != nil {
		return err
	}

	return c.uploadSSHKey(instance, d.GetSSHKeyPath())
}
func ( c * ComputeUtil ) createInstance ( d * Driver ) error { log . Infof ( " " ) var net string if strings . Contains ( d . Network , " " ) { net = d . Network } else { net = c . globalURL + " " + d . Network } instance := & raw . Instance { Name : c . instanceName , Description : " " , MachineType : c . zoneURL + " " + d . MachineType , Disks : [ ] * raw . AttachedDisk { { Boot : true , AutoDelete : true , Type : " " , Mode : " " , } , } , NetworkInterfaces : [ ] * raw . NetworkInterface { { Network : net , } , } , Tags : & raw . Tags { Items : parseTags ( d ) , } , ServiceAccounts : [ ] * raw . ServiceAccount { { Email : d . ServiceAccount , Scopes : strings . Split ( d . Scopes , " " ) , } , } , Scheduling : & raw . Scheduling { Preemptible : c . preemptible , } , } if strings . Contains ( c . subnetwork , " " ) { instance . NetworkInterfaces [ 0 ] . Subnetwork = c . subnetwork } else if c . subnetwork != " " { instance . NetworkInterfaces [ 0 ] . Subnetwork = " " + c . project + " " + c . region ( ) + " " + c . subnetwork } if ! c . useInternalIPOnly { cfg := & raw . AccessConfig { Type : " " , } instance . NetworkInterfaces [ 0 ] . AccessConfigs = append ( instance . NetworkInterfaces [ 0 ] . AccessConfigs , cfg ) } if c . address != " " { staticAddress , err := c . staticAddress ( ) if err != nil { return err } instance . NetworkInterfaces [ 0 ] . AccessConfigs [ 0 ] . NatIP = staticAddress } disk , err := c . disk ( ) if disk == nil || err != nil { instance . Disks [ 0 ] . InitializeParams = & raw . AttachedDiskInitializeParams { DiskName : c . diskName ( ) , SourceImage : " " + d . MachineImage , // The maximum supported disk size is 1000GB, the cast should be fine. DiskSizeGb : int64 ( d . DiskSize ) , DiskType : c . diskType ( ) , } } else { instance . Disks [ 0 ] . Source = c . zoneURL + " " + c . instanceName + " " } op , err := c . service . Instances . Insert ( c . project , c . zone , instance ) . Do ( ) if err != nil { return err } log . Infof ( " " ) if err = c . waitForRegionalOp ( op . Name ) ; err != nil { return err } instance , err = c . instance ( ) if err != nil { return err } return c . uploadSSHKey ( instance , d . GetSSHKeyPath ( ) ) }
// createInstance creates a GCE VM instance.
createInstance creates a GCE VM instance .