Skip to content

feat(VPC): add params to vpc subnet resource #6741

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions docs/resources/vpc_subnet.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ resource "huaweicloud_vpc_subnet" "subnet_with_dhcp" {
vpc_id = huaweicloud_vpc.vpc.id
availability_zone = var.availability_zone

dhcp_lease_time = "24h"
ntp_server_address = "10.100.0.33,10.100.0.34"
dhcp_lease_time = "24h"
dhcp_ipv6_lease_time = "4h"
ntp_server_address = "10.100.0.33,10.100.0.34"
dhcp_domain_name = "test.domainnanme"
}

```
Expand Down Expand Up @@ -107,6 +109,15 @@ The following arguments are supported:
unlimited lease time, or Number+h. the number ranges from 1 to 30,000. For example, the value can be 5h. The default
value is 24h.

* `dhcp_ipv6_lease_time` - (Optional, String) Specifies the DHCP lease expiration time of the IPv6 subnet. The value can
be -1, which indicates unlimited lease time, or Number+h. the number ranges from 1 to 175200. For example, the value
can be 5h. The default value is 2h.

* `dhcp_domain_name` - (Optional, String) Specifies the domain name configured for DNS and is used to obtain the IP address
from the DNS server. A domain name can contain only letters, digits, and hyphens (-) and cannot start or end with a
hyphen (-). Each domain name contains at least two labels separated by periods (.). Max total: 254 characters. Max
label: 63 characters.

* `tags` - (Optional, Map) The key/value pairs to associate with the subnet.

## Attribute Reference
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ func TestAccVpcSubnetV1_dhcp(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "dhcp_lease_time", "48h"),
resource.TestCheckResourceAttr(resourceName, "ntp_server_address", "10.100.0.33"),
resource.TestCheckResourceAttr(resourceName, "dhcp_ipv6_lease_time", "4h"),
resource.TestCheckResourceAttr(resourceName, "dhcp_domain_name", "test.domainname"),
),
},
{
Expand All @@ -139,6 +141,8 @@ func TestAccVpcSubnetV1_dhcp(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "dhcp_lease_time", "72h"),
resource.TestCheckResourceAttr(resourceName, "ntp_server_address", "10.100.0.33,10.100.0.34"),
resource.TestCheckResourceAttr(resourceName, "dhcp_ipv6_lease_time", "8h"),
resource.TestCheckResourceAttr(resourceName, "dhcp_domain_name", "test.domainname.update"),
),
},
},
Expand Down Expand Up @@ -278,10 +282,13 @@ resource "huaweicloud_vpc_subnet" "test" {
cidr = "192.168.0.0/24"
gateway_ip = "192.168.0.1"
vpc_id = huaweicloud_vpc.test.id
ipv6_enable = true
availability_zone = data.huaweicloud_availability_zones.test.names[0]

dhcp_lease_time = "48h"
ntp_server_address = "10.100.0.33"
dhcp_lease_time = "48h"
ntp_server_address = "10.100.0.33"
dhcp_ipv6_lease_time = "4h"
dhcp_domain_name = "test.domainname"
}
`, testAccVpcSubnet_base(rName), rName)
}
Expand All @@ -295,10 +302,13 @@ resource "huaweicloud_vpc_subnet" "test" {
cidr = "192.168.0.0/24"
gateway_ip = "192.168.0.1"
vpc_id = huaweicloud_vpc.test.id
ipv6_enable = true
availability_zone = data.huaweicloud_availability_zones.test.names[0]

dhcp_lease_time = "72h"
ntp_server_address = "10.100.0.33,10.100.0.34"
dhcp_lease_time = "72h"
ntp_server_address = "10.100.0.33,10.100.0.34"
dhcp_ipv6_lease_time = "8h"
dhcp_domain_name = "test.domainname.update"
}
`, testAccVpcSubnet_base(rName), rName)
}
43 changes: 40 additions & 3 deletions huaweicloud/services/vpc/resource_huaweicloud_vpc_subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,15 @@ func ResourceVpcSubnetV1() *schema.Resource {
Optional: true,
Computed: true,
},
"dhcp_ipv6_lease_time": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"dhcp_domain_name": {
Type: schema.TypeString,
Optional: true,
},
"subnet_id": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -231,6 +240,15 @@ func buildDhcpOpts(d *schema.ResourceData, update bool) []subnets.ExtraDhcpOpt {
result = append(result, addressTime)
}

if v, ok := d.GetOk("dhcp_ipv6_lease_time"); ok {
ipv6AddressVal := v.(string)
ipv6AddressTime := subnets.ExtraDhcpOpt{
OptName: "ipv6_addresstime",
OptValue: &ipv6AddressVal,
}
result = append(result, ipv6AddressTime)
}

if v, ok := d.GetOk("ntp_server_address"); ok {
ntpVal := v.(string)
ntp := subnets.ExtraDhcpOpt{
Expand All @@ -245,6 +263,20 @@ func buildDhcpOpts(d *schema.ResourceData, update bool) []subnets.ExtraDhcpOpt {
result = append(result, ntp)
}

if v, ok := d.GetOk("dhcp_domain_name"); ok {
domainNameVal := v.(string)
domainName := subnets.ExtraDhcpOpt{
OptName: "domainname",
OptValue: &domainNameVal,
}
result = append(result, domainName)
} else if update {
domainName := subnets.ExtraDhcpOpt{
OptName: "domainname",
}
result = append(result, domainName)
}

return result
}

Expand Down Expand Up @@ -367,10 +399,15 @@ func resourceVpcSubnetRead(_ context.Context, d *schema.ResourceData, meta inter

// set dhcp extra opts ntp and addresstime
for _, val := range n.ExtraDhcpOpts {
if val.OptName == "ntp" {
switch val.OptName {
case "ntp":
mErr = multierror.Append(mErr, d.Set("ntp_server_address", val.OptValue))
} else if val.OptName == "addresstime" {
case "addresstime":
mErr = multierror.Append(mErr, d.Set("dhcp_lease_time", val.OptValue))
case "ipv6_addresstime":
mErr = multierror.Append(mErr, d.Set("dhcp_ipv6_lease_time", val.OptValue))
case "domainname":
mErr = multierror.Append(mErr, d.Set("dhcp_domain_name", val.OptValue))
}
}

Expand Down Expand Up @@ -419,7 +456,7 @@ func resourceVpcSubnetUpdate(ctx context.Context, d *schema.ResourceData, meta i
dnsList := utils.ExpandToStringList(d.Get("dns_list").([]interface{}))
updateOpts.DnsList = &dnsList
}
if d.HasChanges("dhcp_lease_time", "ntp_server_address") {
if d.HasChanges("dhcp_lease_time", "ntp_server_address", "dhcp_ipv6_lease_time", "dhcp_domain_name") {
updateOpts.ExtraDhcpOpts = buildDhcpOpts(d, true)
}

Expand Down
Loading