From 83affd7e8d75f9dffa40c6001962915f96815028 Mon Sep 17 00:00:00 2001 From: Kobi Samoray Date: Thu, 20 Jun 2024 14:46:38 +0300 Subject: [PATCH] Add VPC path importer func 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 --- nsxt/policy_utils.go | 14 ++++++++++++++ nsxt/resource_nsxt_vpc_gateway_policy.go | 2 +- nsxt/resource_nsxt_vpc_group.go | 2 +- nsxt/resource_nsxt_vpc_security_policy.go | 2 +- 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/nsxt/policy_utils.go b/nsxt/policy_utils.go index b09bfdb98..5b439d817 100644 --- a/nsxt/policy_utils.go +++ b/nsxt/policy_utils.go @@ -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) { diff --git a/nsxt/resource_nsxt_vpc_gateway_policy.go b/nsxt/resource_nsxt_vpc_gateway_policy.go index f39cb9b26..ef7bf8ddf 100644 --- a/nsxt/resource_nsxt_vpc_gateway_policy.go +++ b/nsxt/resource_nsxt_vpc_gateway_policy.go @@ -22,7 +22,7 @@ func resourceNsxtVPCGatewayPolicy() *schema.Resource { Update: resourceNsxtVPCGatewayPolicyUpdate, Delete: resourceNsxtVPCGatewayPolicyDelete, Importer: &schema.ResourceImporter{ - State: nsxtPolicyPathResourceImporter, + State: nsxtVPCPathResourceImporter, }, Schema: getPolicyGatewayPolicySchema(false), diff --git a/nsxt/resource_nsxt_vpc_group.go b/nsxt/resource_nsxt_vpc_group.go index 6bdff0ac1..fc786f6f2 100644 --- a/nsxt/resource_nsxt_vpc_group.go +++ b/nsxt/resource_nsxt_vpc_group.go @@ -12,7 +12,7 @@ func resourceNsxtVPCGroup() *schema.Resource { Update: resourceNsxtVPCGroupUpdate, Delete: resourceNsxtVPCGroupDelete, Importer: &schema.ResourceImporter{ - State: nsxtPolicyPathResourceImporter, + State: nsxtVPCPathResourceImporter, }, Schema: getPolicyGroupSchema(false), diff --git a/nsxt/resource_nsxt_vpc_security_policy.go b/nsxt/resource_nsxt_vpc_security_policy.go index 2f300174d..2b35934eb 100644 --- a/nsxt/resource_nsxt_vpc_security_policy.go +++ b/nsxt/resource_nsxt_vpc_security_policy.go @@ -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), }