Skip to content

Commit

Permalink
VPC Groups implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Kobi Samoray <[email protected]>
  • Loading branch information
ksamoray committed Jun 18, 2024
1 parent d77898b commit eac55fc
Show file tree
Hide file tree
Showing 9 changed files with 469 additions and 61 deletions.
11 changes: 10 additions & 1 deletion api/api_list.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
# - client:
### API model path
# model:
### API type (Local/Global/Multitenancy)
### API type (Local/Global/Multitenancy/VPC)
# type:
### Attributes to be ignored while implementing a method
# ignore_params:
### List results Model path
# list_result_model:
### Name of model within model path package (should be same in all implementations)
Expand Down Expand Up @@ -242,6 +244,13 @@
- client: github.com/vmware/vsphere-automation-sdk-go/services/nsxt/orgs/projects/infra/domains
model: github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model
type: Multitenancy
- client: github.com/vmware/vsphere-automation-sdk-go/services/nsxt/orgs/projects/vpcs
model: github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model
type: VPC
ignore_params:
Delete:
- failIfSubtreeExistsParam
- forceParam
model_name: Group
obj_name: Group
supported_method:
Expand Down
27 changes: 27 additions & 0 deletions api/infra/domains/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
client0 "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra/domains"
model0 "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"
client2 "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/orgs/projects/infra/domains"
client3 "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/orgs/projects/vpcs"

utl "github.com/vmware/terraform-provider-nsxt/api/utl"
)
Expand All @@ -31,6 +32,9 @@ func NewGroupsClient(sessionContext utl.SessionContext, connector vapiProtocolCl
case utl.Multitenancy:
client = client2.NewGroupsClient(connector)

case utl.VPC:
client = client3.NewGroupsClient(connector)

default:
return nil
}
Expand Down Expand Up @@ -67,6 +71,13 @@ func (c GroupClientContext) Get(domainIdParam string, groupIdParam string) (mode
return obj, err
}

case utl.VPC:
client := c.Client.(client3.GroupsClient)
obj, err = client.Get(utl.DefaultOrgID, c.ProjectID, c.VPCID, groupIdParam)
if err != nil {
return obj, err
}

default:
return obj, errors.New("invalid infrastructure for model")
}
Expand Down Expand Up @@ -94,6 +105,10 @@ func (c GroupClientContext) Patch(domainIdParam string, groupIdParam string, gro
client := c.Client.(client2.GroupsClient)
err = client.Patch(utl.DefaultOrgID, c.ProjectID, domainIdParam, groupIdParam, groupParam)

case utl.VPC:
client := c.Client.(client3.GroupsClient)
err = client.Patch(utl.DefaultOrgID, c.ProjectID, c.VPCID, groupIdParam, groupParam)

default:
err = errors.New("invalid infrastructure for model")
}
Expand Down Expand Up @@ -130,6 +145,10 @@ func (c GroupClientContext) Update(domainIdParam string, groupIdParam string, gr
client := c.Client.(client2.GroupsClient)
obj, err = client.Update(utl.DefaultOrgID, c.ProjectID, domainIdParam, groupIdParam, groupParam)

case utl.VPC:
client := c.Client.(client3.GroupsClient)
obj, err = client.Update(utl.DefaultOrgID, c.ProjectID, c.VPCID, groupIdParam, groupParam)

default:
err = errors.New("invalid infrastructure for model")
}
Expand All @@ -153,6 +172,10 @@ func (c GroupClientContext) Delete(domainIdParam string, groupIdParam string, fa
client := c.Client.(client2.GroupsClient)
err = client.Delete(utl.DefaultOrgID, c.ProjectID, domainIdParam, groupIdParam, failIfSubtreeExistsParam, forceParam)

case utl.VPC:
client := c.Client.(client3.GroupsClient)
err = client.Delete(utl.DefaultOrgID, c.ProjectID, c.VPCID, groupIdParam)

default:
err = errors.New("invalid infrastructure for model")
}
Expand Down Expand Up @@ -185,6 +208,10 @@ func (c GroupClientContext) List(domainIdParam string, cursorParam *string, incl
client := c.Client.(client2.GroupsClient)
obj, err = client.List(utl.DefaultOrgID, c.ProjectID, domainIdParam, cursorParam, includeMarkForDeleteObjectsParam, includedFieldsParam, memberTypesParam, pageSizeParam, sortAscendingParam, sortByParam)

case utl.VPC:
client := c.Client.(client3.GroupsClient)
obj, err = client.List(utl.DefaultOrgID, c.ProjectID, c.VPCID, cursorParam, includeMarkForDeleteObjectsParam, includedFieldsParam, memberTypesParam, pageSizeParam, sortAscendingParam, sortByParam)

default:
err = errors.New("invalid infrastructure for model")
}
Expand Down
1 change: 1 addition & 0 deletions nsxt/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,7 @@ func Provider() *schema.Provider {
"nsxt_policy_compute_sub_cluster": resourceNsxtPolicyComputeSubCluster(),
"nsxt_policy_tier0_inter_vrf_routing": resourceNsxtPolicyTier0InterVRFRouting(),
"nsxt_vpc_security_policy": resourceNsxtVPCSecurityPolicy(),
"nsxt_vpc_group": resourceNsxtVPCGroup(),
},

ConfigureFunc: providerConfigure,
Expand Down
125 changes: 83 additions & 42 deletions nsxt/resource_nsxt_policy_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,42 +94,50 @@ func resourceNsxtPolicyGroup() *schema.Resource {
State: nsxtDomainResourceImporter,
},

Schema: map[string]*schema.Schema{
"nsx_id": getNsxIDSchema(),
"path": getPathSchema(),
"display_name": getDisplayNameSchema(),
"description": getDescriptionSchema(),
"revision": getRevisionSchema(),
"tag": getTagsSchema(),
"context": getContextSchema(false, false, false),
"domain": getDomainNameSchema(),
"group_type": {
Type: schema.TypeString,
Description: "Indicates the group type",
ValidateFunc: validation.StringInSlice(groupTypeValues, false),
Optional: true,
},
"criteria": {
Type: schema.TypeList,
Description: "Criteria to determine Group membership",
Elem: getCriteriaSetSchema(),
Optional: true,
},
"conjunction": {
Type: schema.TypeList,
Description: "A conjunction applied to 2 sets of criteria.",
Elem: getConjunctionSchema(),
Optional: true,
},
"extended_criteria": {
Type: schema.TypeList,
Description: "Extended criteria to determine group membership. extended_criteria is implicitly \"AND\" with criteria",
Elem: getExtendedCriteriaSetSchema(),
Optional: true,
MaxItems: 1,
},
Schema: getPolicyGroupSchema(true),
}
}

func getPolicyGroupSchema(withDomain bool) map[string]*schema.Schema {
s := map[string]*schema.Schema{
"nsx_id": getNsxIDSchema(),
"path": getPathSchema(),
"display_name": getDisplayNameSchema(),
"description": getDescriptionSchema(),
"revision": getRevisionSchema(),
"tag": getTagsSchema(),
"context": getContextSchema(false, false, !withDomain),
"group_type": {
Type: schema.TypeString,
Description: "Indicates the group type",
ValidateFunc: validation.StringInSlice(groupTypeValues, false),
Optional: true,
},
"criteria": {
Type: schema.TypeList,
Description: "Criteria to determine Group membership",
Elem: getCriteriaSetSchema(),
Optional: true,
},
"conjunction": {
Type: schema.TypeList,
Description: "A conjunction applied to 2 sets of criteria.",
Elem: getConjunctionSchema(),
Optional: true,
},
"extended_criteria": {
Type: schema.TypeList,
Description: "Extended criteria to determine group membership. extended_criteria is implicitly \"AND\" with criteria",
Elem: getExtendedCriteriaSetSchema(),
Optional: true,
MaxItems: 1,
},
}

if withDomain {
s["domain"] = getDomainNameSchema()
}
return s
}

func getIPAddressExpressionSchema() *schema.Resource {
Expand Down Expand Up @@ -833,10 +841,18 @@ func validateGroupCriteriaAndConjunctions(criteriaSets []interface{}, conjunctio
}

func resourceNsxtPolicyGroupCreate(d *schema.ResourceData, m interface{}) error {
return resourceNsxtPolicyGroupGeneralCreate(d, m, true)
}

func resourceNsxtPolicyGroupGeneralCreate(d *schema.ResourceData, m interface{}, withDomain bool) error {
connector := getPolicyConnector(m)

domainName := ""
if withDomain {
domainName = d.Get("domain").(string)
}
// Initialize resource Id and verify this ID is not yet used
id, err := getOrGenerateID2(d, m, resourceNsxtPolicyGroupExistsInDomainPartial(d.Get("domain").(string)))
id, err := getOrGenerateID2(d, m, resourceNsxtPolicyGroupExistsInDomainPartial(domainName))
if err != nil {
return err
}
Expand Down Expand Up @@ -886,7 +902,7 @@ func resourceNsxtPolicyGroupCreate(d *schema.ResourceData, m interface{}) error
if client == nil {
return policyResourceNotSupportedError()
}
err = client.Patch(d.Get("domain").(string), id, obj)
err = client.Patch(domainName, id, obj)

// Create the resource using PATCH
log.Printf("[INFO] Creating Group with ID %s", id)
Expand All @@ -897,13 +913,20 @@ func resourceNsxtPolicyGroupCreate(d *schema.ResourceData, m interface{}) error
d.SetId(id)
d.Set("nsx_id", id)

return resourceNsxtPolicyGroupRead(d, m)
return resourceNsxtPolicyGroupGeneralRead(d, m, withDomain)
}

func resourceNsxtPolicyGroupRead(d *schema.ResourceData, m interface{}) error {
return resourceNsxtPolicyGroupGeneralRead(d, m, true)
}

func resourceNsxtPolicyGroupGeneralRead(d *schema.ResourceData, m interface{}, withDomain bool) error {
connector := getPolicyConnector(m)
id := d.Id()
domainName := d.Get("domain").(string)
domainName := ""
if withDomain {
domainName = d.Get("domain").(string)
}
if id == "" {
return fmt.Errorf("Error obtaining Group ID")
}
Expand All @@ -920,7 +943,9 @@ func resourceNsxtPolicyGroupRead(d *schema.ResourceData, m interface{}) error {
setPolicyTagsInSchema(d, obj.Tags)
d.Set("nsx_id", id)
d.Set("path", obj.Path)
d.Set("domain", getDomainFromResourcePath(*obj.Path))
if withDomain {
d.Set("domain", getDomainFromResourcePath(*obj.Path))
}
d.Set("revision", obj.Revision)
groupType := ""
if len(obj.GroupType) > 0 && util.NsxVersionHigherOrEqual("3.2.0") {
Expand Down Expand Up @@ -951,6 +976,10 @@ func resourceNsxtPolicyGroupRead(d *schema.ResourceData, m interface{}) error {
}

func resourceNsxtPolicyGroupUpdate(d *schema.ResourceData, m interface{}) error {
return resourceNsxtPolicyGroupGeneralUpdate(d, m, true)
}

func resourceNsxtPolicyGroupGeneralUpdate(d *schema.ResourceData, m interface{}, withDomain bool) error {
connector := getPolicyConnector(m)

id := d.Id()
Expand Down Expand Up @@ -1007,15 +1036,23 @@ func resourceNsxtPolicyGroupUpdate(d *schema.ResourceData, m interface{}) error
}

// Update the resource using PATCH
err = client.Patch(d.Get("domain").(string), id, obj)
domainName := ""
if withDomain {
domainName = d.Get("domain").(string)
}
err = client.Patch(domainName, id, obj)
if err != nil {
return handleUpdateError("Group", id, err)
}

return resourceNsxtPolicyGroupRead(d, m)
return resourceNsxtPolicyGroupGeneralRead(d, m, withDomain)
}

func resourceNsxtPolicyGroupDelete(d *schema.ResourceData, m interface{}) error {
return resourceNsxtPolicyGroupGeneralDelete(d, m, true)
}

func resourceNsxtPolicyGroupGeneralDelete(d *schema.ResourceData, m interface{}, withDomain bool) error {
id := d.Id()
if id == "" {
return fmt.Errorf("Error obtaining Group ID")
Expand All @@ -1030,7 +1067,11 @@ func resourceNsxtPolicyGroupDelete(d *schema.ResourceData, m interface{}) error
if client == nil {
return policyResourceNotSupportedError()
}
return client.Delete(d.Get("domain").(string), id, &failIfSubtreeExists, &forceDelete)
domainName := ""
if withDomain {
domainName = d.Get("domain").(string)
}
return client.Delete(domainName, id, &failIfSubtreeExists, &forceDelete)
}

err := doDelete()
Expand Down
Loading

0 comments on commit eac55fc

Please sign in to comment.