Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions internal/servicedeployer/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -113,15 +113,14 @@ 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 {
return nil, fmt.Errorf("could not create Docker Compose project for service: %w", err)
}

// 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)
}
Expand All @@ -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)
}
Expand All @@ -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
}

Expand Down
30 changes: 26 additions & 4 deletions internal/servicedeployer/terraform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestAddTerraformOutputs(t *testing.T) {
content: []byte(
``,
),
expectedProps: map[string]interface{}{},
expectedProps: nil,
expectedError: true,
},
{
Expand All @@ -43,7 +43,7 @@ func TestAddTerraformOutputs(t *testing.T) {
content: []byte(
`{}`,
),
expectedProps: map[string]interface{}{},
expectedProps: nil,
},
{
testName: "single_value_output",
Expand All @@ -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",
Expand Down Expand Up @@ -138,15 +161,14 @@ func TestAddTerraformOutputs(t *testing.T) {
for _, tc := range testCases {

t.Run(tc.testName, func(t *testing.T) {
tc.svcInfo.CustomProperties = make(map[string]interface{})
Copy link
Contributor Author

@mrodm mrodm Apr 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As now it is used as reference in addTerraformOutputs, there is no need to create the map beforehand.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 this was indeed cheating the test.

tc.svcInfo.OutputDir = t.TempDir()

if err := os.WriteFile(tc.svcInfo.OutputDir+"/tfOutputValues.json", tc.content, 0777); err != nil {
t.Fatal(err)
}

// 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
Expand Down