diff --git a/internal/servicedeployer/terraform.go b/internal/servicedeployer/terraform.go index 67430e2a6a..9c0be72e1f 100644 --- a/internal/servicedeployer/terraform.go +++ b/internal/servicedeployer/terraform.go @@ -48,7 +48,7 @@ type TerraformServiceDeployer struct { // addTerraformOutputs method reads the terraform outputs generated in the json format and // adds them to the custom properties of ServiceInfo and can be used in the handlebars template // like `{{TF_OUTPUT_queue_url}}` where `queue_url` is the output configured -func addTerraformOutputs(svcInfo ServiceInfo) error { +func addTerraformOutputs(svcInfo *ServiceInfo) error { // Read the `output.json` file where terraform outputs are generated outputFile := filepath.Join(svcInfo.OutputDir, terraformOutputJsonFile) content, err := os.ReadFile(outputFile) @@ -113,7 +113,6 @@ func (tsd TerraformServiceDeployer) SetUp(ctx context.Context, svcInfo ServiceIn env: tfEnvironment, shutdownTimeout: 300 * time.Second, } - outCtxt := svcInfo p, err := compose.NewProject(service.project, service.ymlPaths...) if err != nil { @@ -121,7 +120,7 @@ func (tsd TerraformServiceDeployer) SetUp(ctx context.Context, svcInfo ServiceIn } // Clean service logs - err = files.RemoveContent(outCtxt.Logs.Folder.Local) + err = files.RemoveContent(svcInfo.Logs.Folder.Local) if err != nil { return nil, fmt.Errorf("removing service logs failed: %w", err) } @@ -134,7 +133,7 @@ func (tsd TerraformServiceDeployer) SetUp(ctx context.Context, svcInfo ServiceIn if err != nil { return nil, fmt.Errorf("could not get Docker Compose configuration for service: %w", err) } - outCtxt.CustomProperties, err = buildTerraformAliases(serviceComposeConfig) + svcInfo.CustomProperties, err = buildTerraformAliases(serviceComposeConfig) if err != nil { return nil, fmt.Errorf("can't build Terraform aliases: %w", err) } @@ -153,18 +152,18 @@ func (tsd TerraformServiceDeployer) SetUp(ctx context.Context, svcInfo ServiceIn if err != nil { processServiceContainerLogs(ctx, p, compose.CommandOptions{ Env: opts.Env, - }, outCtxt.Name) + }, svcInfo.Name) //lint:ignore ST1005 error starting with product name can be capitalized return nil, fmt.Errorf("Terraform deployer is unhealthy: %w", err) } - outCtxt.Agent.Host.NamePrefix = "docker-fleet-agent" + svcInfo.Agent.Host.NamePrefix = "docker-fleet-agent" - err = addTerraformOutputs(outCtxt) + err = addTerraformOutputs(&svcInfo) if err != nil { return nil, fmt.Errorf("could not handle terraform output: %w", err) } - service.svcInfo = outCtxt + service.svcInfo = svcInfo return &service, nil } diff --git a/internal/servicedeployer/terraform_test.go b/internal/servicedeployer/terraform_test.go index 3b06c8a977..a1c5b2f291 100644 --- a/internal/servicedeployer/terraform_test.go +++ b/internal/servicedeployer/terraform_test.go @@ -31,7 +31,7 @@ func TestAddTerraformOutputs(t *testing.T) { content: []byte( ``, ), - expectedProps: map[string]interface{}{}, + expectedProps: nil, expectedError: true, }, { @@ -43,7 +43,7 @@ func TestAddTerraformOutputs(t *testing.T) { content: []byte( `{}`, ), - expectedProps: map[string]interface{}{}, + expectedProps: nil, }, { testName: "single_value_output", @@ -64,6 +64,29 @@ func TestAddTerraformOutputs(t *testing.T) { "TF_OUTPUT_queue_url": "https://sqs.us-east-1.amazonaws.com/1234654/elastic-package-aws-logs-queue-someId", }, }, + { + testName: "add_single_value_output", + runId: "99999", + svcInfo: ServiceInfo{ + Test: struct{ RunID string }{"99999"}, + CustomProperties: map[string]interface{}{ + "TF_foo": "bar", + }, + }, + content: []byte( + `{ + "queue_url": { + "sensitive": false, + "type": "string", + "value": "https://sqs.us-east-1.amazonaws.com/1234654/elastic-package-aws-logs-queue-someId" + } + }`, + ), + expectedProps: map[string]interface{}{ + "TF_OUTPUT_queue_url": "https://sqs.us-east-1.amazonaws.com/1234654/elastic-package-aws-logs-queue-someId", + "TF_foo": "bar", + }, + }, { testName: "multiple_value_output", runId: "23465", @@ -138,7 +161,6 @@ func TestAddTerraformOutputs(t *testing.T) { for _, tc := range testCases { t.Run(tc.testName, func(t *testing.T) { - tc.svcInfo.CustomProperties = make(map[string]interface{}) tc.svcInfo.OutputDir = t.TempDir() if err := os.WriteFile(tc.svcInfo.OutputDir+"/tfOutputValues.json", tc.content, 0777); err != nil { @@ -146,7 +168,7 @@ func TestAddTerraformOutputs(t *testing.T) { } // Test that the terraform output values are generated correctly - err := addTerraformOutputs(tc.svcInfo) + err := addTerraformOutputs(&tc.svcInfo) if tc.expectedError { require.Error(t, err) return