Skip to content

Commit

Permalink
Add VPC support in search helpers
Browse files Browse the repository at this point in the history
In addition, add an Importer resources with parent path. It will be used
in auto-generated resources.

Signed-off-by: Anna Khmelnitsky <[email protected]>
  • Loading branch information
annakhm committed Jul 2, 2024
1 parent af29c99 commit af82352
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
26 changes: 15 additions & 11 deletions nsxt/policy_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ func listPolicyResourcesByNameAndType(connector client.Connector, context utl.Se
return searchLMPolicyResources(connector, *buildPolicyResourcesQuery(&query, additionalQuery))
case utl.Global:
return searchGMPolicyResources(connector, *buildPolicyResourcesQuery(&query, additionalQuery))
case utl.Multitenancy:
return searchMultitenancyPolicyResources(connector, utl.DefaultOrgID, context.ProjectID, *buildPolicyResourcesQuery(&query, additionalQuery))
case utl.Multitenancy, utl.VPC:
return searchMultitenancyResources(connector, context, *buildPolicyResourcesQuery(&query, additionalQuery))
}

return nil, errors.New("invalid ClientType %d")
return nil, errors.New("invalid ClientType")
}

func listInventoryResourcesByNameAndType(connector client.Connector, context utl.SessionContext, displayName string, resourceType string, additionalQuery *string) ([]*data.StructValue, error) {
Expand Down Expand Up @@ -158,11 +158,11 @@ func listPolicyResourcesByID(connector client.Connector, context utl.SessionCont
return searchLMPolicyResources(connector, *buildPolicyResourcesQuery(&query, additionalQuery))
case utl.Global:
return searchGMPolicyResources(connector, *buildPolicyResourcesQuery(&query, additionalQuery))
case utl.Multitenancy:
return searchMultitenancyPolicyResources(connector, utl.DefaultOrgID, context.ProjectID, *buildPolicyResourcesQuery(&query, additionalQuery))
case utl.Multitenancy, utl.VPC:
return searchMultitenancyResources(connector, context, *buildPolicyResourcesQuery(&query, additionalQuery))
}

return nil, errors.New("invalid ClientType %d")
return nil, errors.New("invalid ClientType")
}

func listPolicyResourcesByNsxID(connector client.Connector, context utl.SessionContext, resourceID *string, additionalQuery *string) ([]*data.StructValue, error) {
Expand All @@ -172,10 +172,10 @@ func listPolicyResourcesByNsxID(connector client.Connector, context utl.SessionC
return searchLMPolicyResources(connector, *buildPolicyResourcesQuery(&query, additionalQuery))
case utl.Global:
return searchGMPolicyResources(connector, *buildPolicyResourcesQuery(&query, additionalQuery))
case utl.Multitenancy:
return searchMultitenancyPolicyResources(connector, utl.DefaultOrgID, context.ProjectID, *buildPolicyResourcesQuery(&query, additionalQuery))
case utl.Multitenancy, utl.VPC:
return searchMultitenancyResources(connector, context, *buildPolicyResourcesQuery(&query, additionalQuery))
}
return nil, errors.New("invalid ClientType %d")
return nil, errors.New("invalid ClientType")
}

func buildPolicyResourcesQuery(query *string, additionalQuery *string) *string {
Expand Down Expand Up @@ -241,7 +241,11 @@ func searchLMPolicyResources(connector client.Connector, query string) ([]*data.
return searchLM(connector, query)
}

func searchMultitenancyPolicyResources(connector client.Connector, org string, project string, query string) ([]*data.StructValue, error) {
query = query + fmt.Sprintf(" AND path:\\/orgs\\/%s\\/projects\\/%s*", org, project)
func searchMultitenancyResources(connector client.Connector, context utl.SessionContext, query string) ([]*data.StructValue, error) {
if len(context.VPCID) > 0 {
query = query + fmt.Sprintf(" AND path:\\/orgs\\/%s\\/projects\\/%s\\/vpcs\\/%s*", utl.DefaultOrgID, context.ProjectID, context.VPCID)
} else {
query = query + fmt.Sprintf(" AND path:\\/orgs\\/%s\\/projects\\/%s*", utl.DefaultOrgID, context.ProjectID)
}
return searchLM(connector, query)
}
15 changes: 15 additions & 0 deletions nsxt/policy_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,21 @@ func nsxtPolicyPathResourceImporterHelper(d *schema.ResourceData, m interface{})
return []*schema.ResourceData{d}, ErrNotAPolicyPath
}

//lint:ignore U1000 Ignore unused function temporarily until used in autogenerated resource
func nsxtParentPathResourceImporter(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
importID := d.Id()
if isPolicyPath(importID) {
pathSegs := strings.Split(importID, "/")
segCount := len(pathSegs)
d.SetId(pathSegs[segCount-1])
// to get parent path size, remove last two tokens (id and separator) plus two slashes
truncateSize := len(pathSegs[segCount-1]) + len(pathSegs[segCount-2]) + 2
d.Set("parent_path", importID[:len(importID)-truncateSize])
return []*schema.ResourceData{d}, nil
}
return []*schema.ResourceData{d}, ErrNotAPolicyPath
}

func isPolicyPath(policyPath string) bool {
pathSegs := strings.Split(policyPath, "/")
if len(pathSegs) < 4 {
Expand Down

0 comments on commit af82352

Please sign in to comment.