Skip to content

Commit

Permalink
Merge pull request #534 from harness/PL-38654-harness-terraform-provi…
Browse files Browse the repository at this point in the history
…der-crashes-when-creating-secret-file

PL-38654 Secret File Not Getting Created with Tags separated by comma
  • Loading branch information
adiyaar24 committed May 11, 2023
2 parents deea93c + 991f9c4 commit 6590ed3
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .changelog/534.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:fix
resource/harness_platform_secret_file: Fixed Bug while creating this resource with tags separated by comma led to crashing.
```
27 changes: 20 additions & 7 deletions internal/service/platform/secret/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,21 +121,34 @@ func buildSpec(d *schema.ResourceData) string {

func buildTag(no_of_tags int, tags *schema.Set) string {
result := "{"
tagMap := make(map[string]string)
for i := 0; i < tags.Len(); i++ {
tag := fmt.Sprintf("%v", tags.List()[i])
split_tag := strings.Split(tag, ":")
key := split_tag[0]
value := split_tag[1]
if i == tags.Len()-1 {
result = result + fmt.Sprintf(`"%[1]s":"%[2]s"`, key, value)
if strings.Contains(tag, ":") {
splitTag := strings.Split(tag, ":")
key := splitTag[0]
value := splitTag[1]
tagMap[key] = value
} else {
result = result + fmt.Sprintf(`"%[1]s":"%[2]s,",`, key, value)
tagMap[tag] = ""
}
}
result = result + "}"

first := true
for key, value := range tagMap {
if !first {
result += ","
} else {
first = false
}
result += fmt.Sprintf(`"%s":"%s"`, key, value)
}

result += "}"
return result
}


func readSecretFile(d *schema.ResourceData, secret *nextgen.Secret) error {
d.Set("secret_manager_identifier", secret.File.SecretManagerIdentifier)
d.SetId(secret.Identifier)
Expand Down
59 changes: 59 additions & 0 deletions internal/service/platform/secret/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,52 @@ func TestAccSecretFile(t *testing.T) {
})
}

func TestAccSecretFileWMultipleTags(t *testing.T) {

id := fmt.Sprintf("%s_%s", t.Name(), utils.RandStringBytes(5))
name := id
updatedName := fmt.Sprintf("%s_updated", name)
resourceName := "harness_platform_secret_file.test"

resource.UnitTest(t, resource.TestCase{
PreCheck: func() { acctest.TestAccPreCheck(t) },
ProviderFactories: acctest.ProviderFactories,
CheckDestroy: testAccSecretDestroy(resourceName),
Steps: []resource.TestStep{
{
Config: testAccResourceSecret_fileMultipleTags(id, name),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "id", id),
resource.TestCheckResourceAttr(resourceName, "identifier", id),
resource.TestCheckResourceAttr(resourceName, "name", name),
resource.TestCheckResourceAttr(resourceName, "description", "test"),
resource.TestCheckResourceAttr(resourceName, "tags.#", "4"),
resource.TestCheckResourceAttr(resourceName, "secret_manager_identifier", "harnessSecretManager"),
),
},
{
Config: testAccResourceSecret_fileMultipleTags(id, updatedName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "id", id),
resource.TestCheckResourceAttr(resourceName, "identifier", id),
resource.TestCheckResourceAttr(resourceName, "name", updatedName),
resource.TestCheckResourceAttr(resourceName, "description", "test"),
resource.TestCheckResourceAttr(resourceName, "tags.#", "4"),
resource.TestCheckResourceAttr(resourceName, "secret_manager_identifier", "harnessSecretManager"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"file_path",
},
},
},
})
}

func TestAccSecretFileOrgLevel(t *testing.T) {

id := fmt.Sprintf("%s_%s", t.Name(), utils.RandStringBytes(5))
Expand Down Expand Up @@ -227,6 +273,19 @@ func testAccResourceSecret_file(id string, name string) string {
`, id, name, getAbsFilePath("../../../acctest/secret_files/secret.txt"))
}

func testAccResourceSecret_fileMultipleTags(id string, name string) string {
return fmt.Sprintf(`
resource "harness_platform_secret_file" "test" {
identifier = "%[1]s"
name = "%[2]s"
description = "test"
tags = ["foo:bar","connector","gcp:sm","tf"]
file_path = "%[3]s"
secret_manager_identifier = "harnessSecretManager"
}
`, id, name, getAbsFilePath("../../../acctest/secret_files/secret.txt"))
}

func getAbsFilePath(file_path string) string {
absPath, _ := filepath.Abs(file_path)
return absPath
Expand Down

0 comments on commit 6590ed3

Please sign in to comment.