From cb6c1fe51196e90742c634c2cedccab9e2c52d60 Mon Sep 17 00:00:00 2001 From: Kobi Samoray Date: Tue, 4 Jun 2024 15:26:51 +0300 Subject: [PATCH] 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 --- nsxt/provider.go | 12 +++++++++--- nsxt/resource_nsxt_policy_security_policy_rule.go | 8 ++++---- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/nsxt/provider.go b/nsxt/provider.go index 957b30949..859168834 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) }