@@ -21,6 +21,7 @@ import (
21
21
"github.com/coder/coder/coderd/rbac"
22
22
"github.com/coder/coder/coderd/schedule"
23
23
"github.com/coder/coder/coderd/telemetry"
24
+ "github.com/coder/coder/coderd/util/ptr"
24
25
"github.com/coder/coder/codersdk"
25
26
"github.com/coder/coder/examples"
26
27
)
@@ -149,6 +150,19 @@ func (api *API) postTemplateByOrganization(rw http.ResponseWriter, r *http.Reque
149
150
if ! httpapi .Read (ctx , rw , r , & createTemplate ) {
150
151
return
151
152
}
153
+
154
+ // Make a temporary struct to represent the template. This is used for
155
+ // auditing if any of the following checks fail. It will be overwritten when
156
+ // the template is inserted into the db.
157
+ templateAudit .New = database.Template {
158
+ OrganizationID : organization .ID ,
159
+ Name : createTemplate .Name ,
160
+ Description : createTemplate .Description ,
161
+ CreatedBy : apiKey .UserID ,
162
+ Icon : createTemplate .Icon ,
163
+ DisplayName : createTemplate .DisplayName ,
164
+ }
165
+
152
166
_ , err := api .Database .GetTemplateByOrganizationAndName (ctx , database.GetTemplateByOrganizationAndNameParams {
153
167
OrganizationID : organization .ID ,
154
168
Name : createTemplate .Name ,
@@ -170,6 +184,7 @@ func (api *API) postTemplateByOrganization(rw http.ResponseWriter, r *http.Reque
170
184
})
171
185
return
172
186
}
187
+
173
188
templateVersion , err := api .Database .GetTemplateVersionByID (ctx , createTemplate .VersionID )
174
189
if errors .Is (err , sql .ErrNoRows ) {
175
190
httpapi .Write (ctx , rw , http .StatusNotFound , codersdk.Response {
@@ -228,22 +243,14 @@ func (api *API) postTemplateByOrganization(rw http.ResponseWriter, r *http.Reque
228
243
}
229
244
230
245
var (
231
- allowUserCancelWorkspaceJobs bool
232
- allowUserAutostart = true
233
- allowUserAutostop = true
246
+ dbTemplate database.Template
247
+ template codersdk.Template
248
+
249
+ allowUserCancelWorkspaceJobs = ptr .NilToDefault (createTemplate .AllowUserCancelWorkspaceJobs , false )
250
+ allowUserAutostart = ptr .NilToDefault (createTemplate .AllowUserAutostart , true )
251
+ allowUserAutostop = ptr .NilToDefault (createTemplate .AllowUserAutostop , true )
234
252
)
235
- if createTemplate .AllowUserCancelWorkspaceJobs != nil {
236
- allowUserCancelWorkspaceJobs = * createTemplate .AllowUserCancelWorkspaceJobs
237
- }
238
- if createTemplate .AllowUserAutostart != nil {
239
- allowUserAutostart = * createTemplate .AllowUserAutostart
240
- }
241
- if createTemplate .AllowUserAutostop != nil {
242
- allowUserAutostop = * createTemplate .AllowUserAutostop
243
- }
244
253
245
- var dbTemplate database.Template
246
- var template codersdk.Template
247
254
err = api .Database .InTx (func (tx database.Store ) error {
248
255
now := database .Now ()
249
256
dbTemplate , err = tx .InsertTemplate (ctx , database.InsertTemplateParams {
0 commit comments