From dbe7a5050b8430e0052be9f74e449b4d76a5215d Mon Sep 17 00:00:00 2001 From: chenhanzhang Date: Fri, 3 Jan 2025 09:38:42 +0800 Subject: [PATCH] New Data Source: alicloud_vpc_ipam_ipams; New Data Source: alicloud_vpc_ipam_ipam_pools; New Data Source: alicloud_vpc_ipam_ipam_pool_allocations. --- ...alicloud_vpc_ipam_ipam_pool_allocations.go | 264 +++++++++++++++ ...oud_vpc_ipam_ipam_pool_allocations_test.go | 151 +++++++++ ...ata_source_alicloud_vpc_ipam_ipam_pools.go | 316 ++++++++++++++++++ ...ource_alicloud_vpc_ipam_ipam_pools_test.go | 178 ++++++++++ .../data_source_alicloud_vpc_ipam_ipams.go | 267 +++++++++++++++ ...ata_source_alicloud_vpc_ipam_ipams_test.go | 123 +++++++ alicloud/provider.go | 59 ++-- alicloud/service_alicloud_vpc_ipam_v2.go | 15 + ...c_ipam_ipam_pool_allocations.html.markdown | 91 +++++ .../docs/d/vpc_ipam_ipam_pools.html.markdown | 96 ++++++ website/docs/d/vpc_ipam_ipams.html.markdown | 79 +++++ 11 files changed, 1611 insertions(+), 28 deletions(-) create mode 100644 alicloud/data_source_alicloud_vpc_ipam_ipam_pool_allocations.go create mode 100644 alicloud/data_source_alicloud_vpc_ipam_ipam_pool_allocations_test.go create mode 100644 alicloud/data_source_alicloud_vpc_ipam_ipam_pools.go create mode 100644 alicloud/data_source_alicloud_vpc_ipam_ipam_pools_test.go create mode 100644 alicloud/data_source_alicloud_vpc_ipam_ipams.go create mode 100644 alicloud/data_source_alicloud_vpc_ipam_ipams_test.go create mode 100644 website/docs/d/vpc_ipam_ipam_pool_allocations.html.markdown create mode 100644 website/docs/d/vpc_ipam_ipam_pools.html.markdown create mode 100644 website/docs/d/vpc_ipam_ipams.html.markdown diff --git a/alicloud/data_source_alicloud_vpc_ipam_ipam_pool_allocations.go b/alicloud/data_source_alicloud_vpc_ipam_ipam_pool_allocations.go new file mode 100644 index 000000000000..9ff0a43288f4 --- /dev/null +++ b/alicloud/data_source_alicloud_vpc_ipam_ipam_pool_allocations.go @@ -0,0 +1,264 @@ +package alicloud + +import ( + "fmt" + "regexp" + "time" + + "github.com/PaesslerAG/jsonpath" + util "github.com/alibabacloud-go/tea-utils/service" + "github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity" + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/helper/validation" +) + +func dataSourceAliCloudVpcIpamIpamPoolAllocations() *schema.Resource { + return &schema.Resource{ + Read: dataSourceAliCloudVpcIpamIpamPoolAllocationRead, + Schema: map[string]*schema.Schema{ + "ids": { + Type: schema.TypeList, + Optional: true, + ForceNew: true, + Elem: &schema.Schema{Type: schema.TypeString}, + Computed: true, + }, + "name_regex": { + Type: schema.TypeString, + Optional: true, + ValidateFunc: validation.ValidateRegexp, + ForceNew: true, + }, + "names": { + Type: schema.TypeList, + Elem: &schema.Schema{Type: schema.TypeString}, + Computed: true, + }, + "cidr": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + "ipam_pool_allocation_id": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + "ipam_pool_allocation_name": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + "ipam_pool_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "allocations": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "cidr": { + Type: schema.TypeString, + Computed: true, + }, + "create_time": { + Type: schema.TypeString, + Computed: true, + }, + "ipam_pool_allocation_description": { + Type: schema.TypeString, + Computed: true, + }, + "ipam_pool_allocation_id": { + Type: schema.TypeString, + Computed: true, + }, + "ipam_pool_allocation_name": { + Type: schema.TypeString, + Computed: true, + }, + "ipam_pool_id": { + Type: schema.TypeString, + Computed: true, + }, + "region_id": { + Type: schema.TypeString, + Computed: true, + }, + "resource_id": { + Type: schema.TypeString, + Computed: true, + }, + "resource_owner_id": { + Type: schema.TypeInt, + Computed: true, + }, + "resource_region_id": { + Type: schema.TypeString, + Computed: true, + }, + "resource_type": { + Type: schema.TypeString, + Computed: true, + }, + "source_cidr": { + Type: schema.TypeString, + Computed: true, + }, + "status": { + Type: schema.TypeString, + Computed: true, + }, + "total_count": { + Type: schema.TypeInt, + Computed: true, + }, + "id": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + "output_file": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + }, + } +} + +func dataSourceAliCloudVpcIpamIpamPoolAllocationRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*connectivity.AliyunClient) + + var objects []map[string]interface{} + var nameRegex *regexp.Regexp + if v, ok := d.GetOk("name_regex"); ok { + r, err := regexp.Compile(v.(string)) + if err != nil { + return WrapError(err) + } + nameRegex = r + } + + idsMap := make(map[string]string) + if v, ok := d.GetOk("ids"); ok { + for _, vv := range v.([]interface{}) { + if vv == nil { + continue + } + idsMap[vv.(string)] = vv.(string) + } + } + + var request map[string]interface{} + var response map[string]interface{} + var query map[string]interface{} + action := "ListIpamPoolAllocations" + conn, err := client.NewVpcipamClient() + if err != nil { + return WrapError(err) + } + request = make(map[string]interface{}) + query = make(map[string]interface{}) + request["RegionId"] = client.RegionId + if v, ok := d.GetOk("cidr"); ok { + request["Cidr"] = v + } + if v, ok := d.GetOk("ipam_pool_allocation_name"); ok { + request["IpamPoolAllocationName"] = v + } + request["IpamPoolId"] = d.Get("ipam_pool_id") + if v, ok := d.GetOk("ipam_pool_allocation_id"); ok { + request["IpamPoolAllocationId"] = v + } + runtime := util.RuntimeOptions{} + runtime.SetAutoretry(true) + request["MaxResults"] = PageSizeLarge + for { + 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) + } + addDebug(action, response, request) + return nil + }) + if err != nil { + return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR) + } + + resp, _ := jsonpath.Get("$.IpamPoolAllocations[*]", response) + + result, _ := resp.([]interface{}) + for _, v := range result { + item := v.(map[string]interface{}) + if nameRegex != nil && !nameRegex.MatchString(fmt.Sprint(item["IpamPoolAllocationName"])) { + continue + } + if len(idsMap) > 0 { + if _, ok := idsMap[fmt.Sprint(item["IpamPoolAllocationId"])]; !ok { + continue + } + } + objects = append(objects, item) + } + + if nextToken, ok := response["NextToken"].(string); ok && nextToken != "" { + request["NextToken"] = nextToken + } else { + break + } + } + + ids := make([]string, 0) + names := make([]interface{}, 0) + s := make([]map[string]interface{}, 0) + for _, objectRaw := range objects { + mapping := map[string]interface{}{} + + mapping["id"] = objectRaw["IpamPoolAllocationId"] + + mapping["cidr"] = objectRaw["Cidr"] + mapping["create_time"] = objectRaw["CreationTime"] + mapping["ipam_pool_allocation_description"] = objectRaw["IpamPoolAllocationDescription"] + mapping["ipam_pool_allocation_name"] = objectRaw["IpamPoolAllocationName"] + mapping["ipam_pool_id"] = objectRaw["IpamPoolId"] + mapping["region_id"] = objectRaw["RegionId"] + mapping["resource_id"] = objectRaw["ResourceId"] + mapping["resource_owner_id"] = objectRaw["ResourceOwnerId"] + mapping["resource_region_id"] = objectRaw["ResourceRegionId"] + mapping["resource_type"] = objectRaw["ResourceType"] + mapping["source_cidr"] = objectRaw["SourceCidr"] + mapping["status"] = objectRaw["Status"] + mapping["ipam_pool_allocation_id"] = objectRaw["IpamPoolAllocationId"] + + ids = append(ids, fmt.Sprint(mapping["id"])) + names = append(names, objectRaw["IpamPoolAllocationName"]) + s = append(s, mapping) + } + + d.SetId(dataResourceIdHash(ids)) + if err := d.Set("ids", ids); err != nil { + return WrapError(err) + } + + if err := d.Set("allocations", s); err != nil { + return WrapError(err) + } + + if output, ok := d.GetOk("output_file"); ok && output.(string) != "" { + writeToFile(output.(string), s) + } + return nil +} diff --git a/alicloud/data_source_alicloud_vpc_ipam_ipam_pool_allocations_test.go b/alicloud/data_source_alicloud_vpc_ipam_ipam_pool_allocations_test.go new file mode 100644 index 000000000000..4b8a74319ca9 --- /dev/null +++ b/alicloud/data_source_alicloud_vpc_ipam_ipam_pool_allocations_test.go @@ -0,0 +1,151 @@ +package alicloud + +import ( + "fmt" + "strings" + "testing" + + "github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity" + "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" +) + +func TestAccAlicloudVpcIpamIpamPoolAllocationDataSource(t *testing.T) { + testAccPreCheckWithRegions(t, true, []connectivity.Region{"cn-hangzhou"}) + rand := acctest.RandIntRange(1000000, 9999999) + + idsConf := dataSourceTestAccConfig{ + existConfig: testAccCheckAlicloudVpcIpamIpamPoolAllocationSourceConfig(rand, map[string]string{ + "ids": `["${alicloud_vpc_ipam_ipam_pool_allocation.default.id}"]`, + "ipam_pool_id": `"${alicloud_vpc_ipam_ipam_pool.defaultIpamPool.id}"`, + }), + fakeConfig: testAccCheckAlicloudVpcIpamIpamPoolAllocationSourceConfig(rand, map[string]string{ + "ids": `["${alicloud_vpc_ipam_ipam_pool_allocation.default.id}_fake"]`, + "ipam_pool_id": `"${alicloud_vpc_ipam_ipam_pool.defaultIpamPool.id}"`, + }), + } + + IpamPoolAllocationNameConf := dataSourceTestAccConfig{ + existConfig: testAccCheckAlicloudVpcIpamIpamPoolAllocationSourceConfig(rand, map[string]string{ + "ids": `["${alicloud_vpc_ipam_ipam_pool_allocation.default.id}"]`, + "ipam_pool_allocation_name": `"${var.name}"`, + "ipam_pool_id": `"${alicloud_vpc_ipam_ipam_pool.defaultIpamPool.id}"`, + }), + fakeConfig: testAccCheckAlicloudVpcIpamIpamPoolAllocationSourceConfig(rand, map[string]string{ + "ids": `["${alicloud_vpc_ipam_ipam_pool_allocation.default.id}_fake"]`, + "ipam_pool_allocation_name": `"${var.name}_fake"`, + "ipam_pool_id": `"${alicloud_vpc_ipam_ipam_pool.defaultIpamPool.id}"`, + }), + } + IpamPoolIdConf := dataSourceTestAccConfig{ + existConfig: testAccCheckAlicloudVpcIpamIpamPoolAllocationSourceConfig(rand, map[string]string{ + "ids": `["${alicloud_vpc_ipam_ipam_pool_allocation.default.id}"]`, + "ipam_pool_id": `"${alicloud_vpc_ipam_ipam_pool.defaultIpamPool.id}"`, + }), + fakeConfig: testAccCheckAlicloudVpcIpamIpamPoolAllocationSourceConfig(rand, map[string]string{ + "ids": `["${alicloud_vpc_ipam_ipam_pool_allocation.default.id}_fake"]`, + "ipam_pool_id": `"${alicloud_vpc_ipam_ipam_pool.defaultIpamPool.id}_fake"`, + }), + } + CidrConf := dataSourceTestAccConfig{ + existConfig: testAccCheckAlicloudVpcIpamIpamPoolAllocationSourceConfig(rand, map[string]string{ + "ids": `["${alicloud_vpc_ipam_ipam_pool_allocation.default.id}"]`, + "cidr": `"10.0.0.0/20"`, + "ipam_pool_id": `"${alicloud_vpc_ipam_ipam_pool.defaultIpamPool.id}"`, + }), + fakeConfig: testAccCheckAlicloudVpcIpamIpamPoolAllocationSourceConfig(rand, map[string]string{ + "ids": `["${alicloud_vpc_ipam_ipam_pool_allocation.default.id}_fake"]`, + "cidr": `"10.0.0.0/20_fake"`, + "ipam_pool_id": `"${alicloud_vpc_ipam_ipam_pool.defaultIpamPool.id}"`, + }), + } + + allConf := dataSourceTestAccConfig{ + existConfig: testAccCheckAlicloudVpcIpamIpamPoolAllocationSourceConfig(rand, map[string]string{ + "ids": `["${alicloud_vpc_ipam_ipam_pool_allocation.default.id}"]`, + "ipam_pool_allocation_name": `"${var.name}"`, + + "ipam_pool_id": `"${alicloud_vpc_ipam_ipam_pool.defaultIpamPool.id}"`, + + "cidr": `"10.0.0.0/20"`, + }), + fakeConfig: testAccCheckAlicloudVpcIpamIpamPoolAllocationSourceConfig(rand, map[string]string{ + "ids": `["${alicloud_vpc_ipam_ipam_pool_allocation.default.id}_fake"]`, + "ipam_pool_allocation_name": `"${var.name}_fake"`, + + "ipam_pool_id": `"${alicloud_vpc_ipam_ipam_pool.defaultIpamPool.id}_fake"`, + + "cidr": `"10.0.0.0/20_fake"`, + }), + } + + VpcIpamIpamPoolAllocationCheckInfo.dataSourceTestCheck(t, rand, idsConf, IpamPoolAllocationNameConf, IpamPoolIdConf, CidrConf, allConf) +} + +var existVpcIpamIpamPoolAllocationMapFunc = func(rand int) map[string]string { + return map[string]string{ + "allocations.#": "1", + "allocations.0.status": CHECKSET, + "allocations.0.ipam_pool_allocation_description": CHECKSET, + "allocations.0.source_cidr": CHECKSET, + "allocations.0.ipam_pool_id": CHECKSET, + "allocations.0.create_time": CHECKSET, + "allocations.0.resource_type": CHECKSET, + "allocations.0.resource_owner_id": CHECKSET, + "allocations.0.ipam_pool_allocation_name": CHECKSET, + "allocations.0.total_count": CHECKSET, + "allocations.0.cidr": CHECKSET, + "allocations.0.ipam_pool_allocation_id": CHECKSET, + "allocations.0.region_id": CHECKSET, + } +} + +var fakeVpcIpamIpamPoolAllocationMapFunc = func(rand int) map[string]string { + return map[string]string{ + "allocations.#": "0", + } +} + +var VpcIpamIpamPoolAllocationCheckInfo = dataSourceAttr{ + resourceId: "data.alicloud_vpc_ipam_ipam_pool_allocations.default", + existMapFunc: existVpcIpamIpamPoolAllocationMapFunc, + fakeMapFunc: fakeVpcIpamIpamPoolAllocationMapFunc, +} + +func testAccCheckAlicloudVpcIpamIpamPoolAllocationSourceConfig(rand int, attrMap map[string]string) string { + var pairs []string + for k, v := range attrMap { + pairs = append(pairs, k+" = "+v) + } + config := fmt.Sprintf(` +variable "name" { + default = "tf-testAccVpcIpamIpamPoolAllocation%d" +} +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 = "cn-hangzhou" +} + +resource "alicloud_vpc_ipam_ipam_pool_cidr" "defaultIpamPoolCidr" { + cidr = "10.0.0.0/8" + ipam_pool_id = alicloud_vpc_ipam_ipam_pool.defaultIpamPool.id +} + + + +resource "alicloud_vpc_ipam_ipam_pool_allocation" "default" { + ipam_pool_allocation_description = "init alloc desc" + ipam_pool_allocation_name = var.name + cidr = "10.0.0.0/20" + ipam_pool_id = alicloud_vpc_ipam_ipam_pool_cidr.defaultIpamPoolCidr.ipam_pool_id +} + +data "alicloud_vpc_ipam_ipam_pool_allocations" "default" { +%s +} +`, rand, strings.Join(pairs, "\n ")) + return config +} diff --git a/alicloud/data_source_alicloud_vpc_ipam_ipam_pools.go b/alicloud/data_source_alicloud_vpc_ipam_ipam_pools.go new file mode 100644 index 000000000000..bfe2271bf2c8 --- /dev/null +++ b/alicloud/data_source_alicloud_vpc_ipam_ipam_pools.go @@ -0,0 +1,316 @@ +// Package alicloud. This file is generated automatically. Please do not modify it manually, thank you! +package alicloud + +import ( + "fmt" + "regexp" + "time" + + "github.com/PaesslerAG/jsonpath" + util "github.com/alibabacloud-go/tea-utils/service" + "github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity" + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/helper/validation" +) + +func dataSourceAliCloudVpcIpamIpamPools() *schema.Resource { + return &schema.Resource{ + Read: dataSourceAliCloudVpcIpamIpamPoolRead, + Schema: map[string]*schema.Schema{ + "ids": { + Type: schema.TypeList, + Optional: true, + ForceNew: true, + Elem: &schema.Schema{Type: schema.TypeString}, + Computed: true, + }, + "name_regex": { + Type: schema.TypeString, + Optional: true, + ValidateFunc: validation.ValidateRegexp, + ForceNew: true, + }, + "names": { + Type: schema.TypeList, + Elem: &schema.Schema{Type: schema.TypeString}, + Computed: true, + }, + "ipam_pool_id": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + "ipam_pool_name": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + "ipam_scope_id": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + "pool_region_id": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + "resource_group_id": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + "source_ipam_pool_id": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + "tags": { + Type: schema.TypeMap, + Optional: true, + ForceNew: true, + }, + "pools": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "allocation_default_cidr_mask": { + Type: schema.TypeInt, + Computed: true, + }, + "allocation_max_cidr_mask": { + Type: schema.TypeInt, + Computed: true, + }, + "allocation_min_cidr_mask": { + Type: schema.TypeInt, + Computed: true, + }, + "auto_import": { + Type: schema.TypeBool, + Computed: true, + }, + "create_time": { + Type: schema.TypeString, + Computed: true, + }, + "has_sub_pool": { + Type: schema.TypeBool, + Computed: true, + }, + "ip_version": { + Type: schema.TypeString, + Computed: true, + }, + "ipam_id": { + Type: schema.TypeString, + Computed: true, + }, + "ipam_pool_description": { + Type: schema.TypeString, + Computed: true, + }, + "ipam_pool_id": { + Type: schema.TypeString, + Computed: true, + }, + "ipam_pool_name": { + Type: schema.TypeString, + Computed: true, + }, + "ipam_scope_id": { + Type: schema.TypeString, + Computed: true, + }, + "pool_depth": { + Type: schema.TypeInt, + Computed: true, + }, + "pool_region_id": { + Type: schema.TypeString, + Computed: true, + }, + "resource_group_id": { + Type: schema.TypeString, + Computed: true, + }, + "source_ipam_pool_id": { + Type: schema.TypeString, + Computed: true, + }, + "status": { + Type: schema.TypeString, + Computed: true, + }, + "tags": { + Type: schema.TypeMap, + Computed: true, + }, + "id": { + Type: schema.TypeString, + Computed: true, + }, + "region_id": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + "output_file": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + }, + } +} + +func dataSourceAliCloudVpcIpamIpamPoolRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*connectivity.AliyunClient) + + var objects []map[string]interface{} + var nameRegex *regexp.Regexp + if v, ok := d.GetOk("name_regex"); ok { + r, err := regexp.Compile(v.(string)) + if err != nil { + return WrapError(err) + } + nameRegex = r + } + + idsMap := make(map[string]string) + if v, ok := d.GetOk("ids"); ok { + for _, vv := range v.([]interface{}) { + if vv == nil { + continue + } + idsMap[vv.(string)] = vv.(string) + } + } + + 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 WrapError(err) + } + request = make(map[string]interface{}) + query = make(map[string]interface{}) + request["RegionId"] = client.RegionId + if v, ok := d.GetOk("ipam_pool_name"); ok { + request["IpamPoolName"] = v + } + request["IpamScopeId"] = d.Get("ipam_scope_id") + if v, ok := d.GetOk("pool_region_id"); ok { + request["PoolRegionId"] = v + } + if v, ok := d.GetOk("resource_group_id"); ok { + request["ResourceGroupId"] = v + } + if v, ok := d.GetOk("source_ipam_pool_id"); ok { + request["SourceIpamPoolId"] = v + } + if v, ok := d.GetOk("tags"); ok { + tagsMap := ConvertTags(v.(map[string]interface{})) + request = expandTagsToMap(request, tagsMap) + } + + runtime := util.RuntimeOptions{} + runtime.SetAutoretry(true) + request["MaxResults"] = PageSizeLarge + for { + 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) + } + addDebug(action, response, request) + return nil + }) + if err != nil { + return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR) + } + + resp, _ := jsonpath.Get("$.IpamPools[*]", response) + + result, _ := resp.([]interface{}) + for _, v := range result { + item := v.(map[string]interface{}) + if nameRegex != nil && !nameRegex.MatchString(fmt.Sprint(item["IpamPoolName"])) { + continue + } + if len(idsMap) > 0 { + if _, ok := idsMap[fmt.Sprint(item["IpamPoolId"])]; !ok { + continue + } + } + objects = append(objects, item) + } + + if nextToken, ok := response["NextToken"].(string); ok && nextToken != "" { + request["NextToken"] = nextToken + } else { + break + } + } + + ids := make([]string, 0) + names := make([]interface{}, 0) + s := make([]map[string]interface{}, 0) + for _, objectRaw := range objects { + mapping := map[string]interface{}{} + + mapping["id"] = objectRaw["IpamPoolId"] + + mapping["allocation_default_cidr_mask"] = objectRaw["AllocationDefaultCidrMask"] + mapping["allocation_max_cidr_mask"] = objectRaw["AllocationMaxCidrMask"] + mapping["allocation_min_cidr_mask"] = objectRaw["AllocationMinCidrMask"] + mapping["auto_import"] = objectRaw["AutoImport"] + mapping["create_time"] = objectRaw["CreateTime"] + mapping["has_sub_pool"] = objectRaw["HasSubPool"] + mapping["ip_version"] = objectRaw["IpVersion"] + mapping["ipam_id"] = objectRaw["IpamId"] + mapping["ipam_pool_description"] = objectRaw["IpamPoolDescription"] + mapping["ipam_pool_name"] = objectRaw["IpamPoolName"] + mapping["ipam_scope_id"] = objectRaw["IpamScopeId"] + mapping["pool_depth"] = objectRaw["PoolDepth"] + mapping["pool_region_id"] = objectRaw["PoolRegionId"] + mapping["resource_group_id"] = objectRaw["ResourceGroupId"] + mapping["source_ipam_pool_id"] = objectRaw["SourceIpamPoolId"] + mapping["status"] = objectRaw["Status"] + mapping["ipam_pool_id"] = objectRaw["IpamPoolId"] + mapping["region_id"] = objectRaw["IpamRegionId"] + + tagsMaps := objectRaw["Tags"] + mapping["tags"] = tagsToMap(tagsMaps) + + ids = append(ids, fmt.Sprint(mapping["id"])) + names = append(names, objectRaw["IpamPoolName"]) + s = append(s, mapping) + } + + d.SetId(dataResourceIdHash(ids)) + if err := d.Set("ids", ids); err != nil { + return WrapError(err) + } + + if err := d.Set("pools", s); err != nil { + return WrapError(err) + } + + if output, ok := d.GetOk("output_file"); ok && output.(string) != "" { + writeToFile(output.(string), s) + } + return nil +} diff --git a/alicloud/data_source_alicloud_vpc_ipam_ipam_pools_test.go b/alicloud/data_source_alicloud_vpc_ipam_ipam_pools_test.go new file mode 100644 index 000000000000..e35c9701710c --- /dev/null +++ b/alicloud/data_source_alicloud_vpc_ipam_ipam_pools_test.go @@ -0,0 +1,178 @@ +package alicloud + +import ( + "fmt" + "strings" + "testing" + + "github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity" + "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" +) + +func TestAccAlicloudVpcIpamIpamPoolDataSource(t *testing.T) { + testAccPreCheckWithRegions(t, true, []connectivity.Region{"cn-hangzhou"}) + rand := acctest.RandIntRange(1000000, 9999999) + + idsConf := dataSourceTestAccConfig{ + existConfig: testAccCheckAlicloudVpcIpamIpamPoolSourceConfig(rand, map[string]string{ + "ids": `["${alicloud_vpc_ipam_ipam_pool.default.id}"]`, + }), + fakeConfig: testAccCheckAlicloudVpcIpamIpamPoolSourceConfig(rand, map[string]string{ + "ids": `["${alicloud_vpc_ipam_ipam_pool.default.id}_fake"]`, + }), + } + + PoolRegionIdConf := dataSourceTestAccConfig{ + existConfig: testAccCheckAlicloudVpcIpamIpamPoolSourceConfig(rand, map[string]string{ + "ids": `["${alicloud_vpc_ipam_ipam_pool.default.id}"]`, + "pool_region_id": `"${alicloud_vpc_ipam_ipam_pool.parentIpamPool.pool_region_id}"`, + }), + fakeConfig: testAccCheckAlicloudVpcIpamIpamPoolSourceConfig(rand, map[string]string{ + "ids": `["${alicloud_vpc_ipam_ipam_pool.default.id}_fake"]`, + "pool_region_id": `"${alicloud_vpc_ipam_ipam_pool.parentIpamPool.pool_region_id}_fake"`, + }), + } + ResourceGroupIdConf := dataSourceTestAccConfig{ + existConfig: testAccCheckAlicloudVpcIpamIpamPoolSourceConfig(rand, map[string]string{ + "ids": `["${alicloud_vpc_ipam_ipam_pool.default.id}"]`, + "resource_group_id": `"${data.alicloud_resource_manager_resource_groups.default.ids.0}"`, + }), + fakeConfig: testAccCheckAlicloudVpcIpamIpamPoolSourceConfig(rand, map[string]string{ + "ids": `["${alicloud_vpc_ipam_ipam_pool.default.id}_fake"]`, + "resource_group_id": `"${data.alicloud_resource_manager_resource_groups.default.ids.0}_fake"`, + }), + } + SourceIpamPoolIdConf := dataSourceTestAccConfig{ + existConfig: testAccCheckAlicloudVpcIpamIpamPoolSourceConfig(rand, map[string]string{ + "ids": `["${alicloud_vpc_ipam_ipam_pool.default.id}"]`, + "source_ipam_pool_id": `"${alicloud_vpc_ipam_ipam_pool.parentIpamPool.id}"`, + }), + fakeConfig: testAccCheckAlicloudVpcIpamIpamPoolSourceConfig(rand, map[string]string{ + "ids": `["${alicloud_vpc_ipam_ipam_pool.default.id}_fake"]`, + "source_ipam_pool_id": `"${alicloud_vpc_ipam_ipam_pool.parentIpamPool.id}_fake"`, + }), + } + IpamScopeIdConf := dataSourceTestAccConfig{ + existConfig: testAccCheckAlicloudVpcIpamIpamPoolSourceConfig(rand, map[string]string{ + "ids": `["${alicloud_vpc_ipam_ipam_pool.default.id}"]`, + "ipam_scope_id": `"${alicloud_vpc_ipam_ipam.defaultIpam.private_default_scope_id}"`, + }), + fakeConfig: testAccCheckAlicloudVpcIpamIpamPoolSourceConfig(rand, map[string]string{ + "ids": `["${alicloud_vpc_ipam_ipam_pool.default.id}_fake"]`, + "ipam_scope_id": `"${alicloud_vpc_ipam_ipam.defaultIpam.private_default_scope_id}_fake"`, + }), + } + IpamPoolNameConf := dataSourceTestAccConfig{ + existConfig: testAccCheckAlicloudVpcIpamIpamPoolSourceConfig(rand, map[string]string{ + "ids": `["${alicloud_vpc_ipam_ipam_pool.default.id}"]`, + "ipam_pool_name": `"${var.name}"`, + }), + fakeConfig: testAccCheckAlicloudVpcIpamIpamPoolSourceConfig(rand, map[string]string{ + "ids": `["${alicloud_vpc_ipam_ipam_pool.default.id}_fake"]`, + "ipam_pool_name": `"${var.name}_fake"`, + }), + } + + allConf := dataSourceTestAccConfig{ + existConfig: testAccCheckAlicloudVpcIpamIpamPoolSourceConfig(rand, map[string]string{ + "ids": `["${alicloud_vpc_ipam_ipam_pool.default.id}"]`, + "pool_region_id": `"${alicloud_vpc_ipam_ipam_pool.parentIpamPool.pool_region_id}"`, + + "resource_group_id": `"${data.alicloud_resource_manager_resource_groups.default.ids.0}"`, + + "source_ipam_pool_id": `"${alicloud_vpc_ipam_ipam_pool.parentIpamPool.id}"`, + + "ipam_scope_id": `"${alicloud_vpc_ipam_ipam.defaultIpam.private_default_scope_id}"`, + + "ipam_pool_name": `"${var.name}"`, + }), + fakeConfig: testAccCheckAlicloudVpcIpamIpamPoolSourceConfig(rand, map[string]string{ + "ids": `["${alicloud_vpc_ipam_ipam_pool.default.id}_fake"]`, + "pool_region_id": `"${alicloud_vpc_ipam_ipam_pool.parentIpamPool.pool_region_id}_fake"`, + + "resource_group_id": `"${data.alicloud_resource_manager_resource_groups.default.ids.0}_fake"`, + + "source_ipam_pool_id": `"${alicloud_vpc_ipam_ipam_pool.parentIpamPool.id}_fake"`, + + "ipam_scope_id": `"${alicloud_vpc_ipam_ipam.defaultIpam.private_default_scope_id}_fake"`, + + "ipam_pool_name": `"${var.name}_fake"`, + }), + } + + VpcIpamIpamPoolCheckInfo.dataSourceTestCheck(t, rand, idsConf, PoolRegionIdConf, ResourceGroupIdConf, SourceIpamPoolIdConf, IpamScopeIdConf, IpamPoolNameConf, allConf) +} + +var existVpcIpamIpamPoolMapFunc = func(rand int) map[string]string { + return map[string]string{ + "pools.#": "1", + "pools.0.status": CHECKSET, + "pools.0.resource_group_id": CHECKSET, + "pools.0.ipam_pool_id": CHECKSET, + "pools.0.ipam_pool_name": CHECKSET, + "pools.0.source_ipam_pool_id": CHECKSET, + "pools.0.ip_version": CHECKSET, + "pools.0.create_time": CHECKSET, + "pools.0.ipam_id": CHECKSET, + "pools.0.allocation_default_cidr_mask": CHECKSET, + "pools.0.allocation_min_cidr_mask": CHECKSET, + "pools.0.ipam_scope_id": CHECKSET, + "pools.0.ipam_pool_description": CHECKSET, + "pools.0.pool_region_id": CHECKSET, + "pools.0.pool_depth": CHECKSET, + "pools.0.has_sub_pool": CHECKSET, + "pools.0.auto_import": CHECKSET, + "pools.0.tags.%": CHECKSET, + "pools.0.allocation_max_cidr_mask": CHECKSET, + } +} + +var fakeVpcIpamIpamPoolMapFunc = func(rand int) map[string]string { + return map[string]string{ + "pools.#": "0", + } +} + +var VpcIpamIpamPoolCheckInfo = dataSourceAttr{ + resourceId: "data.alicloud_vpc_ipam_ipam_pools.default", + existMapFunc: existVpcIpamIpamPoolMapFunc, + fakeMapFunc: fakeVpcIpamIpamPoolMapFunc, +} + +func testAccCheckAlicloudVpcIpamIpamPoolSourceConfig(rand int, attrMap map[string]string) string { + var pairs []string + for k, v := range attrMap { + pairs = append(pairs, k+" = "+v) + } + config := fmt.Sprintf(` +variable "name" { + default = "tf-testAccVpcIpamIpamPool%d" +} +data "alicloud_resource_manager_resource_groups" "default" {} + +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 + pool_region_id = "cn-hangzhou" +} + + + +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" + ipam_pool_description = var.name +} + +data "alicloud_vpc_ipam_ipam_pools" "default" { +%s +} +`, rand, strings.Join(pairs, "\n ")) + return config +} diff --git a/alicloud/data_source_alicloud_vpc_ipam_ipams.go b/alicloud/data_source_alicloud_vpc_ipam_ipams.go new file mode 100644 index 000000000000..9eba450c3c7a --- /dev/null +++ b/alicloud/data_source_alicloud_vpc_ipam_ipams.go @@ -0,0 +1,267 @@ +// Package alicloud. This file is generated automatically. Please do not modify it manually, thank you! +package alicloud + +import ( + "fmt" + "regexp" + "time" + + "github.com/PaesslerAG/jsonpath" + util "github.com/alibabacloud-go/tea-utils/service" + "github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity" + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/helper/validation" +) + +func dataSourceAliCloudVpcIpamIpams() *schema.Resource { + return &schema.Resource{ + Read: dataSourceAliCloudVpcIpamIpamRead, + Schema: map[string]*schema.Schema{ + "ids": { + Type: schema.TypeList, + Optional: true, + ForceNew: true, + Elem: &schema.Schema{Type: schema.TypeString}, + Computed: true, + }, + "name_regex": { + Type: schema.TypeString, + Optional: true, + ValidateFunc: validation.ValidateRegexp, + ForceNew: true, + }, + "names": { + Type: schema.TypeList, + Elem: &schema.Schema{Type: schema.TypeString}, + Computed: true, + }, + "ipam_id": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + "ipam_name": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + "resource_group_id": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + "tags": { + Type: schema.TypeMap, + Optional: true, + ForceNew: true, + }, + "ipams": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "create_time": { + Type: schema.TypeString, + Computed: true, + }, + "default_resource_discovery_association_id": { + Type: schema.TypeString, + Computed: true, + }, + "default_resource_discovery_id": { + Type: schema.TypeString, + Computed: true, + }, + "ipam_description": { + Type: schema.TypeString, + Computed: true, + }, + "ipam_id": { + Type: schema.TypeString, + Computed: true, + }, + "ipam_name": { + Type: schema.TypeString, + Computed: true, + }, + "private_default_scope_id": { + Type: schema.TypeString, + Computed: true, + }, + "public_default_scope_id": { + Type: schema.TypeString, + Computed: true, + }, + "region_id": { + Type: schema.TypeString, + Computed: true, + }, + "resource_discovery_association_count": { + Type: schema.TypeInt, + Computed: true, + }, + "resource_group_id": { + Type: schema.TypeString, + Computed: true, + }, + "status": { + Type: schema.TypeString, + Computed: true, + }, + "tags": { + Type: schema.TypeMap, + Computed: true, + }, + "id": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + "output_file": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + }, + } +} + +func dataSourceAliCloudVpcIpamIpamRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*connectivity.AliyunClient) + + var objects []map[string]interface{} + var nameRegex *regexp.Regexp + if v, ok := d.GetOk("name_regex"); ok { + r, err := regexp.Compile(v.(string)) + if err != nil { + return WrapError(err) + } + nameRegex = r + } + + idsMap := make(map[string]string) + if v, ok := d.GetOk("ids"); ok { + for _, vv := range v.([]interface{}) { + if vv == nil { + continue + } + idsMap[vv.(string)] = vv.(string) + } + } + + 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 WrapError(err) + } + request = make(map[string]interface{}) + query = make(map[string]interface{}) + request["RegionId"] = client.RegionId + if v, ok := d.GetOk("ipam_id"); ok { + request["IpamIds.1"] = v + } + 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("tags"); ok { + tagsMap := ConvertTags(v.(map[string]interface{})) + request = expandTagsToMap(request, tagsMap) + } + + runtime := util.RuntimeOptions{} + runtime.SetAutoretry(true) + request["MaxResults"] = PageSizeLarge + for { + 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) + } + addDebug(action, response, request) + return nil + }) + if err != nil { + return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR) + } + + resp, _ := jsonpath.Get("$.Ipams[*]", response) + + result, _ := resp.([]interface{}) + for _, v := range result { + item := v.(map[string]interface{}) + if nameRegex != nil && !nameRegex.MatchString(fmt.Sprint(item["IpamName"])) { + continue + } + if len(idsMap) > 0 { + if _, ok := idsMap[fmt.Sprint(item["IpamId"])]; !ok { + continue + } + } + objects = append(objects, item) + } + + if nextToken, ok := response["NextToken"].(string); ok && nextToken != "" { + request["NextToken"] = nextToken + } else { + break + } + } + + ids := make([]string, 0) + names := make([]interface{}, 0) + s := make([]map[string]interface{}, 0) + for _, objectRaw := range objects { + mapping := map[string]interface{}{} + + mapping["id"] = objectRaw["IpamId"] + + mapping["create_time"] = objectRaw["CreateTime"] + mapping["default_resource_discovery_association_id"] = objectRaw["DefaultResourceDiscoveryAssociationId"] + mapping["default_resource_discovery_id"] = objectRaw["DefaultResourceDiscoveryId"] + mapping["ipam_description"] = objectRaw["IpamDescription"] + mapping["ipam_name"] = objectRaw["IpamName"] + mapping["private_default_scope_id"] = objectRaw["PrivateDefaultScopeId"] + mapping["public_default_scope_id"] = objectRaw["PublicDefaultScopeId"] + mapping["region_id"] = objectRaw["RegionId"] + mapping["resource_discovery_association_count"] = objectRaw["ResourceDiscoveryAssociationCount"] + mapping["resource_group_id"] = objectRaw["ResourceGroupId"] + mapping["status"] = objectRaw["IpamStatus"] + mapping["ipam_id"] = objectRaw["IpamId"] + + tagsMaps := objectRaw["Tags"] + mapping["tags"] = tagsToMap(tagsMaps) + + ids = append(ids, fmt.Sprint(mapping["id"])) + names = append(names, objectRaw["IpamName"]) + s = append(s, mapping) + } + + d.SetId(dataResourceIdHash(ids)) + if err := d.Set("ids", ids); err != nil { + return WrapError(err) + } + + if err := d.Set("ipams", s); err != nil { + return WrapError(err) + } + + if output, ok := d.GetOk("output_file"); ok && output.(string) != "" { + writeToFile(output.(string), s) + } + return nil +} diff --git a/alicloud/data_source_alicloud_vpc_ipam_ipams_test.go b/alicloud/data_source_alicloud_vpc_ipam_ipams_test.go new file mode 100644 index 000000000000..69d71ea346cf --- /dev/null +++ b/alicloud/data_source_alicloud_vpc_ipam_ipams_test.go @@ -0,0 +1,123 @@ +package alicloud + +import ( + "fmt" + "strings" + "testing" + + "github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity" + "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" +) + +func TestAccAlicloudVpcIpamIpamDataSource(t *testing.T) { + testAccPreCheckWithRegions(t, true, []connectivity.Region{"cn-hangzhou"}) + rand := acctest.RandIntRange(1000000, 9999999) + + idsConf := dataSourceTestAccConfig{ + existConfig: testAccCheckAlicloudVpcIpamIpamSourceConfig(rand, map[string]string{ + "ids": `["${alicloud_vpc_ipam_ipam.default.id}"]`, + }), + fakeConfig: testAccCheckAlicloudVpcIpamIpamSourceConfig(rand, map[string]string{ + "ids": `["${alicloud_vpc_ipam_ipam.default.id}_fake"]`, + }), + } + + IpamNameConf := dataSourceTestAccConfig{ + existConfig: testAccCheckAlicloudVpcIpamIpamSourceConfig(rand, map[string]string{ + "ids": `["${alicloud_vpc_ipam_ipam.default.id}"]`, + "ipam_name": `"${var.name}"`, + }), + fakeConfig: testAccCheckAlicloudVpcIpamIpamSourceConfig(rand, map[string]string{ + "ids": `["${alicloud_vpc_ipam_ipam.default.id}_fake"]`, + "ipam_name": `"${var.name}_fake"`, + }), + } + ResourceGroupIdConf := dataSourceTestAccConfig{ + existConfig: testAccCheckAlicloudVpcIpamIpamSourceConfig(rand, map[string]string{ + "ids": `["${alicloud_vpc_ipam_ipam.default.id}"]`, + "resource_group_id": `"${data.alicloud_resource_manager_resource_groups.default.ids.0}"`, + }), + fakeConfig: testAccCheckAlicloudVpcIpamIpamSourceConfig(rand, map[string]string{ + "ids": `["${alicloud_vpc_ipam_ipam.default.id}_fake"]`, + "resource_group_id": `"${data.alicloud_resource_manager_resource_groups.default.ids.0}_fake"`, + }), + } + + allConf := dataSourceTestAccConfig{ + existConfig: testAccCheckAlicloudVpcIpamIpamSourceConfig(rand, map[string]string{ + "ids": `["${alicloud_vpc_ipam_ipam.default.id}"]`, + "ipam_name": `"${var.name}"`, + + "resource_group_id": `"${data.alicloud_resource_manager_resource_groups.default.ids.0}"`, + }), + fakeConfig: testAccCheckAlicloudVpcIpamIpamSourceConfig(rand, map[string]string{ + "ids": `["${alicloud_vpc_ipam_ipam.default.id}_fake"]`, + "ipam_name": `"${var.name}_fake"`, + + "resource_group_id": `"${data.alicloud_resource_manager_resource_groups.default.ids.0}_fake"`, + }), + } + + VpcIpamIpamCheckInfo.dataSourceTestCheck(t, rand, idsConf, IpamNameConf, ResourceGroupIdConf, allConf) +} + +var existVpcIpamIpamMapFunc = func(rand int) map[string]string { + return map[string]string{ + "ipams.#": "1", + "ipams.0.status": CHECKSET, + "ipams.0.default_resource_discovery_association_id": CHECKSET, + "ipams.0.ipam_name": CHECKSET, + "ipams.0.resource_group_id": CHECKSET, + "ipams.0.ipam_id": CHECKSET, + "ipams.0.create_time": CHECKSET, + "ipams.0.ipam_description": CHECKSET, + "ipams.0.default_resource_discovery_id": CHECKSET, + "ipams.0.resource_discovery_association_count": CHECKSET, + "ipams.0.region_id": CHECKSET, + "ipams.0.private_default_scope_id": CHECKSET, + "ipams.0.public_default_scope_id": CHECKSET, + "ipams.0.tags.%": CHECKSET, + } +} + +var fakeVpcIpamIpamMapFunc = func(rand int) map[string]string { + return map[string]string{ + "ipams.#": "0", + } +} + +var VpcIpamIpamCheckInfo = dataSourceAttr{ + resourceId: "data.alicloud_vpc_ipam_ipams.default", + existMapFunc: existVpcIpamIpamMapFunc, + fakeMapFunc: fakeVpcIpamIpamMapFunc, +} + +func testAccCheckAlicloudVpcIpamIpamSourceConfig(rand int, attrMap map[string]string) string { + var pairs []string + for k, v := range attrMap { + pairs = append(pairs, k+" = "+v) + } + config := fmt.Sprintf(` +variable "name" { + default = "tf-testAccVpcIpamIpam%d" +} +data "alicloud_resource_manager_resource_groups" "default" {} + + + +resource "alicloud_vpc_ipam_ipam" "default" { + ipam_description = "This is my first Ipam." + ipam_name = var.name + resource_group_id = data.alicloud_resource_manager_resource_groups.default.ids.0 + tags = { + "k1": "v1" + } + operating_region_list = ["cn-hangzhou"] +} + +data "alicloud_vpc_ipam_ipams" "default" { +%s +} +`, rand, strings.Join(pairs, "\n ")) + return config +} diff --git a/alicloud/provider.go b/alicloud/provider.go index 8a226d456c7f..f522bd1e6714 100644 --- a/alicloud/provider.go +++ b/alicloud/provider.go @@ -172,34 +172,36 @@ func Provider() terraform.ResourceProvider { }, }, DataSourcesMap: map[string]*schema.Resource{ - "alicloud_gwlb_zones": dataSourceAliCloudGwlbZones(), - "alicloud_gpdb_data_backups": dataSourceAliCloudGpdbDataBackups(), - "alicloud_gpdb_log_backups": dataSourceAliCloudGpdbLogbackups(), - "alicloud_governance_baselines": dataSourceAliCloudGovernanceBaselines(), - "alicloud_vpn_gateway_zones": dataSourceAliCloudVPNGatewayZones(), - "alicloud_account": dataSourceAlicloudAccount(), - "alicloud_caller_identity": dataSourceAlicloudCallerIdentity(), - "alicloud_images": dataSourceAlicloudImages(), - "alicloud_regions": dataSourceAlicloudRegions(), - "alicloud_zones": dataSourceAlicloudZones(), - "alicloud_db_zones": dataSourceAlicloudDBZones(), - "alicloud_instance_type_families": dataSourceAlicloudInstanceTypeFamilies(), - "alicloud_instance_types": dataSourceAlicloudInstanceTypes(), - "alicloud_instances": dataSourceAlicloudInstances(), - "alicloud_disks": dataSourceAliCloudEcsDisks(), - "alicloud_network_interfaces": dataSourceAlicloudEcsNetworkInterfaces(), - "alicloud_snapshots": dataSourceAlicloudEcsSnapshots(), - "alicloud_vpcs": dataSourceAlicloudVpcs(), - "alicloud_vswitches": dataSourceAlicloudVswitches(), - "alicloud_eips": dataSourceAlicloudEipAddresses(), - "alicloud_key_pairs": dataSourceAlicloudEcsKeyPairs(), - "alicloud_kms_keys": dataSourceAlicloudKmsKeys(), - "alicloud_kms_ciphertext": dataSourceAlicloudKmsCiphertext(), - "alicloud_kms_plaintext": dataSourceAlicloudKmsPlaintext(), - "alicloud_dns_resolution_lines": dataSourceAlicloudDnsResolutionLines(), - "alicloud_dns_domains": dataSourceAlicloudAlidnsDomains(), - "alicloud_dns_groups": dataSourceAlicloudDnsGroups(), - "alicloud_dns_records": dataSourceAlicloudDnsRecords(), + "alicloud_vpc_ipam_ipam_pool_allocations": dataSourceAliCloudVpcIpamIpamPoolAllocations(), + "alicloud_vpc_ipam_ipam_pools": dataSourceAliCloudVpcIpamIpamPools(), + "alicloud_gwlb_zones": dataSourceAliCloudGwlbZones(), + "alicloud_gpdb_data_backups": dataSourceAliCloudGpdbDataBackups(), + "alicloud_gpdb_log_backups": dataSourceAliCloudGpdbLogbackups(), + "alicloud_governance_baselines": dataSourceAliCloudGovernanceBaselines(), + "alicloud_vpn_gateway_zones": dataSourceAliCloudVPNGatewayZones(), + "alicloud_account": dataSourceAlicloudAccount(), + "alicloud_caller_identity": dataSourceAlicloudCallerIdentity(), + "alicloud_images": dataSourceAlicloudImages(), + "alicloud_regions": dataSourceAlicloudRegions(), + "alicloud_zones": dataSourceAlicloudZones(), + "alicloud_db_zones": dataSourceAlicloudDBZones(), + "alicloud_instance_type_families": dataSourceAlicloudInstanceTypeFamilies(), + "alicloud_instance_types": dataSourceAlicloudInstanceTypes(), + "alicloud_instances": dataSourceAlicloudInstances(), + "alicloud_disks": dataSourceAliCloudEcsDisks(), + "alicloud_network_interfaces": dataSourceAlicloudEcsNetworkInterfaces(), + "alicloud_snapshots": dataSourceAlicloudEcsSnapshots(), + "alicloud_vpcs": dataSourceAlicloudVpcs(), + "alicloud_vswitches": dataSourceAlicloudVswitches(), + "alicloud_eips": dataSourceAlicloudEipAddresses(), + "alicloud_key_pairs": dataSourceAlicloudEcsKeyPairs(), + "alicloud_kms_keys": dataSourceAlicloudKmsKeys(), + "alicloud_kms_ciphertext": dataSourceAlicloudKmsCiphertext(), + "alicloud_kms_plaintext": dataSourceAlicloudKmsPlaintext(), + "alicloud_dns_resolution_lines": dataSourceAlicloudDnsResolutionLines(), + "alicloud_dns_domains": dataSourceAlicloudAlidnsDomains(), + "alicloud_dns_groups": dataSourceAlicloudDnsGroups(), + "alicloud_dns_records": dataSourceAlicloudDnsRecords(), // alicloud_dns_domain_groups, alicloud_dns_domain_records have been deprecated. "alicloud_dns_domain_groups": dataSourceAlicloudDnsGroups(), "alicloud_dns_domain_records": dataSourceAlicloudDnsRecords(), @@ -877,6 +879,7 @@ func Provider() terraform.ResourceProvider { "alicloud_quotas_template_applications": dataSourceAliCloudQuotasTemplateApplications(), "alicloud_cloud_monitor_service_hybrid_double_writes": dataSourceAliCloudCloudMonitorServiceHybridDoubleWrites(), "alicloud_cms_site_monitors": dataSourceAliCloudCloudMonitorServiceSiteMonitors(), + "alicloud_vpc_ipam_ipams": dataSourceAliCloudVpcIpamIpams(), }, ResourcesMap: map[string]*schema.Resource{ "alicloud_oss_access_point": resourceAliCloudOssAccessPoint(), diff --git a/alicloud/service_alicloud_vpc_ipam_v2.go b/alicloud/service_alicloud_vpc_ipam_v2.go index d9b45d40391c..21042ba4752f 100644 --- a/alicloud/service_alicloud_vpc_ipam_v2.go +++ b/alicloud/service_alicloud_vpc_ipam_v2.go @@ -84,6 +84,13 @@ func (s *VpcIpamServiceV2) VpcIpamIpamStateRefreshFunc(id string, field string, 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)) @@ -339,6 +346,13 @@ func (s *VpcIpamServiceV2) VpcIpamIpamPoolStateRefreshFunc(id string, field stri 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)) @@ -427,6 +441,7 @@ func (s *VpcIpamServiceV2) VpcIpamIpamPoolCidrStateRefreshFunc(id string, field } // DescribeVpcIpamIpamPoolCidr >>> Encapsulated. + // DescribeVpcIpamIpamPoolAllocation <<< Encapsulated get interface for VpcIpam IpamPoolAllocation. func (s *VpcIpamServiceV2) DescribeVpcIpamIpamPoolAllocation(id string) (object map[string]interface{}, err error) { diff --git a/website/docs/d/vpc_ipam_ipam_pool_allocations.html.markdown b/website/docs/d/vpc_ipam_ipam_pool_allocations.html.markdown new file mode 100644 index 000000000000..8e5eca869127 --- /dev/null +++ b/website/docs/d/vpc_ipam_ipam_pool_allocations.html.markdown @@ -0,0 +1,91 @@ +--- +subcategory: "Vpc Ipam" +layout: "alicloud" +page_title: "Alicloud: alicloud_vpc_ipam_ipam_pool_allocations" +sidebar_current: "docs-alicloud-datasource-vpc-ipam-ipam-pool-allocations" +description: |- + Provides a list of Vpc Ipam Ipam Pool Allocation owned by an Alibaba Cloud account. +--- + +# alicloud_vpc_ipam_ipam_pool_allocations + +This data source provides Vpc Ipam Ipam Pool Allocation available to the user.[What is Ipam Pool Allocation](https://www.alibabacloud.com/help/en/) + +-> **NOTE:** Available since v1.241.0. + +## Example Usage + +```terraform +variable "name" { + default = "terraform-example" +} + +provider "alicloud" { + region = "cn-hangzhou" +} + +data "alicloud_resource_manager_resource_groups" "default" {} + +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 = "cn-hangzhou" +} + +resource "alicloud_vpc_ipam_ipam_pool_cidr" "defaultIpamPoolCidr" { + cidr = "10.0.0.0/8" + ipam_pool_id = alicloud_vpc_ipam_ipam_pool.defaultIpamPool.id +} + +resource "alicloud_vpc_ipam_ipam_pool_allocation" "default" { + ipam_pool_allocation_description = "init alloc desc" + ipam_pool_allocation_name = var.name + cidr = "10.0.0.0/20" + ipam_pool_id = alicloud_vpc_ipam_ipam_pool_cidr.defaultIpamPoolCidr.ipam_pool_id +} + +data "alicloud_vpc_ipam_ipam_pool_allocations" "default" { + ids = ["${alicloud_vpc_ipam_ipam_pool_allocation.default.id}"] +} + +output "alicloud_vpc_ipam_ipam_pool_allocation_example_id" { + value = data.alicloud_vpc_ipam_ipam_pool_allocations.default.allocations.0.id +} +``` + +## Argument Reference + +The following arguments are supported: +* `cidr` - (ForceNew, Optional) The allocated address segment. +* `ipam_pool_allocation_id` - (ForceNew, Optional) The instance ID of the ipam pool allocation. +* `ipam_pool_allocation_name` - (ForceNew, Optional) The name of the ipam pool allocation.It must be 1 to 128 characters in length and cannot start with 'http:// 'or 'https. +* `ipam_pool_id` - (Required, ForceNew) The ID of the IPAM Pool. +* `ids` - (Optional, ForceNew, Computed) A list of Ipam Pool Allocation IDs. +* `name_regex` - (Optional, ForceNew) A regex string to filter results by Group Metric Rule name. +* `output_file` - (Optional, ForceNew) File name where to save data source results (after running `terraform plan`). + + +## Attributes Reference + +The following attributes are exported in addition to the arguments listed above: +* `ids` - A list of Ipam Pool Allocation IDs. +* `names` - A list of name of Ipam Pool Allocations. +* `allocations` - A list of Ipam Pool Allocation Entries. Each element contains the following attributes: + * `cidr` - The allocated address segment. + * `create_time` - Instance creation time. + * `ipam_pool_allocation_description` - The description of the ipam pool alloctaion.It must be 1 to 256 characters in length and must start with an English letter or 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_allocation_id` - The instance ID of the ipam pool allocation. + * `ipam_pool_allocation_name` - The name of the ipam pool allocation.It must be 1 to 128 characters in length and cannot start with 'http:// 'or 'https. + * `ipam_pool_id` - The ID of the IPAM Pool. + * `region_id` - When the IPAM Pool to which CIDR is allocated has the region attribute, this attribute is the IPAM Pool region.When the IPAM Pool to which CIDR is allocated does not have the region attribute, this attribute is the IPAM region. + * `resource_id` - The ID of the resource. + * `resource_owner_id` - The ID of the Alibaba Cloud account (primary account) to which the resource belongs. + * `resource_region_id` - The region of the resource. + * `resource_type` - The type of resource. Value:-**VPC**: indicates that the resource type is VPC.-**IpamPool**: indicates that the resource type is a child address pool.-**Custom**: indicates that the resource type is a Custom reserved CIDR block. + * `source_cidr` - The source address segment. + * `status` - The status of the instance. Value:-**Created**: indicates that the creation is complete. + * `total_count` - Total number of records. + * `id` - The ID of the resource supplied above. diff --git a/website/docs/d/vpc_ipam_ipam_pools.html.markdown b/website/docs/d/vpc_ipam_ipam_pools.html.markdown new file mode 100644 index 000000000000..aa6f7b9925e4 --- /dev/null +++ b/website/docs/d/vpc_ipam_ipam_pools.html.markdown @@ -0,0 +1,96 @@ +--- +subcategory: "Vpc Ipam" +layout: "alicloud" +page_title: "Alicloud: alicloud_vpc_ipam_ipam_pools" +sidebar_current: "docs-alicloud-datasource-vpc-ipam-ipam-pools" +description: |- + Provides a list of Vpc Ipam Ipam Pool owned by an Alibaba Cloud account. +--- + +# alicloud_vpc_ipam_ipam_pools + +This data source provides Vpc Ipam Ipam Pool available to the user.[What is Ipam Pool](https://www.alibabacloud.com/help/en/) + +-> **NOTE:** Available since v1.241.0. + +## Example Usage + +```terraform +variable "name" { + default = "terraform-example" +} + +provider "alicloud" { + region = "cn-hangzhou" +} + +data "alicloud_resource_manager_resource_groups" "default" {} + +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 + pool_region_id = "cn-hangzhou" +} + +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" + ipam_pool_description = var.name +} + +data "alicloud_vpc_ipam_ipam_pools" "default" { + name_regex = alicloud_vpc_ipam_ipam_pool.default.name +} + +output "alicloud_vpc_ipam_ipam_pool_example_id" { + value = data.alicloud_vpc_ipam_ipam_pools.default.pools.0.id +} +``` + +## Argument Reference + +The following arguments are supported: +* `ipam_pool_id` - (ForceNew, Optional) The first ID of the resource. +* `ipam_pool_name` - (ForceNew, Optional) The name of the resource. +* `ipam_scope_id` - (ForceNew, Optional) Ipam scope id. +* `pool_region_id` - (ForceNew, Optional) The effective region of the IPAM address pool. +* `resource_group_id` - (ForceNew, Optional) The ID of the resource group. +* `source_ipam_pool_id` - (ForceNew, Optional) The instance ID of the source IPAM address pool.> If this parameter is not entered, the created address pool is the parent address pool. +* `tags` - (ForceNew, Optional) The tag of the resource. +* `ids` - (Optional, ForceNew, Computed) A list of Ipam Pool IDs. +* `name_regex` - (Optional, ForceNew) A regex string to filter results by Group Metric Rule name. +* `output_file` - (Optional, ForceNew) File name where to save data source results (after running `terraform plan`). + + +## Attributes Reference + +The following attributes are exported in addition to the arguments listed above: +* `ids` - A list of Ipam Pool IDs. +* `names` - A list of name of Ipam Pools. +* `pools` - A list of Ipam Pool Entries. Each element contains the following attributes: + * `allocation_default_cidr_mask` - The default network mask assigned by the IPAM address pool.IPv4 network mask value range: **0 to 32** bits. + * `allocation_max_cidr_mask` - The maximum network mask assigned by the IPAM address pool.IPv4 network mask value range: **0 to 32** bits. + * `allocation_min_cidr_mask` - The minimum Network mask assigned by the IPAM address pool.IPv4 network mask value range: **0 to 32** bits. + * `auto_import` - Whether the automatic import function is enabled for the address pool. + * `create_time` - The creation time of the resource. + * `has_sub_pool` - Whether it is a child address pool. Value:-**true**: Yes.-**false**: No. + * `ip_version` - The IP protocol version. Currently, only **IPv4** is supported * *. + * `ipam_id` - Ipam id. + * `ipam_pool_description` - 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_id` - The first ID of the resource. + * `ipam_pool_name` - The name of the resource. + * `ipam_scope_id` - Ipam scope id. + * `pool_depth` - The depth of the IPAM address pool. Value range: **0 to 10 * *. + * `pool_region_id` - The effective region of the IPAM address pool. + * `resource_group_id` - The ID of the resource group. + * `source_ipam_pool_id` - The instance ID of the source IPAM address pool.> If this parameter is not entered, the created address pool is the parent address pool. + * `status` - The status of the resource. + * `tags` - The tag of the resource. + * `id` - The ID of the resource supplied above. + * `region_id` - The region ID of the resource. diff --git a/website/docs/d/vpc_ipam_ipams.html.markdown b/website/docs/d/vpc_ipam_ipams.html.markdown new file mode 100644 index 000000000000..165316c1c80c --- /dev/null +++ b/website/docs/d/vpc_ipam_ipams.html.markdown @@ -0,0 +1,79 @@ +--- +subcategory: "Vpc Ipam" +layout: "alicloud" +page_title: "Alicloud: alicloud_vpc_ipam_ipams" +sidebar_current: "docs-alicloud-datasource-vpc-ipam-ipams" +description: |- + Provides a list of Vpc Ipam Ipam owned by an Alibaba Cloud account. +--- + +# alicloud_vpc_ipam_ipams + +This data source provides Vpc Ipam Ipam available to the user.[What is Ipam](https://www.alibabacloud.com/help/en/) + +-> **NOTE:** Available since v1.241.0. + +## Example Usage + +```terraform +variable "name" { + default = "terraform-example" +} + +provider "alicloud" { + region = "cn-hangzhou" +} + +data "alicloud_resource_manager_resource_groups" "default" {} + +resource "alicloud_vpc_ipam_ipam" "default" { + ipam_description = "This is my first Ipam." + ipam_name = var.name + resource_group_id = data.alicloud_resource_manager_resource_groups.default.ids.0 + tags = { + "k1" : "v1" + } + operating_region_list = ["cn-hangzhou"] +} + +data "alicloud_vpc_ipam_ipams" "default" { + name_regex = alicloud_vpc_ipam_ipam.default.name +} + +output "alicloud_vpc_ipam_ipam_example_id" { + value = data.alicloud_vpc_ipam_ipams.default.ipams.0.id +} +``` + +## Argument Reference + +The following arguments are supported: +* `ipam_id` - (ForceNew, Optional) The first ID of the resource. +* `ipam_name` - (ForceNew, Optional) The name of the resource. +* `resource_group_id` - (ForceNew, Optional) The ID of the resource group. +* `tags` - (ForceNew, Optional) The tag of the resource. +* `ids` - (Optional, ForceNew, Computed) A list of Ipam IDs. +* `name_regex` - (Optional, ForceNew) A regex string to filter results by Group Metric Rule name. +* `output_file` - (Optional, ForceNew) File name where to save data source results (after running `terraform plan`). + + +## Attributes Reference + +The following attributes are exported in addition to the arguments listed above: +* `ids` - A list of Ipam IDs. +* `names` - A list of name of Ipams. +* `ipams` - A list of Ipam Entries. Each element contains the following attributes: + * `create_time` - The creation time of the resource. + * `default_resource_discovery_association_id` - After an IPAM is created, the association between the resource discovery created by the system by default and the IPAM. + * `default_resource_discovery_id` - After IPAM is created, the system creates resource discovery by default. + * `ipam_description` - 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_id` - The first ID of the resource. + * `ipam_name` - The name 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. + * `public_default_scope_id` - After an IPAM is created, the public network IPAM is created by default. + * `region_id` - The region ID of the resource. + * `resource_discovery_association_count` - The number of resource discovery objects associated with IPAM. + * `resource_group_id` - The ID of the resource group. + * `status` - The status of the resource. + * `tags` - The tag of the resource. + * `id` - The ID of the resource supplied above.