From d9606478564908fee9db338d578dde6ea8a6b3df Mon Sep 17 00:00:00 2001 From: chenhanzhang Date: Fri, 18 Oct 2024 14:07:44 +0800 Subject: [PATCH] New Resource: alicloud_vpc_ipam_ipam. --- alicloud/connectivity/client.go | 24 ++ alicloud/provider.go | 1 + alicloud/resource_alicloud_vpc_ipam_ipam.go | 373 +++++++++++++++++ .../resource_alicloud_vpc_ipam_ipam_test.go | 396 ++++++++++++++++++ alicloud/service_alicloud_vpc_ipam_v2.go | 193 +++++++++ website/docs/r/vpc_ipam_ipam.html.markdown | 72 ++++ 6 files changed, 1059 insertions(+) create mode 100644 alicloud/resource_alicloud_vpc_ipam_ipam.go create mode 100644 alicloud/resource_alicloud_vpc_ipam_ipam_test.go create mode 100644 alicloud/service_alicloud_vpc_ipam_v2.go create mode 100644 website/docs/r/vpc_ipam_ipam.html.markdown diff --git a/alicloud/connectivity/client.go b/alicloud/connectivity/client.go index 4e027e09340e..2c0b52580834 100644 --- a/alicloud/connectivity/client.go +++ b/alicloud/connectivity/client.go @@ -5636,3 +5636,27 @@ func (client *AliyunClient) RpcPost(locationCode string, apiVersion string, apiN runtime.SetAutoretry(autoRetry) return conn.DoRequest(tea.String(apiName), nil, tea.String("POST"), tea.String(apiVersion), tea.String("AK"), query, body, runtime) } +func (client *AliyunClient) NewVpcipamClient() (*rpc.Client, error) { + productCode := "vpcipam" + endpoint := "" + if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" { + if err := client.loadEndpoint(productCode); err != nil { + endpoint = fmt.Sprintf("vpcipam.%s.aliyuncs.com", client.config.RegionId) + client.config.Endpoints.Store(productCode, endpoint) + log.Printf("[ERROR] loading %s endpoint got an error: %#v. Using the endpoint %s instead.", productCode, err, endpoint) + } + } + if v, ok := client.config.Endpoints.Load(productCode); ok && v.(string) != "" { + endpoint = v.(string) + } + if endpoint == "" { + return nil, fmt.Errorf("[ERROR] missing the product %s endpoint.", productCode) + } + sdkConfig := client.teaSdkConfig + sdkConfig.SetEndpoint(endpoint) + conn, err := rpc.NewClient(&sdkConfig) + if err != nil { + return nil, fmt.Errorf("unable to initialize the %s client: %#v", productCode, err) + } + return conn, nil +} diff --git a/alicloud/provider.go b/alicloud/provider.go index 5911df07460a..b5a8ed6e5546 100644 --- a/alicloud/provider.go +++ b/alicloud/provider.go @@ -870,6 +870,7 @@ func Provider() terraform.ResourceProvider { "alicloud_cms_site_monitors": dataSourceAliCloudCloudMonitorServiceSiteMonitors(), }, ResourcesMap: map[string]*schema.Resource{ + "alicloud_vpc_ipam_ipam": resourceAliCloudVpcIpamIpam(), "alicloud_sls_collection_policy": resourceAliCloudSlsCollectionPolicy(), "alicloud_gpdb_db_instance_ip_array": resourceAliCloudGpdbDBInstanceIPArray(), "alicloud_quotas_template_service": resourceAliCloudQuotasTemplateService(), diff --git a/alicloud/resource_alicloud_vpc_ipam_ipam.go b/alicloud/resource_alicloud_vpc_ipam_ipam.go new file mode 100644 index 000000000000..a49662908d98 --- /dev/null +++ b/alicloud/resource_alicloud_vpc_ipam_ipam.go @@ -0,0 +1,373 @@ +// Package alicloud. This file is generated automatically. Please do not modify it manually, thank you! +package alicloud + +import ( + "fmt" + "log" + "time" + + 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" +) + +func resourceAliCloudVpcIpamIpam() *schema.Resource { + return &schema.Resource{ + Create: resourceAliCloudVpcIpamIpamCreate, + Read: resourceAliCloudVpcIpamIpamRead, + Update: resourceAliCloudVpcIpamIpamUpdate, + Delete: resourceAliCloudVpcIpamIpamDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + Timeouts: &schema.ResourceTimeout{ + Create: schema.DefaultTimeout(5 * time.Minute), + Update: schema.DefaultTimeout(5 * time.Minute), + Delete: schema.DefaultTimeout(5 * time.Minute), + }, + Schema: map[string]*schema.Schema{ + "create_time": { + Type: schema.TypeString, + Computed: true, + }, + "ipam_description": { + Type: schema.TypeString, + Optional: true, + }, + "ipam_name": { + Type: schema.TypeString, + Optional: true, + }, + "operating_region_list": { + Type: schema.TypeSet, + Required: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + "resource_group_id": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "status": { + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func resourceAliCloudVpcIpamIpamCreate(d *schema.ResourceData, meta interface{}) error { + + client := meta.(*connectivity.AliyunClient) + + action := "CreateIpam" + var request map[string]interface{} + var response map[string]interface{} + query := make(map[string]interface{}) + conn, err := client.NewVpcipamClient() + if err != nil { + return WrapError(err) + } + request = make(map[string]interface{}) + query["RegionId"] = client.RegionId + request["ClientToken"] = buildClientToken(action) + + if v, ok := d.GetOk("ipam_name"); ok { + request["IpamName"] = v + } + if v, ok := d.GetOk("resource_group_id"); ok { + request["ResourceGroupId"] = v + } + if v, ok := d.GetOk("operating_region_list"); ok { + operatingRegionListMaps := v.(*schema.Set).List() + request["OperatingRegionList"] = operatingRegionListMaps + } + + if v, ok := d.GetOk("ipam_description"); ok { + request["IpamDescription"] = 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 = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2023-02-28"), 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_vpc_ipam_ipam", action, AlibabaCloudSdkGoERROR) + } + + d.SetId(fmt.Sprint(response["IpamId"])) + + return resourceAliCloudVpcIpamIpamUpdate(d, meta) +} + +func resourceAliCloudVpcIpamIpamRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*connectivity.AliyunClient) + vpcIpamServiceV2 := VpcIpamServiceV2{client} + + objectRaw, err := vpcIpamServiceV2.DescribeVpcIpamIpam(d.Id()) + if err != nil { + if !d.IsNewResource() && NotFoundError(err) { + log.Printf("[DEBUG] Resource alicloud_vpc_ipam_ipam DescribeVpcIpamIpam Failed!!! %s", err) + d.SetId("") + return nil + } + return WrapError(err) + } + + if objectRaw["CreateTime"] != nil { + d.Set("create_time", objectRaw["CreateTime"]) + } + if objectRaw["IpamDescription"] != nil { + d.Set("ipam_description", objectRaw["IpamDescription"]) + } + if objectRaw["IpamName"] != nil { + d.Set("ipam_name", objectRaw["IpamName"]) + } + if objectRaw["ResourceGroupId"] != nil { + d.Set("resource_group_id", objectRaw["ResourceGroupId"]) + } + if objectRaw["IpamStatus"] != nil { + d.Set("status", objectRaw["IpamStatus"]) + } + + operatingRegionList1Raw := make([]interface{}, 0) + if objectRaw["OperatingRegionList"] != nil { + operatingRegionList1Raw = objectRaw["OperatingRegionList"].([]interface{}) + } + + d.Set("operating_region_list", operatingRegionList1Raw) + tagsMaps := objectRaw["Tags"] + d.Set("tags", tagsToMap(tagsMaps)) + + return nil +} + +func resourceAliCloudVpcIpamIpamUpdate(d *schema.ResourceData, meta interface{}) error { + client := meta.(*connectivity.AliyunClient) + var request map[string]interface{} + var response map[string]interface{} + var query map[string]interface{} + update := false + d.Partial(true) + action := "UpdateIpam" + conn, err := client.NewVpcipamClient() + if err != nil { + return WrapError(err) + } + request = make(map[string]interface{}) + query = make(map[string]interface{}) + query["IpamId"] = d.Id() + query["RegionId"] = client.RegionId + request["ClientToken"] = buildClientToken(action) + if !d.IsNewResource() && d.HasChange("ipam_name") { + update = true + request["IpamName"] = d.Get("ipam_name") + } + + if !d.IsNewResource() && d.HasChange("ipam_description") { + update = true + request["IpamDescription"] = d.Get("ipam_description") + } + + 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 = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2023-02-28"), 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, d.Id(), action, AlibabaCloudSdkGoERROR) + } + } + update = false + action = "ChangeResourceGroup" + conn, err = client.NewVpcipamClient() + if err != nil { + return WrapError(err) + } + request = make(map[string]interface{}) + query = make(map[string]interface{}) + query["ResourceId"] = d.Id() + query["RegionId"] = client.RegionId + if _, ok := d.GetOk("resource_group_id"); ok && !d.IsNewResource() && d.HasChange("resource_group_id") { + update = true + } + request["NewResourceGroupId"] = d.Get("resource_group_id") + request["ResourceType"] = "IPAM" + 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 = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2023-02-28"), 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, d.Id(), action, AlibabaCloudSdkGoERROR) + } + } + + if !d.IsNewResource() && d.HasChange("operating_region_list") { + oldEntry, newEntry := d.GetChange("operating_region_list") + oldEntrySet := oldEntry.(*schema.Set) + newEntrySet := newEntry.(*schema.Set) + removed := oldEntrySet.Difference(newEntrySet) + added := newEntrySet.Difference(oldEntrySet) + + if removed.Len() > 0 { + action := "UpdateIpam" + conn, err := client.NewVpcipamClient() + if err != nil { + return WrapError(err) + } + request = make(map[string]interface{}) + query = make(map[string]interface{}) + query["IpamId"] = d.Id() + query["RegionId"] = client.RegionId + request["ClientToken"] = buildClientToken(action) + localData := removed.List() + removeOperatingRegionMaps := localData + request["RemoveOperatingRegion"] = removeOperatingRegionMaps + + 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 = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2023-02-28"), 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, d.Id(), action, AlibabaCloudSdkGoERROR) + } + + } + + if added.Len() > 0 { + action := "UpdateIpam" + conn, err := client.NewVpcipamClient() + if err != nil { + return WrapError(err) + } + request = make(map[string]interface{}) + query = make(map[string]interface{}) + query["IpamId"] = d.Id() + query["RegionId"] = client.RegionId + request["ClientToken"] = buildClientToken(action) + localData := added.List() + addOperatingRegionMaps := localData + request["AddOperatingRegion"] = addOperatingRegionMaps + + 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 = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2023-02-28"), 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, d.Id(), action, AlibabaCloudSdkGoERROR) + } + + } + + } + if d.HasChange("tags") { + vpcIpamServiceV2 := VpcIpamServiceV2{client} + if err := vpcIpamServiceV2.SetResourceTags(d, "IPAM"); err != nil { + return WrapError(err) + } + } + d.Partial(false) + return resourceAliCloudVpcIpamIpamRead(d, meta) +} + +func resourceAliCloudVpcIpamIpamDelete(d *schema.ResourceData, meta interface{}) error { + + client := meta.(*connectivity.AliyunClient) + action := "DeleteIpam" + var request map[string]interface{} + var response map[string]interface{} + query := make(map[string]interface{}) + conn, err := client.NewVpcipamClient() + if err != nil { + return WrapError(err) + } + request = make(map[string]interface{}) + query["IpamId"] = d.Id() + query["RegionId"] = client.RegionId + + request["ClientToken"] = buildClientToken(action) + + 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("2023-02-28"), 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 +} diff --git a/alicloud/resource_alicloud_vpc_ipam_ipam_test.go b/alicloud/resource_alicloud_vpc_ipam_ipam_test.go new file mode 100644 index 000000000000..066e483fa264 --- /dev/null +++ b/alicloud/resource_alicloud_vpc_ipam_ipam_test.go @@ -0,0 +1,396 @@ +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 VpcIpam Ipam. >>> Resource test cases, automatically generated. +// Case test_ipam_1.1.2正式 8005 +func TestAccAliCloudVpcIpamIpam_basic8005(t *testing.T) { + var v map[string]interface{} + resourceId := "alicloud_vpc_ipam_ipam.default" + ra := resourceAttrInit(resourceId, AlicloudVpcIpamIpamMap8005) + rc := resourceCheckInitWithDescribeMethod(resourceId, &v, func() interface{} { + return &VpcIpamServiceV2{testAccProvider.Meta().(*connectivity.AliyunClient)} + }, "DescribeVpcIpamIpam") + rac := resourceAttrCheckInit(rc, ra) + testAccCheck := rac.resourceAttrMapUpdateSet() + rand := acctest.RandIntRange(10000, 99999) + name := fmt.Sprintf("tf-testacc%svpcipamipam%d", defaultRegionToTest, rand) + testAccConfig := resourceTestAccConfigFunc(resourceId, name, AlicloudVpcIpamIpamBasicDependence8005) + resource.Test(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheckWithRegions(t, true, []connectivity.Region{"cn-hangzhou"}) + testAccPreCheck(t) + }, + IDRefreshName: resourceId, + Providers: testAccProviders, + CheckDestroy: rac.checkResourceDestroy(), + Steps: []resource.TestStep{ + { + Config: testAccConfig(map[string]interface{}{ + "ipam_description": "This is my first Ipam.", + "ipam_name": name, + "resource_group_id": "${data.alicloud_resource_manager_resource_groups.default.ids.0}", + "operating_region_list": []string{ + "cn-hangzhou"}, + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "ipam_description": "This is my first Ipam.", + "ipam_name": name, + "resource_group_id": CHECKSET, + "operating_region_list.#": "1", + }), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "ipam_description": "This is my new ipam.", + "ipam_name": name + "_update", + "resource_group_id": "${data.alicloud_resource_manager_resource_groups.default.ids.1}", + "operating_region_list": []string{ + "cn-hangzhou", "cn-beijing", "cn-qingdao"}, + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "ipam_description": "This is my new ipam.", + "ipam_name": name + "_update", + "resource_group_id": CHECKSET, + "operating_region_list.#": "3", + }), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "resource_group_id": "${data.alicloud_resource_manager_resource_groups.default.ids.0}", + "operating_region_list": []string{ + "cn-hangzhou"}, + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "resource_group_id": CHECKSET, + "operating_region_list.#": "1", + }), + ), + }, + { + ResourceName: resourceId, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{}, + }, + }, + }) +} + +var AlicloudVpcIpamIpamMap8005 = map[string]string{ + "status": CHECKSET, + "create_time": CHECKSET, +} + +func AlicloudVpcIpamIpamBasicDependence8005(name string) string { + return fmt.Sprintf(` +variable "name" { + default = "%s" +} + +data "alicloud_resource_manager_resource_groups" "default" {} + +`, name) +} + +// Case test_ipam_副本_副本 7857 +func TestAccAliCloudVpcIpamIpam_basic7857(t *testing.T) { + var v map[string]interface{} + resourceId := "alicloud_vpc_ipam_ipam.default" + ra := resourceAttrInit(resourceId, AlicloudVpcIpamIpamMap7857) + rc := resourceCheckInitWithDescribeMethod(resourceId, &v, func() interface{} { + return &VpcIpamServiceV2{testAccProvider.Meta().(*connectivity.AliyunClient)} + }, "DescribeVpcIpamIpam") + rac := resourceAttrCheckInit(rc, ra) + testAccCheck := rac.resourceAttrMapUpdateSet() + rand := acctest.RandIntRange(10000, 99999) + name := fmt.Sprintf("tf-testacc%svpcipamipam%d", defaultRegionToTest, rand) + testAccConfig := resourceTestAccConfigFunc(resourceId, name, AlicloudVpcIpamIpamBasicDependence7857) + resource.Test(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheckWithRegions(t, true, []connectivity.Region{"cn-hangzhou"}) + testAccPreCheck(t) + }, + IDRefreshName: resourceId, + Providers: testAccProviders, + CheckDestroy: rac.checkResourceDestroy(), + Steps: []resource.TestStep{ + { + Config: testAccConfig(map[string]interface{}{ + "ipam_description": "This is my first Ipam.", + "ipam_name": name, + "resource_group_id": "${data.alicloud_resource_manager_resource_groups.default.ids.0}", + "operating_region_list": []string{ + "cn-hangzhou"}, + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "ipam_description": "This is my first Ipam.", + "ipam_name": name, + "resource_group_id": CHECKSET, + "operating_region_list.#": "1", + }), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "ipam_description": "This is my new ipam.", + "ipam_name": name + "_update", + "resource_group_id": "${data.alicloud_resource_manager_resource_groups.default.ids.1}", + "operating_region_list": []string{ + "cn-hangzhou", "cn-beijing", "cn-qingdao"}, + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "ipam_description": "This is my new ipam.", + "ipam_name": name + "_update", + "resource_group_id": CHECKSET, + "operating_region_list.#": "3", + }), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "resource_group_id": "${data.alicloud_resource_manager_resource_groups.default.ids.0}", + "operating_region_list": []string{ + "cn-hangzhou"}, + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "resource_group_id": CHECKSET, + "operating_region_list.#": "1", + }), + ), + }, + { + ResourceName: resourceId, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{}, + }, + }, + }) +} + +var AlicloudVpcIpamIpamMap7857 = map[string]string{ + "status": CHECKSET, + "create_time": CHECKSET, +} + +func AlicloudVpcIpamIpamBasicDependence7857(name string) string { + return fmt.Sprintf(` +variable "name" { + default = "%s" +} + +data "alicloud_resource_manager_resource_groups" "default" {} + + +`, name) +} + +// Case test_ipam_副本 7856 +func TestAccAliCloudVpcIpamIpam_basic7856(t *testing.T) { + var v map[string]interface{} + resourceId := "alicloud_vpc_ipam_ipam.default" + ra := resourceAttrInit(resourceId, AlicloudVpcIpamIpamMap7856) + rc := resourceCheckInitWithDescribeMethod(resourceId, &v, func() interface{} { + return &VpcIpamServiceV2{testAccProvider.Meta().(*connectivity.AliyunClient)} + }, "DescribeVpcIpamIpam") + rac := resourceAttrCheckInit(rc, ra) + testAccCheck := rac.resourceAttrMapUpdateSet() + rand := acctest.RandIntRange(10000, 99999) + name := fmt.Sprintf("tf-testacc%svpcipamipam%d", defaultRegionToTest, rand) + testAccConfig := resourceTestAccConfigFunc(resourceId, name, AlicloudVpcIpamIpamBasicDependence7856) + resource.Test(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheckWithRegions(t, true, []connectivity.Region{"cn-hangzhou"}) + testAccPreCheck(t) + }, + IDRefreshName: resourceId, + Providers: testAccProviders, + CheckDestroy: rac.checkResourceDestroy(), + Steps: []resource.TestStep{ + { + Config: testAccConfig(map[string]interface{}{ + "ipam_description": "This is my first Ipam.", + "ipam_name": name, + "resource_group_id": "${data.alicloud_resource_manager_resource_groups.default.ids.0}", + "operating_region_list": []string{ + "cn-hangzhou"}, + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "ipam_description": "This is my first Ipam.", + "ipam_name": name, + "resource_group_id": CHECKSET, + "operating_region_list.#": "1", + }), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "ipam_description": "This is my new ipam.", + "ipam_name": name + "_update", + "resource_group_id": "${data.alicloud_resource_manager_resource_groups.default.ids.1}", + "operating_region_list": []string{ + "cn-hangzhou", "cn-beijing", "cn-qingdao"}, + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "ipam_description": "This is my new ipam.", + "ipam_name": name + "_update", + "resource_group_id": CHECKSET, + "operating_region_list.#": "3", + }), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "resource_group_id": "${data.alicloud_resource_manager_resource_groups.default.ids.0}", + "operating_region_list": []string{ + "cn-hangzhou"}, + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "resource_group_id": CHECKSET, + "operating_region_list.#": "1", + }), + ), + }, + { + ResourceName: resourceId, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{}, + }, + }, + }) +} + +var AlicloudVpcIpamIpamMap7856 = map[string]string{ + "status": CHECKSET, + "create_time": CHECKSET, +} + +func AlicloudVpcIpamIpamBasicDependence7856(name string) string { + return fmt.Sprintf(` +variable "name" { + default = "%s" +} + +data "alicloud_resource_manager_resource_groups" "default" {} + + +`, name) +} + +// Case test_ipam 7530 +func TestAccAliCloudVpcIpamIpam_basic7530(t *testing.T) { + var v map[string]interface{} + resourceId := "alicloud_vpc_ipam_ipam.default" + ra := resourceAttrInit(resourceId, AlicloudVpcIpamIpamMap7530) + rc := resourceCheckInitWithDescribeMethod(resourceId, &v, func() interface{} { + return &VpcIpamServiceV2{testAccProvider.Meta().(*connectivity.AliyunClient)} + }, "DescribeVpcIpamIpam") + rac := resourceAttrCheckInit(rc, ra) + testAccCheck := rac.resourceAttrMapUpdateSet() + rand := acctest.RandIntRange(10000, 99999) + name := fmt.Sprintf("tf-testacc%svpcipamipam%d", defaultRegionToTest, rand) + testAccConfig := resourceTestAccConfigFunc(resourceId, name, AlicloudVpcIpamIpamBasicDependence7530) + resource.Test(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheckWithRegions(t, true, []connectivity.Region{"cn-hangzhou"}) + testAccPreCheck(t) + }, + IDRefreshName: resourceId, + Providers: testAccProviders, + CheckDestroy: rac.checkResourceDestroy(), + Steps: []resource.TestStep{ + { + Config: testAccConfig(map[string]interface{}{ + "ipam_description": "This is my first Ipam.", + "ipam_name": name, + "resource_group_id": "${data.alicloud_resource_manager_resource_groups.default.ids.0}", + "operating_region_list": []string{ + "cn-hangzhou"}, + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "ipam_description": "This is my first Ipam.", + "ipam_name": name, + "resource_group_id": CHECKSET, + "operating_region_list.#": "1", + }), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "ipam_description": "This is my new ipam.", + "ipam_name": name + "_update", + "resource_group_id": "${data.alicloud_resource_manager_resource_groups.default.ids.1}", + "operating_region_list": []string{ + "cn-hangzhou", "cn-beijing", "cn-qingdao"}, + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "ipam_description": "This is my new ipam.", + "ipam_name": name + "_update", + "resource_group_id": CHECKSET, + "operating_region_list.#": "3", + }), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "resource_group_id": "${data.alicloud_resource_manager_resource_groups.default.ids.0}", + "operating_region_list": []string{ + "cn-hangzhou"}, + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "resource_group_id": CHECKSET, + "operating_region_list.#": "1", + }), + ), + }, + { + ResourceName: resourceId, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{}, + }, + }, + }) +} + +var AlicloudVpcIpamIpamMap7530 = map[string]string{ + "status": CHECKSET, + "create_time": CHECKSET, +} + +func AlicloudVpcIpamIpamBasicDependence7530(name string) string { + return fmt.Sprintf(` +variable "name" { + default = "%s" +} + +data "alicloud_resource_manager_resource_groups" "default" {} + + +`, name) +} + +// Test VpcIpam Ipam. <<< Resource test cases, automatically generated. diff --git a/alicloud/service_alicloud_vpc_ipam_v2.go b/alicloud/service_alicloud_vpc_ipam_v2.go new file mode 100644 index 000000000000..b35c8c626e0d --- /dev/null +++ b/alicloud/service_alicloud_vpc_ipam_v2.go @@ -0,0 +1,193 @@ +package alicloud + +import ( + "fmt" + "time" + + "github.com/PaesslerAG/jsonpath" + rpc "github.com/alibabacloud-go/tea-rpc/client" + 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" +) + +type VpcIpamServiceV2 struct { + client *connectivity.AliyunClient +} + +// DescribeVpcIpamIpam <<< Encapsulated get interface for VpcIpam Ipam. + +func (s *VpcIpamServiceV2) DescribeVpcIpamIpam(id string) (object map[string]interface{}, err error) { + client := s.client + var request map[string]interface{} + var response map[string]interface{} + var query map[string]interface{} + action := "ListIpams" + conn, err := client.NewVpcipamClient() + if err != nil { + return object, WrapError(err) + } + request = make(map[string]interface{}) + query = make(map[string]interface{}) + request["IpamIds.1"] = id + query["RegionId"] = client.RegionId + + runtime := util.RuntimeOptions{} + runtime.SetAutoretry(true) + wait := incrementalWait(3*time.Second, 5*time.Second) + err = resource.Retry(1*time.Minute, func() *resource.RetryError { + response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2023-02-28"), 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 object, WrapErrorf(err, DefaultErrorMsg, id, action, AlibabaCloudSdkGoERROR) + } + + v, err := jsonpath.Get("$.Ipams[*]", response) + if err != nil { + return object, WrapErrorf(Error(GetNotFoundMessage("Ipam", id)), NotFoundMsg, response) + } + + if len(v.([]interface{})) == 0 { + return object, WrapErrorf(Error(GetNotFoundMessage("Ipam", id)), NotFoundMsg, response) + } + + currentStatus := v.([]interface{})[0].(map[string]interface{})["IpamId"] + if currentStatus == nil { + return object, WrapErrorf(Error(GetNotFoundMessage("Ipam", id)), NotFoundMsg, response) + } + + return v.([]interface{})[0].(map[string]interface{}), nil +} + +func (s *VpcIpamServiceV2) VpcIpamIpamStateRefreshFunc(id string, field string, failStates []string) resource.StateRefreshFunc { + return func() (interface{}, string, error) { + object, err := s.DescribeVpcIpamIpam(id) + if err != nil { + if NotFoundError(err) { + return object, "", nil + } + return nil, "", WrapError(err) + } + + v, err := jsonpath.Get(field, object) + currentStatus := fmt.Sprint(v) + + for _, failState := range failStates { + if currentStatus == failState { + return object, currentStatus, WrapError(Error(FailedToReachTargetStatus, currentStatus)) + } + } + return object, currentStatus, nil + } +} + +// DescribeVpcIpamIpam >>> Encapsulated. +// SetResourceTags <<< Encapsulated tag function for VpcIpam. +func (s *VpcIpamServiceV2) SetResourceTags(d *schema.ResourceData, resourceType string) error { + if d.HasChange("tags") { + var err error + var action string + var conn *rpc.Client + client := s.client + var request map[string]interface{} + var response map[string]interface{} + query := make(map[string]interface{}) + + added, removed := parsingTags(d) + removedTagKeys := make([]string, 0) + for _, v := range removed { + if !ignoredTags(v, "") { + removedTagKeys = append(removedTagKeys, v) + } + } + if len(removedTagKeys) > 0 { + action = "UntagResources" + conn, err = client.NewVpcipamClient() + if err != nil { + return WrapError(err) + } + request = make(map[string]interface{}) + query = make(map[string]interface{}) + request["ResourceId.1"] = d.Id() + query["RegionId"] = client.RegionId + for i, key := range removedTagKeys { + request[fmt.Sprintf("TagKey.%d", i+1)] = key + } + + request["ResourceType"] = resourceType + 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 = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2023-02-28"), 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, d.Id(), action, AlibabaCloudSdkGoERROR) + } + + } + + if len(added) > 0 { + action = "TagResources" + conn, err = client.NewVpcipamClient() + if err != nil { + return WrapError(err) + } + request = make(map[string]interface{}) + query = make(map[string]interface{}) + request["ResourceId.1"] = d.Id() + query["RegionId"] = client.RegionId + count := 1 + for key, value := range added { + request[fmt.Sprintf("Tag.%d.Key", count)] = key + request[fmt.Sprintf("Tag.%d.Value", count)] = value + count++ + } + + request["ResourceType"] = resourceType + 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 = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2023-02-28"), 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, d.Id(), action, AlibabaCloudSdkGoERROR) + } + + } + } + + return nil +} + +// SetResourceTags >>> tag function encapsulated. diff --git a/website/docs/r/vpc_ipam_ipam.html.markdown b/website/docs/r/vpc_ipam_ipam.html.markdown new file mode 100644 index 000000000000..79221b6f603b --- /dev/null +++ b/website/docs/r/vpc_ipam_ipam.html.markdown @@ -0,0 +1,72 @@ +--- +subcategory: "Vpc Ipam" +layout: "alicloud" +page_title: "Alicloud: alicloud_vpc_ipam_ipam" +description: |- + Provides a Alicloud Vpc Ipam Ipam resource. +--- + +# alicloud_vpc_ipam_ipam + +Provides a Vpc Ipam Ipam resource. + +IP Address Management. + +For information about Vpc Ipam Ipam and how to use it, see [What is Ipam](https://www.alibabacloud.com/help/en/). + +-> **NOTE:** Available since v1.232.0. + +## Example Usage + +Basic Usage + +```terraform +variable "name" { + default = "terraform-example" +} + +provider "alicloud" { + region = "cn-hangzhou" +} + +data "alicloud_resource_manager_resource_groups" "default" {} + +resource "alicloud_vpc_ipam_ipam" "default" { + ipam_description = "This is my first Ipam." + ipam_name = var.name + resource_group_id = alicloud_resource_manager_resource_group.defaultResourceGroup.id + operating_region_list = ["cn-hangzhou"] +} +``` + +## Argument Reference + +The following arguments are supported: +* `ipam_description` - (Optional) The description of IPAM. + + It must be 2 to 256 characters in length and must start with an uppercase letter or a Chinese character, but cannot start with 'http: // 'or 'https. If the description is not filled in, it is blank. The default value is blank. +* `ipam_name` - (Optional) The name of the resource. +* `operating_region_list` - (Required, Set) List of IPAM effective regions. +* `resource_group_id` - (Optional, Computed) The ID of the resource group. + +## Attributes Reference + +The following attributes are exported: +* `id` - The ID of the resource supplied above. +* `create_time` - The creation time of the resource. +* `status` - The status of the resource. + +## Timeouts + +The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/docs/configuration-0-11/resources.html#timeouts) for certain actions: +* `create` - (Defaults to 5 mins) Used when create the Ipam. +* `delete` - (Defaults to 5 mins) Used when delete the Ipam. +* `update` - (Defaults to 5 mins) Used when update the Ipam. + +## Import + +Vpc Ipam Ipam can be imported using the id, e.g. + +```shell +$ terraform import alicloud_vpc_ipam_ipam.example +``` \ No newline at end of file