@@ -12,6 +12,7 @@ import (
12
12
13
13
"github.com/go-chi/render"
14
14
"github.com/google/uuid"
15
+ "golang.org/x/xerrors"
15
16
16
17
"cdr.dev/slog"
17
18
@@ -64,6 +65,7 @@ type ProvisionerJobResource struct {
64
65
Transition database.WorkspaceTransition `json:"workspace_transition"`
65
66
Type string `json:"type"`
66
67
Name string `json:"name"`
68
+ Agent * ProvisionerJobAgent `json:"agent,omitempty"`
67
69
}
68
70
69
71
type ProvisionerJobAgent struct {
@@ -238,6 +240,49 @@ func (api *api) provisionerJobLogsByID(rw http.ResponseWriter, r *http.Request)
238
240
}
239
241
}
240
242
243
+ func (api * api ) provisionerJobResourcesByID (rw http.ResponseWriter , r * http.Request ) {
244
+ job := httpmw .ProvisionerJobParam (r )
245
+ if ! convertProvisionerJob (job ).Status .Completed () {
246
+ httpapi .Write (rw , http .StatusPreconditionFailed , httpapi.Response {
247
+ Message : "Job hasn't completed!" ,
248
+ })
249
+ return
250
+ }
251
+ resources , err := api .Database .GetProvisionerJobResourcesByJobID (r .Context (), job .ID )
252
+ if err != nil {
253
+ httpapi .Write (rw , http .StatusInternalServerError , httpapi.Response {
254
+ Message : fmt .Sprintf ("get provisioner job resources: %s" , err ),
255
+ })
256
+ return
257
+ }
258
+ apiResources := make ([]ProvisionerJobResource , 0 )
259
+ for _ , resource := range resources {
260
+ if ! resource .AgentID .Valid {
261
+ apiResources = append (apiResources , convertProvisionerJobResource (resource , nil ))
262
+ continue
263
+ }
264
+ // TODO: This should be combined.
265
+ agents , err := api .Database .GetProvisionerJobAgentsByResourceIDs (r .Context (), []uuid.UUID {resource .ID })
266
+ if err != nil {
267
+ httpapi .Write (rw , http .StatusInternalServerError , httpapi.Response {
268
+ Message : fmt .Sprintf ("get provisioner job agent: %s" , err ),
269
+ })
270
+ return
271
+ }
272
+ agent := agents [0 ]
273
+ apiAgent , err := convertProvisionerJobAgent (agent )
274
+ if err != nil {
275
+ httpapi .Write (rw , http .StatusInternalServerError , httpapi.Response {
276
+ Message : fmt .Sprintf ("convert provisioner job agent: %s" , err ),
277
+ })
278
+ return
279
+ }
280
+ apiResources = append (apiResources , convertProvisionerJobResource (resource , & apiAgent ))
281
+ }
282
+ render .Status (r , http .StatusOK )
283
+ render .JSON (rw , r , apiResources )
284
+ }
285
+
241
286
func convertProvisionerJobLog (provisionerJobLog database.ProvisionerJobLog ) ProvisionerJobLog {
242
287
return ProvisionerJobLog {
243
288
ID : provisionerJobLog .ID ,
@@ -291,6 +336,37 @@ func convertProvisionerJob(provisionerJob database.ProvisionerJob) ProvisionerJo
291
336
return job
292
337
}
293
338
339
+ func convertProvisionerJobResource (resource database.ProvisionerJobResource , agent * ProvisionerJobAgent ) ProvisionerJobResource {
340
+ return ProvisionerJobResource {
341
+ ID : resource .ID ,
342
+ CreatedAt : resource .CreatedAt ,
343
+ JobID : resource .JobID ,
344
+ Transition : resource .Transition ,
345
+ Type : resource .Type ,
346
+ Name : resource .Name ,
347
+ Agent : agent ,
348
+ }
349
+ }
350
+
351
+ func convertProvisionerJobAgent (agent database.ProvisionerJobAgent ) (ProvisionerJobAgent , error ) {
352
+ var envs map [string ]string
353
+ if agent .EnvironmentVariables .Valid {
354
+ err := json .Unmarshal (agent .EnvironmentVariables .RawMessage , & envs )
355
+ if err != nil {
356
+ return ProvisionerJobAgent {}, xerrors .Errorf ("unmarshal: %w" , err )
357
+ }
358
+ }
359
+ return ProvisionerJobAgent {
360
+ ID : agent .ID ,
361
+ CreatedAt : agent .CreatedAt ,
362
+ UpdatedAt : agent .UpdatedAt .Time ,
363
+ ResourceID : agent .ResourceID ,
364
+ InstanceID : agent .AuthInstanceID .String ,
365
+ StartupScript : agent .StartupScript .String ,
366
+ EnvironmentVariables : envs ,
367
+ }, nil
368
+ }
369
+
294
370
func provisionerJobLogsChannel (jobID uuid.UUID ) string {
295
371
return fmt .Sprintf ("provisioner-log-logs:%s" , jobID )
296
372
}
0 commit comments