Skip to content

Commit

Permalink
Setting tag remove fix (#55)
Browse files Browse the repository at this point in the history
* Setting tag remove fix

* multiple tag tests
  • Loading branch information
laliconfigcat authored Aug 9, 2024
1 parent 7417348 commit 7abfc0a
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 4 deletions.
30 changes: 26 additions & 4 deletions internal/configcat/setting_tag_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func (r *settingTagResource) Delete(ctx context.Context, req resource.DeleteRequ
}

tagIdString := state.TagId.ValueString()
_, tagIdConvErr := strconv.ParseInt(tagIdString, 10, 32)
tagID, tagIdConvErr := strconv.ParseInt(tagIdString, 10, 32)
if tagIdConvErr != nil {
resp.Diagnostics.AddError("Could not parse Tag ID", tagIdConvErr.Error())
return
Expand All @@ -197,10 +197,32 @@ func (r *settingTagResource) Delete(ctx context.Context, req resource.DeleteRequ
return
}

model, getErr := r.client.GetSetting(int32(settingId))
if getErr != nil {
if _, ok := getErr.(client.NotFoundError); ok {
// If the resource is already deleted, we consider the tag to be removed.
return
}
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read "+SettingResourceName+", got error: %s", getErr))
return
}

index := -1
for tagIndex, tag := range model.Tags {
if *tag.TagId == tagID {
index = tagIndex
break
}
}

if index == -1 {
// If the resource is already deleted, we can safely return
return
}

operations := []sw.JsonPatchOperation{{
Op: sw.OPERATIONTYPE_REMOVE,
Path: "/tags/-",
Value: tagIdString,
Op: sw.OPERATIONTYPE_REMOVE,
Path: fmt.Sprintf("/tags/%d", index),
}}

_, err := r.client.UpdateSetting(int32(settingId), operations)
Expand Down
55 changes: 55 additions & 0 deletions internal/configcat/setting_tag_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,61 @@ func TestAccSettingTagResource(t *testing.T) {
},
},
},
{
// Removing the resource should work fine.
ConfigFile: config.TestNameFile("cleanup.tf"),
},
},
})
}

func TestAccSettingTagMultipleResource(t *testing.T) {
const configId = "08dc1bfa-b8b0-45f0-8127-fac0de7a37ac"
const productId = "08d86d63-2721-4da6-8c06-584521d516bc"
const testResource1Name = "configcat_setting_tag.settingTag1"
const testResource2Name = "configcat_setting_tag.settingTag2"
const testResource3Name = "configcat_setting_tag.settingTag3"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
ConfigFile: config.TestNameFile("init.tf"),
ConfigVariables: config.Variables{
"config_id": config.StringVariable(configId),
"product_id": config.StringVariable(productId),
},
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet(testResource1Name, ID),
resource.TestCheckResourceAttrSet(testResource1Name, SettingId),
resource.TestCheckResourceAttrSet(testResource1Name, TagId),
resource.TestCheckResourceAttrSet(testResource2Name, ID),
resource.TestCheckResourceAttrSet(testResource2Name, SettingId),
resource.TestCheckResourceAttrSet(testResource2Name, TagId),
resource.TestCheckResourceAttrSet(testResource3Name, ID),
resource.TestCheckResourceAttrSet(testResource3Name, SettingId),
resource.TestCheckResourceAttrSet(testResource3Name, TagId),
),
},
{
ConfigFile: config.TestNameFile("removeonetag.tf"),
ConfigVariables: config.Variables{
"config_id": config.StringVariable(configId),
"product_id": config.StringVariable(productId),
},
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet(testResource1Name, ID),
resource.TestCheckResourceAttrSet(testResource1Name, SettingId),
resource.TestCheckResourceAttrSet(testResource1Name, TagId),
resource.TestCheckResourceAttrSet(testResource3Name, ID),
resource.TestCheckResourceAttrSet(testResource3Name, SettingId),
resource.TestCheckResourceAttrSet(testResource3Name, TagId),
),
},
{
ConfigFile: config.TestNameFile("removeeverything.tf"),
},
},
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
variable "product_id" {
type = string
}

variable "config_id" {
type = string
}

resource "configcat_setting" "testSetting" {
config_id = var.config_id
key = "testkeywithtag"
name = "test"
order = 0
}

resource "configcat_tag" "testTag1" {
product_id = var.product_id
name = "tag1"
}

resource "configcat_tag" "testTag2" {
product_id = var.product_id
name = "tag2"
}

resource "configcat_tag" "testTag3" {
product_id = var.product_id
name = "tag3"
}

resource "configcat_setting_tag" "settingTag1" {
setting_id = configcat_setting.testSetting.id
tag_id = configcat_tag.testTag1.id
}

resource "configcat_setting_tag" "settingTag2" {
setting_id = configcat_setting.testSetting.id
tag_id = configcat_tag.testTag2.id
}

resource "configcat_setting_tag" "settingTag3" {
setting_id = configcat_setting.testSetting.id
tag_id = configcat_tag.testTag3.id
}

Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
variable "product_id" {
type = string
}

variable "config_id" {
type = string
}

resource "configcat_setting" "testSetting" {
config_id = var.config_id
key = "testkeywithtag"
name = "test"
order = 0
}

resource "configcat_tag" "testTag1" {
product_id = var.product_id
name = "tag1"
}

resource "configcat_tag" "testTag2" {
product_id = var.product_id
name = "tag2"
}

resource "configcat_tag" "testTag3" {
product_id = var.product_id
name = "tag3"
}

resource "configcat_setting_tag" "settingTag1" {
setting_id = configcat_setting.testSetting.id
tag_id = configcat_tag.testTag1.id
}

resource "configcat_setting_tag" "settingTag3" {
setting_id = configcat_setting.testSetting.id
tag_id = configcat_tag.testTag3.id
}

Empty file.

0 comments on commit 7abfc0a

Please sign in to comment.