Skip to content

feat(tcr): [116375010]support parma regionName #2593

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/2593.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/tencentcloud_tcr_instance: Support `region_name` parma
```
56 changes: 46 additions & 10 deletions tencentcloud/services/tcr/resource_tc_tcr_instance.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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: "Replication region name.",
},
"syn_tag": {
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -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)
}
}
}
Expand Down Expand Up @@ -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])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是不是接口支持传 regionName 呢,如果接口支持传 regionName 的话,还需要转 regionId 吗

}
return regionId
}

oSet := schema.NewSet(setFunc, ov)
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -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,
})
}
}
Expand Down
171 changes: 69 additions & 102 deletions tencentcloud/services/tcr/resource_tc_tcr_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -241,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"),
),
},
Expand All @@ -258,108 +255,50 @@ 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"),
),
},
},
})
}

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,
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"},
},
},
}

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 {
Expand Down Expand Up @@ -446,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

Expand All @@ -460,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

Expand All @@ -471,11 +410,39 @@ 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
}
}`

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"
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/tcr_instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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) 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:
Expand Down
Loading