Skip to content

Commit

Permalink
resource/alicloud_vpc_ipam_ipam_pool: add new attribute resource_grou…
Browse files Browse the repository at this point in the history
…p_id. (#8139)
  • Loading branch information
ChenHanZhang authored Jan 16, 2025
1 parent 455e67d commit 93d3c87
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 20 deletions.
71 changes: 62 additions & 9 deletions alicloud/resource_alicloud_vpc_ipam_ipam_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ func resourceAliCloudVpcIpamIpamPool() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"resource_group_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"source_ipam_pool_id": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -140,6 +145,14 @@ func resourceAliCloudVpcIpamIpamPoolCreate(d *schema.ResourceData, meta interfac
if v, ok := d.GetOkExists("auto_import"); ok {
request["AutoImport"] = 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)
wait := incrementalWait(3*time.Second, 5*time.Second)
Expand All @@ -162,7 +175,7 @@ func resourceAliCloudVpcIpamIpamPoolCreate(d *schema.ResourceData, meta interfac

d.SetId(fmt.Sprint(response["IpamPoolId"]))

return resourceAliCloudVpcIpamIpamPoolUpdate(d, meta)
return resourceAliCloudVpcIpamIpamPoolRead(d, meta)
}

func resourceAliCloudVpcIpamIpamPoolRead(d *schema.ResourceData, meta interface{}) error {
Expand Down Expand Up @@ -209,6 +222,9 @@ func resourceAliCloudVpcIpamIpamPoolRead(d *schema.ResourceData, meta interface{
if objectRaw["PoolRegionId"] != nil {
d.Set("pool_region_id", objectRaw["PoolRegionId"])
}
if objectRaw["ResourceGroupId"] != nil {
d.Set("resource_group_id", objectRaw["ResourceGroupId"])
}
if objectRaw["SourceIpamPoolId"] != nil {
d.Set("source_ipam_pool_id", objectRaw["SourceIpamPoolId"])
}
Expand All @@ -231,6 +247,8 @@ func resourceAliCloudVpcIpamIpamPoolUpdate(d *schema.ResourceData, meta interfac
var response map[string]interface{}
var query map[string]interface{}
update := false
d.Partial(true)

action := "UpdateIpamPool"
conn, err := client.NewVpcipamClient()
if err != nil {
Expand All @@ -241,39 +259,74 @@ func resourceAliCloudVpcIpamIpamPoolUpdate(d *schema.ResourceData, meta interfac
request["IpamPoolId"] = d.Id()
request["RegionId"] = client.RegionId
request["ClientToken"] = buildClientToken(action)
if !d.IsNewResource() && d.HasChange("ipam_pool_name") {
if d.HasChange("ipam_pool_name") {
update = true
request["IpamPoolName"] = d.Get("ipam_pool_name")
}

if !d.IsNewResource() && d.HasChange("ipam_pool_description") {
if d.HasChange("ipam_pool_description") {
update = true
request["IpamPoolDescription"] = d.Get("ipam_pool_description")
}

if !d.IsNewResource() && d.HasChange("allocation_default_cidr_mask") {
if 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") {
if 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") {
if 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") {
if 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)
}
}
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.HasChange("resource_group_id") {
update = true
}
request["NewResourceGroupId"] = d.Get("resource_group_id")
request["ResourceType"] = "IPAMPOOL"
if update {
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
Expand All @@ -297,10 +350,11 @@ func resourceAliCloudVpcIpamIpamPoolUpdate(d *schema.ResourceData, meta interfac

if d.HasChange("tags") {
vpcIpamServiceV2 := VpcIpamServiceV2{client}
if err := vpcIpamServiceV2.SetResourceTags(d, "IpamPool"); err != nil {
if err := vpcIpamServiceV2.SetResourceTags(d, "IPAMPOOL"); err != nil {
return WrapError(err)
}
}
d.Partial(false)
return resourceAliCloudVpcIpamIpamPoolRead(d, meta)
}

Expand All @@ -318,7 +372,6 @@ func resourceAliCloudVpcIpamIpamPoolDelete(d *schema.ResourceData, meta interfac
request = make(map[string]interface{})
request["IpamPoolId"] = d.Id()
request["RegionId"] = client.RegionId

request["ClientToken"] = buildClientToken(action)

runtime := util.RuntimeOptions{}
Expand Down
17 changes: 13 additions & 4 deletions alicloud/resource_alicloud_vpc_ipam_ipam_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func TestAccAliCloudVpcIpamIpamPool_basic8374(t *testing.T) {
"ipam_pool_name": name,
"source_ipam_pool_id": "${alicloud_vpc_ipam_ipam_pool.parentIpamPool.id}",
"ip_version": "IPv4",
"resource_group_id": "${data.alicloud_resource_manager_resource_groups.default.ids.0}",
}),
Check: resource.ComposeTestCheckFunc(
testAccCheck(map[string]string{
Expand All @@ -47,6 +48,7 @@ func TestAccAliCloudVpcIpamIpamPool_basic8374(t *testing.T) {
"ipam_pool_name": name,
"source_ipam_pool_id": CHECKSET,
"ip_version": "IPv4",
"resource_group_id": CHECKSET,
}),
),
},
Expand Down Expand Up @@ -114,14 +116,15 @@ variable "name" {
default = "%s"
}
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
ipam_pool_name = format("%%s1", var.name)
pool_region_id = alicloud_vpc_ipam_ipam.defaultIpam.region_id
pool_region_id = "cn-hangzhou"
}
Expand All @@ -138,8 +141,8 @@ func TestAccAliCloudVpcIpamIpamPool_basic8026(t *testing.T) {
}, "DescribeVpcIpamIpamPool")
rac := resourceAttrCheckInit(rc, ra)
testAccCheck := rac.resourceAttrMapUpdateSet()
rand := acctest.RandIntRange(10000, 99999)
name := fmt.Sprintf("tf-testacc%svpcipamipampool%d", defaultRegionToTest, rand)
rand := acctest.RandIntRange(1, 999)
name := fmt.Sprintf("tf_testacc%d", rand)
testAccConfig := resourceTestAccConfigFunc(resourceId, name, AlicloudVpcIpamIpamPoolBasicDependence8026)
resource.Test(t, resource.TestCase{
PreCheck: func() {
Expand All @@ -161,6 +164,7 @@ func TestAccAliCloudVpcIpamIpamPool_basic8026(t *testing.T) {
"allocation_max_cidr_mask": "24",
"pool_region_id": "cn-hangzhou",
"auto_import": "true",
"resource_group_id": "${data.alicloud_resource_manager_resource_groups.default.ids.0}",
}),
Check: resource.ComposeTestCheckFunc(
testAccCheck(map[string]string{
Expand All @@ -173,6 +177,7 @@ func TestAccAliCloudVpcIpamIpamPool_basic8026(t *testing.T) {
"allocation_max_cidr_mask": "24",
"pool_region_id": "cn-hangzhou",
"auto_import": "true",
"resource_group_id": CHECKSET,
}),
),
},
Expand All @@ -184,6 +189,7 @@ func TestAccAliCloudVpcIpamIpamPool_basic8026(t *testing.T) {
"allocation_min_cidr_mask": "12",
"allocation_max_cidr_mask": "26",
"auto_import": "false",
"resource_group_id": "${data.alicloud_resource_manager_resource_groups.default.ids.1}",
"clear_allocation_default_cidr_mask": "false",
}),
Check: resource.ComposeTestCheckFunc(
Expand All @@ -194,6 +200,7 @@ func TestAccAliCloudVpcIpamIpamPool_basic8026(t *testing.T) {
"allocation_min_cidr_mask": "12",
"allocation_max_cidr_mask": "26",
"auto_import": "false",
"resource_group_id": CHECKSET,
"clear_allocation_default_cidr_mask": "false",
}),
),
Expand Down Expand Up @@ -268,6 +275,8 @@ variable "name" {
default = "%s"
}
data "alicloud_resource_manager_resource_groups" "default" {}
resource "alicloud_vpc_ipam_ipam" "defaultIpam" {
operating_region_list = ["cn-hangzhou"]
ipam_name = var.name
Expand Down
2 changes: 1 addition & 1 deletion alicloud/service_alicloud_vpc_ipam_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,6 @@ func (s *VpcIpamServiceV2) DescribeVpcIpamIpamPool(id string) (object map[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 object, WrapError(err)
Expand All @@ -302,6 +301,7 @@ func (s *VpcIpamServiceV2) DescribeVpcIpamIpamPool(id string) (object map[string
query = make(map[string]interface{})
request["IpamPoolIds.1"] = id
request["RegionId"] = client.RegionId
action := "ListIpamPools"

runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
Expand Down
7 changes: 1 addition & 6 deletions website/docs/r/vpc_ipam_ipam_pool.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@ For information about Vpc Ipam Ipam Pool and how to use it, see [What is Ipam Po

Basic Usage

<div style="display: block;margin-bottom: 40px;"><div class="oics-button" style="float: right;position: absolute;margin-bottom: 10px;">
<a href="https://api.aliyun.com/terraform?resource=alicloud_vpc_ipam_ipam_pool&exampleId=2a160962-cd7a-9d64-32d2-d2a0f8a3cf67cf4b594f&activeTab=example&spm=docs.r.vpc_ipam_ipam_pool.0.2a160962cd&intl_lang=EN_US" target="_blank">
<img alt="Open in AliCloud" src="https://img.alicdn.com/imgextra/i1/O1CN01hjjqXv1uYUlY56FyX_!!6000000006049-55-tps-254-36.svg" style="max-height: 44px; max-width: 100%;">
</a>
</div></div>

```terraform
variable "name" {
default = "terraform-example"
Expand Down Expand Up @@ -78,6 +72,7 @@ The following arguments are supported:
* `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.
* `resource_group_id` - (Optional, Computed, Available since v1.242.0) The ID of the resource group.
* `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.
Expand Down

0 comments on commit 93d3c87

Please sign in to comment.