This repository has been archived by the owner on Mar 24, 2022. It is now read-only.
/
resource.go
232 lines (193 loc) · 5.21 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package db
import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"time"
sq "github.com/Masterminds/squirrel"
"github.com/concourse/atc"
"github.com/lib/pq"
)
//go:generate counterfeiter . Resource
type Resource interface {
ID() int
Name() string
PipelineName() string
TeamName() string
Type() string
Source() atc.Source
CheckEvery() string
CheckTimeout() string
LastChecked() time.Time
Tags() atc.Tags
CheckError() error
Paused() bool
WebhookToken() string
PinnedVersion() atc.Version
FailingToCheck() bool
SetResourceConfig(int) error
Pause() error
Unpause() error
Reload() (bool, error)
}
var resourcesQuery = psql.Select("r.id, r.name, r.config, r.check_error, r.paused, r.last_checked, r.pipeline_id, r.nonce, p.name, t.name").
From("resources r").
Join("pipelines p ON p.id = r.pipeline_id").
Join("teams t ON t.id = p.team_id").
Where(sq.Eq{"r.active": true})
type resource struct {
id int
name string
pipelineID int
pipelineName string
teamName string
type_ string
source atc.Source
checkEvery string
checkTimeout string
lastChecked time.Time
tags atc.Tags
checkError error
paused bool
webhookToken string
pinnedVersion atc.Version
conn Conn
}
type ResourceNotFoundError struct {
Name string
}
func (e ResourceNotFoundError) Error() string {
return fmt.Sprintf("resource '%s' not found", e.Name)
}
type Resources []Resource
func (resources Resources) Lookup(name string) (Resource, bool) {
for _, resource := range resources {
if resource.Name() == name {
return resource, true
}
}
return nil, false
}
func (resources Resources) Configs() atc.ResourceConfigs {
var configs atc.ResourceConfigs
for _, r := range resources {
configs = append(configs, atc.ResourceConfig{
Name: r.Name(),
WebhookToken: r.WebhookToken(),
Type: r.Type(),
Source: r.Source(),
CheckEvery: r.CheckEvery(),
Tags: r.Tags(),
Version: r.PinnedVersion(),
})
}
return configs
}
func (resources Resources) PinnedVersions() map[string]atc.Version {
var pinnedVersions map[string]atc.Version
for _, r := range resources {
pinnedVersions[r.Name()] = r.PinnedVersion()
}
return pinnedVersions
}
func (r *resource) ID() int { return r.id }
func (r *resource) Name() string { return r.name }
func (r *resource) PipelineID() int { return r.pipelineID }
func (r *resource) PipelineName() string { return r.pipelineName }
func (r *resource) TeamName() string { return r.teamName }
func (r *resource) Type() string { return r.type_ }
func (r *resource) Source() atc.Source { return r.source }
func (r *resource) CheckEvery() string { return r.checkEvery }
func (r *resource) CheckTimeout() string { return r.checkTimeout }
func (r *resource) LastChecked() time.Time { return r.lastChecked }
func (r *resource) Tags() atc.Tags { return r.tags }
func (r *resource) CheckError() error { return r.checkError }
func (r *resource) Paused() bool { return r.paused }
func (r *resource) WebhookToken() string { return r.webhookToken }
func (r *resource) PinnedVersion() atc.Version { return r.pinnedVersion }
func (r *resource) FailingToCheck() bool {
return r.checkError != nil
}
func (r *resource) Reload() (bool, error) {
row := resourcesQuery.Where(sq.Eq{"r.id": r.id}).
RunWith(r.conn).
QueryRow()
err := scanResource(r, row)
if err != nil {
if err == sql.ErrNoRows {
return false, nil
}
return false, err
}
return true, nil
}
func (r *resource) Pause() error {
_, err := psql.Update("resources").
Set("paused", true).
Where(sq.Eq{
"id": r.id,
}).
RunWith(r.conn).
Exec()
return err
}
func (r *resource) Unpause() error {
_, err := psql.Update("resources").
Set("paused", false).
Where(sq.Eq{
"id": r.id,
}).
RunWith(r.conn).
Exec()
return err
}
func (r *resource) SetResourceConfig(resourceConfigID int) error {
_, err := psql.Update("resources").
Set("resource_config_id", resourceConfigID).
Where(sq.Eq{"id": r.id}).
Where(sq.Or{
sq.Eq{"resource_config_id": nil},
sq.NotEq{"resource_config_id": resourceConfigID},
}).
RunWith(r.conn).
Exec()
return err
}
func scanResource(r *resource, row scannable) error {
var (
configBlob []byte
checkErr, nonce sql.NullString
lastChecked pq.NullTime
)
err := row.Scan(&r.id, &r.name, &configBlob, &checkErr, &r.paused, &lastChecked, &r.pipelineID, &nonce, &r.pipelineName, &r.teamName)
if err != nil {
return err
}
r.lastChecked = lastChecked.Time
es := r.conn.EncryptionStrategy()
var noncense *string
if nonce.Valid {
noncense = &nonce.String
}
decryptedConfig, err := es.Decrypt(string(configBlob), noncense)
if err != nil {
return err
}
var config atc.ResourceConfig
err = json.Unmarshal(decryptedConfig, &config)
if err != nil {
return err
}
r.type_ = config.Type
r.source = config.Source
r.checkEvery = config.CheckEvery
r.checkTimeout = config.CheckTimeout
r.tags = config.Tags
r.webhookToken = config.WebhookToken
r.pinnedVersion = config.Version
if checkErr.Valid {
r.checkError = errors.New(checkErr.String)
}
return nil
}