Untitled

mail@pastecode.io avatar
unknown
golang
3 years ago
4.0 kB
4
Indexable
Never
package main

import (
	"context"
	"fmt"
	"log"

	"google.golang.org/api/compute/v1"
)

func main() {
	ctx := context.Background()
	computeService, err := compute.NewService(ctx)
	if err != nil {
		log.Fatalf("failed to create compute client: %s", err)
	}

	f := false
	startupScript := "echo \"up\""

	properties := &compute.InstanceProperties{
		// MachineType: "projects/cg-vraycloud/zones/us-central1-c/machineTypes/e2-medium",
		MachineType: "/n1-highmem-2",
		// MachineType: "n1-standard-1",
		Disks: []*compute.AttachedDisk{
			{
				AutoDelete: true,
				Boot:       true,
				DeviceName: "disk-1",
				Type:       "PERSISTENT",
				Mode:       "READ_WRITE",
				InitializeParams: &compute.AttachedDiskInitializeParams{
					SourceImage: "projects/debian-cloud/global/images/debian-10-buster-v20220317",
					DiskType:    "projects/cg-vraycloud/zones/europe-west4-a/diskTypes/pd-balanced",
					DiskSizeGb:  10,
					Labels:      map[string]string{},
				},
			},
		},
		CanIpForward: false,
		NetworkInterfaces: []*compute.NetworkInterface{
			{
				Network: fmt.Sprintf("projects/%s/global/networks/default", project),
				AccessConfigs: []*compute.AccessConfig{
					{
						Name: "External NAT",
						Type: "ONE_TO_ONE_NAT",
					},
				},
			},
		},
		Labels: map[string]string{},
		ServiceAccounts: []*compute.ServiceAccount{
			{
				Email:  "58614676228-compute@developer.gserviceaccount.com",
				Scopes: []string{"https://www.googleapis.com/auth/cloud-platform"},
			},
		},
		Scheduling: &compute.Scheduling{
			Preemptible:       true,
			OnHostMaintenance: "TERMINATE",
			AutomaticRestart:  &f,
		},
		Metadata: &compute.Metadata{
			Items: []*compute.MetadataItems{
				{
					Key:   "startup-script",
					Value: &startupScript,
				},
			},
		},
	}

	pproperties := map[string]compute.BulkInsertInstanceResourcePerInstanceProperties{
		"one": {},
		"two": {},
	}

	bresource := &compute.BulkInsertInstanceResource{
		Count:                 2,
		MinCount:              1,
		InstanceProperties:    properties,
		PerInstanceProperties: pproperties,
	}
	op, err := computeService.RegionInstances.BulkInsert(project, region, bresource).Do()
	// instance := &compute.Instance{
	// 	Name:        "foo",
	// 	MachineType: "projects/cg-vraycloud/zones/us-central1-c/machineTypes/e2-medium",
	// 	Disks: []*compute.AttachedDisk{
	// 		{
	// 			AutoDelete: true,
	// 			Boot:       true,
	// 			DeviceName: "disk-1",
	// 			Type:       "PERSISTENT",
	// 			Mode:       "READ_WRITE",
	// 			InitializeParams: &compute.AttachedDiskInitializeParams{
	// 				SourceImage: "projects/debian-cloud/global/images/debian-10-buster-v20220317",
	// 				DiskType:    "projects/cg-vraycloud/zones/europe-west4-a/diskTypes/pd-balanced",
	// 				DiskSizeGb:  10,
	// 				Labels:      map[string]string{},
	// 			},
	// 		},
	// 	},
	// 	CanIpForward: false,
	// 	NetworkInterfaces: []*compute.NetworkInterface{
	// 		{
	// 			Network: fmt.Sprintf("projects/%s/global/networks/default", project),
	// 			AccessConfigs: []*compute.AccessConfig{
	// 				{
	// 					Name: "External NAT",
	// 					Type: "ONE_TO_ONE_NAT",
	// 				},
	// 			},
	// 		},
	// 	},
	// 	Labels: map[string]string{},
	// 	ServiceAccounts: []*compute.ServiceAccount{
	// 		{
	// 			Email:  "58614676228-compute@developer.gserviceaccount.com",
	// 			Scopes: []string{"https://www.googleapis.com/auth/cloud-platform"},
	// 		},
	// 	},
	// 	Scheduling: &compute.Scheduling{
	// 		Preemptible:       true,
	// 		OnHostMaintenance: "TERMINATE",
	// 		AutomaticRestart:  &f,
	// 	},
	// 	Metadata: &compute.Metadata{
	// 		Items: []*compute.MetadataItems{
	// 			{
	// 				Key:   "startup-script",
	// 				Value: &startupScript,
	// 			},
	// 		},
	// 	},
	// }

	// op, err := computeService.Instances.Insert(project, region+"-c", instance).Do()
	if err != nil {
		log.Fatalf("failed to insert: %s", err)
	}
	fmt.Println(op.Id)
	fmt.Println(op.OperationGroupId)
}

var project = "cg-vraycloud"
var region = "us-central1"