@@ -1432,28 +1432,45 @@ func (s *server) CompleteJob(ctx context.Context, completed *proto.CompletedJob)
1432
1432
return nil ,xerrors .Errorf ("update template version external auth providers: %w" ,err )
1433
1433
}
1434
1434
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
+ }
1450
1467
}
1451
1468
1452
1469
err = s .Database .InsertTemplateVersionTerraformValuesByJobID (ctx , database.InsertTemplateVersionTerraformValuesByJobIDParams {
1453
1470
JobID :jobID ,
1454
1471
UpdatedAt :now ,
1455
- CachedPlan :jobType . TemplateImport . Plan ,
1456
- CachedModuleFiles :uuid. NullUUID { Valid : true , UUID : file . ID } ,
1472
+ CachedPlan :plan ,
1473
+ CachedModuleFiles :fileID ,
1457
1474
})
1458
1475
if err != nil {
1459
1476
return nil ,xerrors .Errorf ("insert template version terraform data: %w" ,err )