From a5e658bb5ac31e15a98994776d9f3bc6d069e0cf Mon Sep 17 00:00:00 2001 From: chenhanzhang Date: Wed, 15 Jan 2025 16:21:36 +0800 Subject: [PATCH] New Resource: alicloud_vpc_ipam_service. --- alicloud/provider.go | 1 + .../resource_alicloud_vpc_ipam_service.go | 104 ++++++++++++++++++ ...resource_alicloud_vpc_ipam_service_test.go | 65 +++++++++++ alicloud/service_alicloud_vpc_ipam_v2.go | 72 ++++++++++++ website/docs/r/vpc_ipam_service.html.markdown | 62 +++++++++++ 5 files changed, 304 insertions(+) create mode 100644 alicloud/resource_alicloud_vpc_ipam_service.go create mode 100644 alicloud/resource_alicloud_vpc_ipam_service_test.go create mode 100644 website/docs/r/vpc_ipam_service.html.markdown diff --git a/alicloud/provider.go b/alicloud/provider.go index 2e15250b5753..2919e6c2054f 100644 --- a/alicloud/provider.go +++ b/alicloud/provider.go @@ -887,6 +887,7 @@ func Provider() terraform.ResourceProvider { "alicloud_vpc_ipam_ipams": dataSourceAliCloudVpcIpamIpams(), }, ResourcesMap: map[string]*schema.Resource{ + "alicloud_vpc_ipam_service": resourceAliCloudVpcIpamService(), "alicloud_alb_load_balancer_zone_shifted_attachment": resourceAliCloudAlbLoadBalancerZoneShiftedAttachment(), "alicloud_alb_load_balancer_access_log_config_attachment": resourceAliCloudAlbLoadBalancerAccessLogConfigAttachment(), "alicloud_data_works_di_alarm_rule": resourceAliCloudDataWorksDiAlarmRule(), diff --git a/alicloud/resource_alicloud_vpc_ipam_service.go b/alicloud/resource_alicloud_vpc_ipam_service.go new file mode 100644 index 000000000000..c2070f254be0 --- /dev/null +++ b/alicloud/resource_alicloud_vpc_ipam_service.go @@ -0,0 +1,104 @@ +// Package alicloud. This file is generated automatically. Please do not modify it manually, thank you! +package alicloud + +import ( + "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 resourceAliCloudVpcIpamService() *schema.Resource { + return &schema.Resource{ + Create: resourceAliCloudVpcIpamServiceCreate, + Read: resourceAliCloudVpcIpamServiceRead, + Delete: resourceAliCloudVpcIpamServiceDelete, + 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{ + "enabled": { + Type: schema.TypeBool, + Computed: true, + }, + }, + } +} + +func resourceAliCloudVpcIpamServiceCreate(d *schema.ResourceData, meta interface{}) error { + + client := meta.(*connectivity.AliyunClient) + + action := "OpenVpcIpamService" + 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{}) + request["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.TimeoutCreate), func() *resource.RetryError { + response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2023-02-28"), StringPointer("AK"), query, request, &runtime) + if IsExpectedErrors(err, []string{"CURRENT_USER_QUANTITY_EXCEED"}) { + return nil + } + 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_service", action, AlibabaCloudSdkGoERROR) + } + + accountId, err := client.AccountId() + d.SetId(accountId) + + return resourceAliCloudVpcIpamServiceRead(d, meta) +} + +func resourceAliCloudVpcIpamServiceRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*connectivity.AliyunClient) + vpcIpamServiceV2 := VpcIpamServiceV2{client} + + objectRaw, err := vpcIpamServiceV2.DescribeVpcIpamService(d.Id()) + if err != nil { + if !d.IsNewResource() && NotFoundError(err) { + log.Printf("[DEBUG] Resource alicloud_vpc_ipam_service DescribeVpcIpamService Failed!!! %s", err) + d.SetId("") + return nil + } + return WrapError(err) + } + + if objectRaw["Enabled"] != nil { + d.Set("enabled", objectRaw["Enabled"]) + } + + return nil +} + +func resourceAliCloudVpcIpamServiceDelete(d *schema.ResourceData, meta interface{}) error { + log.Printf("[WARN] Cannot destroy resource AliCloud Resource Service. Terraform will remove this resource from the state file, however resources may remain.") + return nil +} diff --git a/alicloud/resource_alicloud_vpc_ipam_service_test.go b/alicloud/resource_alicloud_vpc_ipam_service_test.go new file mode 100644 index 000000000000..948b4220cd9c --- /dev/null +++ b/alicloud/resource_alicloud_vpc_ipam_service_test.go @@ -0,0 +1,65 @@ +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 Service. >>> Resource test cases, automatically generated. +// Case ccc 8406 +func TestAccAliCloudVpcIpamService_basic8406(t *testing.T) { + var v map[string]interface{} + resourceId := "alicloud_vpc_ipam_service.default" + ra := resourceAttrInit(resourceId, AlicloudVpcIpamServiceMap8406) + rc := resourceCheckInitWithDescribeMethod(resourceId, &v, func() interface{} { + return &VpcIpamServiceV2{testAccProvider.Meta().(*connectivity.AliyunClient)} + }, "DescribeVpcIpamService") + rac := resourceAttrCheckInit(rc, ra) + testAccCheck := rac.resourceAttrMapUpdateSet() + rand := acctest.RandIntRange(10000, 99999) + name := fmt.Sprintf("tf-testacc%svpcipamservice%d", defaultRegionToTest, rand) + testAccConfig := resourceTestAccConfigFunc(resourceId, name, AlicloudVpcIpamServiceBasicDependence8406) + resource.Test(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheckWithRegions(t, true, []connectivity.Region{"cn-hangzhou"}) + testAccPreCheck(t) + }, + IDRefreshName: resourceId, + Providers: testAccProviders, + CheckDestroy: nil, + Steps: []resource.TestStep{ + { + Config: testAccConfig(map[string]interface{}{}), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{}), + ), + }, + { + ResourceName: resourceId, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{}, + }, + }, + }) +} + +var AlicloudVpcIpamServiceMap8406 = map[string]string{ + "enabled": CHECKSET, +} + +func AlicloudVpcIpamServiceBasicDependence8406(name string) string { + return fmt.Sprintf(` +variable "name" { + default = "%s" +} + + +`, name) +} + +// Test VpcIpam Service. <<< Resource test cases, automatically generated. diff --git a/alicloud/service_alicloud_vpc_ipam_v2.go b/alicloud/service_alicloud_vpc_ipam_v2.go index 7c8b37579670..6cd01d36d851 100644 --- a/alicloud/service_alicloud_vpc_ipam_v2.go +++ b/alicloud/service_alicloud_vpc_ipam_v2.go @@ -529,3 +529,75 @@ func (s *VpcIpamServiceV2) VpcIpamIpamPoolAllocationStateRefreshFunc(id string, } // DescribeVpcIpamIpamPoolAllocation >>> Encapsulated. + +// DescribeVpcIpamService <<< Encapsulated get interface for VpcIpam Service. + +func (s *VpcIpamServiceV2) DescribeVpcIpamService(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{} + conn, err := client.NewVpcipamClient() + if err != nil { + return object, WrapError(err) + } + request = make(map[string]interface{}) + query = make(map[string]interface{}) + request["RegionId"] = client.RegionId + action := "GetVpcIpamServiceStatus" + request["ClientToken"] = buildClientToken(action) + + 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) + 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 { + return object, WrapErrorf(err, DefaultErrorMsg, id, action, AlibabaCloudSdkGoERROR) + } + + return response, nil +} + +func (s *VpcIpamServiceV2) VpcIpamServiceStateRefreshFunc(id string, field string, failStates []string) resource.StateRefreshFunc { + return func() (interface{}, string, error) { + object, err := s.DescribeVpcIpamService(id) + if err != nil { + if NotFoundError(err) { + return object, "", nil + } + return nil, "", WrapError(err) + } + + v, err := jsonpath.Get(field, object) + currentStatus := fmt.Sprint(v) + + if strings.HasPrefix(field, "#") { + v, _ := jsonpath.Get(strings.TrimPrefix(field, "#"), object) + if v != nil { + currentStatus = "#CHECKSET" + } + } + + for _, failState := range failStates { + if currentStatus == failState { + return object, currentStatus, WrapError(Error(FailedToReachTargetStatus, currentStatus)) + } + } + return object, currentStatus, nil + } +} + +// DescribeVpcIpamService >>> Encapsulated. diff --git a/website/docs/r/vpc_ipam_service.html.markdown b/website/docs/r/vpc_ipam_service.html.markdown new file mode 100644 index 000000000000..3eddd40629da --- /dev/null +++ b/website/docs/r/vpc_ipam_service.html.markdown @@ -0,0 +1,62 @@ +--- +subcategory: "Vpc Ipam" +layout: "alicloud" +page_title: "Alicloud: alicloud_vpc_ipam_service" +description: |- + Provides a Alicloud Vpc Ipam Service resource. +--- + +# alicloud_vpc_ipam_service + +Provides a Vpc Ipam Service resource. + +Ipam service, used to support automatic provisioning of Terraform. + +For information about Vpc Ipam Service and how to use it, see [What is Service](https://www.alibabacloud.com/help/en/). + +-> **NOTE:** Available since v1.242.0. + +## Example Usage + +Basic Usage + +```terraform +variable "name" { + default = "terraform-example" +} + +provider "alicloud" { + region = "cn-hangzhou" +} + + +resource "alicloud_vpc_ipam_service" "default" { +} +``` + +### Deleting `alicloud_vpc_ipam_service` or removing it from your configuration + +Terraform cannot destroy resource `alicloud_vpc_ipam_service`. Terraform will remove this resource from the state file, however resources may remain. + +## Argument Reference + +The following arguments are supported: + +## Attributes Reference + +The following attributes are exported: +* `id` - The ID of the resource supplied above.The value is formulated as ``. +* `enabled` - Whether the IPAM service has been activated. + +## 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 Service. + +## Import + +Vpc Ipam Service can be imported using the id, e.g. + +```shell +$ terraform import alicloud_vpc_ipam_service.example +``` \ No newline at end of file