Skip to content
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

Improve vcd_nsxt_network_dhcp to support Isolated network, modes, and lease time #929

Merged
merged 25 commits into from
Nov 15, 2022
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
2 changes: 2 additions & 0 deletions .changes/v3.8.0/929-improvements.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* Resource and data source `vcd_nsxt_network_dhcp` support Isolated networks, different DHCP modes
lvirbalas marked this conversation as resolved.
Show resolved Hide resolved
('EDGE', 'NETWORK', 'DHCP') and lease time [GH-929]
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/hashicorp/go-version v1.6.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.24.1
github.com/kr/pretty v0.2.1
github.com/vmware/go-vcloud-director/v2 v2.17.0-alpha.8
github.com/vmware/go-vcloud-director/v2 v2.17.0-alpha.9
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ github.com/vmihailenco/msgpack/v4 v4.3.12 h1:07s4sz9IReOgdikxLTKNbBdqDMLsjPKXwvC
github.com/vmihailenco/msgpack/v4 v4.3.12/go.mod h1:gborTTJjAo/GWTqqRjrLCn9pgNN+NXzzngzBKDPIqw4=
github.com/vmihailenco/tagparser v0.1.1 h1:quXMXlA39OCbd2wAdTsGDlK9RkOk6Wuw+x37wVyIuWY=
github.com/vmihailenco/tagparser v0.1.1/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI=
github.com/vmware/go-vcloud-director/v2 v2.17.0-alpha.8 h1:qUyPHup11h2RQad852UwAyvCZhLm6obdLLmIT33acgA=
github.com/vmware/go-vcloud-director/v2 v2.17.0-alpha.8/go.mod h1:KjnB8t5l1bRrc+jLKDJbx0vZLRzz2RPzNQ7xzg7yI3o=
github.com/vmware/go-vcloud-director/v2 v2.17.0-alpha.9 h1:dBf7JY4UYQlsnvgl1mVWimirdDyLdWNHGRZvbHcnFFU=
github.com/vmware/go-vcloud-director/v2 v2.17.0-alpha.9/go.mod h1:KjnB8t5l1bRrc+jLKDJbx0vZLRzz2RPzNQ7xzg7yI3o=
github.com/xanzy/ssh-agent v0.3.0 h1:wUMzuKtKilRgBAD1sUb8gOwwRr2FGoBVumcjoOACClI=
github.com/xanzy/ssh-agent v0.3.0/go.mod h1:3s9xbODqPuuhK9JV1R321M/FlMZSBvE5aY6eAcqrDh0=
github.com/zclconf/go-cty v1.1.0/go.mod h1:xnAOWiHeOqg2nWS62VtQ7pbOu17FtxJNW8RLEih+O3s=
Expand Down
1 change: 1 addition & 0 deletions scripts/skip-upgrade-tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,4 @@ vcd.ResourceSchema-vcd_vapp.tf v3.7.0 "Deprecated 'metadata' attribute is now Co
vcd.ResourceSchema-vcd_vapp_vm.tf v3.7.0 "Deprecated 'metadata' attribute is now Computed (PR 917)"
vcd.ResourceSchema-vcd_vm.tf v3.7.0 "Deprecated 'metadata' attribute is now Computed (PR 917)"
vcd.TestAccVcdStandaloneVmTemplate.tf v3.7.0 "Added 'status', 'status_text' and 'metadata_entry' computed attributes (PR 901)"
vcd.ResourceSchema-vcd_nsxt_network_dhcp.tf v3.7.0 "Added 'mode', 'lease_time' and 'listener_ip_address' computed attributes (PR 929)"
2 changes: 1 addition & 1 deletion vcd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1107,7 +1107,7 @@ func importStateIdOrgNsxtVdcObject(objectName string) resource.ImportStateIdFunc
// Group (such as Vapp, networks, edge gateway) in NSX-T VDC
func importStateIdOrgNsxtVdcGroupObject(vdcGroupName, objectName string) resource.ImportStateIdFunc {
return func(*terraform.State) (string, error) {
if testConfig.VCD.Org == "" || testConfig.Nsxt.Vdc == "" || objectName == "" {
if testConfig.VCD.Org == "" || objectName == "" {
return "", fmt.Errorf("missing information to generate import path")
}
return testConfig.VCD.Org +
Expand Down
15 changes: 15 additions & 0 deletions vcd/datasource_vcd_nsxt_network_dhcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ func datasourceVcdOpenApiDhcp() *schema.Resource {
ForceNew: true,
Description: "Parent Org VDC network name",
},
"mode": {
Type: schema.TypeString,
Computed: true,
Description: "DHCP mode. One of 'EDGE', 'NETWORK', 'RELAY'",
},
"pool": {
Type: schema.TypeSet,
Computed: true,
Expand All @@ -61,6 +66,16 @@ func datasourceVcdOpenApiDhcp() *schema.Resource {
Type: schema.TypeString,
},
},
"lease_time": {
Type: schema.TypeInt,
Computed: true,
Description: "Lease time in seconds",
},
"listener_ip_address": {
Type: schema.TypeString,
Computed: true,
Description: "IP Address of DHCP server in network",
},
},
}
}
Expand Down
87 changes: 64 additions & 23 deletions vcd/resource_vcd_nsxt_network_dhcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

var nsxtDhcpPoolSetSchema = &schema.Resource{
Expand Down Expand Up @@ -58,9 +59,19 @@ func resourceVcdOpenApiDhcp() *schema.Resource {
ForceNew: true,
Description: "Parent Org VDC network ID",
},
"mode": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "EDGE",
ValidateFunc: validation.StringInSlice([]string{"EDGE", "NETWORK", "RELAY"}, false),
Description: "DHCP mode. One of 'EDGE' (default), 'NETWORK', 'RELAY'",
},
"pool": {
Type: schema.TypeSet,
Required: true,
Type: schema.TypeSet,
// Pool specification is optional, because mode=RELAY requires to have no pool
// configuration
Optional: true,
Description: "IP ranges used for DHCP pool allocation in the network",
Elem: nsxtDhcpPoolSetSchema,
},
Expand All @@ -73,6 +84,21 @@ func resourceVcdOpenApiDhcp() *schema.Resource {
Type: schema.TypeString,
},
},
"lease_time": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: "Lease time in seconds",
},
"listener_ip_address": {
Type: schema.TypeString,
// API still does not allow to change IP address in 10.4.0, but the error is human
// readable and it might allow changing in future. For this reason ForceNew remains
// commented.
// ForceNew: true,
Optional: true,
Description: "IP Address of DHCP server in network. Only applicable when mode=NETWORK",
},
},
}
}
Expand Down Expand Up @@ -206,31 +232,29 @@ func resourceVcdOpenApiDhcpImport(_ context.Context, d *schema.ResourceData, met
func getOpenAPIOrgVdcNetworkDhcpType(d *schema.ResourceData) *types.OpenApiOrgVdcNetworkDhcp {
orgVdcNetDhcp := &types.OpenApiOrgVdcNetworkDhcp{
DhcpPools: nil,
Mode: d.Get("mode").(string),
}

dhcpPool := d.Get("pool")
if dhcpPool == nil {
return orgVdcNetDhcp
}

dhcpPoolSet := dhcpPool.(*schema.Set)
dhcpPoolList := dhcpPoolSet.List()

if len(dhcpPoolList) > 0 {
dhcpPools := make([]types.OpenApiOrgVdcNetworkDhcpPools, len(dhcpPoolList))
for index, pool := range dhcpPoolList {
poolMap := pool.(map[string]interface{})
onePool := types.OpenApiOrgVdcNetworkDhcpPools{
IPRange: types.OpenApiOrgVdcNetworkDhcpIpRange{
StartAddress: poolMap["start_address"].(string),
EndAddress: poolMap["end_address"].(string),
},
if dhcpPool, dhcpPoolIsSet := d.GetOk("pool"); dhcpPoolIsSet {
dhcpPoolSet := dhcpPool.(*schema.Set)
dhcpPoolList := dhcpPoolSet.List()

if len(dhcpPoolList) > 0 {
dhcpPools := make([]types.OpenApiOrgVdcNetworkDhcpPools, len(dhcpPoolList))
for index, pool := range dhcpPoolList {
poolMap := pool.(map[string]interface{})
onePool := types.OpenApiOrgVdcNetworkDhcpPools{
IPRange: types.OpenApiOrgVdcNetworkDhcpIpRange{
StartAddress: poolMap["start_address"].(string),
EndAddress: poolMap["end_address"].(string),
},
}
dhcpPools[index] = onePool
}
dhcpPools[index] = onePool
}

// Inject data into main structure
orgVdcNetDhcp.DhcpPools = dhcpPools
// Inject data into main structure
orgVdcNetDhcp.DhcpPools = dhcpPools
}
}

dnsServers, ok := d.GetOk("dns_servers")
Expand All @@ -242,6 +266,15 @@ func getOpenAPIOrgVdcNetworkDhcpType(d *schema.ResourceData) *types.OpenApiOrgVd
orgVdcNetDhcp.DnsServers = dnsServerSet
}

if leaseTime, isLeaseTimeSet := d.GetOk("lease_time"); isLeaseTimeSet {
leaseTimeInt := leaseTime.(int)
orgVdcNetDhcp.LeaseTime = &leaseTimeInt
}

if ipAddress, ipAddressIsSet := d.GetOk("listener_ip_address"); ipAddressIsSet {
orgVdcNetDhcp.IPAddress = ipAddress.(string)
}

return orgVdcNetDhcp
}

Expand Down Expand Up @@ -272,5 +305,13 @@ func setOpenAPIOrgVdcNetworkDhcpData(orgNetworkId string, orgVdcNetwork *types.O
}
}

dSet(d, "mode", orgVdcNetwork.Mode)
if orgVdcNetwork.LeaseTime != nil {
dSet(d, "lease_time", *orgVdcNetwork.LeaseTime)
}
if orgVdcNetwork.IPAddress != "" {
dSet(d, "listener_ip_address", orgVdcNetwork.IPAddress)
}

return nil
}
Loading