Skip to content

Commit

Permalink
Configure project and VPC at provider level
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
ksamoray committed Jun 4, 2024
1 parent 6ea23e2 commit cb6c1fe
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
12 changes: 9 additions & 3 deletions nsxt/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ type nsxtClients struct {
Host string
PolicyEnforcementPoint string
PolicyGlobalManager bool
ProjectID string
VPCID string
}

// Provider for VMWare NSX-T
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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{})
Expand All @@ -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 != "" {
Expand Down
8 changes: 4 additions & 4 deletions nsxt/resource_nsxt_policy_security_policy_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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{})
Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit cb6c1fe

Please sign in to comment.