@@ -16,6 +16,7 @@ import (
16
16
"github.com/moby/moby/pkg/namesgenerator"
17
17
"golang.org/x/xerrors"
18
18
19
+ "github.com/coder/coder/coderd/audit"
19
20
"github.com/coder/coder/coderd/database"
20
21
"github.com/coder/coder/coderd/httpapi"
21
22
"github.com/coder/coder/coderd/httpmw"
@@ -82,7 +83,18 @@ func (api *API) template(rw http.ResponseWriter, r *http.Request) {
82
83
}
83
84
84
85
func (api * API ) deleteTemplate (rw http.ResponseWriter , r * http.Request ) {
85
- template := httpmw .TemplateParam (r )
86
+ var (
87
+ template = httpmw .TemplateParam (r )
88
+ aReq , commitAudit = audit .InitRequest [database.Template ](rw , & audit.RequestParams {
89
+ Features : api .FeaturesService ,
90
+ Log : api .Logger ,
91
+ Request : r ,
92
+ Action : database .AuditActionDelete ,
93
+ })
94
+ )
95
+ defer commitAudit ()
96
+ aReq .Old = template
97
+
86
98
if ! api .Authorize (r , rbac .ActionDelete , template ) {
87
99
httpapi .ResourceNotFound (rw )
88
100
return
@@ -91,10 +103,7 @@ func (api *API) deleteTemplate(rw http.ResponseWriter, r *http.Request) {
91
103
workspaces , err := api .Database .GetWorkspaces (r .Context (), database.GetWorkspacesParams {
92
104
TemplateIds : []uuid.UUID {template .ID },
93
105
})
94
- if errors .Is (err , sql .ErrNoRows ) {
95
- err = nil
96
- }
97
- if err != nil {
106
+ if err != nil && ! errors .Is (err , sql .ErrNoRows ) {
98
107
httpapi .Write (rw , http .StatusInternalServerError , codersdk.Response {
99
108
Message : "Internal error fetching workspaces by template id." ,
100
109
Detail : err .Error (),
@@ -126,9 +135,26 @@ func (api *API) deleteTemplate(rw http.ResponseWriter, r *http.Request) {
126
135
127
136
// Create a new template in an organization.
128
137
func (api * API ) postTemplateByOrganization (rw http.ResponseWriter , r * http.Request ) {
129
- var createTemplate codersdk.CreateTemplateRequest
130
- organization := httpmw .OrganizationParam (r )
131
- apiKey := httpmw .APIKey (r )
138
+ var (
139
+ createTemplate codersdk.CreateTemplateRequest
140
+ organization = httpmw .OrganizationParam (r )
141
+ apiKey = httpmw .APIKey (r )
142
+ templateAudit , commitTemplateAudit = audit .InitRequest [database.Template ](rw , & audit.RequestParams {
143
+ Features : api .FeaturesService ,
144
+ Log : api .Logger ,
145
+ Request : r ,
146
+ Action : database .AuditActionCreate ,
147
+ })
148
+ templateVersionAudit , commitTemplateVersionAudit = audit .InitRequest [database.TemplateVersion ](rw , & audit.RequestParams {
149
+ Features : api .FeaturesService ,
150
+ Log : api .Logger ,
151
+ Request : r ,
152
+ Action : database .AuditActionWrite ,
153
+ })
154
+ )
155
+ defer commitTemplateAudit ()
156
+ defer commitTemplateVersionAudit ()
157
+
132
158
if ! api .Authorize (r , rbac .ActionCreate , rbac .ResourceTemplate .InOrg (organization .ID )) {
133
159
httpapi .ResourceNotFound (rw )
134
160
return
@@ -175,6 +201,8 @@ func (api *API) postTemplateByOrganization(rw http.ResponseWriter, r *http.Reque
175
201
})
176
202
return
177
203
}
204
+ templateVersionAudit .Old = templateVersion
205
+
178
206
importJob , err := api .Database .GetProvisionerJobByID (r .Context (), templateVersion .JobID )
179
207
if err != nil {
180
208
httpapi .Write (rw , http .StatusInternalServerError , codersdk.Response {
@@ -234,6 +262,8 @@ func (api *API) postTemplateByOrganization(rw http.ResponseWriter, r *http.Reque
234
262
return xerrors .Errorf ("insert template: %s" , err )
235
263
}
236
264
265
+ templateAudit .New = dbTemplate
266
+
237
267
err = db .UpdateTemplateVersionByID (r .Context (), database.UpdateTemplateVersionByIDParams {
238
268
ID : templateVersion .ID ,
239
269
TemplateID : uuid.NullUUID {
@@ -245,6 +275,12 @@ func (api *API) postTemplateByOrganization(rw http.ResponseWriter, r *http.Reque
245
275
if err != nil {
246
276
return xerrors .Errorf ("insert template version: %s" , err )
247
277
}
278
+ newTemplateVersion := templateVersion
279
+ newTemplateVersion .TemplateID = uuid.NullUUID {
280
+ UUID : dbTemplate .ID ,
281
+ Valid : true ,
282
+ }
283
+ templateVersionAudit .New = newTemplateVersion
248
284
249
285
for _ , parameterValue := range createTemplate .ParameterValues {
250
286
_ , err = db .InsertParameterValue (r .Context (), database.InsertParameterValueParams {
@@ -397,7 +433,18 @@ func (api *API) templateByOrganizationAndName(rw http.ResponseWriter, r *http.Re
397
433
}
398
434
399
435
func (api * API ) patchTemplateMeta (rw http.ResponseWriter , r * http.Request ) {
400
- template := httpmw .TemplateParam (r )
436
+ var (
437
+ template = httpmw .TemplateParam (r )
438
+ aReq , commitAudit = audit .InitRequest [database.Template ](rw , & audit.RequestParams {
439
+ Features : api .FeaturesService ,
440
+ Log : api .Logger ,
441
+ Request : r ,
442
+ Action : database .AuditActionWrite ,
443
+ })
444
+ )
445
+ defer commitAudit ()
446
+ aReq .Old = template
447
+
401
448
if ! api .Authorize (r , rbac .ActionUpdate , template ) {
402
449
httpapi .ResourceNotFound (rw )
403
450
return
@@ -474,36 +521,32 @@ func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) {
474
521
minAutostartInterval = time .Duration (template .MinAutostartInterval )
475
522
}
476
523
477
- if err : = s .UpdateTemplateMetaByID (r .Context (), database.UpdateTemplateMetaByIDParams {
524
+ updated , err = s .UpdateTemplateMetaByID (r .Context (), database.UpdateTemplateMetaByIDParams {
478
525
ID : template .ID ,
479
526
UpdatedAt : database .Now (),
480
527
Name : name ,
481
528
Description : desc ,
482
529
Icon : icon ,
483
530
MaxTtl : int64 (maxTTL ),
484
531
MinAutostartInterval : int64 (minAutostartInterval ),
485
- }); err != nil {
486
- return err
487
- }
488
-
489
- updated , err = s .GetTemplateByID (r .Context (), template .ID )
532
+ })
490
533
if err != nil {
491
534
return err
492
535
}
536
+
493
537
return nil
494
538
})
495
539
if err != nil {
496
- httpapi .Write (rw , http .StatusInternalServerError , codersdk.Response {
497
- Message : "Internal error updating template metadata." ,
498
- Detail : err .Error (),
499
- })
540
+ httpapi .InternalServerError (rw , err )
500
541
return
501
542
}
502
543
503
544
if updated .UpdatedAt .IsZero () {
545
+ aReq .New = template
504
546
httpapi .Write (rw , http .StatusNotModified , nil )
505
547
return
506
548
}
549
+ aReq .New = updated
507
550
508
551
createdByNameMap , err := getCreatedByNamesByTemplateIDs (r .Context (), api .Database , []database.Template {updated })
509
552
if err != nil {
0 commit comments