/
func.go
94 lines (80 loc) · 2.44 KB
/
func.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package request
import (
"errors"
"fmt"
"github.com/tidwall/gjson"
)
func (r *DefaultHttpRequest) ListManagedZones(project string) ([]string, error) {
var zones []string
r.Path = fmt.Sprintf("/dns/v1/projects/%s/managedZones", project)
body, err := r.DoGetRequest()
if err != nil {
return zones, err
}
items := gjson.Get(string(body), "managedZones").Array()
if len(items) == 0 {
err = errors.New(gjson.Get(string(body), "error.status").String())
}
for _, i := range items {
zones = append(zones, i.Get("name").String())
}
return zones, err
}
func (r *DefaultHttpRequest) ListRRSets(project, zone string) ([]gjson.Result, error) {
r.Path = fmt.Sprintf("/dns/v1/projects/%s/managedZones/%s/rrsets", project, zone)
body, err := r.DoGetRequest()
if err != nil {
return nil, err
}
items := gjson.Get(string(body), "rrsets").Array()
if len(items) == 0 {
err = errors.New(gjson.Get(string(body), "error.status").String())
}
return items, err
}
func (r *DefaultHttpRequest) ListZones(project string) ([]string, error) {
var zones []string
r.Path = fmt.Sprintf("/compute/v1/projects/%s/zones", project)
body, err := r.DoGetRequest()
if err != nil {
return zones, err
}
items := gjson.Get(string(body), "items").Array()
if len(items) == 0 {
err = errors.New(gjson.Get(string(body), "error.message").String())
}
for _, i := range items {
zones = append(zones, i.Get("name").String())
}
return zones, err
}
// https://cloud.google.com/compute/docs/reference/rest/v1/instances/list
func (r *DefaultHttpRequest) ListInstances(project, zone string) ([]gjson.Result, error) {
r.Path = fmt.Sprintf("/compute/v1/projects/%s/zones/%s/instances", project, zone)
body, err := r.DoGetRequest()
if err != nil {
return nil, err
}
items := gjson.Get(string(body), "items").Array()
if len(items) == 0 {
err = errors.New(gjson.Get(string(body), "error.status").String())
}
return items, err
}
func (r *DefaultHttpRequest) ListServiceAccounts(project string) (map[string]string, error) {
accounts := make(map[string]string)
r.Path = fmt.Sprintf("/v1/projects/%s/serviceAccounts", project)
body, err := r.DoGetRequest()
if err != nil {
return accounts, err
}
items := gjson.Get(string(body), "accounts").Array()
if len(items) == 0 {
err = errors.New(gjson.Get(string(body), "error.status").String())
}
for _, i := range items {
name := i.Get("displayName").String()
accounts[name] = i.Get("uniqueId").String()
}
return accounts, err
}