Skip to content

Commit

Permalink
Suggest path sample when parsing policy path fails
Browse files Browse the repository at this point in the history
Signed-off-by: Anna Khmelnitsky <[email protected]>
  • Loading branch information
annakhm committed Dec 31, 2024
1 parent f1fbea5 commit 09e4786
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 34 deletions.
2 changes: 1 addition & 1 deletion nsxt/data_source_nsxt_policy_transit_gateway_nat.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func dataSourceNsxtPolicyTransitGatewayNatRead(d *schema.ResourceData, m interfa
connector := getPolicyConnector(m)

parentPath := d.Get("transit_gateway_path").(string)
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 3)
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 3, "/orgs/[org]/projects/[project]/transit-gateways/[gateway]")
if pathErr != nil {
return fmt.Errorf("invalid transit_gateway_path: %v", pathErr)
}
Expand Down
2 changes: 1 addition & 1 deletion nsxt/data_source_nsxt_vpc_subnet_port.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func dataSourceNsxtVpcSubnetPort() *schema.Resource {
func listVpcSubnetPorts(connector client.Connector, subnetPath string) ([]model.VpcSubnetPort, error) {

var results []model.VpcSubnetPort
parents, pathErr := parseStandardPolicyPathVerifySize(subnetPath, 4)
parents, pathErr := parseStandardPolicyPathVerifySize(subnetPath, 4, "/orgs/[org]/projects/[project]/vpcs/[vpc]/subnets/[subnet]")
if pathErr != nil {
return results, pathErr
}
Expand Down
4 changes: 2 additions & 2 deletions nsxt/policy_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,13 +343,13 @@ func parseStandardPolicyPath(path string) ([]string, error) {
return parents, nil
}

func parseStandardPolicyPathVerifySize(path string, expectedSize int) ([]string, error) {
func parseStandardPolicyPathVerifySize(path string, expectedSize int, pathExample string) ([]string, error) {
parents, err := parseStandardPolicyPath(path)
if err != nil {
return parents, err
}
if len(parents) != expectedSize {
return parents, fmt.Errorf("Unexpected parent path %s (expected %d parent ids, got %d)", path, expectedSize, len(parents))
return parents, fmt.Errorf("Unexpected parent path %s (expected format %s)", path, pathExample)
}

return parents, nil
Expand Down
6 changes: 3 additions & 3 deletions nsxt/policy_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ func TestNegativeParseStandardPolicyPath(t *testing.T) {

func TestParseStandardPolicyPathVerifySize(t *testing.T) {

_, err := parseStandardPolicyPathVerifySize("/infra/things/thing1/sub-things/sub-thing1", 3)
_, err := parseStandardPolicyPathVerifySize("/infra/things/thing1/sub-things/sub-thing1", 3, "sample")
assert.NotNil(t, err)

parents, err := parseStandardPolicyPathVerifySize("/infra/things/thing1/sub-things/sub-thing1", 2)
parents, err := parseStandardPolicyPathVerifySize("/infra/things/thing1/sub-things/sub-thing1", 2, "sample")
assert.Nil(t, err)
assert.Equal(t, 2, len(parents))

_, err = parseStandardPolicyPathVerifySize("/global-infra/things/1/sub-things/2/fine-tuned-thing/3", 1)
_, err = parseStandardPolicyPathVerifySize("/global-infra/things/1/sub-things/2/fine-tuned-thing/3", 1, "sample")
assert.NotNil(t, err)
}
12 changes: 7 additions & 5 deletions nsxt/resource_nsxt_policy_transit_gateway_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"github.com/vmware/terraform-provider-nsxt/nsxt/metadata"
)

var transitGatewayPathSample = "/orgs/[org]/projects/[project]/transit-gateways/[gateway]"

var transitGatewayAttachmentSchema = map[string]*metadata.ExtendedSchema{
"nsx_id": metadata.GetExtendedSchema(getNsxIDSchema()),
"path": metadata.GetExtendedSchema(getPathSchema()),
Expand Down Expand Up @@ -54,7 +56,7 @@ func resourceNsxtPolicyTransitGatewayAttachment() *schema.Resource {

func resourceNsxtPolicyTransitGatewayAttachmentExists(sessionContext utl.SessionContext, parentPath string, id string, connector client.Connector) (bool, error) {
var err error
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 3)
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 3, transitGatewayPathSample)
if pathErr != nil {
return false, pathErr
}
Expand All @@ -80,7 +82,7 @@ func resourceNsxtPolicyTransitGatewayAttachmentCreate(d *schema.ResourceData, m
}

parentPath := d.Get("parent_path").(string)
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 3)
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 3, transitGatewayPathSample)
if pathErr != nil {
return pathErr
}
Expand Down Expand Up @@ -122,7 +124,7 @@ func resourceNsxtPolicyTransitGatewayAttachmentRead(d *schema.ResourceData, m in

client := clientLayer.NewAttachmentsClient(connector)
parentPath := d.Get("parent_path").(string)
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 3)
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 3, transitGatewayPathSample)
if pathErr != nil {
return pathErr
}
Expand Down Expand Up @@ -152,7 +154,7 @@ func resourceNsxtPolicyTransitGatewayAttachmentUpdate(d *schema.ResourceData, m
}

parentPath := d.Get("parent_path").(string)
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 3)
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 3, transitGatewayPathSample)
if pathErr != nil {
return pathErr
}
Expand Down Expand Up @@ -190,7 +192,7 @@ func resourceNsxtPolicyTransitGatewayAttachmentDelete(d *schema.ResourceData, m

connector := getPolicyConnector(m)
parentPath := d.Get("parent_path").(string)
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 3)
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 3, transitGatewayPathSample)
if pathErr != nil {
return pathErr
}
Expand Down
12 changes: 7 additions & 5 deletions nsxt/resource_nsxt_policy_transit_gateway_nat_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"github.com/vmware/terraform-provider-nsxt/nsxt/metadata"
)

var transitGatewayNatPathSample = "/orgs/[org]/projects/[project]/transit-gateways/[gateway]/nat/[type]"

func resourceNsxtPolicyTransitGatewayNatRule() *schema.Resource {
return &schema.Resource{
Create: resourceNsxtPolicyTransitGatewayNatRuleCreate,
Expand All @@ -34,7 +36,7 @@ func resourceNsxtPolicyTransitGatewayNatRule() *schema.Resource {

func resourceNsxtPolicyTransitGatewayNatRuleExists(sessionContext utl.SessionContext, parentPath string, id string, connector client.Connector) (bool, error) {
var err error
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 4)
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 4, transitGatewayNatPathSample)
if pathErr != nil {
return false, pathErr
}
Expand All @@ -60,7 +62,7 @@ func resourceNsxtPolicyTransitGatewayNatRuleCreate(d *schema.ResourceData, m int
}

parentPath := d.Get("parent_path").(string)
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 4)
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 4, transitGatewayNatPathSample)
if pathErr != nil {
return pathErr
}
Expand Down Expand Up @@ -102,7 +104,7 @@ func resourceNsxtPolicyTransitGatewayNatRuleRead(d *schema.ResourceData, m inter

client := clientLayer.NewNatRulesClient(connector)
parentPath := d.Get("parent_path").(string)
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 4)
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 4, transitGatewayNatPathSample)
if pathErr != nil {
return pathErr
}
Expand Down Expand Up @@ -132,7 +134,7 @@ func resourceNsxtPolicyTransitGatewayNatRuleUpdate(d *schema.ResourceData, m int
}

parentPath := d.Get("parent_path").(string)
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 4)
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 4, transitGatewayNatPathSample)
if pathErr != nil {
return pathErr
}
Expand Down Expand Up @@ -170,7 +172,7 @@ func resourceNsxtPolicyTransitGatewayNatRuleDelete(d *schema.ResourceData, m int

connector := getPolicyConnector(m)
parentPath := d.Get("parent_path").(string)
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 4)
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 4, transitGatewayNatPathSample)
if pathErr != nil {
return pathErr
}
Expand Down
12 changes: 7 additions & 5 deletions nsxt/resource_nsxt_vpc_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"github.com/vmware/terraform-provider-nsxt/nsxt/metadata"
)

var vpcPathSample = "/orgs/[org]/projects/[project]/vpcs/[vpc]"

var vpcAttachmentSchema = map[string]*metadata.ExtendedSchema{
"nsx_id": metadata.GetExtendedSchema(getNsxIDSchema()),
"path": metadata.GetExtendedSchema(getPathSchema()),
Expand Down Expand Up @@ -53,7 +55,7 @@ func resourceNsxtVpcAttachment() *schema.Resource {

func resourceNsxtVpcAttachmentExists(sessionContext utl.SessionContext, parentPath string, id string, connector client.Connector) (bool, error) {
var err error
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 3)
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 3, vpcPathSample)
if pathErr != nil {
return false, pathErr
}
Expand All @@ -79,7 +81,7 @@ func resourceNsxtVpcAttachmentCreate(d *schema.ResourceData, m interface{}) erro
}

parentPath := d.Get("parent_path").(string)
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 3)
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 3, vpcPathSample)
if pathErr != nil {
return pathErr
}
Expand Down Expand Up @@ -121,7 +123,7 @@ func resourceNsxtVpcAttachmentRead(d *schema.ResourceData, m interface{}) error

client := clientLayer.NewAttachmentsClient(connector)
parentPath := d.Get("parent_path").(string)
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 3)
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 3, vpcPathSample)
if pathErr != nil {
return pathErr
}
Expand Down Expand Up @@ -151,7 +153,7 @@ func resourceNsxtVpcAttachmentUpdate(d *schema.ResourceData, m interface{}) erro
}

parentPath := d.Get("parent_path").(string)
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 3)
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 3, vpcPathSample)
if pathErr != nil {
return pathErr
}
Expand Down Expand Up @@ -190,7 +192,7 @@ func resourceNsxtVpcAttachmentDelete(d *schema.ResourceData, m interface{}) erro
connector := getPolicyConnector(m)
parentPath := d.Get("parent_path").(string)

parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 3)
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 3, vpcPathSample)
if pathErr != nil {
return pathErr
}
Expand Down
12 changes: 7 additions & 5 deletions nsxt/resource_nsxt_vpc_dhcp_v4_static_binding_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"github.com/vmware/terraform-provider-nsxt/nsxt/metadata"
)

var vpcSubnetPathSample = "/orgs/[org]/projects/[project]/vpcs/[vpc]/subnets/[subnet]"

var dhcpV4StaticBindingConfigSchema = map[string]*metadata.ExtendedSchema{
"nsx_id": metadata.GetExtendedSchema(getNsxIDSchema()),
"path": metadata.GetExtendedSchema(getPathSchema()),
Expand Down Expand Up @@ -212,7 +214,7 @@ func resourceNsxtVpcSubnetDhcpV4StaticBindingConfig() *schema.Resource {

func resourceNsxtVpcSubnetDhcpV4StaticBindingConfigExists(sessionContext utl.SessionContext, parentPath string, id string, connector client.Connector) (bool, error) {
var err error
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 4)
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 4, vpcSubnetPathSample)
if pathErr != nil {
return false, pathErr
}
Expand All @@ -238,7 +240,7 @@ func resourceNsxtVpcSubnetDhcpV4StaticBindingConfigCreate(d *schema.ResourceData
}

parentPath := d.Get("parent_path").(string)
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 4)
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 4, vpcSubnetPathSample)
if pathErr != nil {
return pathErr
}
Expand Down Expand Up @@ -287,7 +289,7 @@ func resourceNsxtVpcSubnetDhcpV4StaticBindingConfigRead(d *schema.ResourceData,

client := clientLayer.NewDhcpStaticBindingConfigsClient(connector)
parentPath := d.Get("parent_path").(string)
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 4)
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 4, vpcSubnetPathSample)
if pathErr != nil {
return pathErr
}
Expand Down Expand Up @@ -327,7 +329,7 @@ func resourceNsxtVpcSubnetDhcpV4StaticBindingConfigUpdate(d *schema.ResourceData
}

parentPath := d.Get("parent_path").(string)
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 4)
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 4, vpcSubnetPathSample)
if pathErr != nil {
return pathErr
}
Expand Down Expand Up @@ -376,7 +378,7 @@ func resourceNsxtVpcSubnetDhcpV4StaticBindingConfigDelete(d *schema.ResourceData

connector := getPolicyConnector(m)
parentPath := d.Get("parent_path").(string)
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 4)
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 4, vpcSubnetPathSample)
if pathErr != nil {
return pathErr
}
Expand Down
6 changes: 4 additions & 2 deletions nsxt/resource_nsxt_vpc_external_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/orgs/projects/vpcs/subnets"
)

var vpcSubnetPortPathSample = "/orgs/[org]/projects/[project]/vpcs/[vpc]/subnets/[subnet]/ports/[port]"

func resourceNsxtVpcExternalAddress() *schema.Resource {
return &schema.Resource{
Create: resourceNsxtVpcExternalAddressCreate,
Expand All @@ -35,7 +37,7 @@ func updatePort(d *schema.ResourceData, m interface{}, deleteFlow bool) error {
addressPath := d.Get("allocated_external_ip_path").(string)
log.Printf("[DEBUG] Updating external address binding for port %s", portPath)

parents, pathErr := parseStandardPolicyPathVerifySize(portPath, 5)
parents, pathErr := parseStandardPolicyPathVerifySize(portPath, 5, vpcSubnetPortPathSample)
if pathErr != nil {
return pathErr
}
Expand Down Expand Up @@ -73,7 +75,7 @@ func resourceNsxtVpcExternalAddressCreate(d *schema.ResourceData, m interface{})
func resourceNsxtVpcExternalAddressRead(d *schema.ResourceData, m interface{}) error {
portPath := d.Get("parent_path").(string)

parents, pathErr := parseStandardPolicyPathVerifySize(portPath, 5)
parents, pathErr := parseStandardPolicyPathVerifySize(portPath, 5, vpcSubnetPortPathSample)
if pathErr != nil {
return pathErr
}
Expand Down
12 changes: 7 additions & 5 deletions nsxt/resource_nsxt_vpc_nat_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ var policyVpcNatRuleFirewallMatchValues = []string{
model.PolicyVpcNatRule_FIREWALL_MATCH_BYPASS,
}

var vpcNatPathSample = "/orgs/[org]/projects/[project]/vpcs/[vpc]/nat/[type]"

var policyVpcNatRuleSchema = map[string]*metadata.ExtendedSchema{
"nsx_id": metadata.GetExtendedSchema(getNsxIDSchema()),
"path": metadata.GetExtendedSchema(getPathSchema()),
Expand Down Expand Up @@ -144,7 +146,7 @@ func resourceNsxtPolicyVpcNatRule() *schema.Resource {

func resourceNsxtPolicyVpcNatRuleExists(sessionContext utl.SessionContext, parentPath string, id string, connector client.Connector) (bool, error) {
var err error
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 4)
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 4, vpcNatPathSample)
if pathErr != nil {
return false, pathErr
}
Expand All @@ -170,7 +172,7 @@ func resourceNsxtPolicyVpcNatRuleCreate(d *schema.ResourceData, m interface{}) e
}

parentPath := d.Get("parent_path").(string)
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 4)
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 4, vpcNatPathSample)
if pathErr != nil {
return pathErr
}
Expand Down Expand Up @@ -212,7 +214,7 @@ func resourceNsxtPolicyVpcNatRuleRead(d *schema.ResourceData, m interface{}) err

client := clientLayer.NewNatRulesClient(connector)
parentPath := d.Get("parent_path").(string)
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 4)
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 4, vpcNatPathSample)
if pathErr != nil {
return pathErr
}
Expand Down Expand Up @@ -242,7 +244,7 @@ func resourceNsxtPolicyVpcNatRuleUpdate(d *schema.ResourceData, m interface{}) e
}

parentPath := d.Get("parent_path").(string)
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 4)
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 4, vpcNatPathSample)
if pathErr != nil {
return pathErr
}
Expand Down Expand Up @@ -283,7 +285,7 @@ func resourceNsxtPolicyVpcNatRuleDelete(d *schema.ResourceData, m interface{}) e

connector := getPolicyConnector(m)
parentPath := d.Get("parent_path").(string)
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 4)
parents, pathErr := parseStandardPolicyPathVerifySize(parentPath, 4, vpcNatPathSample)
if pathErr != nil {
return pathErr
}
Expand Down

0 comments on commit 09e4786

Please sign in to comment.