diff --git a/nsxt/provider.go b/nsxt/provider.go index 957b30949..10619ec04 100644 --- a/nsxt/provider.go +++ b/nsxt/provider.go @@ -68,6 +68,8 @@ type nsxtClients struct { Host string PolicyEnforcementPoint string PolicyGlobalManager bool + ProjectID string + VPCID string } // Provider for VMWare NSX-T @@ -241,6 +243,7 @@ func Provider() *schema.Provider { Description: "Avoid initializing NSX connection on startup", DefaultFunc: schema.EnvDefaultFunc("NSXT_ON_DEMAND_CONNECTION", false), }, + "context": getContextSchema(false, false, true), }, DataSourcesMap: map[string]*schema.Resource{ @@ -782,6 +785,7 @@ func configurePolicyConnectorData(d *schema.ResourceData, clients *nsxtClients) clientAuthDefined := (len(clientAuthCertFile) > 0) || (len(clientAuthCert) > 0) policyEnforcementPoint := d.Get("enforcement_point").(string) policyGlobalManager := d.Get("global_manager").(bool) + projectID, vpcID := getContextDataFromSchema(d, &clients) vmcInfo := getVmcAuthInfo(d) isVMC := false @@ -827,6 +831,8 @@ func configurePolicyConnectorData(d *schema.ResourceData, clients *nsxtClients) clients.Host = host clients.PolicyEnforcementPoint = policyEnforcementPoint clients.PolicyGlobalManager = policyGlobalManager + clients.ProjectID = projectID + clients.VPCID = vpcID if onDemandConn { // version init will happen on demand @@ -1209,7 +1215,7 @@ func getGlobalPolicyEnforcementPointPath(m interface{}, sitePath *string) string return fmt.Sprintf("%s/enforcement-points/%s", *sitePath, getPolicyEnforcementPoint(m)) } -func getContextDataFromSchema(d *schema.ResourceData) (string, string) { +func getContextDataFromSchema(d *schema.ResourceData, m interface{}) (string, string) { ctxPtr := d.Get("context") if ctxPtr != nil { contexts := ctxPtr.([]interface{}) @@ -1223,12 +1229,12 @@ func getContextDataFromSchema(d *schema.ResourceData) (string, string) { return data["project_id"].(string), vpcID } } - return "", "" + return m.(nsxtClients).ProjectID, m.(nsxtClients).VPCID } func getSessionContext(d *schema.ResourceData, m interface{}) tf_api.SessionContext { var clientType tf_api.ClientType - projectID, vpcID := getContextDataFromSchema(d) + projectID, vpcID := getContextDataFromSchema(d, m) if projectID != "" { clientType = tf_api.Multitenancy if vpcID != "" { diff --git a/nsxt/resource_nsxt_policy_security_policy_rule.go b/nsxt/resource_nsxt_policy_security_policy_rule.go index a8855737d..8a99512e6 100644 --- a/nsxt/resource_nsxt_policy_security_policy_rule.go +++ b/nsxt/resource_nsxt_policy_security_policy_rule.go @@ -43,7 +43,7 @@ func resourceNsxtPolicySecurityPolicyRuleCreate(d *schema.ResourceData, m interf return err } - if err := setSecurityPolicyRuleContext(d, projectID); err != nil { + if err := setSecurityPolicyRuleContext(d, m, projectID); err != nil { return handleCreateError("SecurityPolicyRule", fmt.Sprintf("%s/%s", policyPath, id), err) } @@ -61,8 +61,8 @@ func resourceNsxtPolicySecurityPolicyRuleCreate(d *schema.ResourceData, m interf return resourceNsxtPolicySecurityPolicyRuleRead(d, m) } -func setSecurityPolicyRuleContext(d *schema.ResourceData, projectID string) error { - providedProjectID, _ := getContextDataFromSchema(d) +func setSecurityPolicyRuleContext(d *schema.ResourceData, m interface{}, projectID string) error { + providedProjectID, _ := getContextDataFromSchema(d, m) if providedProjectID == "" { contexts := make([]interface{}, 1) ctxMap := make(map[string]interface{}) @@ -157,7 +157,7 @@ func resourceNsxtPolicySecurityPolicyRuleRead(d *schema.ResourceData, m interfac domain := getDomainFromResourcePath(policyPath) policyID := getPolicyIDFromPath(policyPath) - if err := setSecurityPolicyRuleContext(d, projectID); err != nil { + if err := setSecurityPolicyRuleContext(d, m, projectID); err != nil { return handleReadError(d, "SecurityPolicyRule", fmt.Sprintf("%s/%s", policyPath, id), err) } diff --git a/website/docs/index.html.markdown b/website/docs/index.html.markdown index 61f4e2bb5..f2ef7254e 100644 --- a/website/docs/index.html.markdown +++ b/website/docs/index.html.markdown @@ -251,6 +251,9 @@ The following arguments are used to configure the VMware NSX-T Provider: for VMC environments, and is not supported with deprecated NSX manager resources and data sources. Note - this setting is useful when NSX manager is not yet available at time of provider evaluation, and not recommended to be turned on otherwise. +* `context` - (Optional) The context which the object belongs to + * `project_id` - (Optional) The ID of the project which the object belongs to + * `vpc_id` - (Optional) The ID of the VPC which the object belongs to ## NSX Logical Networking