@@ -2,11 +2,12 @@ package main
2
2
3
3
import (
4
4
"encoding/json"
5
- "github.com/ansible-semaphore/semaphore/db"
6
- trans "github.com/snikch/goodman/transaction"
7
5
"regexp"
8
6
"strconv"
9
7
"strings"
8
+
9
+ "github.com/semaphoreui/semaphore/db"
10
+ trans "github.com/snikch/goodman/transaction"
10
11
)
11
12
12
13
// STATE
@@ -18,23 +19,32 @@ var userKey *db.AccessKey
18
19
var task * db.Task
19
20
var schedule * db.Schedule
20
21
var view * db.View
22
+ var integration * db.Integration
23
+ var integrationextractvalue * db.IntegrationExtractValue
24
+ var integrationmatch * db.IntegrationMatcher
21
25
22
26
// Runtime created simple ID values for some items we need to reference in other objects
23
27
var repoID int
24
28
var inventoryID int
25
29
var environmentID int
26
30
var templateID int
31
+ var integrationID int
32
+ var integrationExtractValueID int
33
+ var integrationMatchID int
27
34
28
35
var capabilities = map [string ][]string {
29
- "user" : {},
30
- "project" : {"user" },
31
- "repository" : {"access_key" },
32
- "inventory" : {"repository" },
33
- "environment" : {"repository" },
34
- "template" : {"repository" , "inventory" , "environment" , "view" },
35
- "task" : {"template" },
36
- "schedule" : {"template" },
37
- "view" : {},
36
+ "user" : {},
37
+ "project" : {"user" },
38
+ "repository" : {"access_key" },
39
+ "inventory" : {"repository" },
40
+ "environment" : {"repository" },
41
+ "template" : {"repository" , "inventory" , "environment" , "view" },
42
+ "task" : {"template" },
43
+ "schedule" : {"template" },
44
+ "view" : {},
45
+ "integration" : {"project" , "template" },
46
+ "integrationextractvalue" : {"integration" },
47
+ "integrationmatcher" : {"integration" },
38
48
}
39
49
40
50
func capabilityWrapper (cap string ) func (t * trans.Transaction ) {
@@ -91,17 +101,25 @@ func resolveCapability(caps []string, resolved []string, uid string) {
91
101
repoID = pRepo .ID
92
102
case "inventory" :
93
103
res , err := store .CreateInventory (db.Inventory {
94
- ProjectID : userProject .ID ,
95
- Name : "ITI-" + uid ,
96
- Type : "static" ,
97
- SSHKeyID : & userKey .ID ,
98
- Inventory : "Test Inventory" ,
104
+ ProjectID : userProject .ID ,
105
+ Name : "ITI-" + uid ,
106
+ Type : "static" ,
107
+ SSHKeyID : & userKey .ID ,
108
+ BecomeKeyID : & userKey .ID ,
109
+ Inventory : "Test Inventory" ,
110
+ RepositoryID : & repoID ,
99
111
})
100
112
printError (err )
101
113
inventoryID = res .ID
102
114
case "environment" :
103
115
pwd := "test-pass"
104
116
env := "{}"
117
+ secret := db.EnvironmentSecret {
118
+ Type : db .EnvironmentSecretEnv ,
119
+ Name : "TEST" ,
120
+ Secret : "VALUE" ,
121
+ Operation : "create" ,
122
+ }
105
123
res , err := store .CreateEnvironment (db.Environment {
106
124
ProjectID : userProject .ID ,
107
125
Name : "ITI-" + uid ,
@@ -110,13 +128,22 @@ func resolveCapability(caps []string, resolved []string, uid string) {
110
128
ENV : & env ,
111
129
})
112
130
printError (err )
131
+ _ , err = store .CreateAccessKey (db.AccessKey {
132
+ Name : string (secret .Type ) + "." + secret .Name ,
133
+ String : secret .Secret ,
134
+ EnvironmentID : & res .ID ,
135
+ ProjectID : & userProject .ID ,
136
+ Type : db .AccessKeyString ,
137
+ })
138
+ printError (err )
113
139
environmentID = res .ID
114
140
case "template" :
115
141
args := "[]"
116
142
desc := "Hello, World!"
143
+ branch := "main"
117
144
res , err := store .CreateTemplate (db.Template {
118
145
ProjectID : userProject .ID ,
119
- InventoryID : inventoryID ,
146
+ InventoryID : & inventoryID ,
120
147
RepositoryID : repoID ,
121
148
EnvironmentID : & environmentID ,
122
149
Name : "Test-" + uid ,
@@ -125,12 +152,24 @@ func resolveCapability(caps []string, resolved []string, uid string) {
125
152
AllowOverrideArgsInTask : false ,
126
153
Description : & desc ,
127
154
ViewID : & view .ID ,
155
+ App : db .AppAnsible ,
156
+ GitBranch : & branch ,
157
+ SurveyVars : []db.SurveyVar {},
128
158
})
129
159
130
160
printError (err )
131
161
templateID = res .ID
132
162
case "task" :
133
163
task = addTask ()
164
+ case "integration" :
165
+ integration = addIntegration ()
166
+ integrationID = integration .ID
167
+ case "integrationextractvalue" :
168
+ integrationextractvalue = addIntegrationExtractValue ()
169
+ integrationExtractValueID = integrationextractvalue .ID
170
+ case "integrationmatcher" :
171
+ integrationmatch = addIntegrationMatcher ()
172
+ integrationMatchID = integrationmatch .ID
134
173
default :
135
174
panic ("unknown capability " + v )
136
175
}
@@ -150,13 +189,16 @@ var pathSubPatterns = []func() string{
150
189
func () string { return strconv .Itoa (userProject .ID ) },
151
190
func () string { return strconv .Itoa (userPathTestUser .ID ) },
152
191
func () string { return strconv .Itoa (userKey .ID ) },
153
- func () string { return strconv .Itoa (int ( repoID ) ) },
154
- func () string { return strconv .Itoa (int ( inventoryID ) ) },
155
- func () string { return strconv .Itoa (int ( environmentID ) ) },
156
- func () string { return strconv .Itoa (int ( templateID ) ) },
192
+ func () string { return strconv .Itoa (repoID ) },
193
+ func () string { return strconv .Itoa (inventoryID ) },
194
+ func () string { return strconv .Itoa (environmentID ) },
195
+ func () string { return strconv .Itoa (templateID ) },
157
196
func () string { return strconv .Itoa (task .ID ) },
158
197
func () string { return strconv .Itoa (schedule .ID ) },
159
198
func () string { return strconv .Itoa (view .ID ) },
199
+ func () string { return strconv .Itoa (integration .ID ) },
200
+ func () string { return strconv .Itoa (integrationextractvalue .ID ) },
201
+ func () string { return strconv .Itoa (integrationmatch .ID ) },
160
202
}
161
203
162
204
// alterRequestPath with the above slice of functions
@@ -165,12 +207,14 @@ func alterRequestPath(t *trans.Transaction) {
165
207
exploded := make ([]string , len (pathArgs ))
166
208
copy (exploded , pathArgs )
167
209
for k , v := range pathSubPatterns {
210
+
168
211
pos , exists := stringInSlice (strconv .Itoa (k + 1 ), exploded )
169
212
if exists {
170
213
pathArgs [pos ] = v ()
171
214
}
172
215
}
173
216
t .FullPath = strings .Join (pathArgs , "/" )
217
+
174
218
t .Request .URI = t .FullPath
175
219
}
176
220
@@ -184,11 +228,13 @@ func alterRequestBody(t *trans.Transaction) {
184
228
bodyFieldProcessor ("json" , "{}" , & request )
185
229
if userKey != nil {
186
230
bodyFieldProcessor ("ssh_key_id" , userKey .ID , & request )
231
+ bodyFieldProcessor ("become_key_id" , userKey .ID , & request )
187
232
}
188
233
bodyFieldProcessor ("environment_id" , environmentID , & request )
189
234
bodyFieldProcessor ("inventory_id" , inventoryID , & request )
190
235
bodyFieldProcessor ("repository_id" , repoID , & request )
191
236
bodyFieldProcessor ("template_id" , templateID , & request )
237
+ bodyFieldProcessor ("build_template_id" , nil , & request )
192
238
if task != nil {
193
239
bodyFieldProcessor ("task_id" , task .ID , & request )
194
240
}
@@ -198,9 +244,21 @@ func alterRequestBody(t *trans.Transaction) {
198
244
if view != nil {
199
245
bodyFieldProcessor ("view_id" , view .ID , & request )
200
246
}
247
+
248
+ if integration != nil {
249
+ bodyFieldProcessor ("integration_id" , integration .ID , & request )
250
+ }
251
+ if integrationextractvalue != nil {
252
+ bodyFieldProcessor ("value_id" , integrationextractvalue .ID , & request )
253
+ }
254
+ if integrationmatch != nil {
255
+ bodyFieldProcessor ("matcher_id" , integrationmatch .ID , & request )
256
+ }
257
+
201
258
// Inject object ID to body for PUT requests
202
259
if strings .ToLower (t .Request .Method ) == "put" {
203
- putRequestPathRE := regexp .MustCompile (`/api/(?:project/\d+/)?\w+/(\d+)/?$` )
260
+
261
+ putRequestPathRE := regexp .MustCompile (`\w+/(\d+)/?$` )
204
262
m := putRequestPathRE .FindStringSubmatch (t .FullPath )
205
263
if len (m ) > 0 {
206
264
objectID , err := strconv .Atoi (m [1 ])
0 commit comments