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 pathcloudFunctionsDir.go
61 lines (53 loc) · 1.66 KB
/
cloudFunctionsDir.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
package gcp
import (
"context"
"fmt"
"net/http"
"github.com/puppetlabs/wash/plugin"
"google.golang.org/api/cloudfunctions/v1"
"google.golang.org/api/option"
)
type cloudFunctionsProjectService struct {
*cloudfunctions.Service
projectID string
// We need to pass this around to access cloud function logs
client *http.Client
}
type cloudFunctionsDir struct {
plugin.EntryBase
service cloudFunctionsProjectService
}
func newCloudFunctionsDir(ctx context.Context, client *http.Client, projID string) (*cloudFunctionsDir, error) {
svc, err := cloudfunctions.NewService(context.Background(), option.WithHTTPClient(client))
if err != nil {
return nil, err
}
cf := &cloudFunctionsDir{
EntryBase: plugin.NewEntry("cloud_functions"),
service: cloudFunctionsProjectService{Service: svc, projectID: projID, client: client},
}
if _, err := plugin.List(ctx, cf); err != nil {
cf.MarkInaccessible(ctx, err)
}
return cf, nil
}
func (cf *cloudFunctionsDir) List(ctx context.Context) ([]plugin.Entry, error) {
var entries []plugin.Entry
functionsReq := cf.service.Projects.Locations.Functions.List(fmt.Sprintf("projects/%v/locations/-", cf.service.projectID))
err := functionsReq.Pages(ctx, func(resp *cloudfunctions.ListFunctionsResponse) error {
for _, function := range resp.Functions {
entries = append(entries, newCloudFunction(function, cf.service))
}
return nil
})
return entries, err
}
func (cf *cloudFunctionsDir) Schema() *plugin.EntrySchema {
return plugin.NewEntrySchema(cf, "cloud_functions").
IsSingleton()
}
func (cf *cloudFunctionsDir) ChildSchemas() []*plugin.EntrySchema {
return []*plugin.EntrySchema{
(&cloudFunction{}).Schema(),
}
}