Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5fa9216

Browse files
committed
fix up inserts a little
1 parent 2b5800e commit 5fa9216

File tree

1 file changed

+34
-17
lines changed

1 file changed

+34
-17
lines changed

coderd/provisionerdserver/provisionerdserver.go

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,28 +1432,45 @@ func (s *server) CompleteJob(ctx context.Context, completed *proto.CompletedJob)
14321432
return nil, xerrors.Errorf("update template version external auth providers: %w", err)
14331433
}
14341434

1435-
if len(jobType.TemplateImport.Plan) > 0 {
1436-
moduleFilesTar := jobType.TemplateImport.ModuleFiles
1437-
hashBytes := sha256.Sum256(moduleFilesTar)
1438-
hash := hex.EncodeToString(hashBytes[:])
1439-
// nolint:gocritic // Requires system privileges
1440-
file, err := s.Database.InsertFile(dbauthz.AsSystemRestricted(ctx), database.InsertFileParams{
1441-
ID: uuid.New(),
1442-
Hash: hash,
1443-
CreatedBy: uuid.Nil, // TODO
1444-
CreatedAt: dbtime.Now(),
1445-
Mimetype: tarMimeType,
1446-
Data: moduleFilesTar,
1447-
})
1448-
if err != nil {
1449-
return nil, xerrors.Errorf("insert template version terraform modules: %w", err)
1435+
plan := jobType.TemplateImport.Plan
1436+
moduleFiles := jobType.TemplateImport.ModuleFiles
1437+
// If there is a plan, or a module files archive we need to insert a
1438+
// template_version_terraform_values row.
1439+
if len(plan) > 0 || len(moduleFiles) > 0 {
1440+
// ...but the plan and the module files archive are both optional! So
1441+
// we need to fallback to a valid JSON object if the plan was omitted.
1442+
if len(plan) == 0 {
1443+
plan = []byte("{}")
1444+
}
1445+
1446+
// ...and we only want to insert a files row if an archive was provided.
1447+
var fileID uuid.NullUUID
1448+
if len(moduleFiles) > 0 {
1449+
hashBytes := sha256.Sum256(moduleFiles)
1450+
hash := hex.EncodeToString(hashBytes[:])
1451+
// nolint:gocritic // Requires system privileges
1452+
file, err := s.Database.InsertFile(dbauthz.AsSystemRestricted(ctx), database.InsertFileParams{
1453+
ID: uuid.New(),
1454+
Hash: hash,
1455+
CreatedBy: uuid.Nil, // TODO
1456+
CreatedAt: dbtime.Now(),
1457+
Mimetype: tarMimeType,
1458+
Data: moduleFiles,
1459+
})
1460+
if err != nil {
1461+
return nil, xerrors.Errorf("insert template version terraform modules: %w", err)
1462+
}
1463+
fileID = uuid.NullUUID{
1464+
Valid: true,
1465+
UUID: file.ID,
1466+
}
14501467
}
14511468

14521469
err = s.Database.InsertTemplateVersionTerraformValuesByJobID(ctx, database.InsertTemplateVersionTerraformValuesByJobIDParams{
14531470
JobID: jobID,
14541471
UpdatedAt: now,
1455-
CachedPlan: jobType.TemplateImport.Plan,
1456-
CachedModuleFiles: uuid.NullUUID{Valid: true, UUID: file.ID},
1472+
CachedPlan: plan,
1473+
CachedModuleFiles: fileID,
14571474
})
14581475
if err != nil {
14591476
return nil, xerrors.Errorf("insert template version terraform data: %w", err)

0 commit comments

Comments
 (0)