forked from flynn/flynn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource.go
152 lines (140 loc) · 4.14 KB
/
resource.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
package main
import (
"strings"
"github.com/flynn/flynn/Godeps/_workspace/src/github.com/flynn/go-sql"
"github.com/flynn/flynn/Godeps/_workspace/src/github.com/flynn/pq/hstore"
ct "github.com/flynn/flynn/controller/types"
"github.com/flynn/flynn/pkg/random"
)
type ResourceRepo struct {
db *DB
}
func NewResourceRepo(db *DB) *ResourceRepo {
return &ResourceRepo{db}
}
func (rr *ResourceRepo) Add(r *ct.Resource) error {
if r.ID == "" {
r.ID = random.UUID()
}
tx, err := rr.db.Begin()
if err != nil {
return err
}
err = tx.QueryRow(`INSERT INTO resources (resource_id, provider_id, external_id, env)
VALUES ($1, $2, $3, $4)
RETURNING created_at`,
r.ID, r.ProviderID, r.ExternalID, envHstore(r.Env)).Scan(&r.CreatedAt)
if err != nil {
tx.Rollback()
return err
}
for i, appID := range r.Apps {
var filterSQL string
var args []interface{}
if idPattern.MatchString(appID) {
filterSQL = "app_id = $1 OR name = $2), $3)"
args = []interface{}{appID, appID, r.ID}
} else {
filterSQL = "name = $1), $2)"
args = []interface{}{appID, r.ID}
}
err = tx.QueryRow("INSERT INTO app_resources (app_id, resource_id) VALUES ((SELECT app_id FROM apps WHERE "+
filterSQL+" RETURNING app_id", args...).Scan(&r.Apps[i])
if err != nil {
tx.Rollback()
return err
}
r.Apps[i] = cleanUUID(r.Apps[i])
}
r.ID = cleanUUID(r.ID)
return tx.Commit()
}
func envHstore(m map[string]string) hstore.Hstore {
res := hstore.Hstore{Map: make(map[string]sql.NullString, len(m))}
for k, v := range m {
res.Map[k] = sql.NullString{String: v, Valid: true}
}
return res
}
func split(s string, sep string) []string {
if s == "" {
return nil
}
return strings.Split(s, ",")
}
func scanResource(s Scanner) (*ct.Resource, error) {
r := &ct.Resource{}
var env hstore.Hstore
var appIDs string
err := s.Scan(&r.ID, &r.ProviderID, &r.ExternalID, &env, &appIDs, &r.CreatedAt)
if err == sql.ErrNoRows {
err = ErrNotFound
}
r.ID = cleanUUID(r.ID)
r.ProviderID = cleanUUID(r.ProviderID)
r.Env = make(map[string]string, len(env.Map))
for k, v := range env.Map {
r.Env[k] = v.String
}
if appIDs != "" {
r.Apps = split(appIDs[1:len(appIDs)-1], ",")
}
for i, id := range r.Apps {
r.Apps[i] = cleanUUID(id)
}
return r, err
}
func (r *ResourceRepo) Get(id string) (*ct.Resource, error) {
row := r.db.QueryRow(`SELECT resource_id, provider_id, external_id, env,
ARRAY(SELECT app_id
FROM app_resources a
WHERE a.resource_id = r.resource_id AND a.deleted_at IS NULL
ORDER BY a.created_at DESC),
created_at
FROM resources r
WHERE resource_id = $1 AND deleted_at IS NULL`, id)
return scanResource(row)
}
func (r *ResourceRepo) ProviderList(providerID string) ([]*ct.Resource, error) {
rows, err := r.db.Query(`SELECT resource_id, provider_id, external_id, env,
ARRAY(SELECT a.app_id
FROM app_resources a
WHERE a.resource_id = r.resource_id AND a.deleted_at IS NULL
ORDER BY a.created_at DESC),
created_at
FROM resources r
WHERE provider_id = $1 AND deleted_at IS NULL
ORDER BY created_at DESC`, providerID)
if err != nil {
return nil, err
}
return resourceList(rows)
}
func resourceList(rows *sql.Rows) ([]*ct.Resource, error) {
var resources []*ct.Resource
for rows.Next() {
resource, err := scanResource(rows)
if err != nil {
rows.Close()
return nil, err
}
resources = append(resources, resource)
}
return resources, rows.Err()
}
func (r *ResourceRepo) AppList(appID string) ([]*ct.Resource, error) {
rows, err := r.db.Query(`SELECT DISTINCT(r.resource_id), r.provider_id, r.external_id, r.env,
ARRAY(SELECT a.app_id
FROM app_resources a
WHERE a.resource_id = r.resource_id AND a.deleted_at IS NULL
ORDER BY a.created_at DESC),
r.created_at
FROM resources r
JOIN app_resources a USING (resource_id)
WHERE a.app_id = $1 AND r.deleted_at IS NULL
ORDER BY r.created_at DESC`, appID)
if err != nil {
return nil, err
}
return resourceList(rows)
}