Skip to content

Commit

Permalink
resource/alicloud_vpc_peer_connection: add new attribute link_type, r…
Browse files Browse the repository at this point in the history
…egion_id; resource/alicloud_vpc_peer_connection_accepter: add new attribute link_type, region_id.
  • Loading branch information
ChenHanZhang committed Dec 25, 2024
1 parent 9ec4d89 commit 0fb6b88
Show file tree
Hide file tree
Showing 7 changed files with 371 additions and 142 deletions.
202 changes: 120 additions & 82 deletions alicloud/resource_alicloud_vpc_peer_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/PaesslerAG/jsonpath"
util "github.com/alibabacloud-go/tea-utils/service"
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -65,11 +66,21 @@ func resourceAliCloudVpcPeerPeerConnection() *schema.Resource {
Type: schema.TypeBool,
Optional: true,
},
"link_type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: StringInSlice([]string{"Platinum", "Gold", "Silver"}, false),
},
"peer_connection_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"region_id": {
Type: schema.TypeString,
Computed: true,
},
"resource_group_id": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -100,7 +111,7 @@ func resourceAliCloudVpcPeerPeerConnectionCreate(d *schema.ResourceData, meta in
query := make(map[string]interface{})
var err error
request = make(map[string]interface{})
query["RegionId"] = client.RegionId
request["RegionId"] = client.RegionId
request["ClientToken"] = buildClientToken(action)

request["VpcId"] = d.Get("vpc_id")
Expand All @@ -122,6 +133,11 @@ func resourceAliCloudVpcPeerPeerConnectionCreate(d *schema.ResourceData, meta in
if v, ok := d.GetOkExists("dry_run"); ok {
request["DryRun"] = v
}
if v, ok := d.GetOk("link_type"); ok {
request["LinkType"] = v
}
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
response, err = client.RpcPost("VpcPeer", "2022-01-01", action, query, request, true)
Expand All @@ -132,9 +148,9 @@ func resourceAliCloudVpcPeerPeerConnectionCreate(d *schema.ResourceData, meta in
}
return resource.NonRetryableError(err)
}
addDebug(action, response, request)
return nil
})
addDebug(action, response, request)

if err != nil {
return WrapErrorf(err, DefaultErrorMsg, "alicloud_vpc_peer_connection", action, AlibabaCloudSdkGoERROR)
Expand Down Expand Up @@ -180,9 +196,15 @@ func resourceAliCloudVpcPeerPeerConnectionRead(d *schema.ResourceData, meta inte
if objectRaw["Description"] != nil {
d.Set("description", objectRaw["Description"])
}
if objectRaw["LinkType"] != nil {
d.Set("link_type", objectRaw["LinkType"])
}
if objectRaw["Name"] != nil {
d.Set("peer_connection_name", objectRaw["Name"])
}
if objectRaw["RegionId"] != nil {
d.Set("region_id", objectRaw["RegionId"])
}
if objectRaw["ResourceGroupId"] != nil {
d.Set("resource_group_id", objectRaw["ResourceGroupId"])
}
Expand Down Expand Up @@ -221,83 +243,6 @@ func resourceAliCloudVpcPeerPeerConnectionUpdate(d *schema.ResourceData, meta in
var query map[string]interface{}
update := false
d.Partial(true)
action := "ModifyVpcPeerConnection"
var err error
request = make(map[string]interface{})
query = make(map[string]interface{})
request["InstanceId"] = d.Id()

request["ClientToken"] = buildClientToken(action)
if !d.IsNewResource() && d.HasChange("description") {
update = true
request["Description"] = d.Get("description")
}

if !d.IsNewResource() && d.HasChange("bandwidth") {
update = true
request["Bandwidth"] = d.Get("bandwidth")
}

if !d.IsNewResource() && d.HasChange("peer_connection_name") {
update = true
request["Name"] = d.Get("peer_connection_name")
}

if v, ok := d.GetOkExists("dry_run"); ok {
request["DryRun"] = v
}
if update {
wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
response, err = client.RpcPost("VpcPeer", "2022-01-01", action, query, request, true)
if err != nil {
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
addDebug(action, response, request)
return nil
})
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR)
}
vpcPeerServiceV2 := VpcPeerServiceV2{client}
stateConf := BuildStateConf([]string{}, []string{"Activated"}, d.Timeout(schema.TimeoutUpdate), 5*time.Second, vpcPeerServiceV2.VpcPeerPeerConnectionStateRefreshFunc(d.Id(), "Status", []string{}))
if _, err := stateConf.WaitForState(); err != nil {
return WrapErrorf(err, IdMsg, d.Id())
}
}
update = false
action = "MoveResourceGroup"
request = make(map[string]interface{})
query = make(map[string]interface{})
query["ResourceId"] = d.Id()
query["RegionId"] = client.RegionId
request["ResourceType"] = "PeerConnection"
if _, ok := d.GetOk("resource_group_id"); ok && !d.IsNewResource() && d.HasChange("resource_group_id") {
update = true
}
request["NewResourceGroupId"] = d.Get("resource_group_id")
if update {
wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
response, err = client.RpcPost("VpcPeer", "2022-01-01", action, query, request, false)
if err != nil {
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
addDebug(action, response, request)
return nil
})
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR)
}
}

if d.HasChange("status") {
vpcPeerServiceV2 := VpcPeerServiceV2{client}
Expand All @@ -321,6 +266,8 @@ func resourceAliCloudVpcPeerPeerConnectionUpdate(d *schema.ResourceData, meta in
if v, ok := d.GetOkExists("dry_run"); ok {
request["DryRun"] = v
}
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
response, err = client.RpcPost("VpcPeer", "2022-01-01", action, query, request, true)
Expand All @@ -331,9 +278,9 @@ func resourceAliCloudVpcPeerPeerConnectionUpdate(d *schema.ResourceData, meta in
}
return resource.NonRetryableError(err)
}
addDebug(action, response, request)
return nil
})
addDebug(action, response, request)
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR)
}
Expand All @@ -354,6 +301,8 @@ func resourceAliCloudVpcPeerPeerConnectionUpdate(d *schema.ResourceData, meta in
if v, ok := d.GetOkExists("dry_run"); ok {
request["DryRun"] = v
}
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
response, err = client.RpcPost("VpcPeer", "2022-01-01", action, query, request, true)
Expand All @@ -364,9 +313,9 @@ func resourceAliCloudVpcPeerPeerConnectionUpdate(d *schema.ResourceData, meta in
}
return resource.NonRetryableError(err)
}
addDebug(action, response, request)
return nil
})
addDebug(action, response, request)
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR)
}
Expand All @@ -380,6 +329,93 @@ func resourceAliCloudVpcPeerPeerConnectionUpdate(d *schema.ResourceData, meta in
}
}

action := "ModifyVpcPeerConnection"
var err error
request = make(map[string]interface{})
query = make(map[string]interface{})
request["InstanceId"] = d.Id()

request["ClientToken"] = buildClientToken(action)
if !d.IsNewResource() && d.HasChange("description") {
update = true
request["Description"] = d.Get("description")
}

if !d.IsNewResource() && d.HasChange("bandwidth") {
update = true
request["Bandwidth"] = d.Get("bandwidth")
}

if !d.IsNewResource() && d.HasChange("peer_connection_name") {
update = true
request["Name"] = d.Get("peer_connection_name")
}

if v, ok := d.GetOkExists("dry_run"); ok {
request["DryRun"] = v
}
if !d.IsNewResource() && d.HasChange("link_type") {
update = true
request["LinkType"] = d.Get("link_type")
}

if update {
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
response, err = client.RpcPost("VpcPeer", "2022-01-01", action, query, request, true)
if err != nil {
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})
addDebug(action, response, request)
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR)
}
vpcPeerServiceV2 := VpcPeerServiceV2{client}
stateConf := BuildStateConf([]string{}, []string{"Activated"}, d.Timeout(schema.TimeoutUpdate), 5*time.Second, vpcPeerServiceV2.VpcPeerPeerConnectionStateRefreshFunc(d.Id(), "Status", []string{}))
if _, err := stateConf.WaitForState(); err != nil {
return WrapErrorf(err, IdMsg, d.Id())
}
}
update = false
action = "MoveResourceGroup"
request = make(map[string]interface{})
query = make(map[string]interface{})
request["ResourceId"] = d.Id()
request["RegionId"] = client.RegionId
request["ResourceType"] = "PeerConnection"
if _, ok := d.GetOk("resource_group_id"); ok && !d.IsNewResource() && d.HasChange("resource_group_id") {
update = true
}
request["NewResourceGroupId"] = d.Get("resource_group_id")
if update {
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
response, err = client.RpcPost("VpcPeer", "2022-01-01", action, query, request, false)
if err != nil {
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})
addDebug(action, response, request)
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR)
}
}

if d.HasChange("tags") {
vpcPeerServiceV2 := VpcPeerServiceV2{client}
if err := vpcPeerServiceV2.SetResourceTags(d, "PeerConnection"); err != nil {
Expand Down Expand Up @@ -409,6 +445,8 @@ func resourceAliCloudVpcPeerPeerConnectionDelete(d *schema.ResourceData, meta in
if v, ok := d.GetOkExists("force_delete"); ok {
request["Force"] = v
}
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutDelete), func() *resource.RetryError {
response, err = client.RpcPost("VpcPeer", "2022-01-01", action, query, request, true)
Expand All @@ -421,9 +459,9 @@ func resourceAliCloudVpcPeerPeerConnectionDelete(d *schema.ResourceData, meta in
}
return resource.NonRetryableError(err)
}
addDebug(action, response, request)
return nil
})
addDebug(action, response, request)

if err != nil {
if IsExpectedErrors(err, []string{"ResourceNotFound.InstanceId"}) || NotFoundError(err) {
Expand Down
Loading

0 comments on commit 0fb6b88

Please sign in to comment.