Skip to content

Commit

Permalink
New Resource: alicloud_alb_load_balancer_zone_shifted_attachment.
Browse files Browse the repository at this point in the history
  • Loading branch information
ChenHanZhang committed Jan 15, 2025
1 parent b586902 commit 858b913
Show file tree
Hide file tree
Showing 5 changed files with 519 additions and 0 deletions.
1 change: 1 addition & 0 deletions alicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,7 @@ func Provider() terraform.ResourceProvider {
"alicloud_vpc_ipam_ipams": dataSourceAliCloudVpcIpamIpams(),
},
ResourcesMap: map[string]*schema.Resource{
"alicloud_alb_load_balancer_zone_shifted_attachment": resourceAliCloudAlbLoadBalancerZoneShiftedAttachment(),
"alicloud_alb_load_balancer_access_log_config_attachment": resourceAliCloudAlbLoadBalancerAccessLogConfigAttachment(),
"alicloud_data_works_di_alarm_rule": resourceAliCloudDataWorksDiAlarmRule(),
"alicloud_data_works_di_job": resourceAliCloudDataWorksDiJob(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
// Package alicloud. This file is generated automatically. Please do not modify it manually, thank you!
package alicloud

import (
"encoding/json"
"fmt"
"log"
"strings"
"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"
"github.com/tidwall/sjson"
)

func resourceAliCloudAlbLoadBalancerZoneShiftedAttachment() *schema.Resource {
return &schema.Resource{
Create: resourceAliCloudAlbLoadBalancerZoneShiftedAttachmentCreate,
Read: resourceAliCloudAlbLoadBalancerZoneShiftedAttachmentRead,
Delete: resourceAliCloudAlbLoadBalancerZoneShiftedAttachmentDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(5 * time.Minute),
Delete: schema.DefaultTimeout(5 * time.Minute),
},
Schema: map[string]*schema.Schema{
"load_balancer_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"vswitch_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"zone_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}

func resourceAliCloudAlbLoadBalancerZoneShiftedAttachmentCreate(d *schema.ResourceData, meta interface{}) error {

client := meta.(*connectivity.AliyunClient)

action := "StartShiftLoadBalancerZones"
var request map[string]interface{}
var response map[string]interface{}
query := make(map[string]interface{})
conn, err := client.NewAlbClient()
if err != nil {
return WrapError(err)
}
request = make(map[string]interface{})
if v, ok := d.GetOk("load_balancer_id"); ok {
request["LoadBalancerId"] = v
}

request["ClientToken"] = buildClientToken(action)

jsonString := convertObjectToJsonString(request)
jsonString, _ = sjson.Set(jsonString, "ZoneMappings.0.ZoneId", d.Get("zone_id"))
jsonString, _ = sjson.Set(jsonString, "ZoneMappings.0.VSwitchId", d.Get("vswitch_id"))
err = json.Unmarshal([]byte(jsonString), &request)
if err != nil {
return WrapError(err)
}

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 = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2020-06-16"), StringPointer("AK"), query, request, &runtime)
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, "alicloud_alb_load_balancer_zone_shifted_attachment", action, AlibabaCloudSdkGoERROR)
}

ZoneMappingsVSwitchIdVar, _ := jsonpath.Get("ZoneMappings[0].VSwitchId", request)
ZoneMappingsZoneIdVar, _ := jsonpath.Get("ZoneMappings[0].ZoneId", request)
d.SetId(fmt.Sprintf("%v:%v:%v", request["LoadBalancerId"], ZoneMappingsVSwitchIdVar, ZoneMappingsZoneIdVar))

return resourceAliCloudAlbLoadBalancerZoneShiftedAttachmentRead(d, meta)
}

func resourceAliCloudAlbLoadBalancerZoneShiftedAttachmentRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
albServiceV2 := AlbServiceV2{client}

objectRaw, err := albServiceV2.DescribeAlbLoadBalancerZoneShiftedAttachment(d.Id())
if err != nil {
if !d.IsNewResource() && NotFoundError(err) {
log.Printf("[DEBUG] Resource alicloud_alb_load_balancer_zone_shifted_attachment DescribeAlbLoadBalancerZoneShiftedAttachment Failed!!! %s", err)
d.SetId("")
return nil
}
return WrapError(err)
}

if objectRaw["Status"] != nil {
d.Set("status", objectRaw["Status"])
}
if objectRaw["VSwitchId"] != nil {
d.Set("vswitch_id", objectRaw["VSwitchId"])
}
if objectRaw["ZoneId"] != nil {
d.Set("zone_id", objectRaw["ZoneId"])
}

parts := strings.Split(d.Id(), ":")
d.Set("load_balancer_id", parts[0])

return nil
}

func resourceAliCloudAlbLoadBalancerZoneShiftedAttachmentDelete(d *schema.ResourceData, meta interface{}) error {

client := meta.(*connectivity.AliyunClient)
parts := strings.Split(d.Id(), ":")
action := "CancelShiftLoadBalancerZones"
var request map[string]interface{}
var response map[string]interface{}
query := make(map[string]interface{})
conn, err := client.NewAlbClient()
if err != nil {
return WrapError(err)
}
request = make(map[string]interface{})
request["LoadBalancerId"] = parts[0]

request["ClientToken"] = buildClientToken(action)

jsonString := convertObjectToJsonString(request)
jsonString, _ = sjson.Set(jsonString, "ZoneMappings.0.VSwitchId", parts[1])
jsonString, _ = sjson.Set(jsonString, "ZoneMappings.0.ZoneId", parts[2])
err = json.Unmarshal([]byte(jsonString), &request)
if err != nil {
return WrapError(err)
}

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 = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2020-06-16"), StringPointer("AK"), query, request, &runtime)
request["ClientToken"] = buildClientToken(action)

if err != nil {
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})
addDebug(action, response, request)

if err != nil {
if NotFoundError(err) {
return nil
}
return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR)
}

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package alicloud

import (
"fmt"
"testing"

"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

// Test Alb LoadBalancerZoneShiftedAttachment. >>> Resource test cases, automatically generated.
// Case test 9795
func TestAccAliCloudAlbLoadBalancerZoneShiftedAttachment_basic9795(t *testing.T) {
var v map[string]interface{}
resourceId := "alicloud_alb_load_balancer_zone_shifted_attachment.default"
ra := resourceAttrInit(resourceId, AlicloudAlbLoadBalancerZoneShiftedAttachmentMap9795)
rc := resourceCheckInitWithDescribeMethod(resourceId, &v, func() interface{} {
return &AlbServiceV2{testAccProvider.Meta().(*connectivity.AliyunClient)}
}, "DescribeAlbLoadBalancerZoneShiftedAttachment")
rac := resourceAttrCheckInit(rc, ra)
testAccCheck := rac.resourceAttrMapUpdateSet()
rand := acctest.RandIntRange(10000, 99999)
name := fmt.Sprintf("tf-testacc%salbloadbalancerzoneshiftedattachment%d", defaultRegionToTest, rand)
testAccConfig := resourceTestAccConfigFunc(resourceId, name, AlicloudAlbLoadBalancerZoneShiftedAttachmentBasicDependence9795)
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheckWithRegions(t, true, []connectivity.Region{"cn-beijing"})
testAccPreCheck(t)
},
IDRefreshName: resourceId,
Providers: testAccProviders,
CheckDestroy: rac.checkResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testAccConfig(map[string]interface{}{
"zone_id": "${alicloud_vswitch.defaultDSY0JJ.zone_id}",
"vswitch_id": "${alicloud_vswitch.defaultDSY0JJ.id}",
"load_balancer_id": "${alicloud_alb_load_balancer.default78TIYG.id}",
}),
Check: resource.ComposeTestCheckFunc(
testAccCheck(map[string]string{
"zone_id": CHECKSET,
"vswitch_id": CHECKSET,
"load_balancer_id": CHECKSET,
}),
),
},
{
ResourceName: resourceId,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{},
},
},
})
}

var AlicloudAlbLoadBalancerZoneShiftedAttachmentMap9795 = map[string]string{
"status": CHECKSET,
}

func AlicloudAlbLoadBalancerZoneShiftedAttachmentBasicDependence9795(name string) string {
return fmt.Sprintf(`
variable "name" {
default = "%s"
}
resource "alicloud_vpc" "alb_test_tf_vpc" {
vpc_name = var.name
cidr_block = "192.168.0.0/16"
}
resource "alicloud_vswitch" "alb_test_tf_j" {
vpc_id = alicloud_vpc.alb_test_tf_vpc.id
zone_id = "cn-beijing-j"
cidr_block = "192.168.1.0/24"
vswitch_name = format("%%s1", var.name)
}
resource "alicloud_vswitch" "alb_test_tf_k" {
vpc_id = alicloud_vpc.alb_test_tf_vpc.id
zone_id = "cn-beijing-k"
cidr_block = "192.168.2.0/24"
vswitch_name = format("%%s2", var.name)
}
resource "alicloud_vswitch" "defaultDSY0JJ" {
vpc_id = alicloud_vpc.alb_test_tf_vpc.id
zone_id = "cn-beijing-f"
cidr_block = "192.168.3.0/24"
vswitch_name = format("%%s3", var.name)
}
resource "alicloud_alb_load_balancer" "default78TIYG" {
load_balancer_edition = "Standard"
vpc_id = alicloud_vpc.alb_test_tf_vpc.id
load_balancer_billing_config {
pay_type = "PayAsYouGo"
}
address_type = "Intranet"
address_allocated_mode = "Fixed"
zone_mappings {
vswitch_id = alicloud_vswitch.alb_test_tf_j.id
zone_id = alicloud_vswitch.alb_test_tf_j.zone_id
}
zone_mappings {
vswitch_id = alicloud_vswitch.alb_test_tf_k.id
zone_id = alicloud_vswitch.alb_test_tf_k.zone_id
}
zone_mappings {
vswitch_id = alicloud_vswitch.defaultDSY0JJ.id
zone_id = alicloud_vswitch.defaultDSY0JJ.zone_id
}
}
`, name)
}

// Test Alb LoadBalancerZoneShiftedAttachment. <<< Resource test cases, automatically generated.
Loading

0 comments on commit 858b913

Please sign in to comment.