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 pathcloudFunction.go
62 lines (54 loc) · 1.64 KB
/
cloudFunction.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
package gcp
import (
"context"
"fmt"
"strings"
"time"
"github.com/puppetlabs/wash/plugin"
"google.golang.org/api/cloudfunctions/v1"
)
type cloudFunction struct {
plugin.EntryBase
service cloudFunctionsProjectService
region string
}
func newCloudFunction(function *cloudfunctions.CloudFunction, service cloudFunctionsProjectService) *cloudFunction {
// functionPath is formatted as projects/<project_id>/locations/<region>/functions/<function_name>
functionPath := function.Name
segments := strings.Split(functionPath, "/")
region, functionName := segments[3], segments[5]
cf := &cloudFunction{
EntryBase: plugin.NewEntry(functionName),
service: service,
region: region,
}
mtime, err := time.Parse(time.RFC3339, function.UpdateTime)
if err != nil {
panic(fmt.Sprintf("Timestamp for %v was not expected format RFC3339: %v", cf, function.UpdateTime))
}
cf.
SetPartialMetadata(function).
Attributes().
SetMtime(mtime)
return cf
}
func (cf *cloudFunction) List(ctx context.Context) ([]plugin.Entry, error) {
cfl, err := newCloudFunctionLog(ctx, cf.service, cf.region, cf.Name())
if err != nil {
return nil, err
}
return []plugin.Entry{cfl}, nil
}
func (cf *cloudFunction) Delete(ctx context.Context) (bool, error) {
_, err := cf.service.Projects.Locations.Functions.Delete(cf.Name()).Context(ctx).Do()
return false, err
}
func (cf *cloudFunction) Schema() *plugin.EntrySchema {
return plugin.NewEntrySchema(cf, "cloud_function").
SetPartialMetadataSchema(cloudfunctions.CloudFunction{})
}
func (cf *cloudFunction) ChildSchemas() []*plugin.EntrySchema {
return []*plugin.EntrySchema{
(&cloudFunctionLog{}).Schema(),
}
}