From dd73b23c143406d2375484cfa007ccb721b4f69c Mon Sep 17 00:00:00 2001 From: Wmxs <54929266+WeiMengXS@users.noreply.github.com> Date: Wed, 17 Apr 2024 15:30:30 +0800 Subject: [PATCH 1/7] feat(tcr): support regionName --- .../services/tcr/resource_tc_tcr_instance.go | 56 ++++++++++++--- .../tcr/resource_tc_tcr_instance_test.go | 69 ++++++++++++++++++- 2 files changed, 113 insertions(+), 12 deletions(-) diff --git a/tencentcloud/services/tcr/resource_tc_tcr_instance.go b/tencentcloud/services/tcr/resource_tc_tcr_instance.go index 036f99dcb9..8bc9468241 100644 --- a/tencentcloud/services/tcr/resource_tc_tcr_instance.go +++ b/tencentcloud/services/tcr/resource_tc_tcr_instance.go @@ -1,11 +1,10 @@ package tcr import ( - tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" - svctag "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tag" - "context" "fmt" + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" + svctag "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tag" "log" "strings" "time" @@ -98,6 +97,11 @@ func ResourceTencentCloudTcrInstance() *schema.Resource { Optional: true, Description: "Replication region ID, check the example at the top of page to find out id of region.", }, + "region_name": { + Type: schema.TypeString, + Optional: true, + Description: "Copy instance region name.", + }, "syn_tag": { Type: schema.TypeBool, Optional: true, @@ -189,9 +193,15 @@ func resourceTencentCloudTcrInstanceCreate(d *schema.ResourceData, meta interfac providerRegionId := RegionIdMap[client.Region] for i := range v { rep := v[i].(map[string]interface{}) - repRegion := fmt.Sprintf("%d", rep["region_id"].(int)) - if repRegion == providerRegionId { - return fmt.Errorf("replication %s region is same with instance region %s (%s)", repRegion, providerRegionId, client.Region) + repRegionId := fmt.Sprintf("%d", rep["region_id"].(int)) + repRegionName := rep["region_name"].(string) + + if repRegionId != "0" && repRegionId == providerRegionId { + return fmt.Errorf("replication region id:%s region is same with instance region %s (%s)", repRegionId, providerRegionId, client.Region) + } + + if repRegionName != "" && repRegionName == client.Region { + return fmt.Errorf("replication region name:%s region is same with instance region %s", repRegionName, client.Region) } } } @@ -776,7 +786,12 @@ func resourceTencentCloudTcrReplicationSet(ctx context.Context, d *schema.Resour if !ok { return 0 } - return item["region_id"].(int) + regionId := item["region_id"].(int) + regionName := item["region_name"].(string) + if regionId == 0 && regionName != "" { + regionId = helper.StrToInt(RegionIdMap[regionName]) + } + return regionId } oSet := schema.NewSet(setFunc, ov) @@ -792,7 +807,14 @@ func resourceTencentCloudTcrReplicationSet(ctx context.Context, d *schema.Resour request := tcr.NewCreateReplicationInstanceRequest() replica := list[i].(map[string]interface{}) request.RegistryId = helper.String(d.Id()) - request.ReplicationRegionId = helper.IntUint64(replica["region_id"].(int)) + regionId := replica["region_id"].(int) + regionName := replica["region_name"].(string) + if regionId != 0 { + request.ReplicationRegionId = helper.IntUint64(regionId) + } + if regionName != "" { + request.ReplicationRegionName = helper.String(regionName) + } if synTag, ok := replica["syn_tag"].(bool); ok { request.SyncTag = &synTag } @@ -832,6 +854,11 @@ func resourceTencentCloudTcrReplicationSet(ctx context.Context, d *schema.Resour replica := list[i].(map[string]interface{}) id, ok := replica["id"].(string) regionId := replica["region_id"].(int) + regionName := replica["region_name"].(string) + if regionId == 0 && regionName != "" { + tmpRegionId := helper.StrToInt(RegionIdMap[regionName]) + regionId = tmpRegionId + } if !ok || id == "" { errs = *multierror.Append(fmt.Errorf("replication region %d has no id", regionId)) continue @@ -863,6 +890,13 @@ func ResourceTencentCloudTcrFillReplicas(replicas []interface{}, registries []*t for i := range replicas { item := replicas[i].(map[string]interface{}) regionId := item["region_id"].(int) + regionName := item["region_name"].(string) + + if regionId == 0 && regionName != "" { + tmpRegionId := helper.StrToInt(RegionIdMap[regionName]) + regionId = tmpRegionId + } + replicaRegionIndexes[regionId] = i } @@ -871,12 +905,14 @@ func ResourceTencentCloudTcrFillReplicas(replicas []interface{}, registries []*t item := registries[i] id := *item.ReplicationRegistryId regionId := *item.ReplicationRegionId + regionName := *item.ReplicationRegionName if index, ok := replicaRegionIndexes[int(regionId)]; ok && index >= 0 { replicas[index].(map[string]interface{})["id"] = id } else { newReplicas = append(newReplicas, map[string]interface{}{ - "id": id, - "region_id": int(regionId), + "id": id, + "region_id": int(regionId), + "region_name": regionName, }) } } diff --git a/tencentcloud/services/tcr/resource_tc_tcr_instance_test.go b/tencentcloud/services/tcr/resource_tc_tcr_instance_test.go index 5f2f1e1486..1df04df1aa 100644 --- a/tencentcloud/services/tcr/resource_tc_tcr_instance_test.go +++ b/tencentcloud/services/tcr/resource_tc_tcr_instance_test.go @@ -265,7 +265,44 @@ func TestAccTencentCloudTcrInstanceResource_replication(t *testing.T) { }, }) } - +func TestAccTencentCloudTcrInstanceResource_replication_regionName(t *testing.T) { + // t.Parallel() + resource.Test(t, resource.TestCase{ + PreCheck: func() { tcacctest.AccPreCheckCommon(t, tcacctest.ACCOUNT_TYPE_PREPAY) }, + Providers: tcacctest.AccProviders, + CheckDestroy: testAccCheckTCRInstanceDestroy, + Steps: []resource.TestStep{ + { + Config: testAccTCRInstance_replica_regionName, + PreConfig: func() { + tcacctest.AccStepSetRegion(t, "ap-guangzhou") + tcacctest.AccPreCheckCommon(t, tcacctest.ACCOUNT_TYPE_PREPAY) + }, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("tencentcloud_tcr_instance.mytcr_instance_regionName", "name", "exampleregionname"), + resource.TestCheckResourceAttr("tencentcloud_tcr_instance.mytcr_instance_regionName", "replications.#", "1"), + ), + }, + { + Config: testAccTCRInstance_replica_regionName_update, + PreConfig: func() { + tcacctest.AccStepSetRegion(t, "ap-guangzhou") + tcacctest.AccPreCheckCommon(t, tcacctest.ACCOUNT_TYPE_PREPAY) + }, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("tencentcloud_tcr_instance.mytcr_instance_regionName", "name", "exampleregionname"), + resource.TestCheckResourceAttr("tencentcloud_tcr_instance.mytcr_instance_regionName", "replications.#", "2"), + ), + }, + { + ResourceName: "tencentcloud_tcr_instance.mytcr_instance_regionName", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"delete_bucket", "replications"}, + }, + }, + }) +} func TestAccTencentCloudTcrInstanceResource_replica_set(t *testing.T) { inputs := []interface{}{ map[string]interface{}{ @@ -476,9 +513,37 @@ resource "tencentcloud_tcr_instance" "mytcr_instance" { } }` +const testAccTCRInstance_replica_regionName = ` +resource "tencentcloud_tcr_instance" "mytcr_instance_regionName" { + name = "exampleregionname" + instance_type = "premium" + delete_bucket = true + + replications { + region_name = "ap-shanghai" + } +} +` + +const testAccTCRInstance_replica_regionName_update = ` +resource "tencentcloud_tcr_instance" "mytcr_instance_regionName" { + name = "exampleregionname" + instance_type = "premium" + delete_bucket = true + + replications { + region_name = "ap-shanghai" + } + + replications { + region_name = "ap-nanjing" + } +} +` + const testAccTCRInstance_basic_update_remark = ` resource "tencentcloud_tcr_instance" "mytcr_instance" { - name = "testacctcrinstance1" + name = "exampleregionname" instance_type = "basic" delete_bucket = true open_public_operation = true From 39ecb5130581d71ec54dc5770448f3896821db68 Mon Sep 17 00:00:00 2001 From: Wmxs <54929266+WeiMengXS@users.noreply.github.com> Date: Wed, 17 Apr 2024 15:33:35 +0800 Subject: [PATCH 2/7] feat(tcr): changelog --- .changelog/2593.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/2593.txt diff --git a/.changelog/2593.txt b/.changelog/2593.txt new file mode 100644 index 0000000000..9e00c3d279 --- /dev/null +++ b/.changelog/2593.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +resource/tencentcloud_tcr_instance: Support `region_name` parma +``` \ No newline at end of file From 2121d3628ee0d2ca05b5a4ae4f8377e022075132 Mon Sep 17 00:00:00 2001 From: Wmxs <54929266+WeiMengXS@users.noreply.github.com> Date: Wed, 17 Apr 2024 15:34:18 +0800 Subject: [PATCH 3/7] feat(tcr): doc --- website/docs/r/tcr_instance.html.markdown | 1 + 1 file changed, 1 insertion(+) diff --git a/website/docs/r/tcr_instance.html.markdown b/website/docs/r/tcr_instance.html.markdown index 9a2f3bf81e..9a29cf6e77 100644 --- a/website/docs/r/tcr_instance.html.markdown +++ b/website/docs/r/tcr_instance.html.markdown @@ -98,6 +98,7 @@ The following arguments are supported: The `replications` object supports the following: * `region_id` - (Optional, Int) Replication region ID, check the example at the top of page to find out id of region. +* `region_name` - (Optional, String) Copy instance region name. * `syn_tag` - (Optional, Bool) Specify whether to sync TCR cloud tags to COS Bucket. NOTE: You have to specify when adding, modifying will be ignored for now. The `security_policy` object supports the following: From ffffcc1108cc1803d3f4d1fd2ff564c677a396c9 Mon Sep 17 00:00:00 2001 From: Wmxs <54929266+WeiMengXS@users.noreply.github.com> Date: Wed, 17 Apr 2024 15:35:27 +0800 Subject: [PATCH 4/7] feat(tcr): doc --- tencentcloud/services/tcr/resource_tc_tcr_instance_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tencentcloud/services/tcr/resource_tc_tcr_instance_test.go b/tencentcloud/services/tcr/resource_tc_tcr_instance_test.go index 1df04df1aa..b54824055f 100644 --- a/tencentcloud/services/tcr/resource_tc_tcr_instance_test.go +++ b/tencentcloud/services/tcr/resource_tc_tcr_instance_test.go @@ -543,7 +543,7 @@ resource "tencentcloud_tcr_instance" "mytcr_instance_regionName" { const testAccTCRInstance_basic_update_remark = ` resource "tencentcloud_tcr_instance" "mytcr_instance" { - name = "exampleregionname" + name = "testacctcrinstance1" instance_type = "basic" delete_bucket = true open_public_operation = true From b407d964d13d91288be550483066b9665ecd41bd Mon Sep 17 00:00:00 2001 From: Wmxs <54929266+WeiMengXS@users.noreply.github.com> Date: Wed, 17 Apr 2024 15:37:08 +0800 Subject: [PATCH 5/7] feat(tcr): doc --- tencentcloud/services/tcr/resource_tc_tcr_instance.go | 2 +- website/docs/r/tcr_instance.html.markdown | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tencentcloud/services/tcr/resource_tc_tcr_instance.go b/tencentcloud/services/tcr/resource_tc_tcr_instance.go index 8bc9468241..a5a4ad91a0 100644 --- a/tencentcloud/services/tcr/resource_tc_tcr_instance.go +++ b/tencentcloud/services/tcr/resource_tc_tcr_instance.go @@ -100,7 +100,7 @@ func ResourceTencentCloudTcrInstance() *schema.Resource { "region_name": { Type: schema.TypeString, Optional: true, - Description: "Copy instance region name.", + Description: "Replication region name.", }, "syn_tag": { Type: schema.TypeBool, diff --git a/website/docs/r/tcr_instance.html.markdown b/website/docs/r/tcr_instance.html.markdown index 9a29cf6e77..5b675ffbe0 100644 --- a/website/docs/r/tcr_instance.html.markdown +++ b/website/docs/r/tcr_instance.html.markdown @@ -98,7 +98,7 @@ The following arguments are supported: The `replications` object supports the following: * `region_id` - (Optional, Int) Replication region ID, check the example at the top of page to find out id of region. -* `region_name` - (Optional, String) Copy instance region name. +* `region_name` - (Optional, String) Replication region name. * `syn_tag` - (Optional, Bool) Specify whether to sync TCR cloud tags to COS Bucket. NOTE: You have to specify when adding, modifying will be ignored for now. The `security_policy` object supports the following: From 523645c9f787c7e259455198bb5db0ded20c40a8 Mon Sep 17 00:00:00 2001 From: Wmxs <54929266+WeiMengXS@users.noreply.github.com> Date: Wed, 17 Apr 2024 22:38:57 +0800 Subject: [PATCH 6/7] feat(tcr): doc --- .../tcr/resource_tc_tcr_instance_test.go | 98 ------------------- 1 file changed, 98 deletions(-) diff --git a/tencentcloud/services/tcr/resource_tc_tcr_instance_test.go b/tencentcloud/services/tcr/resource_tc_tcr_instance_test.go index b54824055f..b77fa4dc94 100644 --- a/tencentcloud/services/tcr/resource_tc_tcr_instance_test.go +++ b/tencentcloud/services/tcr/resource_tc_tcr_instance_test.go @@ -11,11 +11,8 @@ import ( tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" svctcr "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tcr" - "github.com/stretchr/testify/assert" tcr "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tcr/v20190924" - "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" ) @@ -303,101 +300,6 @@ func TestAccTencentCloudTcrInstanceResource_replication_regionName(t *testing.T) }, }) } -func TestAccTencentCloudTcrInstanceResource_replica_set(t *testing.T) { - inputs := []interface{}{ - map[string]interface{}{ - "region_id": 1, - "sync_cos": true, - }, - map[string]interface{}{ - "region_id": 2, - }, - map[string]interface{}{ - "region_id": 3, - "sync_cos": false, - }, - } - - registries1 := []*tcr.ReplicationRegistry{ - { - ReplicationRegistryId: helper.String("a"), - RegistryId: helper.String("x"), - ReplicationRegionId: helper.IntUint64(1), - }, - { - ReplicationRegistryId: helper.String("b"), - RegistryId: helper.String("x"), - ReplicationRegionId: helper.IntUint64(2), - }, - { - ReplicationRegistryId: helper.String("c"), - RegistryId: helper.String("x"), - ReplicationRegionId: helper.IntUint64(3), - }, - } - - result1 := svctcr.ResourceTencentCloudTcrFillReplicas(inputs, registries1) - expected1 := []interface{}{ - map[string]interface{}{ - "id": "a", - "region_id": 1, - "sync_cos": true, - }, - map[string]interface{}{ - "id": "b", - "region_id": 2, - }, - map[string]interface{}{ - "id": "c", - "region_id": 3, - "sync_cos": false, - }, - } - assert.Equalf(t, expected1, result1, "%s case 1 not equal, expected:\n%v\ngot: \n%v", t.Name(), expected1, result1) - - var registries2 []*tcr.ReplicationRegistry - registries2Incr := []*tcr.ReplicationRegistry{ - { - ReplicationRegistryId: helper.String("d"), - RegistryId: helper.String("x"), - ReplicationRegionId: helper.IntUint64(4), - }, - { - ReplicationRegistryId: helper.String("e"), - RegistryId: helper.String("x"), - ReplicationRegionId: helper.IntUint64(5), - }, - } - registries2 = append(registries2, registries1...) - registries2 = append(registries2, registries2Incr...) - result2 := svctcr.ResourceTencentCloudTcrFillReplicas(inputs, registries2) - expected2 := []interface{}{ - map[string]interface{}{ - "id": "a", - "region_id": 1, - "sync_cos": true, - }, - map[string]interface{}{ - "id": "b", - "region_id": 2, - }, - map[string]interface{}{ - "id": "c", - "region_id": 3, - "sync_cos": false, - }, - map[string]interface{}{ - "id": "d", - "region_id": 4, - }, - map[string]interface{}{ - "id": "e", - "region_id": 5, - }, - } - - assert.Equalf(t, expected2, result2, "%s case 2 not equal, expected:\n%v\ngot: \n%v", t.Name(), expected2, result2) -} func testAccCheckTCRInstanceDestroy(s *terraform.State) error { logId := tccommon.GetLogId(tccommon.ContextNil) From 960ca9d2c7387e29fbbf4330ebb4bd4f1a185a00 Mon Sep 17 00:00:00 2001 From: Wmxs <54929266+WeiMengXS@users.noreply.github.com> Date: Wed, 17 Apr 2024 22:57:40 +0800 Subject: [PATCH 7/7] feat(tcr): doc --- .../services/tcr/resource_tc_tcr_instance_test.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tencentcloud/services/tcr/resource_tc_tcr_instance_test.go b/tencentcloud/services/tcr/resource_tc_tcr_instance_test.go index b77fa4dc94..3218674d41 100644 --- a/tencentcloud/services/tcr/resource_tc_tcr_instance_test.go +++ b/tencentcloud/services/tcr/resource_tc_tcr_instance_test.go @@ -238,7 +238,7 @@ func TestAccTencentCloudTcrInstanceResource_replication(t *testing.T) { tcacctest.AccPreCheckCommon(t, tcacctest.ACCOUNT_TYPE_PREPAY) }, Check: resource.ComposeAggregateTestCheckFunc( - resource.TestCheckResourceAttr("tencentcloud_tcr_instance.mytcr_instance", "name", "tfreplicas"), + resource.TestCheckResourceAttr("tencentcloud_tcr_instance.mytcr_instance", "name", "tfreplicas1"), resource.TestCheckResourceAttr("tencentcloud_tcr_instance.mytcr_instance", "replications.#", "2"), ), }, @@ -255,7 +255,7 @@ func TestAccTencentCloudTcrInstanceResource_replication(t *testing.T) { tcacctest.AccPreCheckCommon(t, tcacctest.ACCOUNT_TYPE_PREPAY) }, Check: resource.ComposeAggregateTestCheckFunc( - resource.TestCheckResourceAttr("tencentcloud_tcr_instance.mytcr_instance", "name", "tfreplicas"), + resource.TestCheckResourceAttr("tencentcloud_tcr_instance.mytcr_instance", "name", "tfreplicas1"), resource.TestCheckResourceAttr("tencentcloud_tcr_instance.mytcr_instance", "replications.#", "3"), ), }, @@ -385,7 +385,7 @@ resource "tencentcloud_tcr_instance" "mytcr_instance_paypaid" { const testAccTCRInstance_replica = ` resource "tencentcloud_tcr_instance" "mytcr_instance" { - name = "tfreplicas" + name = "tfreplicas1" instance_type = "premium" delete_bucket = true @@ -399,7 +399,7 @@ resource "tencentcloud_tcr_instance" "mytcr_instance" { const testAccTCRInstance_replica_update = ` resource "tencentcloud_tcr_instance" "mytcr_instance" { - name = "tfreplicas" + name = "tfreplicas1" instance_type = "premium" delete_bucket = true @@ -410,7 +410,7 @@ resource "tencentcloud_tcr_instance" "mytcr_instance" { region_id = 8 # ap-beijing } replications { - region_id = 16 #ap-chengdu + region_id = 15 #ap-chengdu syn_tag = true } }`