diff --git a/internal/configcat/setting_tag_resource.go b/internal/configcat/setting_tag_resource.go index 26577b72..060448a8 100644 --- a/internal/configcat/setting_tag_resource.go +++ b/internal/configcat/setting_tag_resource.go @@ -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 @@ -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) diff --git a/internal/configcat/setting_tag_resource_test.go b/internal/configcat/setting_tag_resource_test.go index a156a67d..854332f6 100644 --- a/internal/configcat/setting_tag_resource_test.go +++ b/internal/configcat/setting_tag_resource_test.go @@ -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"), + }, }, }) } diff --git a/internal/configcat/testdata/TestAccSettingTagMultipleResource/init.tf b/internal/configcat/testdata/TestAccSettingTagMultipleResource/init.tf new file mode 100644 index 00000000..ea4afa1d --- /dev/null +++ b/internal/configcat/testdata/TestAccSettingTagMultipleResource/init.tf @@ -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 +} + diff --git a/internal/configcat/testdata/TestAccSettingTagMultipleResource/removeeverything.tf b/internal/configcat/testdata/TestAccSettingTagMultipleResource/removeeverything.tf new file mode 100644 index 00000000..e69de29b diff --git a/internal/configcat/testdata/TestAccSettingTagMultipleResource/removeonetag.tf b/internal/configcat/testdata/TestAccSettingTagMultipleResource/removeonetag.tf new file mode 100644 index 00000000..e4787bda --- /dev/null +++ b/internal/configcat/testdata/TestAccSettingTagMultipleResource/removeonetag.tf @@ -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 +} + diff --git a/internal/configcat/testdata/TestAccSettingTagResource/cleanup.tf b/internal/configcat/testdata/TestAccSettingTagResource/cleanup.tf new file mode 100644 index 00000000..e69de29b