diff --git a/alicloud/connectivity/client.go b/alicloud/connectivity/client.go index bf5827b633b2..312de244faaf 100644 --- a/alicloud/connectivity/client.go +++ b/alicloud/connectivity/client.go @@ -5673,3 +5673,27 @@ func (client *AliyunClient) NewPaiworkspaceClient() (*roa.Client, error) { } return conn, nil } +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 03958b3f8cb3..a0acd1acd321 100644 --- a/alicloud/provider.go +++ b/alicloud/provider.go @@ -871,6 +871,10 @@ func Provider() terraform.ResourceProvider { "alicloud_cms_site_monitors": dataSourceAliCloudCloudMonitorServiceSiteMonitors(), }, ResourcesMap: map[string]*schema.Resource{ + "alicloud_vpc_ipam_ipam_pool_cidr": resourceAliCloudVpcIpamIpamPoolCidr(), + "alicloud_vpc_ipam_ipam_pool": resourceAliCloudVpcIpamIpamPool(), + "alicloud_vpc_ipam_ipam_scope": resourceAliCloudVpcIpamIpamScope(), + "alicloud_vpc_ipam_ipam": resourceAliCloudVpcIpamIpam(), "alicloud_pai_workspace_workspace": resourceAliCloudPaiWorkspaceWorkspace(), "alicloud_gpdb_database": resourceAliCloudGpdbDatabase(), "alicloud_sls_collection_policy": resourceAliCloudSlsCollectionPolicy(), diff --git a/alicloud/resource_alicloud_vpc_ipam_ipam.go b/alicloud/resource_alicloud_vpc_ipam_ipam.go new file mode 100644 index 000000000000..386a1e1b391f --- /dev/null +++ b/alicloud/resource_alicloud_vpc_ipam_ipam.go @@ -0,0 +1,388 @@ +// 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}, + }, + "private_default_scope_id": { + Type: schema.TypeString, + Computed: true, + }, + "region_id": { + Type: schema.TypeString, + Computed: true, + }, + "resource_group_id": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "status": { + Type: schema.TypeString, + Computed: true, + }, + "tags": tagsSchema(), + }, + } +} + +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{}) + request["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 { + operatingRegionListMapsArray := v.(*schema.Set).List() + request["OperatingRegionList"] = operatingRegionListMapsArray + } + + 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["PrivateDefaultScopeId"] != nil { + d.Set("private_default_scope_id", objectRaw["PrivateDefaultScopeId"]) + } + if objectRaw["RegionId"] != nil { + d.Set("region_id", objectRaw["RegionId"]) + } + 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{}) + request["IpamId"] = d.Id() + request["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{}) + request["ResourceId"] = d.Id() + request["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{}) + request["IpamId"] = d.Id() + request["RegionId"] = client.RegionId + request["ClientToken"] = buildClientToken(action) + localData := removed.List() + removeOperatingRegionMapsArray := localData + request["RemoveOperatingRegion"] = removeOperatingRegionMapsArray + + 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{}) + request["IpamId"] = d.Id() + request["RegionId"] = client.RegionId + request["ClientToken"] = buildClientToken(action) + localData := added.List() + addOperatingRegionMapsArray := localData + request["AddOperatingRegion"] = addOperatingRegionMapsArray + + 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{}) + request["IpamId"] = d.Id() + 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.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_pool.go b/alicloud/resource_alicloud_vpc_ipam_ipam_pool.go new file mode 100644 index 000000000000..3e7959881426 --- /dev/null +++ b/alicloud/resource_alicloud_vpc_ipam_ipam_pool.go @@ -0,0 +1,350 @@ +// 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 resourceAliCloudVpcIpamIpamPool() *schema.Resource { + return &schema.Resource{ + Create: resourceAliCloudVpcIpamIpamPoolCreate, + Read: resourceAliCloudVpcIpamIpamPoolRead, + Update: resourceAliCloudVpcIpamIpamPoolUpdate, + Delete: resourceAliCloudVpcIpamIpamPoolDelete, + 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{ + "allocation_default_cidr_mask": { + Type: schema.TypeInt, + Optional: true, + }, + "allocation_max_cidr_mask": { + Type: schema.TypeInt, + Optional: true, + Computed: true, + }, + "allocation_min_cidr_mask": { + Type: schema.TypeInt, + Optional: true, + }, + "auto_import": { + Type: schema.TypeBool, + Optional: true, + }, + "clear_allocation_default_cidr_mask": { + Type: schema.TypeBool, + Optional: true, + }, + "create_time": { + Type: schema.TypeString, + Computed: true, + }, + "ip_version": { + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + }, + "ipam_pool_description": { + Type: schema.TypeString, + Optional: true, + }, + "ipam_pool_name": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "ipam_scope_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "pool_region_id": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + "region_id": { + Type: schema.TypeString, + Computed: true, + }, + "source_ipam_pool_id": { + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + }, + "status": { + Type: schema.TypeString, + Computed: true, + }, + "tags": tagsSchema(), + }, + } +} + +func resourceAliCloudVpcIpamIpamPoolCreate(d *schema.ResourceData, meta interface{}) error { + + client := meta.(*connectivity.AliyunClient) + + action := "CreateIpamPool" + 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) + + request["IpamScopeId"] = d.Get("ipam_scope_id") + if v, ok := d.GetOk("ipam_pool_name"); ok { + request["IpamPoolName"] = v + } + if v, ok := d.GetOk("ipam_pool_description"); ok { + request["IpamPoolDescription"] = v + } + if v, ok := d.GetOk("pool_region_id"); ok { + request["PoolRegionId"] = v + } + if v, ok := d.GetOkExists("allocation_default_cidr_mask"); ok { + request["AllocationDefaultCidrMask"] = v + } + if v, ok := d.GetOkExists("allocation_max_cidr_mask"); ok { + request["AllocationMaxCidrMask"] = v + } + if v, ok := d.GetOkExists("allocation_min_cidr_mask"); ok { + request["AllocationMinCidrMask"] = v + } + if v, ok := d.GetOk("ip_version"); ok { + request["IpVersion"] = v + } + if v, ok := d.GetOk("source_ipam_pool_id"); ok { + request["SourceIpamPoolId"] = v + } + if v, ok := d.GetOkExists("auto_import"); ok { + request["AutoImport"] = 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_pool", action, AlibabaCloudSdkGoERROR) + } + + d.SetId(fmt.Sprint(response["IpamPoolId"])) + + return resourceAliCloudVpcIpamIpamPoolUpdate(d, meta) +} + +func resourceAliCloudVpcIpamIpamPoolRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*connectivity.AliyunClient) + vpcIpamServiceV2 := VpcIpamServiceV2{client} + + objectRaw, err := vpcIpamServiceV2.DescribeVpcIpamIpamPool(d.Id()) + if err != nil { + if !d.IsNewResource() && NotFoundError(err) { + log.Printf("[DEBUG] Resource alicloud_vpc_ipam_ipam_pool DescribeVpcIpamIpamPool Failed!!! %s", err) + d.SetId("") + return nil + } + return WrapError(err) + } + + if objectRaw["AllocationDefaultCidrMask"] != nil { + d.Set("allocation_default_cidr_mask", objectRaw["AllocationDefaultCidrMask"]) + } + if objectRaw["AllocationMaxCidrMask"] != nil { + d.Set("allocation_max_cidr_mask", objectRaw["AllocationMaxCidrMask"]) + } + if objectRaw["AllocationMinCidrMask"] != nil { + d.Set("allocation_min_cidr_mask", objectRaw["AllocationMinCidrMask"]) + } + if objectRaw["AutoImport"] != nil { + d.Set("auto_import", objectRaw["AutoImport"]) + } + if objectRaw["CreateTime"] != nil { + d.Set("create_time", objectRaw["CreateTime"]) + } + if objectRaw["IpVersion"] != nil { + d.Set("ip_version", objectRaw["IpVersion"]) + } + if objectRaw["IpamPoolDescription"] != nil { + d.Set("ipam_pool_description", objectRaw["IpamPoolDescription"]) + } + if objectRaw["IpamPoolName"] != nil { + d.Set("ipam_pool_name", objectRaw["IpamPoolName"]) + } + if objectRaw["IpamScopeId"] != nil { + d.Set("ipam_scope_id", objectRaw["IpamScopeId"]) + } + if objectRaw["PoolRegionId"] != nil { + d.Set("pool_region_id", objectRaw["PoolRegionId"]) + } + if objectRaw["SourceIpamPoolId"] != nil { + d.Set("source_ipam_pool_id", objectRaw["SourceIpamPoolId"]) + } + if objectRaw["Status"] != nil { + d.Set("status", objectRaw["Status"]) + } + if objectRaw["IpamRegionId"] != nil { + d.Set("region_id", objectRaw["IpamRegionId"]) + } + + tagsMaps := objectRaw["Tags"] + d.Set("tags", tagsToMap(tagsMaps)) + + return nil +} + +func resourceAliCloudVpcIpamIpamPoolUpdate(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 + action := "UpdateIpamPool" + conn, err := client.NewVpcipamClient() + if err != nil { + return WrapError(err) + } + request = make(map[string]interface{}) + query = make(map[string]interface{}) + request["IpamPoolId"] = d.Id() + request["RegionId"] = client.RegionId + request["ClientToken"] = buildClientToken(action) + if !d.IsNewResource() && d.HasChange("ipam_pool_name") { + update = true + request["IpamPoolName"] = d.Get("ipam_pool_name") + } + + if !d.IsNewResource() && d.HasChange("ipam_pool_description") { + update = true + request["IpamPoolDescription"] = d.Get("ipam_pool_description") + } + + if !d.IsNewResource() && d.HasChange("allocation_default_cidr_mask") { + update = true + request["AllocationDefaultCidrMask"] = d.Get("allocation_default_cidr_mask") + } + + if !d.IsNewResource() && d.HasChange("allocation_max_cidr_mask") { + update = true + request["AllocationMaxCidrMask"] = d.Get("allocation_max_cidr_mask") + } + + if !d.IsNewResource() && d.HasChange("allocation_min_cidr_mask") { + update = true + request["AllocationMinCidrMask"] = d.Get("allocation_min_cidr_mask") + } + + if v, ok := d.GetOkExists("clear_allocation_default_cidr_mask"); ok { + request["ClearAllocationDefaultCidrMask"] = v + } + if !d.IsNewResource() && d.HasChange("auto_import") { + update = true + request["AutoImport"] = d.Get("auto_import") + } + + 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.HasChange("tags") { + vpcIpamServiceV2 := VpcIpamServiceV2{client} + if err := vpcIpamServiceV2.SetResourceTags(d, "IpamPool"); err != nil { + return WrapError(err) + } + } + return resourceAliCloudVpcIpamIpamPoolRead(d, meta) +} + +func resourceAliCloudVpcIpamIpamPoolDelete(d *schema.ResourceData, meta interface{}) error { + + client := meta.(*connectivity.AliyunClient) + action := "DeleteIpamPool" + 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["IpamPoolId"] = d.Id() + 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.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_pool_cidr.go b/alicloud/resource_alicloud_vpc_ipam_ipam_pool_cidr.go new file mode 100644 index 000000000000..8b6dced6e277 --- /dev/null +++ b/alicloud/resource_alicloud_vpc_ipam_ipam_pool_cidr.go @@ -0,0 +1,162 @@ +// Package alicloud. This file is generated automatically. Please do not modify it manually, thank you! +package alicloud + +import ( + "fmt" + "log" + "strings" + "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 resourceAliCloudVpcIpamIpamPoolCidr() *schema.Resource { + return &schema.Resource{ + Create: resourceAliCloudVpcIpamIpamPoolCidrCreate, + Read: resourceAliCloudVpcIpamIpamPoolCidrRead, + Delete: resourceAliCloudVpcIpamIpamPoolCidrDelete, + 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{ + "cidr": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "ipam_pool_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "status": { + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func resourceAliCloudVpcIpamIpamPoolCidrCreate(d *schema.ResourceData, meta interface{}) error { + + client := meta.(*connectivity.AliyunClient) + + action := "AddIpamPoolCidr" + 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["IpamPoolId"] = d.Get("ipam_pool_id") + request["Cidr"] = d.Get("cidr") + 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 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_pool_cidr", action, AlibabaCloudSdkGoERROR) + } + + d.SetId(fmt.Sprintf("%v:%v", request["IpamPoolId"], request["Cidr"])) + + return resourceAliCloudVpcIpamIpamPoolCidrRead(d, meta) +} + +func resourceAliCloudVpcIpamIpamPoolCidrRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*connectivity.AliyunClient) + vpcIpamServiceV2 := VpcIpamServiceV2{client} + + objectRaw, err := vpcIpamServiceV2.DescribeVpcIpamIpamPoolCidr(d.Id()) + if err != nil { + if !d.IsNewResource() && NotFoundError(err) { + log.Printf("[DEBUG] Resource alicloud_vpc_ipam_ipam_pool_cidr DescribeVpcIpamIpamPoolCidr Failed!!! %s", err) + d.SetId("") + return nil + } + return WrapError(err) + } + + if objectRaw["Status"] != nil { + d.Set("status", objectRaw["Status"]) + } + if objectRaw["Cidr"] != nil { + d.Set("cidr", objectRaw["Cidr"]) + } + if objectRaw["IpamPoolId"] != nil { + d.Set("ipam_pool_id", objectRaw["IpamPoolId"]) + } + + return nil +} + +func resourceAliCloudVpcIpamIpamPoolCidrDelete(d *schema.ResourceData, meta interface{}) error { + + client := meta.(*connectivity.AliyunClient) + parts := strings.Split(d.Id(), ":") + action := "DeleteIpamPoolCidr" + 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["IpamPoolId"] = parts[0] + request["Cidr"] = parts[1] + 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.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_pool_cidr_test.go b/alicloud/resource_alicloud_vpc_ipam_ipam_pool_cidr_test.go new file mode 100644 index 000000000000..05b236d71b01 --- /dev/null +++ b/alicloud/resource_alicloud_vpc_ipam_ipam_pool_cidr_test.go @@ -0,0 +1,81 @@ +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 IpamPoolCidr. >>> Resource test cases, automatically generated. +// Case test_ipam_pool_cidr 8028 +func TestAccAliCloudVpcIpamIpamPoolCidr_basic8028(t *testing.T) { + var v map[string]interface{} + resourceId := "alicloud_vpc_ipam_ipam_pool_cidr.default" + ra := resourceAttrInit(resourceId, AlicloudVpcIpamIpamPoolCidrMap8028) + rc := resourceCheckInitWithDescribeMethod(resourceId, &v, func() interface{} { + return &VpcIpamServiceV2{testAccProvider.Meta().(*connectivity.AliyunClient)} + }, "DescribeVpcIpamIpamPoolCidr") + rac := resourceAttrCheckInit(rc, ra) + testAccCheck := rac.resourceAttrMapUpdateSet() + rand := acctest.RandIntRange(10000, 99999) + name := fmt.Sprintf("tf-testacc%svpcipamipampoolcidr%d", defaultRegionToTest, rand) + testAccConfig := resourceTestAccConfigFunc(resourceId, name, AlicloudVpcIpamIpamPoolCidrBasicDependence8028) + 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{}{ + "cidr": "10.0.0.0/8", + "ipam_pool_id": "${alicloud_vpc_ipam_ipam_pool.defaultIpamPool.id}", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "cidr": "10.0.0.0/8", + "ipam_pool_id": CHECKSET, + }), + ), + }, + { + ResourceName: resourceId, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{}, + }, + }, + }) +} + +var AlicloudVpcIpamIpamPoolCidrMap8028 = map[string]string{ + "status": CHECKSET, +} + +func AlicloudVpcIpamIpamPoolCidrBasicDependence8028(name string) string { + return fmt.Sprintf(` +variable "name" { + default = "%s" +} + +resource "alicloud_vpc_ipam_ipam" "defaultIpam" { + operating_region_list = ["cn-hangzhou"] +} + +resource "alicloud_vpc_ipam_ipam_pool" "defaultIpamPool" { + ipam_scope_id = alicloud_vpc_ipam_ipam.defaultIpam.private_default_scope_id + pool_region_id = alicloud_vpc_ipam_ipam.defaultIpam.region_id + ip_version = "IPv4" +} + + +`, name) +} + +// Test VpcIpam IpamPoolCidr. <<< Resource test cases, automatically generated. diff --git a/alicloud/resource_alicloud_vpc_ipam_ipam_pool_test.go b/alicloud/resource_alicloud_vpc_ipam_ipam_pool_test.go new file mode 100644 index 000000000000..edb9b8c676dc --- /dev/null +++ b/alicloud/resource_alicloud_vpc_ipam_ipam_pool_test.go @@ -0,0 +1,280 @@ +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 IpamPool. >>> Resource test cases, automatically generated. +// Case test_parent_ipam_pool 8374 +func TestAccAliCloudVpcIpamIpamPool_basic8374(t *testing.T) { + var v map[string]interface{} + resourceId := "alicloud_vpc_ipam_ipam_pool.default" + ra := resourceAttrInit(resourceId, AlicloudVpcIpamIpamPoolMap8374) + rc := resourceCheckInitWithDescribeMethod(resourceId, &v, func() interface{} { + return &VpcIpamServiceV2{testAccProvider.Meta().(*connectivity.AliyunClient)} + }, "DescribeVpcIpamIpamPool") + rac := resourceAttrCheckInit(rc, ra) + testAccCheck := rac.resourceAttrMapUpdateSet() + rand := acctest.RandIntRange(10000, 99999) + name := fmt.Sprintf("tf-testacc%svpcipamipampool%d", defaultRegionToTest, rand) + testAccConfig := resourceTestAccConfigFunc(resourceId, name, AlicloudVpcIpamIpamPoolBasicDependence8374) + 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_scope_id": "${alicloud_vpc_ipam_ipam.defaultIpam.private_default_scope_id}", + "pool_region_id": "${alicloud_vpc_ipam_ipam_pool.parentIpamPool.pool_region_id}", + "ipam_pool_name": name, + "source_ipam_pool_id": "${alicloud_vpc_ipam_ipam_pool.parentIpamPool.id}", + "ip_version": "IPv4", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "ipam_scope_id": CHECKSET, + "pool_region_id": CHECKSET, + "ipam_pool_name": name, + "source_ipam_pool_id": CHECKSET, + "ip_version": "IPv4", + }), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "tags": map[string]string{ + "Created": "TF", + "For": "Test", + }, + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "tags.%": "2", + "tags.Created": "TF", + "tags.For": "Test", + }), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "tags": map[string]string{ + "Created": "TF-update", + "For": "Test-update", + }, + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "tags.%": "2", + "tags.Created": "TF-update", + "tags.For": "Test-update", + }), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "tags": REMOVEKEY, + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "tags.%": "0", + "tags.Created": REMOVEKEY, + "tags.For": REMOVEKEY, + }), + ), + }, + { + ResourceName: resourceId, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"clear_allocation_default_cidr_mask", "region_id"}, + }, + }, + }) +} + +var AlicloudVpcIpamIpamPoolMap8374 = map[string]string{ + "status": CHECKSET, + "create_time": CHECKSET, + "region_id": CHECKSET, +} + +func AlicloudVpcIpamIpamPoolBasicDependence8374(name string) string { + return fmt.Sprintf(` +variable "name" { + default = "%s" +} + +resource "alicloud_vpc_ipam_ipam" "defaultIpam" { + operating_region_list = ["cn-hangzhou"] +} + +resource "alicloud_vpc_ipam_ipam_pool" "parentIpamPool" { + ipam_scope_id = alicloud_vpc_ipam_ipam.defaultIpam.private_default_scope_id + ipam_pool_name = format("%%s1", var.name) + pool_region_id = alicloud_vpc_ipam_ipam.defaultIpam.region_id +} + + +`, name) +} + +// Case test_ipam_pool 8026 +func TestAccAliCloudVpcIpamIpamPool_basic8026(t *testing.T) { + var v map[string]interface{} + resourceId := "alicloud_vpc_ipam_ipam_pool.default" + ra := resourceAttrInit(resourceId, AlicloudVpcIpamIpamPoolMap8026) + rc := resourceCheckInitWithDescribeMethod(resourceId, &v, func() interface{} { + return &VpcIpamServiceV2{testAccProvider.Meta().(*connectivity.AliyunClient)} + }, "DescribeVpcIpamIpamPool") + rac := resourceAttrCheckInit(rc, ra) + testAccCheck := rac.resourceAttrMapUpdateSet() + rand := acctest.RandIntRange(10000, 99999) + name := fmt.Sprintf("tf-testacc%svpcipamipampool%d", defaultRegionToTest, rand) + testAccConfig := resourceTestAccConfigFunc(resourceId, name, AlicloudVpcIpamIpamPoolBasicDependence8026) + 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_scope_id": "${alicloud_vpc_ipam_ipam.defaultIpam.private_default_scope_id}", + "ipam_pool_description": "This is my ipam pool.", + "ipam_pool_name": name, + "ip_version": "IPv4", + "allocation_default_cidr_mask": "20", + "allocation_min_cidr_mask": "16", + "allocation_max_cidr_mask": "24", + "pool_region_id": "cn-hangzhou", + "auto_import": "true", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "ipam_scope_id": CHECKSET, + "ipam_pool_description": "This is my ipam pool.", + "ipam_pool_name": name, + "ip_version": "IPv4", + "allocation_default_cidr_mask": "20", + "allocation_min_cidr_mask": "16", + "allocation_max_cidr_mask": "24", + "pool_region_id": "cn-hangzhou", + "auto_import": "true", + }), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "ipam_pool_description": "This is my new ipam pool description.", + "ipam_pool_name": name + "_update", + "allocation_default_cidr_mask": "24", + "allocation_min_cidr_mask": "12", + "allocation_max_cidr_mask": "26", + "auto_import": "false", + "clear_allocation_default_cidr_mask": "false", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "ipam_pool_description": "This is my new ipam pool description.", + "ipam_pool_name": name + "_update", + "allocation_default_cidr_mask": "24", + "allocation_min_cidr_mask": "12", + "allocation_max_cidr_mask": "26", + "auto_import": "false", + "clear_allocation_default_cidr_mask": "false", + }), + ), + }, + { + Config: testAccConfig(map[string]interface{}{}), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{}), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "tags": map[string]string{ + "Created": "TF", + "For": "Test", + }, + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "tags.%": "2", + "tags.Created": "TF", + "tags.For": "Test", + }), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "tags": map[string]string{ + "Created": "TF-update", + "For": "Test-update", + }, + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "tags.%": "2", + "tags.Created": "TF-update", + "tags.For": "Test-update", + }), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "tags": REMOVEKEY, + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "tags.%": "0", + "tags.Created": REMOVEKEY, + "tags.For": REMOVEKEY, + }), + ), + }, + { + ResourceName: resourceId, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"clear_allocation_default_cidr_mask", "region_id"}, + }, + }, + }) +} + +var AlicloudVpcIpamIpamPoolMap8026 = map[string]string{ + "status": CHECKSET, + "create_time": CHECKSET, + "region_id": CHECKSET, +} + +func AlicloudVpcIpamIpamPoolBasicDependence8026(name string) string { + return fmt.Sprintf(` +variable "name" { + default = "%s" +} + +resource "alicloud_vpc_ipam_ipam" "defaultIpam" { + operating_region_list = ["cn-hangzhou"] + ipam_name = var.name +} + + +`, name) +} + +// Test VpcIpam IpamPool. <<< Resource test cases, automatically generated. diff --git a/alicloud/resource_alicloud_vpc_ipam_ipam_scope.go b/alicloud/resource_alicloud_vpc_ipam_ipam_scope.go new file mode 100644 index 000000000000..911262e108fd --- /dev/null +++ b/alicloud/resource_alicloud_vpc_ipam_ipam_scope.go @@ -0,0 +1,257 @@ +// 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 resourceAliCloudVpcIpamIpamScope() *schema.Resource { + return &schema.Resource{ + Create: resourceAliCloudVpcIpamIpamScopeCreate, + Read: resourceAliCloudVpcIpamIpamScopeRead, + Update: resourceAliCloudVpcIpamIpamScopeUpdate, + Delete: resourceAliCloudVpcIpamIpamScopeDelete, + 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_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "ipam_scope_description": { + Type: schema.TypeString, + Optional: true, + }, + "ipam_scope_name": { + Type: schema.TypeString, + Optional: true, + }, + "ipam_scope_type": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + "region_id": { + Type: schema.TypeString, + Computed: true, + }, + "status": { + Type: schema.TypeString, + Computed: true, + }, + "tags": tagsSchema(), + }, + } +} + +func resourceAliCloudVpcIpamIpamScopeCreate(d *schema.ResourceData, meta interface{}) error { + + client := meta.(*connectivity.AliyunClient) + + action := "CreateIpamScope" + 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) + + if v, ok := d.GetOk("ipam_scope_name"); ok { + request["IpamScopeName"] = v + } + request["IpamId"] = d.Get("ipam_id") + if v, ok := d.GetOk("ipam_scope_description"); ok { + request["IpamScopeDescription"] = v + } + if v, ok := d.GetOk("ipam_scope_type"); ok { + request["IpamScopeType"] = 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_scope", action, AlibabaCloudSdkGoERROR) + } + + d.SetId(fmt.Sprint(response["IpamScopeId"])) + + return resourceAliCloudVpcIpamIpamScopeUpdate(d, meta) +} + +func resourceAliCloudVpcIpamIpamScopeRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*connectivity.AliyunClient) + vpcIpamServiceV2 := VpcIpamServiceV2{client} + + objectRaw, err := vpcIpamServiceV2.DescribeVpcIpamIpamScope(d.Id()) + if err != nil { + if !d.IsNewResource() && NotFoundError(err) { + log.Printf("[DEBUG] Resource alicloud_vpc_ipam_ipam_scope DescribeVpcIpamIpamScope Failed!!! %s", err) + d.SetId("") + return nil + } + return WrapError(err) + } + + if objectRaw["CreateTime"] != nil { + d.Set("create_time", objectRaw["CreateTime"]) + } + if objectRaw["IpamId"] != nil { + d.Set("ipam_id", objectRaw["IpamId"]) + } + if objectRaw["IpamScopeDescription"] != nil { + d.Set("ipam_scope_description", objectRaw["IpamScopeDescription"]) + } + if objectRaw["IpamScopeName"] != nil { + d.Set("ipam_scope_name", objectRaw["IpamScopeName"]) + } + if objectRaw["IpamScopeType"] != nil { + d.Set("ipam_scope_type", objectRaw["IpamScopeType"]) + } + if objectRaw["Status"] != nil { + d.Set("status", objectRaw["Status"]) + } + if objectRaw["RegionId"] != nil { + d.Set("region_id", objectRaw["RegionId"]) + } + + tagsMaps := objectRaw["Tags"] + d.Set("tags", tagsToMap(tagsMaps)) + + return nil +} + +func resourceAliCloudVpcIpamIpamScopeUpdate(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 + action := "UpdateIpamScope" + conn, err := client.NewVpcipamClient() + if err != nil { + return WrapError(err) + } + request = make(map[string]interface{}) + query = make(map[string]interface{}) + request["IpamScopeId"] = d.Id() + request["RegionId"] = client.RegionId + request["ClientToken"] = buildClientToken(action) + if !d.IsNewResource() && d.HasChange("ipam_scope_name") { + update = true + request["IpamScopeName"] = d.Get("ipam_scope_name") + } + + if !d.IsNewResource() && d.HasChange("ipam_scope_description") { + update = true + request["IpamScopeDescription"] = d.Get("ipam_scope_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) + } + } + + if d.HasChange("tags") { + vpcIpamServiceV2 := VpcIpamServiceV2{client} + if err := vpcIpamServiceV2.SetResourceTags(d, "IpamScope"); err != nil { + return WrapError(err) + } + } + return resourceAliCloudVpcIpamIpamScopeRead(d, meta) +} + +func resourceAliCloudVpcIpamIpamScopeDelete(d *schema.ResourceData, meta interface{}) error { + + client := meta.(*connectivity.AliyunClient) + action := "DeleteIpamScope" + 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["IpamScopeId"] = d.Id() + 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.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 IsExpectedErrors(err, []string{"ResourceNotFound.IpamScope"}) || NotFoundError(err) { + return nil + } + return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR) + } + + return nil +} diff --git a/alicloud/resource_alicloud_vpc_ipam_ipam_scope_test.go b/alicloud/resource_alicloud_vpc_ipam_ipam_scope_test.go new file mode 100644 index 000000000000..6e603aef911c --- /dev/null +++ b/alicloud/resource_alicloud_vpc_ipam_ipam_scope_test.go @@ -0,0 +1,142 @@ +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 IpamScope. >>> Resource test cases, automatically generated. +// Case test_ipam_scope 8006 +func TestAccAliCloudVpcIpamIpamScope_basic8006(t *testing.T) { + var v map[string]interface{} + resourceId := "alicloud_vpc_ipam_ipam_scope.default" + ra := resourceAttrInit(resourceId, AlicloudVpcIpamIpamScopeMap8006) + rc := resourceCheckInitWithDescribeMethod(resourceId, &v, func() interface{} { + return &VpcIpamServiceV2{testAccProvider.Meta().(*connectivity.AliyunClient)} + }, "DescribeVpcIpamIpamScope") + rac := resourceAttrCheckInit(rc, ra) + testAccCheck := rac.resourceAttrMapUpdateSet() + rand := acctest.RandIntRange(10000, 99999) + name := fmt.Sprintf("tf-testacc%svpcipamipamscope%d", defaultRegionToTest, rand) + testAccConfig := resourceTestAccConfigFunc(resourceId, name, AlicloudVpcIpamIpamScopeBasicDependence8006) + 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_scope_name": name, + "ipam_id": "${alicloud_vpc_ipam_ipam.defaultIpam.id}", + "ipam_scope_description": "This is a ipam scope.", + "ipam_scope_type": "private", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "ipam_scope_name": name, + "ipam_id": CHECKSET, + "ipam_scope_description": "This is a ipam scope.", + "ipam_scope_type": "private", + }), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "ipam_scope_name": name + "_update", + "ipam_scope_description": "This is my ipam scope.", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "ipam_scope_name": name + "_update", + "ipam_scope_description": "This is my ipam scope.", + }), + ), + }, + { + Config: testAccConfig(map[string]interface{}{}), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{}), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "tags": map[string]string{ + "Created": "TF", + "For": "Test", + }, + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "tags.%": "2", + "tags.Created": "TF", + "tags.For": "Test", + }), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "tags": map[string]string{ + "Created": "TF-update", + "For": "Test-update", + }, + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "tags.%": "2", + "tags.Created": "TF-update", + "tags.For": "Test-update", + }), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "tags": REMOVEKEY, + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "tags.%": "0", + "tags.Created": REMOVEKEY, + "tags.For": REMOVEKEY, + }), + ), + }, + { + ResourceName: resourceId, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"region_id"}, + }, + }, + }) +} + +var AlicloudVpcIpamIpamScopeMap8006 = map[string]string{ + "status": CHECKSET, + "create_time": CHECKSET, + "region_id": CHECKSET, +} + +func AlicloudVpcIpamIpamScopeBasicDependence8006(name string) string { + return fmt.Sprintf(` +variable "name" { + default = "%s" +} + +resource "alicloud_vpc_ipam_ipam" "defaultIpam" { + operating_region_list = ["cn-hangzhou"] + ipam_name = var.name +} + + +`, name) +} + +// Test VpcIpam IpamScope. <<< Resource test cases, automatically generated. 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..d76dd6c732c9 --- /dev/null +++ b/alicloud/resource_alicloud_vpc_ipam_ipam_test.go @@ -0,0 +1,153 @@ +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 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", + }), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "tags": map[string]string{ + "Created": "TF", + "For": "Test", + }, + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "tags.%": "2", + "tags.Created": "TF", + "tags.For": "Test", + }), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "tags": map[string]string{ + "Created": "TF-update", + "For": "Test-update", + }, + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "tags.%": "2", + "tags.Created": "TF-update", + "tags.For": "Test-update", + }), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "tags": REMOVEKEY, + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "tags.%": "0", + "tags.Created": REMOVEKEY, + "tags.For": REMOVEKEY, + }), + ), + }, + { + ResourceName: resourceId, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{}, + }, + }, + }) +} + +var AlicloudVpcIpamIpamMap7856 = map[string]string{ + "status": CHECKSET, + "create_time": CHECKSET, + "region_id": CHECKSET, + "private_default_scope_id": CHECKSET, +} + +func AlicloudVpcIpamIpamBasicDependence7856(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..f44c471bde0d --- /dev/null +++ b/alicloud/service_alicloud_vpc_ipam_v2.go @@ -0,0 +1,429 @@ +package alicloud + +import ( + "fmt" + "strings" + "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 + request["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() + request["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() + request["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. + +// DescribeVpcIpamIpamScope <<< Encapsulated get interface for VpcIpam IpamScope. + +func (s *VpcIpamServiceV2) DescribeVpcIpamIpamScope(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 := "ListIpamScopes" + conn, err := client.NewVpcipamClient() + if err != nil { + return object, WrapError(err) + } + request = make(map[string]interface{}) + query = make(map[string]interface{}) + request["IpamScopeIds.1"] = id + request["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("$.IpamScopes[*]", response) + if err != nil { + return object, WrapErrorf(Error(GetNotFoundMessage("IpamScope", id)), NotFoundMsg, response) + } + + if len(v.([]interface{})) == 0 { + return object, WrapErrorf(Error(GetNotFoundMessage("IpamScope", id)), NotFoundMsg, response) + } + + currentStatus := v.([]interface{})[0].(map[string]interface{})["IpamScopeId"] + if currentStatus == nil { + return object, WrapErrorf(Error(GetNotFoundMessage("IpamScope", id)), NotFoundMsg, response) + } + + return v.([]interface{})[0].(map[string]interface{}), nil +} + +func (s *VpcIpamServiceV2) VpcIpamIpamScopeStateRefreshFunc(id string, field string, failStates []string) resource.StateRefreshFunc { + return func() (interface{}, string, error) { + object, err := s.DescribeVpcIpamIpamScope(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 + } +} + +// DescribeVpcIpamIpamScope >>> Encapsulated. + +// DescribeVpcIpamIpamPool <<< Encapsulated get interface for VpcIpam IpamPool. + +func (s *VpcIpamServiceV2) DescribeVpcIpamIpamPool(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 := "ListIpamPools" + conn, err := client.NewVpcipamClient() + if err != nil { + return object, WrapError(err) + } + request = make(map[string]interface{}) + query = make(map[string]interface{}) + request["IpamPoolIds.1"] = id + request["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("$.IpamPools[*]", response) + if err != nil { + return object, WrapErrorf(Error(GetNotFoundMessage("IpamPool", id)), NotFoundMsg, response) + } + + if len(v.([]interface{})) == 0 { + return object, WrapErrorf(Error(GetNotFoundMessage("IpamPool", id)), NotFoundMsg, response) + } + + currentStatus := v.([]interface{})[0].(map[string]interface{})["IpamPoolId"] + if currentStatus == nil { + return object, WrapErrorf(Error(GetNotFoundMessage("IpamPool", id)), NotFoundMsg, response) + } + + return v.([]interface{})[0].(map[string]interface{}), nil +} + +func (s *VpcIpamServiceV2) VpcIpamIpamPoolStateRefreshFunc(id string, field string, failStates []string) resource.StateRefreshFunc { + return func() (interface{}, string, error) { + object, err := s.DescribeVpcIpamIpamPool(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 + } +} + +// DescribeVpcIpamIpamPool >>> Encapsulated. + +// DescribeVpcIpamIpamPoolCidr <<< Encapsulated get interface for VpcIpam IpamPoolCidr. + +func (s *VpcIpamServiceV2) DescribeVpcIpamIpamPoolCidr(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{} + parts := strings.Split(id, ":") + if len(parts) != 2 { + err = WrapError(fmt.Errorf("invalid Resource Id %s. Expected parts' length %d, got %d", id, 2, len(parts))) + } + action := "ListIpamPoolCidrs" + conn, err := client.NewVpcipamClient() + if err != nil { + return object, WrapError(err) + } + request = make(map[string]interface{}) + query = make(map[string]interface{}) + request["Cidr"] = parts[1] + request["IpamPoolId"] = parts[0] + request["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("$.IpamPoolCidrs[*]", response) + if err != nil { + return object, WrapErrorf(err, FailedGetAttributeMsg, id, "$.IpamPoolCidrs[*]", response) + } + + if len(v.([]interface{})) == 0 { + return object, WrapErrorf(Error(GetNotFoundMessage("IpamPoolCidr", id)), NotFoundMsg, response) + } + + return v.([]interface{})[0].(map[string]interface{}), nil +} + +func (s *VpcIpamServiceV2) VpcIpamIpamPoolCidrStateRefreshFunc(id string, field string, failStates []string) resource.StateRefreshFunc { + return func() (interface{}, string, error) { + object, err := s.DescribeVpcIpamIpamPoolCidr(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 + } +} + +// DescribeVpcIpamIpamPoolCidr >>> 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..612cb91b03be --- /dev/null +++ b/website/docs/r/vpc_ipam_ipam.html.markdown @@ -0,0 +1,75 @@ +--- +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.234.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 + 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. +* `tags` - (Optional, Map) The tag of the resource. + +## Attributes Reference + +The following attributes are exported: +* `id` - The ID of the resource supplied above. +* `create_time` - The creation time of the resource. +* `private_default_scope_id` - After an IPAM is created, the scope of the private network IPAM created by the system by default. +* `region_id` - The region ID 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 diff --git a/website/docs/r/vpc_ipam_ipam_pool.html.markdown b/website/docs/r/vpc_ipam_ipam_pool.html.markdown new file mode 100644 index 000000000000..9b490dbe561d --- /dev/null +++ b/website/docs/r/vpc_ipam_ipam_pool.html.markdown @@ -0,0 +1,102 @@ +--- +subcategory: "Vpc Ipam" +layout: "alicloud" +page_title: "Alicloud: alicloud_vpc_ipam_ipam_pool" +description: |- + Provides a Alicloud Vpc Ipam Ipam Pool resource. +--- + +# alicloud_vpc_ipam_ipam_pool + +Provides a Vpc Ipam Ipam Pool resource. + +IP Address Management Pool. + +For information about Vpc Ipam Ipam Pool and how to use it, see [What is Ipam Pool](https://www.alibabacloud.com/help/en/). + +-> **NOTE:** Available since v1.234.0. + +## Example Usage + +Basic Usage + +```terraform +variable "name" { + default = "terraform-example" +} + +provider "alicloud" { + region = "cn-hangzhou" +} + +resource "alicloud_vpc_ipam_ipam" "defaultIpam" { + operating_region_list = ["cn-hangzhou"] +} + +resource "alicloud_vpc_ipam_ipam_pool" "parentIpamPool" { + ipam_scope_id = alicloud_vpc_ipam_ipam.defaultIpam.private_default_scope_id + ipam_pool_name = format("%s1", var.name) + pool_region_id = alicloud_vpc_ipam_ipam.defaultIpam.region_id +} + + +resource "alicloud_vpc_ipam_ipam_pool" "default" { + ipam_scope_id = alicloud_vpc_ipam_ipam.defaultIpam.private_default_scope_id + pool_region_id = alicloud_vpc_ipam_ipam_pool.parentIpamPool.pool_region_id + ipam_pool_name = var.name + source_ipam_pool_id = alicloud_vpc_ipam_ipam_pool.parentIpamPool.id + ip_version = "IPv4" +} +``` + +## Argument Reference + +The following arguments are supported: +* `allocation_default_cidr_mask` - (Optional, Int) The default network mask assigned by the IPAM address pool. + + IPv4 network mask value range: **0 to 32** bits. +* `allocation_max_cidr_mask` - (Optional, Computed, Int) The maximum network mask assigned by the IPAM address pool. + + IPv4 network mask value range: **0 to 32** bits. +* `allocation_min_cidr_mask` - (Optional, Int) The minimum Network mask assigned by the IPAM address pool. + + IPv4 network mask value range: **0 to 32** bits. +* `auto_import` - (Optional) Whether the automatic import function is enabled for the address pool. +* `clear_allocation_default_cidr_mask` - (Optional) Whether to clear the default network mask of the IPAM address pool. Value: + - `true`: Yes. + - `false`: No. +* `ip_version` - (Optional, ForceNew, Computed) The IP protocol version. Currently, only `IPv4` is supported * *. +* `ipam_pool_description` - (Optional) The description of the IPAM address pool. + + It must be 2 to 256 characters in length and must start with an English letter or a Chinese character, but cannot start with 'http:// 'or 'https. If it is not filled in, it is empty. The default value is empty. +* `ipam_pool_name` - (Optional, Computed) The name of the resource. +* `ipam_scope_id` - (Required, ForceNew) Ipam scope id. +* `pool_region_id` - (Optional, ForceNew) The effective region of the IPAM address pool. +* `source_ipam_pool_id` - (Optional, ForceNew, Computed) The instance ID of the source IPAM address pool. + +-> **NOTE:** If this parameter is not entered, the created address pool is the parent address pool. + +* `tags` - (Optional, Map) The tag of the resource. + +## Attributes Reference + +The following attributes are exported: +* `id` - The ID of the resource supplied above. +* `create_time` - The creation time of the resource. +* `region_id` - The ID of the IPAM hosting region. +* `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 Pool. +* `delete` - (Defaults to 5 mins) Used when delete the Ipam Pool. +* `update` - (Defaults to 5 mins) Used when update the Ipam Pool. + +## Import + +Vpc Ipam Ipam Pool can be imported using the id, e.g. + +```shell +$ terraform import alicloud_vpc_ipam_ipam_pool.example +``` \ No newline at end of file diff --git a/website/docs/r/vpc_ipam_ipam_pool_cidr.html.markdown b/website/docs/r/vpc_ipam_ipam_pool_cidr.html.markdown new file mode 100644 index 000000000000..c0af51fffe4c --- /dev/null +++ b/website/docs/r/vpc_ipam_ipam_pool_cidr.html.markdown @@ -0,0 +1,76 @@ +--- +subcategory: "Vpc Ipam" +layout: "alicloud" +page_title: "Alicloud: alicloud_vpc_ipam_ipam_pool_cidr" +description: |- + Provides a Alicloud Vpc Ipam Ipam Pool Cidr resource. +--- + +# alicloud_vpc_ipam_ipam_pool_cidr + +Provides a Vpc Ipam Ipam Pool Cidr resource. + +Ipam address pool preset CIDR. + +For information about Vpc Ipam Ipam Pool Cidr and how to use it, see [What is Ipam Pool Cidr](https://www.alibabacloud.com/help/en/). + +-> **NOTE:** Available since v1.234.0. + +## Example Usage + +Basic Usage + +```terraform +variable "name" { + default = "terraform-example" +} + +provider "alicloud" { + region = "cn-hangzhou" +} + +resource "alicloud_vpc_ipam_ipam" "defaultIpam" { + operating_region_list = ["cn-hangzhou"] +} + +resource "alicloud_vpc_ipam_ipam_pool" "defaultIpamPool" { + ipam_scope_id = alicloud_vpc_ipam_ipam.defaultIpam.private_default_scope_id + pool_region_id = alicloud_vpc_ipam_ipam.defaultIpam.region_id + ip_version = "IPv4" +} + + +resource "alicloud_vpc_ipam_ipam_pool_cidr" "default" { + cidr = "10.0.0.0/8" + ipam_pool_id = alicloud_vpc_ipam_ipam_pool.defaultIpamPool.id +} +``` + +## Argument Reference + +The following arguments are supported: +* `cidr` - (Required, ForceNew) The CIDR address segment to be preset. + +-> **NOTE:** currently, only IPv4 address segments are supported. + +* `ipam_pool_id` - (Required, ForceNew) The ID of the IPAM pool instance. + +## Attributes Reference + +The following attributes are exported: +* `id` - The ID of the resource supplied above.The value is formulated as `:`. +* `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 Pool Cidr. +* `delete` - (Defaults to 5 mins) Used when delete the Ipam Pool Cidr. + +## Import + +Vpc Ipam Ipam Pool Cidr can be imported using the id, e.g. + +```shell +$ terraform import alicloud_vpc_ipam_ipam_pool_cidr.example : +``` \ No newline at end of file diff --git a/website/docs/r/vpc_ipam_ipam_scope.html.markdown b/website/docs/r/vpc_ipam_ipam_scope.html.markdown new file mode 100644 index 000000000000..25e362708f90 --- /dev/null +++ b/website/docs/r/vpc_ipam_ipam_scope.html.markdown @@ -0,0 +1,83 @@ +--- +subcategory: "Vpc Ipam" +layout: "alicloud" +page_title: "Alicloud: alicloud_vpc_ipam_ipam_scope" +description: |- + Provides a Alicloud Vpc Ipam Ipam Scope resource. +--- + +# alicloud_vpc_ipam_ipam_scope + +Provides a Vpc Ipam Ipam Scope resource. + +IP Address Management Scope. + +For information about Vpc Ipam Ipam Scope and how to use it, see [What is Ipam Scope](https://www.alibabacloud.com/help/en/). + +-> **NOTE:** Available since v1.234.0. + +## Example Usage + +Basic Usage + +```terraform +variable "name" { + default = "terraform-example" +} + +provider "alicloud" { + region = "cn-hangzhou" +} + +resource "alicloud_vpc_ipam_ipam" "defaultIpam" { + operating_region_list = ["cn-hangzhou"] + ipam_name = var.name +} + + +resource "alicloud_vpc_ipam_ipam_scope" "default" { + ipam_scope_name = var.name + ipam_id = alicloud_vpc_ipam_ipam.defaultIpam.id + ipam_scope_description = "This is a ipam scope." + ipam_scope_type = "private" +} +``` + +## Argument Reference + +The following arguments are supported: +* `ipam_id` - (Required, ForceNew) The id of the Ipam instance. +* `ipam_scope_description` - (Optional) The description of the IPAM's scope of action. + + It must be 2 to 256 characters in length and must start with a lowercase letter, but cannot start with 'http:// 'or 'https. If it is not filled in, it is empty. The default value is empty. +* `ipam_scope_name` - (Optional) The name of the resource. +* `ipam_scope_type` - (Optional, ForceNew) IPAM scope of action type: +`private`. + + +-> **NOTE:** Currently, only the role scope of the private network is supported. + +* `tags` - (Optional, Map) The tag of the resource. + +## Attributes Reference + +The following attributes are exported: +* `id` - The ID of the resource supplied above. +* `create_time` - The creation time of the resource. +* `region_id` - The region ID 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 Scope. +* `delete` - (Defaults to 5 mins) Used when delete the Ipam Scope. +* `update` - (Defaults to 5 mins) Used when update the Ipam Scope. + +## Import + +Vpc Ipam Ipam Scope can be imported using the id, e.g. + +```shell +$ terraform import alicloud_vpc_ipam_ipam_scope.example +``` \ No newline at end of file