You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

108 lines
2.9 KiB

  1. // Copyright 2017 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "fmt"
  17. "log"
  18. "net/http"
  19. "os"
  20. "strings"
  21. compute "google.golang.org/api/compute/v1"
  22. "google.golang.org/api/googleapi"
  23. )
  24. func init() {
  25. scopes := strings.Join([]string{
  26. compute.DevstorageFullControlScope,
  27. compute.ComputeScope,
  28. }, " ")
  29. registerDemo("compute", scopes, computeMain)
  30. }
  31. func computeMain(client *http.Client, argv []string) {
  32. if len(argv) != 2 {
  33. fmt.Fprintln(os.Stderr, "Usage: compute project_id instance_name (to start an instance)")
  34. return
  35. }
  36. service, err := compute.New(client)
  37. if err != nil {
  38. log.Fatalf("Unable to create Compute service: %v", err)
  39. }
  40. projectID := argv[0]
  41. instanceName := argv[1]
  42. prefix := "https://www.googleapis.com/compute/v1/projects/" + projectID
  43. imageURL := "https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-7-wheezy-v20140606"
  44. zone := "us-central1-a"
  45. // Show the current images that are available.
  46. res, err := service.Images.List(projectID).Do()
  47. log.Printf("Got compute.Images.List, err: %#v, %v", res, err)
  48. instance := &compute.Instance{
  49. Name: instanceName,
  50. Description: "compute sample instance",
  51. MachineType: prefix + "/zones/" + zone + "/machineTypes/n1-standard-1",
  52. Disks: []*compute.AttachedDisk{
  53. {
  54. AutoDelete: true,
  55. Boot: true,
  56. Type: "PERSISTENT",
  57. InitializeParams: &compute.AttachedDiskInitializeParams{
  58. DiskName: "my-root-pd",
  59. SourceImage: imageURL,
  60. },
  61. },
  62. },
  63. NetworkInterfaces: []*compute.NetworkInterface{
  64. {
  65. AccessConfigs: []*compute.AccessConfig{
  66. {
  67. Type: "ONE_TO_ONE_NAT",
  68. Name: "External NAT",
  69. },
  70. },
  71. Network: prefix + "/global/networks/default",
  72. },
  73. },
  74. ServiceAccounts: []*compute.ServiceAccount{
  75. {
  76. Email: "default",
  77. Scopes: []string{
  78. compute.DevstorageFullControlScope,
  79. compute.ComputeScope,
  80. },
  81. },
  82. },
  83. }
  84. op, err := service.Instances.Insert(projectID, zone, instance).Do()
  85. log.Printf("Got compute.Operation, err: %#v, %v", op, err)
  86. etag := op.Header.Get("Etag")
  87. log.Printf("Etag=%v", etag)
  88. inst, err := service.Instances.Get(projectID, zone, instanceName).IfNoneMatch(etag).Do()
  89. log.Printf("Got compute.Instance, err: %#v, %v", inst, err)
  90. if googleapi.IsNotModified(err) {
  91. log.Printf("Instance not modified since insert.")
  92. } else {
  93. log.Printf("Instance modified since insert.")
  94. }
  95. }