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 pathstorageDir.go
73 lines (62 loc) · 1.78 KB
/
storageDir.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
package gcp
import (
"context"
"net/http"
monitoring "cloud.google.com/go/monitoring/apiv3"
"cloud.google.com/go/storage"
"github.com/puppetlabs/wash/activity"
"github.com/puppetlabs/wash/plugin"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
)
type storageProjectClient struct {
*storage.Client
metrics *monitoring.MetricClient
projectID string
}
type storageDir struct {
plugin.EntryBase
storageProjectClient
}
const storageScope = storage.ScopeReadOnly
func newStorageDir(ctx context.Context, client *http.Client, projID string) (*storageDir, error) {
clientContext := context.Background()
cli, err := storage.NewClient(clientContext, option.WithHTTPClient(client))
if err != nil {
return nil, err
}
metrics, err := monitoring.NewMetricClient(clientContext)
if err != nil {
activity.Record(ctx, "Unable to create metrics client for %v/storage: %v", projID, err)
}
s := &storageDir{
EntryBase: plugin.NewEntry("storage"),
storageProjectClient: storageProjectClient{Client: cli, metrics: metrics, projectID: projID},
}
if _, err := plugin.List(ctx, s); err != nil {
s.MarkInaccessible(ctx, err)
}
return s, nil
}
// List all storage buckets as dirs.
func (s *storageDir) List(ctx context.Context) ([]plugin.Entry, error) {
var entries []plugin.Entry
it := s.Buckets(ctx, s.projectID)
for {
bucketAttrs, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
return nil, err
}
entries = append(entries, newStorageBucket(s.storageProjectClient, bucketAttrs))
}
return entries, nil
}
func (s *storageDir) Schema() *plugin.EntrySchema {
return plugin.NewEntrySchema(s, "storage").IsSingleton()
}
func (s *storageDir) ChildSchemas() []*plugin.EntrySchema {
return []*plugin.EntrySchema{(&storageBucket{}).Schema()}
}