Skip to content

Commit

Permalink
Adjust to DHCP changes in service and subnet
Browse files Browse the repository at this point in the history
Signed-off-by: Anna Khmelnitsky <[email protected]>
  • Loading branch information
annakhm committed Nov 22, 2024
1 parent 8aa682d commit b1171cb
Show file tree
Hide file tree
Showing 12 changed files with 466 additions and 445 deletions.
22 changes: 12 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ module github.com/vmware/terraform-provider-nsxt
go 1.19

replace (
github.com/vmware/vsphere-automation-sdk-go/lib => github.com/annakhm/vsphere-automation-sdk-go/lib v0.0.0-20241110024359-e42c5fcc5482
github.com/vmware/vsphere-automation-sdk-go/runtime => github.com/annakhm/vsphere-automation-sdk-go/runtime v0.0.0-20241110024359-e42c5fcc5482
github.com/vmware/vsphere-automation-sdk-go/services/nsxt => github.com/annakhm/vsphere-automation-sdk-go/services/nsxt v0.0.0-20241110024359-e42c5fcc5482
github.com/vmware/vsphere-automation-sdk-go/services/nsxt-gm => github.com/annakhm/vsphere-automation-sdk-go/services/nsxt-gm v0.0.0-20241110024359-e42c5fcc5482
github.com/vmware/vsphere-automation-sdk-go/services/nsxt-mp => github.com/annakhm/vsphere-automation-sdk-go/services/nsxt-mp v0.0.0-20241110024359-e42c5fcc5482
github.com/vmware/vsphere-automation-sdk-go/lib => github.com/vmware/vsphere-automation-sdk-go/lib v0.7.1-0.20241113023437-5938c535c194
github.com/vmware/vsphere-automation-sdk-go/runtime => github.com/vmware/vsphere-automation-sdk-go/runtime v0.7.1-0.20241113023437-5938c535c194
github.com/vmware/vsphere-automation-sdk-go/services/nsxt => github.com/vmware/vsphere-automation-sdk-go/services/nsxt v0.12.1-0.20241113023437-5938c535c194
github.com/vmware/vsphere-automation-sdk-go/services/nsxt-gm => github.com/vmware/vsphere-automation-sdk-go/services/nsxt-gm v0.9.1-0.20241113023437-5938c535c194
github.com/vmware/vsphere-automation-sdk-go/services/nsxt-mp => github.com/vmware/vsphere-automation-sdk-go/services/nsxt-mp v0.6.1-0.20241113023437-5938c535c194
)

require (
Expand All @@ -30,13 +30,14 @@ require (
github.com/antihax/optional v1.0.0 // indirect
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
github.com/beevik/etree v1.1.0 // indirect
github.com/client9/misspell v0.3.4 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/gibson042/canonicaljson-go v1.0.3 // indirect
github.com/golang-jwt/jwt/v4 v4.3.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-checkpoint v0.5.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
Expand All @@ -46,7 +47,7 @@ require (
github.com/hashicorp/go-plugin v1.5.1 // indirect
github.com/hashicorp/go-uuid v1.0.3 // indirect
github.com/hashicorp/hc-install v0.6.0 // indirect
github.com/hashicorp/hcl/v2 v2.18.0 // indirect
github.com/hashicorp/hcl/v2 v2.20.1 // indirect
github.com/hashicorp/logutils v1.0.0 // indirect
github.com/hashicorp/terraform-exec v0.19.0 // indirect
github.com/hashicorp/terraform-json v0.17.1 // indirect
Expand All @@ -55,21 +56,22 @@ require (
github.com/hashicorp/terraform-registry-address v0.2.2 // indirect
github.com/hashicorp/terraform-svchost v0.1.1 // indirect
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d // indirect
github.com/katbyte/terrafmt v0.5.4 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/oklog/run v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/zclconf/go-cty v1.14.0 // indirect
github.com/zclconf/go-cty v1.14.3 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/mod v0.16.0 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/oauth2 v0.7.0 // indirect
golang.org/x/sys v0.18.0 // indirect
Expand Down
73 changes: 63 additions & 10 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion nsxt/data_source_nsxt_vpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func testAccDataSourceNsxtVPCDeleteByName(name string) error {
}
for _, objInList := range objList.Results {
if *objInList.DisplayName == name {
err := client.Delete(defaultOrgID, projID, *objInList.Id)
err := client.Delete(defaultOrgID, projID, *objInList.Id, nil)
if err != nil {
return handleDeleteError("VPC", *objInList.Id, err)
}
Expand Down
2 changes: 1 addition & 1 deletion nsxt/resource_nsxt_vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func resourceNsxtVpcDelete(d *schema.ResourceData, m interface{}) error {
parents := getVpcParentsFromContext(getSessionContext(d, m))

client := clientLayer.NewVpcsClient(connector)
err := client.Delete(parents[0], parents[1], id)
err := client.Delete(parents[0], parents[1], id, nil)

if err != nil {
return handleDeleteError("Vpc", id, err)
Expand Down
6 changes: 3 additions & 3 deletions nsxt/resource_nsxt_vpc_dhcp_v4_static_binding_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,9 @@ resource "nsxt_vpc_subnet" "test" {
ip_addresses = ["192.168.240.0/26"]
access_mode = "Isolated"
dhcp_config {
enable_dhcp = true
static_pool_config {
ipv4_pool_size = 32
mode = "DHCP_SERVER"
dhcp_server_additional_config {
reserved_ip_ranges = ["192.168.240.40-192.168.240.60"]
}
}
}
Expand Down
166 changes: 90 additions & 76 deletions nsxt/resource_nsxt_vpc_service_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"reflect"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/vmware/vsphere-automation-sdk-go/runtime/protocol/client"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"
clientLayer "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/orgs/projects"
Expand Down Expand Up @@ -61,32 +60,13 @@ var vpcServiceProfileSchema = map[string]*metadata.ExtendedSchema{
Required: true,
Elem: &metadata.ExtendedResource{
Schema: map[string]*metadata.ExtendedSchema{
"ntp_servers": {
Schema: schema.Schema{
Type: schema.TypeList,
Elem: &metadata.ExtendedSchema{
Schema: schema.Schema{
Type: schema.TypeString,
ValidateFunc: validateSingleIPOrHostName(),
},
Metadata: metadata.Metadata{
SchemaType: "string",
},
},
Optional: true,
},
Metadata: metadata.Metadata{
SchemaType: "list",
SdkFieldName: "NtpServers",
},
},
"dns_client_config": {
"dhcp_relay_config": {
Schema: schema.Schema{
Type: schema.TypeList,
MaxItems: 1,
Elem: &metadata.ExtendedResource{
Schema: map[string]*metadata.ExtendedSchema{
"dns_server_ips": {
"server_addresses": {
Schema: schema.Schema{
Type: schema.TypeList,
Elem: &metadata.ExtendedSchema{
Expand All @@ -101,7 +81,7 @@ var vpcServiceProfileSchema = map[string]*metadata.ExtendedSchema{
},
Metadata: metadata.Metadata{
SchemaType: "list",
SdkFieldName: "DnsServerIps",
SdkFieldName: "ServerAddresses",
},
},
},
Expand All @@ -110,73 +90,68 @@ var vpcServiceProfileSchema = map[string]*metadata.ExtendedSchema{
},
Metadata: metadata.Metadata{
SchemaType: "struct",
SdkFieldName: "DnsClientConfig",
ReflectType: reflect.TypeOf(model.DnsClientConfig{}),
},
},
"lease_time": {
Schema: schema.Schema{
Type: schema.TypeInt,
Optional: true,
Default: 86400,
},
Metadata: metadata.Metadata{
SchemaType: "int",
SdkFieldName: "LeaseTime",
},
},
"mode": {
Schema: schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice(vpcServiceProfileModeValues, false),
Optional: true,
Default: model.VpcProfileDhcpConfig_MODE_SERVER,
},
Metadata: metadata.Metadata{
SchemaType: "string",
SdkFieldName: "Mode",
SdkFieldName: "DhcpRelayConfig",
ReflectType: reflect.TypeOf(model.VpcDhcpRelayConfig{}),
},
},
"advanced_config": {
"dhcp_server_config": {
Schema: schema.Schema{
Type: schema.TypeList,
MaxItems: 1,
Elem: &metadata.ExtendedResource{
Schema: map[string]*metadata.ExtendedSchema{
"is_distributed_dhcp": {
"dns_client_config": {
Schema: schema.Schema{
Type: schema.TypeBool,
Type: schema.TypeList,
MaxItems: 1,
Elem: &metadata.ExtendedResource{
Schema: map[string]*metadata.ExtendedSchema{
"dns_server_ips": {
Schema: schema.Schema{
Type: schema.TypeList,
Elem: &metadata.ExtendedSchema{
Schema: schema.Schema{
Type: schema.TypeString,
},
Metadata: metadata.Metadata{
SchemaType: "string",
},
},
Optional: true,
},
Metadata: metadata.Metadata{
SchemaType: "list",
SdkFieldName: "DnsServerIps",
},
},
},
},
Optional: true,
Default: true,
},
Metadata: metadata.Metadata{
SchemaType: "bool",
SdkFieldName: "IsDistributedDhcp",
SchemaType: "struct",
SdkFieldName: "DnsClientConfig",
ReflectType: reflect.TypeOf(model.DnsClientConfig{}),
},
},
},
},
Optional: true,
Computed: true,
},
Metadata: metadata.Metadata{
SchemaType: "struct",
SdkFieldName: "AdvancedConfig",
ReflectType: reflect.TypeOf(model.VpcDhcpAdvancedConfig{}),
},
},
"dhcp_relay_config": {
Schema: schema.Schema{
Type: schema.TypeList,
MaxItems: 1,
Elem: &metadata.ExtendedResource{
Schema: map[string]*metadata.ExtendedSchema{
"server_addresses": {
"lease_time": {
Schema: schema.Schema{
Type: schema.TypeInt,
Optional: true,
Default: 86400,
},
Metadata: metadata.Metadata{
SchemaType: "int",
SdkFieldName: "LeaseTime",
},
},
"ntp_servers": {
Schema: schema.Schema{
Type: schema.TypeList,
Elem: &metadata.ExtendedSchema{
Schema: schema.Schema{
Type: schema.TypeString,
Type: schema.TypeString,
ValidateFunc: validateSingleIPOrHostName(),
},
Metadata: metadata.Metadata{
SchemaType: "string",
Expand All @@ -186,7 +161,35 @@ var vpcServiceProfileSchema = map[string]*metadata.ExtendedSchema{
},
Metadata: metadata.Metadata{
SchemaType: "list",
SdkFieldName: "ServerAddresses",
SdkFieldName: "NtpServers",
},
},
"advanced_config": {
Schema: schema.Schema{
Type: schema.TypeList,
MaxItems: 1,
Elem: &metadata.ExtendedResource{
Schema: map[string]*metadata.ExtendedSchema{
"is_distributed_dhcp": {
Schema: schema.Schema{
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
Metadata: metadata.Metadata{
SchemaType: "bool",
SdkFieldName: "IsDistributedDhcp",
},
},
},
},
Optional: true,
Computed: true,
},
Metadata: metadata.Metadata{
SchemaType: "struct",
SdkFieldName: "AdvancedConfig",
ReflectType: reflect.TypeOf(model.VpcDhcpAdvancedConfig{}),
},
},
},
Expand All @@ -195,8 +198,8 @@ var vpcServiceProfileSchema = map[string]*metadata.ExtendedSchema{
},
Metadata: metadata.Metadata{
SchemaType: "struct",
SdkFieldName: "DhcpRelayConfig",
ReflectType: reflect.TypeOf(model.VpcDhcpRelayConfig{}),
SdkFieldName: "DhcpServerConfig",
ReflectType: reflect.TypeOf(model.VpcDhcpServerConfig{}),
},
},
},
Expand Down Expand Up @@ -299,6 +302,11 @@ func resourceNsxtVpcServiceProfileCreate(d *schema.ResourceData, m interface{})
return err
}

if obj.DhcpConfig == nil {
// NSX requires to send this struct even if empty
obj.DhcpConfig = &model.VpcProfileDhcpConfig{}
}

log.Printf("[INFO] Creating VpcServiceProfile with ID %s", id)

client := clientLayer.NewVpcServiceProfilesClient(connector)
Expand Down Expand Up @@ -368,6 +376,12 @@ func resourceNsxtVpcServiceProfileUpdate(d *schema.ResourceData, m interface{})
if err := metadata.SchemaToStruct(elem, d, vpcServiceProfileSchema, "", nil); err != nil {
return err
}

if obj.DhcpConfig == nil {
// NSX requires to send this struct even if empty
obj.DhcpConfig = &model.VpcProfileDhcpConfig{}
}

client := clientLayer.NewVpcServiceProfilesClient(connector)
_, err := client.Update(parents[0], parents[1], id, obj)
if err != nil {
Expand Down
Loading

0 comments on commit b1171cb

Please sign in to comment.