Skip to content

Commit cb6c1fe

Browse files
committed
Configure project and VPC at provider level
Allow configuration of the attributes above in the provider globally for the whole context, instead of the resource level. Signed-off-by: Kobi Samoray <[email protected]>
1 parent 6ea23e2 commit cb6c1fe

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

nsxt/provider.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ type nsxtClients struct {
6868
Host string
6969
PolicyEnforcementPoint string
7070
PolicyGlobalManager bool
71+
ProjectID string
72+
VPCID string
7173
}
7274

7375
// Provider for VMWare NSX-T
@@ -241,6 +243,7 @@ func Provider() *schema.Provider {
241243
Description: "Avoid initializing NSX connection on startup",
242244
DefaultFunc: schema.EnvDefaultFunc("NSXT_ON_DEMAND_CONNECTION", false),
243245
},
246+
"context": getContextSchema(false, false, true),
244247
},
245248

246249
DataSourcesMap: map[string]*schema.Resource{
@@ -782,6 +785,7 @@ func configurePolicyConnectorData(d *schema.ResourceData, clients *nsxtClients)
782785
clientAuthDefined := (len(clientAuthCertFile) > 0) || (len(clientAuthCert) > 0)
783786
policyEnforcementPoint := d.Get("enforcement_point").(string)
784787
policyGlobalManager := d.Get("global_manager").(bool)
788+
projectID, vpcID := getContextDataFromSchema(d, clients)
785789
vmcInfo := getVmcAuthInfo(d)
786790

787791
isVMC := false
@@ -827,6 +831,8 @@ func configurePolicyConnectorData(d *schema.ResourceData, clients *nsxtClients)
827831
clients.Host = host
828832
clients.PolicyEnforcementPoint = policyEnforcementPoint
829833
clients.PolicyGlobalManager = policyGlobalManager
834+
clients.ProjectID = projectID
835+
clients.VPCID = vpcID
830836

831837
if onDemandConn {
832838
// version init will happen on demand
@@ -1209,7 +1215,7 @@ func getGlobalPolicyEnforcementPointPath(m interface{}, sitePath *string) string
12091215
return fmt.Sprintf("%s/enforcement-points/%s", *sitePath, getPolicyEnforcementPoint(m))
12101216
}
12111217

1212-
func getContextDataFromSchema(d *schema.ResourceData) (string, string) {
1218+
func getContextDataFromSchema(d *schema.ResourceData, m interface{}) (string, string) {
12131219
ctxPtr := d.Get("context")
12141220
if ctxPtr != nil {
12151221
contexts := ctxPtr.([]interface{})
@@ -1223,12 +1229,12 @@ func getContextDataFromSchema(d *schema.ResourceData) (string, string) {
12231229
return data["project_id"].(string), vpcID
12241230
}
12251231
}
1226-
return "", ""
1232+
return m.(nsxtClients).ProjectID, m.(nsxtClients).VPCID
12271233
}
12281234

12291235
func getSessionContext(d *schema.ResourceData, m interface{}) tf_api.SessionContext {
12301236
var clientType tf_api.ClientType
1231-
projectID, vpcID := getContextDataFromSchema(d)
1237+
projectID, vpcID := getContextDataFromSchema(d, m)
12321238
if projectID != "" {
12331239
clientType = tf_api.Multitenancy
12341240
if vpcID != "" {

nsxt/resource_nsxt_policy_security_policy_rule.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func resourceNsxtPolicySecurityPolicyRuleCreate(d *schema.ResourceData, m interf
4343
return err
4444
}
4545

46-
if err := setSecurityPolicyRuleContext(d, projectID); err != nil {
46+
if err := setSecurityPolicyRuleContext(d, m, projectID); err != nil {
4747
return handleCreateError("SecurityPolicyRule", fmt.Sprintf("%s/%s", policyPath, id), err)
4848
}
4949

@@ -61,8 +61,8 @@ func resourceNsxtPolicySecurityPolicyRuleCreate(d *schema.ResourceData, m interf
6161
return resourceNsxtPolicySecurityPolicyRuleRead(d, m)
6262
}
6363

64-
func setSecurityPolicyRuleContext(d *schema.ResourceData, projectID string) error {
65-
providedProjectID, _ := getContextDataFromSchema(d)
64+
func setSecurityPolicyRuleContext(d *schema.ResourceData, m interface{}, projectID string) error {
65+
providedProjectID, _ := getContextDataFromSchema(d, m)
6666
if providedProjectID == "" {
6767
contexts := make([]interface{}, 1)
6868
ctxMap := make(map[string]interface{})
@@ -157,7 +157,7 @@ func resourceNsxtPolicySecurityPolicyRuleRead(d *schema.ResourceData, m interfac
157157
domain := getDomainFromResourcePath(policyPath)
158158
policyID := getPolicyIDFromPath(policyPath)
159159

160-
if err := setSecurityPolicyRuleContext(d, projectID); err != nil {
160+
if err := setSecurityPolicyRuleContext(d, m, projectID); err != nil {
161161
return handleReadError(d, "SecurityPolicyRule", fmt.Sprintf("%s/%s", policyPath, id), err)
162162
}
163163

0 commit comments

Comments
 (0)