~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/google.golang.org/api/examples/compute.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package main
 
2
 
 
3
import (
 
4
        "fmt"
 
5
        "log"
 
6
        "net/http"
 
7
        "os"
 
8
        "strings"
 
9
 
 
10
        compute "google.golang.org/api/compute/v1"
 
11
)
 
12
 
 
13
func init() {
 
14
        scopes := strings.Join([]string{
 
15
                compute.DevstorageFull_controlScope,
 
16
                compute.ComputeScope,
 
17
        }, " ")
 
18
        registerDemo("compute", scopes, computeMain)
 
19
}
 
20
 
 
21
func computeMain(client *http.Client, argv []string) {
 
22
        if len(argv) != 2 {
 
23
                fmt.Fprintln(os.Stderr, "Usage: compute project_id instance_name (to start an instance)")
 
24
                return
 
25
        }
 
26
 
 
27
        service, _ := compute.New(client)
 
28
        projectId := argv[0]
 
29
        instanceName := argv[1]
 
30
 
 
31
        prefix := "https://www.googleapis.com/compute/v1/projects/" + projectId
 
32
        imageURL := "https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-7-wheezy-v20140606"
 
33
        zone := "us-central1-a"
 
34
 
 
35
        // Show the current images that are available.
 
36
        res, err := service.Images.List(projectId).Do()
 
37
        log.Printf("Got compute.Images.List, err: %#v, %v", res, err)
 
38
 
 
39
        instance := &compute.Instance{
 
40
                Name:        instanceName,
 
41
                Description: "compute sample instance",
 
42
                MachineType: prefix + "/zones/" + zone + "/machineTypes/n1-standard-1",
 
43
                Disks: []*compute.AttachedDisk{
 
44
                        {
 
45
                                AutoDelete: true,
 
46
                                Boot:       true,
 
47
                                Type:       "PERSISTENT",
 
48
                                InitializeParams: &compute.AttachedDiskInitializeParams{
 
49
                                        DiskName:    "my-root-pd",
 
50
                                        SourceImage: imageURL,
 
51
                                },
 
52
                        },
 
53
                },
 
54
                NetworkInterfaces: []*compute.NetworkInterface{
 
55
                        &compute.NetworkInterface{
 
56
                                AccessConfigs: []*compute.AccessConfig{
 
57
                                        &compute.AccessConfig{
 
58
                                                Type: "ONE_TO_ONE_NAT",
 
59
                                                Name: "External NAT",
 
60
                                        },
 
61
                                },
 
62
                                Network: prefix + "/global/networks/default",
 
63
                        },
 
64
                },
 
65
                ServiceAccounts: []*compute.ServiceAccount{
 
66
                        {
 
67
                                Email: "default",
 
68
                                Scopes: []string{
 
69
                                        compute.DevstorageFull_controlScope,
 
70
                                        compute.ComputeScope,
 
71
                                },
 
72
                        },
 
73
                },
 
74
        }
 
75
 
 
76
        op, err := service.Instances.Insert(projectId, zone, instance).Do()
 
77
        log.Printf("Got compute.Operation, err: %#v, %v", op, err)
 
78
}