This repository has been archived by the owner on Jun 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathcomputeDir.go
72 lines (61 loc) · 1.84 KB
/
computeDir.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
package gcp
import (
"context"
"net/http"
"github.com/puppetlabs/wash/plugin"
"google.golang.org/api/compute/v1"
"google.golang.org/api/option"
)
type computeProjectService struct {
*compute.Service
projectID string
}
type computeDir struct {
plugin.EntryBase
service computeProjectService
}
var _ = plugin.Parent(&computeDir{})
const computeScope = compute.CloudPlatformScope
func newComputeDir(ctx context.Context, client *http.Client, projID string) (*computeDir, error) {
svc, err := compute.NewService(context.Background(), option.WithHTTPClient(client))
if err != nil {
return nil, err
}
c := &computeDir{
EntryBase: plugin.NewEntry("compute"),
service: computeProjectService{Service: svc, projectID: projID},
}
if _, err := plugin.List(ctx, c); err != nil {
c.MarkInaccessible(ctx, err)
}
return c, nil
}
// List all services as dirs.
func (c *computeDir) List(ctx context.Context) ([]plugin.Entry, error) {
var entries []plugin.Entry
instReq := c.service.Instances.AggregatedList(c.service.projectID)
err := instReq.Pages(ctx, func(instancePage *compute.InstanceAggregatedList) error {
for _, zone := range instancePage.Items {
for _, instance := range zone.Instances {
inst := newComputeInstance(instance, c.service)
entries = append(entries, inst)
}
}
return nil
})
return entries, err
}
func (c *computeDir) Metadata(ctx context.Context) (plugin.JSONObject, error) {
// Return project metadata specific to Google Compute.
proj, err := c.service.Projects.Get(c.service.projectID).Context(ctx).Do()
if err != nil {
return nil, err
}
return plugin.ToJSONObject(proj), nil
}
func (c *computeDir) Schema() *plugin.EntrySchema {
return plugin.NewEntrySchema(c, "compute").IsSingleton()
}
func (c *computeDir) ChildSchemas() []*plugin.EntrySchema {
return []*plugin.EntrySchema{(&computeInstance{}).Schema()}
}