Skip to content

Commit 9012c6e

Browse files
authored
feat(tcr): [116375010]support parma regionName (#2593)
* feat(tcr): support regionName * feat(tcr): changelog * feat(tcr): doc * feat(tcr): doc * feat(tcr): doc * feat(tcr): doc * feat(tcr): doc
1 parent 6ca7637 commit 9012c6e

File tree

4 files changed

+119
-112
lines changed

4 files changed

+119
-112
lines changed

.changelog/2593.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
resource/tencentcloud_tcr_instance: Support `region_name` parma
3+
```

tencentcloud/services/tcr/resource_tc_tcr_instance.go

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package tcr
22

33
import (
4-
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
5-
svctag "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tag"
6-
74
"context"
85
"fmt"
6+
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
7+
svctag "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tag"
98
"log"
109
"strings"
1110
"time"
@@ -98,6 +97,11 @@ func ResourceTencentCloudTcrInstance() *schema.Resource {
9897
Optional: true,
9998
Description: "Replication region ID, check the example at the top of page to find out id of region.",
10099
},
100+
"region_name": {
101+
Type: schema.TypeString,
102+
Optional: true,
103+
Description: "Replication region name.",
104+
},
101105
"syn_tag": {
102106
Type: schema.TypeBool,
103107
Optional: true,
@@ -189,9 +193,15 @@ func resourceTencentCloudTcrInstanceCreate(d *schema.ResourceData, meta interfac
189193
providerRegionId := RegionIdMap[client.Region]
190194
for i := range v {
191195
rep := v[i].(map[string]interface{})
192-
repRegion := fmt.Sprintf("%d", rep["region_id"].(int))
193-
if repRegion == providerRegionId {
194-
return fmt.Errorf("replication %s region is same with instance region %s (%s)", repRegion, providerRegionId, client.Region)
196+
repRegionId := fmt.Sprintf("%d", rep["region_id"].(int))
197+
repRegionName := rep["region_name"].(string)
198+
199+
if repRegionId != "0" && repRegionId == providerRegionId {
200+
return fmt.Errorf("replication region id:%s region is same with instance region %s (%s)", repRegionId, providerRegionId, client.Region)
201+
}
202+
203+
if repRegionName != "" && repRegionName == client.Region {
204+
return fmt.Errorf("replication region name:%s region is same with instance region %s", repRegionName, client.Region)
195205
}
196206
}
197207
}
@@ -776,7 +786,12 @@ func resourceTencentCloudTcrReplicationSet(ctx context.Context, d *schema.Resour
776786
if !ok {
777787
return 0
778788
}
779-
return item["region_id"].(int)
789+
regionId := item["region_id"].(int)
790+
regionName := item["region_name"].(string)
791+
if regionId == 0 && regionName != "" {
792+
regionId = helper.StrToInt(RegionIdMap[regionName])
793+
}
794+
return regionId
780795
}
781796

782797
oSet := schema.NewSet(setFunc, ov)
@@ -792,7 +807,14 @@ func resourceTencentCloudTcrReplicationSet(ctx context.Context, d *schema.Resour
792807
request := tcr.NewCreateReplicationInstanceRequest()
793808
replica := list[i].(map[string]interface{})
794809
request.RegistryId = helper.String(d.Id())
795-
request.ReplicationRegionId = helper.IntUint64(replica["region_id"].(int))
810+
regionId := replica["region_id"].(int)
811+
regionName := replica["region_name"].(string)
812+
if regionId != 0 {
813+
request.ReplicationRegionId = helper.IntUint64(regionId)
814+
}
815+
if regionName != "" {
816+
request.ReplicationRegionName = helper.String(regionName)
817+
}
796818
if synTag, ok := replica["syn_tag"].(bool); ok {
797819
request.SyncTag = &synTag
798820
}
@@ -832,6 +854,11 @@ func resourceTencentCloudTcrReplicationSet(ctx context.Context, d *schema.Resour
832854
replica := list[i].(map[string]interface{})
833855
id, ok := replica["id"].(string)
834856
regionId := replica["region_id"].(int)
857+
regionName := replica["region_name"].(string)
858+
if regionId == 0 && regionName != "" {
859+
tmpRegionId := helper.StrToInt(RegionIdMap[regionName])
860+
regionId = tmpRegionId
861+
}
835862
if !ok || id == "" {
836863
errs = *multierror.Append(fmt.Errorf("replication region %d has no id", regionId))
837864
continue
@@ -863,6 +890,13 @@ func ResourceTencentCloudTcrFillReplicas(replicas []interface{}, registries []*t
863890
for i := range replicas {
864891
item := replicas[i].(map[string]interface{})
865892
regionId := item["region_id"].(int)
893+
regionName := item["region_name"].(string)
894+
895+
if regionId == 0 && regionName != "" {
896+
tmpRegionId := helper.StrToInt(RegionIdMap[regionName])
897+
regionId = tmpRegionId
898+
}
899+
866900
replicaRegionIndexes[regionId] = i
867901
}
868902

@@ -871,12 +905,14 @@ func ResourceTencentCloudTcrFillReplicas(replicas []interface{}, registries []*t
871905
item := registries[i]
872906
id := *item.ReplicationRegistryId
873907
regionId := *item.ReplicationRegionId
908+
regionName := *item.ReplicationRegionName
874909
if index, ok := replicaRegionIndexes[int(regionId)]; ok && index >= 0 {
875910
replicas[index].(map[string]interface{})["id"] = id
876911
} else {
877912
newReplicas = append(newReplicas, map[string]interface{}{
878-
"id": id,
879-
"region_id": int(regionId),
913+
"id": id,
914+
"region_id": int(regionId),
915+
"region_name": regionName,
880916
})
881917
}
882918
}

tencentcloud/services/tcr/resource_tc_tcr_instance_test.go

Lines changed: 69 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,8 @@ import (
1111
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
1212
svctcr "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tcr"
1313

14-
"github.com/stretchr/testify/assert"
1514
tcr "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tcr/v20190924"
1615

17-
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
18-
1916
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
2017
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
2118
)
@@ -241,7 +238,7 @@ func TestAccTencentCloudTcrInstanceResource_replication(t *testing.T) {
241238
tcacctest.AccPreCheckCommon(t, tcacctest.ACCOUNT_TYPE_PREPAY)
242239
},
243240
Check: resource.ComposeAggregateTestCheckFunc(
244-
resource.TestCheckResourceAttr("tencentcloud_tcr_instance.mytcr_instance", "name", "tfreplicas"),
241+
resource.TestCheckResourceAttr("tencentcloud_tcr_instance.mytcr_instance", "name", "tfreplicas1"),
245242
resource.TestCheckResourceAttr("tencentcloud_tcr_instance.mytcr_instance", "replications.#", "2"),
246243
),
247244
},
@@ -258,108 +255,50 @@ func TestAccTencentCloudTcrInstanceResource_replication(t *testing.T) {
258255
tcacctest.AccPreCheckCommon(t, tcacctest.ACCOUNT_TYPE_PREPAY)
259256
},
260257
Check: resource.ComposeAggregateTestCheckFunc(
261-
resource.TestCheckResourceAttr("tencentcloud_tcr_instance.mytcr_instance", "name", "tfreplicas"),
258+
resource.TestCheckResourceAttr("tencentcloud_tcr_instance.mytcr_instance", "name", "tfreplicas1"),
262259
resource.TestCheckResourceAttr("tencentcloud_tcr_instance.mytcr_instance", "replications.#", "3"),
263260
),
264261
},
265262
},
266263
})
267264
}
268-
269-
func TestAccTencentCloudTcrInstanceResource_replica_set(t *testing.T) {
270-
inputs := []interface{}{
271-
map[string]interface{}{
272-
"region_id": 1,
273-
"sync_cos": true,
274-
},
275-
map[string]interface{}{
276-
"region_id": 2,
277-
},
278-
map[string]interface{}{
279-
"region_id": 3,
280-
"sync_cos": false,
281-
},
282-
}
283-
284-
registries1 := []*tcr.ReplicationRegistry{
285-
{
286-
ReplicationRegistryId: helper.String("a"),
287-
RegistryId: helper.String("x"),
288-
ReplicationRegionId: helper.IntUint64(1),
289-
},
290-
{
291-
ReplicationRegistryId: helper.String("b"),
292-
RegistryId: helper.String("x"),
293-
ReplicationRegionId: helper.IntUint64(2),
294-
},
295-
{
296-
ReplicationRegistryId: helper.String("c"),
297-
RegistryId: helper.String("x"),
298-
ReplicationRegionId: helper.IntUint64(3),
299-
},
300-
}
301-
302-
result1 := svctcr.ResourceTencentCloudTcrFillReplicas(inputs, registries1)
303-
expected1 := []interface{}{
304-
map[string]interface{}{
305-
"id": "a",
306-
"region_id": 1,
307-
"sync_cos": true,
308-
},
309-
map[string]interface{}{
310-
"id": "b",
311-
"region_id": 2,
312-
},
313-
map[string]interface{}{
314-
"id": "c",
315-
"region_id": 3,
316-
"sync_cos": false,
317-
},
318-
}
319-
assert.Equalf(t, expected1, result1, "%s case 1 not equal, expected:\n%v\ngot: \n%v", t.Name(), expected1, result1)
320-
321-
var registries2 []*tcr.ReplicationRegistry
322-
registries2Incr := []*tcr.ReplicationRegistry{
323-
{
324-
ReplicationRegistryId: helper.String("d"),
325-
RegistryId: helper.String("x"),
326-
ReplicationRegionId: helper.IntUint64(4),
327-
},
328-
{
329-
ReplicationRegistryId: helper.String("e"),
330-
RegistryId: helper.String("x"),
331-
ReplicationRegionId: helper.IntUint64(5),
332-
},
333-
}
334-
registries2 = append(registries2, registries1...)
335-
registries2 = append(registries2, registries2Incr...)
336-
result2 := svctcr.ResourceTencentCloudTcrFillReplicas(inputs, registries2)
337-
expected2 := []interface{}{
338-
map[string]interface{}{
339-
"id": "a",
340-
"region_id": 1,
341-
"sync_cos": true,
342-
},
343-
map[string]interface{}{
344-
"id": "b",
345-
"region_id": 2,
346-
},
347-
map[string]interface{}{
348-
"id": "c",
349-
"region_id": 3,
350-
"sync_cos": false,
351-
},
352-
map[string]interface{}{
353-
"id": "d",
354-
"region_id": 4,
355-
},
356-
map[string]interface{}{
357-
"id": "e",
358-
"region_id": 5,
265+
func TestAccTencentCloudTcrInstanceResource_replication_regionName(t *testing.T) {
266+
// t.Parallel()
267+
resource.Test(t, resource.TestCase{
268+
PreCheck: func() { tcacctest.AccPreCheckCommon(t, tcacctest.ACCOUNT_TYPE_PREPAY) },
269+
Providers: tcacctest.AccProviders,
270+
CheckDestroy: testAccCheckTCRInstanceDestroy,
271+
Steps: []resource.TestStep{
272+
{
273+
Config: testAccTCRInstance_replica_regionName,
274+
PreConfig: func() {
275+
tcacctest.AccStepSetRegion(t, "ap-guangzhou")
276+
tcacctest.AccPreCheckCommon(t, tcacctest.ACCOUNT_TYPE_PREPAY)
277+
},
278+
Check: resource.ComposeAggregateTestCheckFunc(
279+
resource.TestCheckResourceAttr("tencentcloud_tcr_instance.mytcr_instance_regionName", "name", "exampleregionname"),
280+
resource.TestCheckResourceAttr("tencentcloud_tcr_instance.mytcr_instance_regionName", "replications.#", "1"),
281+
),
282+
},
283+
{
284+
Config: testAccTCRInstance_replica_regionName_update,
285+
PreConfig: func() {
286+
tcacctest.AccStepSetRegion(t, "ap-guangzhou")
287+
tcacctest.AccPreCheckCommon(t, tcacctest.ACCOUNT_TYPE_PREPAY)
288+
},
289+
Check: resource.ComposeAggregateTestCheckFunc(
290+
resource.TestCheckResourceAttr("tencentcloud_tcr_instance.mytcr_instance_regionName", "name", "exampleregionname"),
291+
resource.TestCheckResourceAttr("tencentcloud_tcr_instance.mytcr_instance_regionName", "replications.#", "2"),
292+
),
293+
},
294+
{
295+
ResourceName: "tencentcloud_tcr_instance.mytcr_instance_regionName",
296+
ImportState: true,
297+
ImportStateVerify: true,
298+
ImportStateVerifyIgnore: []string{"delete_bucket", "replications"},
299+
},
359300
},
360-
}
361-
362-
assert.Equalf(t, expected2, result2, "%s case 2 not equal, expected:\n%v\ngot: \n%v", t.Name(), expected2, result2)
301+
})
363302
}
364303

365304
func testAccCheckTCRInstanceDestroy(s *terraform.State) error {
@@ -446,7 +385,7 @@ resource "tencentcloud_tcr_instance" "mytcr_instance_paypaid" {
446385

447386
const testAccTCRInstance_replica = `
448387
resource "tencentcloud_tcr_instance" "mytcr_instance" {
449-
name = "tfreplicas"
388+
name = "tfreplicas1"
450389
instance_type = "premium"
451390
delete_bucket = true
452391
@@ -460,7 +399,7 @@ resource "tencentcloud_tcr_instance" "mytcr_instance" {
460399

461400
const testAccTCRInstance_replica_update = `
462401
resource "tencentcloud_tcr_instance" "mytcr_instance" {
463-
name = "tfreplicas"
402+
name = "tfreplicas1"
464403
instance_type = "premium"
465404
delete_bucket = true
466405
@@ -471,11 +410,39 @@ resource "tencentcloud_tcr_instance" "mytcr_instance" {
471410
region_id = 8 # ap-beijing
472411
}
473412
replications {
474-
region_id = 16 #ap-chengdu
413+
region_id = 15 #ap-chengdu
475414
syn_tag = true
476415
}
477416
}`
478417

418+
const testAccTCRInstance_replica_regionName = `
419+
resource "tencentcloud_tcr_instance" "mytcr_instance_regionName" {
420+
name = "exampleregionname"
421+
instance_type = "premium"
422+
delete_bucket = true
423+
424+
replications {
425+
region_name = "ap-shanghai"
426+
}
427+
}
428+
`
429+
430+
const testAccTCRInstance_replica_regionName_update = `
431+
resource "tencentcloud_tcr_instance" "mytcr_instance_regionName" {
432+
name = "exampleregionname"
433+
instance_type = "premium"
434+
delete_bucket = true
435+
436+
replications {
437+
region_name = "ap-shanghai"
438+
}
439+
440+
replications {
441+
region_name = "ap-nanjing"
442+
}
443+
}
444+
`
445+
479446
const testAccTCRInstance_basic_update_remark = `
480447
resource "tencentcloud_tcr_instance" "mytcr_instance" {
481448
name = "testacctcrinstance1"

website/docs/r/tcr_instance.html.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ The following arguments are supported:
9898
The `replications` object supports the following:
9999

100100
* `region_id` - (Optional, Int) Replication region ID, check the example at the top of page to find out id of region.
101+
* `region_name` - (Optional, String) Replication region name.
101102
* `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.
102103

103104
The `security_policy` object supports the following:

0 commit comments

Comments
 (0)