Skip to content

Commit

Permalink
Add validators to transport node resource
Browse files Browse the repository at this point in the history
When user provides empty strings or empty lists, NSX fails the
request without specifying what exactly is missing. It is preferrable
to fail those invalid configs on plan stage.

Signed-off-by: Anna Khmelnitsky <[email protected]>
  • Loading branch information
annakhm committed Feb 12, 2024
1 parent 8c0081c commit 52c7b50
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
15 changes: 9 additions & 6 deletions nsxt/resource_nsxt_edge_transport_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,9 +558,10 @@ func getTransportZoneEndpointSchema() *schema.Schema {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"transport_zone": {
Type: schema.TypeString,
Required: true,
Description: "Unique ID identifying the transport zone for this endpoint",
Type: schema.TypeString,
Required: true,
Description: "Unique ID identifying the transport zone for this endpoint",
ValidateFunc: validation.StringIsNotWhiteSpace,
},
"transport_zone_profiles": {
Type: schema.TypeList,
Expand All @@ -584,9 +585,10 @@ func getUplinksSchema() *schema.Schema {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"uplink_name": {
Type: schema.TypeString,
Required: true,
Description: "Uplink name from UplinkHostSwitch profile",
Type: schema.TypeString,
Required: true,
Description: "Uplink name from UplinkHostSwitch profile",
ValidateFunc: validation.StringIsNotWhiteSpace,
},
"vds_lag_name": {
Type: schema.TypeString,
Expand All @@ -611,6 +613,7 @@ func getHostSwitchProfileIDsSchema() *schema.Schema {
Elem: &schema.Schema{
Type: schema.TypeString,
},
ValidateFunc: validateNonEmptyList,
}
}

Expand Down
14 changes: 14 additions & 0 deletions nsxt/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,3 +560,17 @@ func validateLdapOrLdapsURL() schema.SchemaValidateFunc {
return
}
}

func validateNonEmptyList(i interface{}, k string) (warnings []string, errors []error) {
v, ok := i.([]interface{})
if !ok {
errors = append(errors, fmt.Errorf("expected type of %q to be List", k))
return warnings, errors
}

if len(v) == 0 {
errors = append(errors, fmt.Errorf("expected %q to be non-empty", k))
}

return warnings, errors
}

0 comments on commit 52c7b50

Please sign in to comment.