Skip to content

Instantly share code, notes, and snippets.

@stgleb
Created November 5, 2018 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stgleb/4656b16313ddc56b4770c71ae6a58285 to your computer and use it in GitHub Desktop.
Save stgleb/4656b16313ddc56b4770c71ae6a58285 to your computer and use it in GitHub Desktop.
// Copyright 2017 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"log"
"google.golang.org/api/compute/v1"
"google.golang.org/api/googleapi"
"golang.org/x/oauth2/jwt"
)
func main() {
projectId := ""
instanceName := "test"
clientScopes := []string{
"https://www.googleapis.com/auth/compute",
"https://www.googleapis.com/auth/cloud-platform",
"https://www.googleapis.com/auth/ndev.clouddns.readwrite",
"https://www.googleapis.com/auth/devstorage.full_control",
}
conf := jwt.Config{
Email: "",
PrivateKey: []byte(``),
Scopes: clientScopes,
TokenURL: "https://accounts.google.com/o/oauth2/token",
}
client := conf.Client(context.Background())
computeService, err := compute.New(client)
if err != nil {
log.Fatal(err)
}
prefix := "https://www.googleapis.com/compute/v1/projects/" + projectId
imageURL := "https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-7-wheezy-v20140606"
zone := "us-central1-a"
// Show the current images that are available.
res, err := computeService.Images.List(projectId).Do()
log.Printf("Got compute.Images.List, err: %#v, %v", res, err)
instance := &compute.Instance{
Name: instanceName,
Description: "compute sample instance",
MachineType: prefix + "/zones/" + zone + "/machineTypes/n1-standard-1",
Disks: []*compute.AttachedDisk{
{
AutoDelete: true,
Boot: true,
Type: "PERSISTENT",
InitializeParams: &compute.AttachedDiskInitializeParams{
DiskName: "my-root-pd",
SourceImage: imageURL,
},
},
},
NetworkInterfaces: []*compute.NetworkInterface{
{
AccessConfigs: []*compute.AccessConfig{
{
Type: "ONE_TO_ONE_NAT",
Name: "External NAT",
},
},
Network: prefix + "/global/networks/default",
},
},
ServiceAccounts: []*compute.ServiceAccount{
{
Email: "default",
Scopes: []string{
compute.DevstorageFullControlScope,
compute.ComputeScope,
},
},
},
}
op, err := computeService.Instances.Insert(projectId, zone, instance).Do()
log.Printf("Got compute.Operation, err: %#v, %v", op, err)
if err != nil {
log.Fatal(err)
}
etag := op.Header.Get("Etag")
log.Printf("Etag=%v", etag)
inst, err := computeService.Instances.Get(projectId, zone, instanceName).IfNoneMatch(etag).Do()
log.Printf("Got compute.Instance, err: %#v, %v", inst, err)
if googleapi.IsNotModified(err) {
log.Printf("Instance not modified since insert.")
} else {
log.Printf("Instance modified since insert.")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment