Skip to content

Commit

Permalink
feat(terraform-recipe): resource group tagging
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisBiondic committed Jul 18, 2022
1 parent 2600f80 commit 16e1a0d
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ pkg/hq/test.logs
pkg/recipes/terraform/test.tfvars

cmd/example-infra/example-infra.log

pkg/recipes/terraform/.plans/
2 changes: 2 additions & 0 deletions pkg/recipes/terraform/backend_storage_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package terraform

type BackendStorageSettings struct {
CreateResourceGroup bool
ResourceGroupTags map[string]string
BlobContainerName string
BlobContainerKey string
}

var DefaultBackendStorageSettings = BackendStorageSettings{
CreateResourceGroup: true,
ResourceGroupTags: map[string]string{},
BlobContainerName: "tfstate",
BlobContainerKey: "terraform.tfstate",
}
18 changes: 18 additions & 0 deletions pkg/recipes/terraform/tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package terraform

import (
"fmt"
"strings"
)

// serializeTagsIntoAzureCliString converts the given tags from the map to a text that can be passed to an azure cli command
// with the --tags parameter
// The key and value will be quoted e.g. "This is a key"="This is a value" (to support key and value with spaces)
func serializeTagsIntoAzureCliString(tags map[string]string) string {
var tagTexts []string
for key, value := range tags {
tagTexts = append(tagTexts, fmt.Sprintf("%q=%q", key, value))
}

return strings.Join(tagTexts, " ")
}
19 changes: 19 additions & 0 deletions pkg/recipes/terraform/tags_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package terraform

import (
"github.com/stretchr/testify/assert"
"testing"
)

func Test_TagSerialization(t *testing.T) {
result := serializeTagsIntoAzureCliString(map[string]string{
"tag1": "valueA",
"tag_b": "long string value",
"tag-c": "123",
})

// has to be asserted separately, because iterating go maps does not guarantee order!
assert.Contains(t, result, "\"tag1\"=\"valueA\"", result)
assert.Contains(t, result, "\"tag_b\"=\"long string value\"", result)
assert.Contains(t, result, "\"tag-c\"=\"123\"", result)
}
8 changes: 7 additions & 1 deletion pkg/recipes/terraform/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,13 @@ type terraformWrapper struct {
func (tf *terraformWrapper) Init() error {
if tf.storageSettings.CreateResourceGroup {
logrus.Info("Deploying the project " + tf.projectName + " resource group " + tf.resourceGroupName + "...")
_, err := tf.executor.Execute("az group create -l " + tf.region + " -n " + tf.resourceGroupName)

optionalTags := ""
if len(tf.storageSettings.ResourceGroupTags) > 0 {
optionalTags = " --tags " + serializeTagsIntoAzureCliString(tf.storageSettings.ResourceGroupTags)
}

_, err := tf.executor.Execute("az group create -l " + tf.region + " -n " + tf.resourceGroupName + optionalTags)

if err != nil {
return internal.ReturnErrorOrPanic(err)
Expand Down
6 changes: 5 additions & 1 deletion pkg/recipes/terraform/terraform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,14 @@ type variablesStruct struct {
func createSimpleTerraformWithDefaultSettings(projectName string) (Terraform, *executorMock) {
executor := &executorMock{}

backendStorageSettings := DefaultBackendStorageSettings
backendStorageSettings.ResourceGroupTags["test"] = "value"

return New(executor, projectName, "1234", "3214",
"westeurope", "testrg", "storeaccount",
// important to keep the current directory configured, since some tests rely on this
// location to verify that expected directories / files are created
filepath.Join("."),
DefaultBackendStorageSettings, DefaultDeploymentSettings), executor
backendStorageSettings,
DefaultDeploymentSettings), executor
}

0 comments on commit 16e1a0d

Please sign in to comment.