Skip to content

Commit 4380076

Browse files
committed
VPC Groups implementation
Signed-off-by: Kobi Samoray <[email protected]>
1 parent 0e0bd77 commit 4380076

9 files changed

+469
-61
lines changed

api/api_list.yaml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
# - client:
66
### API model path
77
# model:
8-
### API type (Local/Global/Multitenancy)
8+
### API type (Local/Global/Multitenancy/VPC)
99
# type:
10+
### Attributes to be ignored while implementing a method
11+
# ignore_params:
1012
### List results Model path
1113
# list_result_model:
1214
### Name of model within model path package (should be same in all implementations)
@@ -242,6 +244,13 @@
242244
- client: github.com/vmware/vsphere-automation-sdk-go/services/nsxt/orgs/projects/infra/domains
243245
model: github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model
244246
type: Multitenancy
247+
- client: github.com/vmware/vsphere-automation-sdk-go/services/nsxt/orgs/projects/vpcs
248+
model: github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model
249+
type: VPC
250+
ignore_params:
251+
Delete:
252+
- failIfSubtreeExistsParam
253+
- forceParam
245254
model_name: Group
246255
obj_name: Group
247256
supported_method:

api/infra/domains/group.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
client0 "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra/domains"
1212
model0 "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"
1313
client2 "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/orgs/projects/infra/domains"
14+
client3 "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/orgs/projects/vpcs"
1415

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

35+
case utl.VPC:
36+
client = client3.NewGroupsClient(connector)
37+
3438
default:
3539
return nil
3640
}
@@ -67,6 +71,13 @@ func (c GroupClientContext) Get(domainIdParam string, groupIdParam string) (mode
6771
return obj, err
6872
}
6973

74+
case utl.VPC:
75+
client := c.Client.(client3.GroupsClient)
76+
obj, err = client.Get(utl.DefaultOrgID, c.ProjectID, c.VPCID, groupIdParam)
77+
if err != nil {
78+
return obj, err
79+
}
80+
7081
default:
7182
return obj, errors.New("invalid infrastructure for model")
7283
}
@@ -94,6 +105,10 @@ func (c GroupClientContext) Patch(domainIdParam string, groupIdParam string, gro
94105
client := c.Client.(client2.GroupsClient)
95106
err = client.Patch(utl.DefaultOrgID, c.ProjectID, domainIdParam, groupIdParam, groupParam)
96107

108+
case utl.VPC:
109+
client := c.Client.(client3.GroupsClient)
110+
err = client.Patch(utl.DefaultOrgID, c.ProjectID, c.VPCID, groupIdParam, groupParam)
111+
97112
default:
98113
err = errors.New("invalid infrastructure for model")
99114
}
@@ -130,6 +145,10 @@ func (c GroupClientContext) Update(domainIdParam string, groupIdParam string, gr
130145
client := c.Client.(client2.GroupsClient)
131146
obj, err = client.Update(utl.DefaultOrgID, c.ProjectID, domainIdParam, groupIdParam, groupParam)
132147

148+
case utl.VPC:
149+
client := c.Client.(client3.GroupsClient)
150+
obj, err = client.Update(utl.DefaultOrgID, c.ProjectID, c.VPCID, groupIdParam, groupParam)
151+
133152
default:
134153
err = errors.New("invalid infrastructure for model")
135154
}
@@ -153,6 +172,10 @@ func (c GroupClientContext) Delete(domainIdParam string, groupIdParam string, fa
153172
client := c.Client.(client2.GroupsClient)
154173
err = client.Delete(utl.DefaultOrgID, c.ProjectID, domainIdParam, groupIdParam, failIfSubtreeExistsParam, forceParam)
155174

175+
case utl.VPC:
176+
client := c.Client.(client3.GroupsClient)
177+
err = client.Delete(utl.DefaultOrgID, c.ProjectID, c.VPCID, groupIdParam)
178+
156179
default:
157180
err = errors.New("invalid infrastructure for model")
158181
}
@@ -185,6 +208,10 @@ func (c GroupClientContext) List(domainIdParam string, cursorParam *string, incl
185208
client := c.Client.(client2.GroupsClient)
186209
obj, err = client.List(utl.DefaultOrgID, c.ProjectID, domainIdParam, cursorParam, includeMarkForDeleteObjectsParam, includedFieldsParam, memberTypesParam, pageSizeParam, sortAscendingParam, sortByParam)
187210

211+
case utl.VPC:
212+
client := c.Client.(client3.GroupsClient)
213+
obj, err = client.List(utl.DefaultOrgID, c.ProjectID, c.VPCID, cursorParam, includeMarkForDeleteObjectsParam, includedFieldsParam, memberTypesParam, pageSizeParam, sortAscendingParam, sortByParam)
214+
188215
default:
189216
err = errors.New("invalid infrastructure for model")
190217
}

nsxt/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,7 @@ func Provider() *schema.Provider {
497497
"nsxt_policy_compute_sub_cluster": resourceNsxtPolicyComputeSubCluster(),
498498
"nsxt_policy_tier0_inter_vrf_routing": resourceNsxtPolicyTier0InterVRFRouting(),
499499
"nsxt_vpc_security_policy": resourceNsxtVPCSecurityPolicy(),
500+
"nsxt_vpc_group": resourceNsxtVPCGroup(),
500501
},
501502

502503
ConfigureFunc: providerConfigure,

nsxt/resource_nsxt_policy_group.go

Lines changed: 83 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -94,42 +94,50 @@ func resourceNsxtPolicyGroup() *schema.Resource {
9494
State: nsxtDomainResourceImporter,
9595
},
9696

97-
Schema: map[string]*schema.Schema{
98-
"nsx_id": getNsxIDSchema(),
99-
"path": getPathSchema(),
100-
"display_name": getDisplayNameSchema(),
101-
"description": getDescriptionSchema(),
102-
"revision": getRevisionSchema(),
103-
"tag": getTagsSchema(),
104-
"context": getContextSchema(false, false, false),
105-
"domain": getDomainNameSchema(),
106-
"group_type": {
107-
Type: schema.TypeString,
108-
Description: "Indicates the group type",
109-
ValidateFunc: validation.StringInSlice(groupTypeValues, false),
110-
Optional: true,
111-
},
112-
"criteria": {
113-
Type: schema.TypeList,
114-
Description: "Criteria to determine Group membership",
115-
Elem: getCriteriaSetSchema(),
116-
Optional: true,
117-
},
118-
"conjunction": {
119-
Type: schema.TypeList,
120-
Description: "A conjunction applied to 2 sets of criteria.",
121-
Elem: getConjunctionSchema(),
122-
Optional: true,
123-
},
124-
"extended_criteria": {
125-
Type: schema.TypeList,
126-
Description: "Extended criteria to determine group membership. extended_criteria is implicitly \"AND\" with criteria",
127-
Elem: getExtendedCriteriaSetSchema(),
128-
Optional: true,
129-
MaxItems: 1,
130-
},
97+
Schema: getPolicyGroupSchema(true),
98+
}
99+
}
100+
101+
func getPolicyGroupSchema(withDomain bool) map[string]*schema.Schema {
102+
s := map[string]*schema.Schema{
103+
"nsx_id": getNsxIDSchema(),
104+
"path": getPathSchema(),
105+
"display_name": getDisplayNameSchema(),
106+
"description": getDescriptionSchema(),
107+
"revision": getRevisionSchema(),
108+
"tag": getTagsSchema(),
109+
"context": getContextSchema(false, false, !withDomain),
110+
"group_type": {
111+
Type: schema.TypeString,
112+
Description: "Indicates the group type",
113+
ValidateFunc: validation.StringInSlice(groupTypeValues, false),
114+
Optional: true,
115+
},
116+
"criteria": {
117+
Type: schema.TypeList,
118+
Description: "Criteria to determine Group membership",
119+
Elem: getCriteriaSetSchema(),
120+
Optional: true,
121+
},
122+
"conjunction": {
123+
Type: schema.TypeList,
124+
Description: "A conjunction applied to 2 sets of criteria.",
125+
Elem: getConjunctionSchema(),
126+
Optional: true,
127+
},
128+
"extended_criteria": {
129+
Type: schema.TypeList,
130+
Description: "Extended criteria to determine group membership. extended_criteria is implicitly \"AND\" with criteria",
131+
Elem: getExtendedCriteriaSetSchema(),
132+
Optional: true,
133+
MaxItems: 1,
131134
},
132135
}
136+
137+
if withDomain {
138+
s["domain"] = getDomainNameSchema()
139+
}
140+
return s
133141
}
134142

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

835843
func resourceNsxtPolicyGroupCreate(d *schema.ResourceData, m interface{}) error {
844+
return resourceNsxtPolicyGroupGeneralCreate(d, m, true)
845+
}
846+
847+
func resourceNsxtPolicyGroupGeneralCreate(d *schema.ResourceData, m interface{}, withDomain bool) error {
836848
connector := getPolicyConnector(m)
837849

850+
domainName := ""
851+
if withDomain {
852+
domainName = d.Get("domain").(string)
853+
}
838854
// Initialize resource Id and verify this ID is not yet used
839-
id, err := getOrGenerateID2(d, m, resourceNsxtPolicyGroupExistsInDomainPartial(d.Get("domain").(string)))
855+
id, err := getOrGenerateID2(d, m, resourceNsxtPolicyGroupExistsInDomainPartial(domainName))
840856
if err != nil {
841857
return err
842858
}
@@ -886,7 +902,7 @@ func resourceNsxtPolicyGroupCreate(d *schema.ResourceData, m interface{}) error
886902
if client == nil {
887903
return policyResourceNotSupportedError()
888904
}
889-
err = client.Patch(d.Get("domain").(string), id, obj)
905+
err = client.Patch(domainName, id, obj)
890906

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

900-
return resourceNsxtPolicyGroupRead(d, m)
916+
return resourceNsxtPolicyGroupGeneralRead(d, m, withDomain)
901917
}
902918

903919
func resourceNsxtPolicyGroupRead(d *schema.ResourceData, m interface{}) error {
920+
return resourceNsxtPolicyGroupGeneralRead(d, m, true)
921+
}
922+
923+
func resourceNsxtPolicyGroupGeneralRead(d *schema.ResourceData, m interface{}, withDomain bool) error {
904924
connector := getPolicyConnector(m)
905925
id := d.Id()
906-
domainName := d.Get("domain").(string)
926+
domainName := ""
927+
if withDomain {
928+
domainName = d.Get("domain").(string)
929+
}
907930
if id == "" {
908931
return fmt.Errorf("Error obtaining Group ID")
909932
}
@@ -920,7 +943,9 @@ func resourceNsxtPolicyGroupRead(d *schema.ResourceData, m interface{}) error {
920943
setPolicyTagsInSchema(d, obj.Tags)
921944
d.Set("nsx_id", id)
922945
d.Set("path", obj.Path)
923-
d.Set("domain", getDomainFromResourcePath(*obj.Path))
946+
if withDomain {
947+
d.Set("domain", getDomainFromResourcePath(*obj.Path))
948+
}
924949
d.Set("revision", obj.Revision)
925950
groupType := ""
926951
if len(obj.GroupType) > 0 && util.NsxVersionHigherOrEqual("3.2.0") {
@@ -951,6 +976,10 @@ func resourceNsxtPolicyGroupRead(d *schema.ResourceData, m interface{}) error {
951976
}
952977

953978
func resourceNsxtPolicyGroupUpdate(d *schema.ResourceData, m interface{}) error {
979+
return resourceNsxtPolicyGroupGeneralUpdate(d, m, true)
980+
}
981+
982+
func resourceNsxtPolicyGroupGeneralUpdate(d *schema.ResourceData, m interface{}, withDomain bool) error {
954983
connector := getPolicyConnector(m)
955984

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

10091038
// Update the resource using PATCH
1010-
err = client.Patch(d.Get("domain").(string), id, obj)
1039+
domainName := ""
1040+
if withDomain {
1041+
domainName = d.Get("domain").(string)
1042+
}
1043+
err = client.Patch(domainName, id, obj)
10111044
if err != nil {
10121045
return handleUpdateError("Group", id, err)
10131046
}
10141047

1015-
return resourceNsxtPolicyGroupRead(d, m)
1048+
return resourceNsxtPolicyGroupGeneralRead(d, m, withDomain)
10161049
}
10171050

10181051
func resourceNsxtPolicyGroupDelete(d *schema.ResourceData, m interface{}) error {
1052+
return resourceNsxtPolicyGroupGeneralDelete(d, m, true)
1053+
}
1054+
1055+
func resourceNsxtPolicyGroupGeneralDelete(d *schema.ResourceData, m interface{}, withDomain bool) error {
10191056
id := d.Id()
10201057
if id == "" {
10211058
return fmt.Errorf("Error obtaining Group ID")
@@ -1030,7 +1067,11 @@ func resourceNsxtPolicyGroupDelete(d *schema.ResourceData, m interface{}) error
10301067
if client == nil {
10311068
return policyResourceNotSupportedError()
10321069
}
1033-
return client.Delete(d.Get("domain").(string), id, &failIfSubtreeExists, &forceDelete)
1070+
domainName := ""
1071+
if withDomain {
1072+
domainName = d.Get("domain").(string)
1073+
}
1074+
return client.Delete(domainName, id, &failIfSubtreeExists, &forceDelete)
10341075
}
10351076

10361077
err := doDelete()

0 commit comments

Comments
 (0)