Skip to content

Commit

Permalink
Add VPC path importer func
Browse files Browse the repository at this point in the history
VPC resources use policy path to import resources into the TF state.
This importer func does some validation that the required attributes for
a policy resource import do exist.

Signed-off-by: Kobi Samoray <[email protected]>
  • Loading branch information
ksamoray committed Jun 20, 2024
1 parent 2e0a840 commit 83affd7
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
14 changes: 14 additions & 0 deletions nsxt/policy_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,20 @@ func nsxtPolicyPathResourceImporter(d *schema.ResourceData, m interface{}) ([]*s
return rd, nil
}

func nsxtVPCPathResourceImporter(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
rd, err := nsxtPolicyPathResourceImporterHelper(d, m)
if errors.Is(err, ErrNotAPolicyPath) {
return rd, nil
} else if err != nil {
return rd, err
}
projectID, vpcID := getContextDataFromSchema(d)
if projectID == "" || vpcID == "" {
return rd, fmt.Errorf("imported resource policy path should have both project_id and vpc_id fields")
}
return rd, nil
}

func nsxtPolicyPathResourceImporterHelper(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
importID := d.Id()
if isPolicyPath(importID) {
Expand Down
2 changes: 1 addition & 1 deletion nsxt/resource_nsxt_vpc_gateway_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func resourceNsxtVPCGatewayPolicy() *schema.Resource {
Update: resourceNsxtVPCGatewayPolicyUpdate,
Delete: resourceNsxtVPCGatewayPolicyDelete,
Importer: &schema.ResourceImporter{
State: nsxtPolicyPathResourceImporter,
State: nsxtVPCPathResourceImporter,
},

Schema: getPolicyGatewayPolicySchema(false),
Expand Down
2 changes: 1 addition & 1 deletion nsxt/resource_nsxt_vpc_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func resourceNsxtVPCGroup() *schema.Resource {
Update: resourceNsxtVPCGroupUpdate,
Delete: resourceNsxtVPCGroupDelete,
Importer: &schema.ResourceImporter{
State: nsxtPolicyPathResourceImporter,
State: nsxtVPCPathResourceImporter,
},

Schema: getPolicyGroupSchema(false),
Expand Down
2 changes: 1 addition & 1 deletion nsxt/resource_nsxt_vpc_security_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func resourceNsxtVPCSecurityPolicy() *schema.Resource {
Update: resourceNsxtVPCSecurityPolicyUpdate,
Delete: resourceNsxtVPCSecurityPolicyDelete,
Importer: &schema.ResourceImporter{
State: nsxtPolicyPathResourceImporter,
State: nsxtVPCPathResourceImporter,
},
Schema: getPolicySecurityPolicySchema(false, true, true, false),
}
Expand Down

0 comments on commit 83affd7

Please sign in to comment.