-
Notifications
You must be signed in to change notification settings - Fork 0
/
firestore.go
154 lines (138 loc) · 4.38 KB
/
firestore.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package persistence
import (
"context"
"log"
"sync"
"cloud.google.com/go/firestore"
firebase "firebase.google.com/go"
"github.com/cheld/miniprow/pkg/boskos/common"
"github.com/cheld/miniprow/pkg/common/core"
"github.com/cheld/miniprow/pkg/common/util"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
)
type store struct {
lock sync.RWMutex
client *firestore.Client
ctx context.Context
}
// NewResourceMemoryStorage creates an in memory persistence layer
func NewFirestore() Persistence {
// Use a service account
ctx := context.Background()
var app *firebase.App
var err error
if util.FileExists("../firestore.json") {
sa := option.WithCredentialsFile("../firestore.json")
app, err = firebase.NewApp(ctx, nil, sa)
} else {
conf := &firebase.Config{ProjectID: "smart-altar-272110"}
app, err = firebase.NewApp(ctx, conf)
}
if err != nil {
log.Fatalln(err)
}
client, err := app.Firestore(ctx)
if err != nil {
log.Fatalln(err)
}
store := &store{
client: client,
ctx: ctx,
}
return store
}
func (s *store) Close() {
s.client.Close()
}
func (s *store) Add(r common.Resource, tenant core.Tenant) error {
baseQuery := s.client.Collection("organizations").Doc(tenant.Organization).Collection("projects").Doc(tenant.Project)
_, err := baseQuery.Collection("resources").Doc(r.Name).Set(s.ctx, r)
if err != nil {
log.Fatalf("Failed adding resource to firestore: %v", err)
}
return nil
}
func (s *store) Delete(name string, tenant core.Tenant) error {
baseQuery := s.client.Collection("organizations").Doc(tenant.Organization).Collection("projects").Doc(tenant.Project)
_, err := baseQuery.Collection("resources").Doc(name).Delete(s.ctx)
if err != nil {
log.Fatalf("Failed deleting resource from firestore: %v", err)
}
return nil
}
func (s *store) Update(r common.Resource, tenant core.Tenant) (common.Resource, error) {
baseQuery := s.client.Collection("organizations").Doc(tenant.Organization).Collection("projects").Doc(tenant.Project)
_, err := baseQuery.Collection("resources").Doc(r.Name).Set(s.ctx, r)
if err != nil {
log.Fatalf("Failed updating resource to firestore: %v", err)
}
return r, nil
}
func (s *store) Get(name string, tenant core.Tenant) (common.Resource, error) {
baseQuery := s.client.Collection("organizations").Doc(tenant.Organization).Collection("projects").Doc(tenant.Project)
dsnap, err := baseQuery.Collection("resources").Doc(name).Get(s.ctx)
if err != nil {
return common.Resource{}, err
}
var r common.Resource
dsnap.DataTo(&r)
return r, nil
}
func (s *store) List(tenant core.Tenant) ([]common.Resource, error) {
result := []common.Resource{}
baseQuery := s.client.Collection("organizations").Doc(tenant.Organization).Collection("projects").Doc(tenant.Project)
iter := baseQuery.Collection("resources").Documents(s.ctx)
for {
doc, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
return result, err
}
var r common.Resource
doc.DataTo(&r)
result = append(result, r)
}
return result, nil
}
func (s *store) AddDynamicResourceLifeCycle(r common.DynamicResourceLifeCycle, tenant core.Tenant) error {
baseQuery := s.client.Collection("organizations").Doc(tenant.Organization).Collection("projects").Doc(tenant.Project)
_, err := baseQuery.Collection("drlc").Doc(r.Type).Set(s.ctx, r)
if err != nil {
log.Fatalf("Failed adding resource to firestore: %v", err)
}
return nil
}
func (s *store) GetDynamicResourceLifeCycle(rtype string, tenant core.Tenant) (common.DynamicResourceLifeCycle, error) {
baseQuery := s.client.Collection("organizations").Doc(tenant.Organization).Collection("projects").Doc(tenant.Project)
dsnap, err := baseQuery.Collection("resources").Doc(rtype).Get(s.ctx)
if err != nil {
return common.DynamicResourceLifeCycle{}, err
}
var r common.DynamicResourceLifeCycle
dsnap.DataTo(&r)
return r, nil
}
func (s *store) AddToken(token string, tenant core.Tenant) error {
s.lock.Lock()
defer s.lock.Unlock()
_, err := s.client.Collection("tokens").Doc(token).Set(s.ctx, map[string]interface{}{
"organization": tenant.Organization,
})
if err != nil {
log.Fatalf("Failed adding alovelace: %v", err)
}
return nil
}
func (s *store) DeleteToken(tenant core.Tenant) error {
s.lock.Lock()
defer s.lock.Unlock()
return nil
}
func (s *store) GetTenantFromToken(token, project string) (core.Tenant, error) {
s.lock.Lock()
defer s.lock.Unlock()
return core.NewTenant(), nil
}