1
1
package coderd
2
2
3
3
import (
4
+ "context"
4
5
"database/sql"
5
6
"errors"
6
7
"fmt"
@@ -49,7 +50,16 @@ func (api *API) template(rw http.ResponseWriter, r *http.Request) {
49
50
count = uint32 (workspaceCounts [0 ].Count )
50
51
}
51
52
52
- httpapi .Write (rw ,http .StatusOK ,convertTemplate (template ,count ))
53
+ createdByNameMap ,err := getCreatedByNamesByTemplateIDs (r .Context (),api .Database , []database.Template {template })
54
+ if err != nil {
55
+ httpapi .Write (rw ,http .StatusInternalServerError , httpapi.Response {
56
+ Message :"Internal error fetching creator name." ,
57
+ Detail :err .Error (),
58
+ })
59
+ return
60
+ }
61
+
62
+ httpapi .Write (rw ,http .StatusOK ,convertTemplate (template ,count ,createdByNameMap [template .ID .String ()]))
53
63
}
54
64
55
65
func (api * API )deleteTemplate (rw http.ResponseWriter ,r * http.Request ) {
@@ -97,6 +107,7 @@ func (api *API) deleteTemplate(rw http.ResponseWriter, r *http.Request) {
97
107
func (api * API )postTemplateByOrganization (rw http.ResponseWriter ,r * http.Request ) {
98
108
var createTemplate codersdk.CreateTemplateRequest
99
109
organization := httpmw .OrganizationParam (r )
110
+ apiKey := httpmw .APIKey (r )
100
111
if ! api .Authorize (rw ,r ,rbac .ActionCreate ,rbac .ResourceTemplate .InOrg (organization .ID )) {
101
112
return
102
113
}
@@ -175,6 +186,10 @@ func (api *API) postTemplateByOrganization(rw http.ResponseWriter, r *http.Reque
175
186
Description :createTemplate .Description ,
176
187
MaxTtl :int64 (maxTTL ),
177
188
MinAutostartInterval :int64 (minAutostartInterval ),
189
+ CreatedBy : uuid.NullUUID {
190
+ UUID :apiKey .UserID ,
191
+ Valid :true ,
192
+ },
178
193
})
179
194
if err != nil {
180
195
return xerrors .Errorf ("insert template: %s" ,err )
@@ -208,7 +223,12 @@ func (api *API) postTemplateByOrganization(rw http.ResponseWriter, r *http.Reque
208
223
}
209
224
}
210
225
211
- template = convertTemplate (dbTemplate ,0 )
226
+ createdByNameMap ,err := getCreatedByNamesByTemplateIDs (r .Context (),db , []database.Template {dbTemplate })
227
+ if err != nil {
228
+ return xerrors .Errorf ("get creator name: %w" ,err )
229
+ }
230
+
231
+ template = convertTemplate (dbTemplate ,0 ,createdByNameMap [dbTemplate .ID .String ()])
212
232
return nil
213
233
})
214
234
if err != nil {
@@ -258,7 +278,16 @@ func (api *API) templatesByOrganization(rw http.ResponseWriter, r *http.Request)
258
278
return
259
279
}
260
280
261
- httpapi .Write (rw ,http .StatusOK ,convertTemplates (templates ,workspaceCounts ))
281
+ createdByNameMap ,err := getCreatedByNamesByTemplateIDs (r .Context (),api .Database ,templates )
282
+ if err != nil {
283
+ httpapi .Write (rw ,http .StatusInternalServerError , httpapi.Response {
284
+ Message :"Internal error fetching creator names." ,
285
+ Detail :err .Error (),
286
+ })
287
+ return
288
+ }
289
+
290
+ httpapi .Write (rw ,http .StatusOK ,convertTemplates (templates ,workspaceCounts ,createdByNameMap ))
262
291
}
263
292
264
293
func (api * API )templateByOrganizationAndName (rw http.ResponseWriter ,r * http.Request ) {
@@ -304,7 +333,16 @@ func (api *API) templateByOrganizationAndName(rw http.ResponseWriter, r *http.Re
304
333
count = uint32 (workspaceCounts [0 ].Count )
305
334
}
306
335
307
- httpapi .Write (rw ,http .StatusOK ,convertTemplate (template ,count ))
336
+ createdByNameMap ,err := getCreatedByNamesByTemplateIDs (r .Context (),api .Database , []database.Template {template })
337
+ if err != nil {
338
+ httpapi .Write (rw ,http .StatusInternalServerError , httpapi.Response {
339
+ Message :"Internal error fetching creator name." ,
340
+ Detail :err .Error (),
341
+ })
342
+ return
343
+ }
344
+
345
+ httpapi .Write (rw ,http .StatusOK ,convertTemplate (template ,count ,createdByNameMap [template .ID .String ()]))
308
346
}
309
347
310
348
func (api * API )patchTemplateMeta (rw http.ResponseWriter ,r * http.Request ) {
@@ -400,29 +438,54 @@ func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) {
400
438
return
401
439
}
402
440
403
- httpapi .Write (rw ,http .StatusOK ,convertTemplate (updated ,count ))
441
+ createdByNameMap ,err := getCreatedByNamesByTemplateIDs (r .Context (),api .Database , []database.Template {updated })
442
+ if err != nil {
443
+ httpapi .Write (rw ,http .StatusInternalServerError , httpapi.Response {
444
+ Message :"Internal error fetching creator name." ,
445
+ Detail :err .Error (),
446
+ })
447
+ return
448
+ }
449
+
450
+ httpapi .Write (rw ,http .StatusOK ,convertTemplate (updated ,count ,createdByNameMap [updated .ID .String ()]))
451
+ }
452
+
453
+ func getCreatedByNamesByTemplateIDs (ctx context.Context ,db database.Store ,templates []database.Template ) (map [string ]string ,error ) {
454
+ creators := make (map [string ]string ,len (templates ))
455
+ for _ ,template := range templates {
456
+ if template .CreatedBy .Valid {
457
+ creator ,err := db .GetUserByID (ctx ,template .CreatedBy .UUID )
458
+ if err != nil {
459
+ return map [string ]string {},err
460
+ }
461
+ creators [template .ID .String ()]= creator .Username
462
+ }else {
463
+ creators [template .ID .String ()]= ""
464
+ }
465
+ }
466
+ return creators ,nil
404
467
}
405
468
406
- func convertTemplates (templates []database.Template ,workspaceCounts []database.GetWorkspaceOwnerCountsByTemplateIDsRow ) []codersdk.Template {
469
+ func convertTemplates (templates []database.Template ,workspaceCounts []database.GetWorkspaceOwnerCountsByTemplateIDsRow , createdByNameMap map [ string ] string ) []codersdk.Template {
407
470
apiTemplates := make ([]codersdk.Template ,0 ,len (templates ))
408
471
for _ ,template := range templates {
409
472
found := false
410
473
for _ ,workspaceCount := range workspaceCounts {
411
474
if workspaceCount .TemplateID .String ()!= template .ID .String () {
412
475
continue
413
476
}
414
- apiTemplates = append (apiTemplates ,convertTemplate (template ,uint32 (workspaceCount .Count )))
477
+ apiTemplates = append (apiTemplates ,convertTemplate (template ,uint32 (workspaceCount .Count ), createdByNameMap [ template . ID . String ()] ))
415
478
found = true
416
479
break
417
480
}
418
481
if ! found {
419
- apiTemplates = append (apiTemplates ,convertTemplate (template ,uint32 (0 )))
482
+ apiTemplates = append (apiTemplates ,convertTemplate (template ,uint32 (0 ), createdByNameMap [ template . ID . String ()] ))
420
483
}
421
484
}
422
485
return apiTemplates
423
486
}
424
487
425
- func convertTemplate (template database.Template ,workspaceOwnerCount uint32 ) codersdk.Template {
488
+ func convertTemplate (template database.Template ,workspaceOwnerCount uint32 , createdByName string ) codersdk.Template {
426
489
return codersdk.Template {
427
490
ID :template .ID ,
428
491
CreatedAt :template .CreatedAt ,
@@ -435,5 +498,7 @@ func convertTemplate(template database.Template, workspaceOwnerCount uint32) cod
435
498
Description :template .Description ,
436
499
MaxTTLMillis :time .Duration (template .MaxTtl ).Milliseconds (),
437
500
MinAutostartIntervalMillis :time .Duration (template .MinAutostartInterval ).Milliseconds (),
501
+ CreatedByID :template .CreatedBy ,
502
+ CreatedByName :createdByName ,
438
503
}
439
504
}