-
Notifications
You must be signed in to change notification settings - Fork 2
/
gcp.go
68 lines (55 loc) · 1.6 KB
/
gcp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// my-go-examples cloud-services/google-gcp
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"golang.org/x/net/context"
"google.golang.org/api/compute/v1"
"google.golang.org/api/option"
)
func main() {
project := "jeffs-project-174816"
jsonPath := "/home/jeff/.config/gcloud/jeffs-service-account.json"
zone := "us-west1-a"
ctx := context.Background()
// Reads credentials from the specified path.
computeService, err := compute.NewService(ctx, option.WithCredentialsFile(jsonPath))
if err != nil {
log.Fatal(err)
}
// Use the Service to get gce image list
fmt.Printf("\n\n**** Use the Service to get gce image list *****************************\n\n")
req := computeService.Images.List(project)
if err := req.Pages(ctx, func(page *compute.ImageList) error {
for _, image := range page.Items {
// TODO: Change code below to process each `image` resource:
b, err := json.MarshalIndent(image, "", " ")
if err != nil {
log.Fatal(err)
}
os.Stdout.Write(b)
}
return nil
}); err != nil {
log.Fatal(err)
}
fmt.Println("----------------")
// Use the service to get gce instances list
fmt.Printf("\n\n**** Use the service to get gce instances list *****************************\n\n")
req2 := computeService.Instances.List(project, zone)
if err := req2.Pages(ctx, func(page *compute.InstanceList) error {
for _, image := range page.Items {
// TODO: Change code below to process each `image` resource:
b, err := json.MarshalIndent(image, "", " ")
if err != nil {
log.Fatal(err)
}
os.Stdout.Write(b)
}
return nil
}); err != nil {
log.Fatal(err)
}
}