Skip to content

Commit

Permalink
refactor remove duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
HuyPhanNguyen committed Aug 12, 2024
1 parent c8953c9 commit 625200c
Showing 1 changed file with 6 additions and 33 deletions.
39 changes: 6 additions & 33 deletions octopusdeploy_framework/resource_tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ func TestAccTag(t *testing.T) {
CheckDestroy: testAccTagDestroy,
Steps: []resource.TestStep{
{
Config: testAccTagConfig(tagSetName, tagName, tagColor),
Config: testTagConfig(tagSetName, tagName, tagColor),
Check: resource.ComposeTestCheckFunc(
testAccTagExists(tagResourceName),
testTagExists(tagResourceName),
resource.TestCheckResourceAttr(tagResourceName, "name", tagName),
resource.TestCheckResourceAttr(tagResourceName, "color", tagColor),
resource.TestCheckResourceAttrSet(tagResourceName, "id"),
Expand All @@ -33,9 +33,9 @@ func TestAccTag(t *testing.T) {
),
},
{
Config: testAccTagConfigUpdate(tagSetName, tagName, "#ff0000"),
Config: testTagConfigUpdate(tagSetName, tagName, "#ff0000"),
Check: resource.ComposeTestCheckFunc(
testAccTagExists(tagResourceName),
testTagExists(tagResourceName),
resource.TestCheckResourceAttr(tagResourceName, "name", tagName),
resource.TestCheckResourceAttr(tagResourceName, "color", "#ff0000"),
resource.TestCheckResourceAttrSet(tagResourceName, "id"),
Expand All @@ -53,7 +53,7 @@ func TestAccTag(t *testing.T) {
})
}

func testAccTagConfig(tagSetName, tagName, tagColor string) string {
func testTagConfig(tagSetName, tagName, tagColor string) string {
var tfConfig = fmt.Sprintf(`
resource "octopusdeploy_tag_set" "%s" {
name = "%s"
Expand All @@ -70,7 +70,7 @@ func testAccTagConfig(tagSetName, tagName, tagColor string) string {
return tfConfig
}

func testAccTagConfigUpdate(tagSetName, tagName, tagColor string) string {
func testTagConfigUpdate(tagSetName, tagName, tagColor string) string {
var tfConfig = fmt.Sprintf(`
resource "octopusdeploy_tag_set" "%s" {
name = "%s"
Expand All @@ -87,33 +87,6 @@ func testAccTagConfigUpdate(tagSetName, tagName, tagColor string) string {
return tfConfig
}

func testAccTagExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("No Tag ID is set")
}

tagSetID := rs.Primary.Attributes["tag_set_id"]
tagSet, err := tagsets.GetByID(octoClient, rs.Primary.Attributes["space_id"], tagSetID)
if err != nil {
return err
}

for _, tag := range tagSet.Tags {
if tag.ID == rs.Primary.ID {
return nil
}
}

return fmt.Errorf("Tag not found")
}
}

func testAccTagDestroy(s *terraform.State) error {

for _, rs := range s.RootModule().Resources {
Expand Down

0 comments on commit 625200c

Please sign in to comment.