Skip to content

Commit

Permalink
VPC N-S security policy resource
Browse files Browse the repository at this point in the history
Add VPC support to the API wrapper generator and implement the resource
for north-south VPC security policy.

Signed-off-by: Kobi Samoray <[email protected]>
  • Loading branch information
ksamoray committed Jun 2, 2024
1 parent 7c83f69 commit ef46eec
Show file tree
Hide file tree
Showing 10 changed files with 544 additions and 27 deletions.
3 changes: 3 additions & 0 deletions api/api_list.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@
- client: github.com/vmware/vsphere-automation-sdk-go/services/nsxt/orgs/projects/infra/domains
model: github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model
type: Multitenancy
- client: github.com/vmware/vsphere-automation-sdk-go/services/nsxt/orgs/projects/vpcs
model: github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model
type: VPC
model_name: GatewayPolicy
obj_name: GatewayPolicy
client_name: GatewayPoliciesClient
Expand Down
27 changes: 27 additions & 0 deletions api/infra/domains/gateway_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
client0 "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra/domains"
model0 "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"
client2 "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/orgs/projects/infra/domains"
client3 "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/orgs/projects/vpcs"

utl "github.com/vmware/terraform-provider-nsxt/api/utl"
)
Expand All @@ -31,6 +32,9 @@ func NewGatewayPoliciesClient(sessionContext utl.SessionContext, connector vapiP
case utl.Multitenancy:
client = client2.NewGatewayPoliciesClient(connector)

case utl.VPC:
client = client3.NewGatewayPoliciesClient(connector)

default:
return nil
}
Expand Down Expand Up @@ -67,6 +71,13 @@ func (c GatewayPolicyClientContext) Get(domainIdParam string, gatewayPolicyIdPar
return obj, err
}

case utl.VPC:
client := c.Client.(client3.GatewayPoliciesClient)
obj, err = client.Get(utl.DefaultOrgID, c.ProjectID, c.VPCID, gatewayPolicyIdParam)
if err != nil {
return obj, err
}

default:
return obj, errors.New("invalid infrastructure for model")
}
Expand Down Expand Up @@ -94,6 +105,10 @@ func (c GatewayPolicyClientContext) Patch(domainIdParam string, gatewayPolicyIdP
client := c.Client.(client2.GatewayPoliciesClient)
err = client.Patch(utl.DefaultOrgID, c.ProjectID, domainIdParam, gatewayPolicyIdParam, gatewayPolicyParam)

case utl.VPC:
client := c.Client.(client3.GatewayPoliciesClient)
err = client.Patch(utl.DefaultOrgID, c.ProjectID, c.VPCID, gatewayPolicyIdParam, gatewayPolicyParam)

default:
err = errors.New("invalid infrastructure for model")
}
Expand Down Expand Up @@ -130,6 +145,10 @@ func (c GatewayPolicyClientContext) Update(domainIdParam string, gatewayPolicyId
client := c.Client.(client2.GatewayPoliciesClient)
obj, err = client.Update(utl.DefaultOrgID, c.ProjectID, domainIdParam, gatewayPolicyIdParam, gatewayPolicyParam)

case utl.VPC:
client := c.Client.(client3.GatewayPoliciesClient)
obj, err = client.Update(utl.DefaultOrgID, c.ProjectID, c.VPCID, gatewayPolicyIdParam, gatewayPolicyParam)

default:
err = errors.New("invalid infrastructure for model")
}
Expand All @@ -153,6 +172,10 @@ func (c GatewayPolicyClientContext) Delete(domainIdParam string, gatewayPolicyId
client := c.Client.(client2.GatewayPoliciesClient)
err = client.Delete(utl.DefaultOrgID, c.ProjectID, domainIdParam, gatewayPolicyIdParam)

case utl.VPC:
client := c.Client.(client3.GatewayPoliciesClient)
err = client.Delete(utl.DefaultOrgID, c.ProjectID, c.VPCID, gatewayPolicyIdParam)

default:
err = errors.New("invalid infrastructure for model")
}
Expand Down Expand Up @@ -185,6 +208,10 @@ func (c GatewayPolicyClientContext) List(domainIdParam string, cursorParam *stri
client := c.Client.(client2.GatewayPoliciesClient)
obj, err = client.List(utl.DefaultOrgID, c.ProjectID, domainIdParam, cursorParam, includeMarkForDeleteObjectsParam, includeRuleCountParam, includedFieldsParam, pageSizeParam, sortAscendingParam, sortByParam)

case utl.VPC:
client := c.Client.(client3.GatewayPoliciesClient)
obj, err = client.List(utl.DefaultOrgID, c.ProjectID, c.VPCID, cursorParam, includeMarkForDeleteObjectsParam, includeRuleCountParam, includedFieldsParam, pageSizeParam, sortAscendingParam, sortByParam)

default:
err = errors.New("invalid infrastructure for model")
}
Expand Down
6 changes: 3 additions & 3 deletions nsxt/policy_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,13 +310,13 @@ func getSecurityPolicyAndGatewayRuleSchema(scopeRequired bool, isIds bool, nsxID
return ruleSchema
}

func getPolicyGatewayPolicySchema() map[string]*schema.Schema {
secPolicy := getPolicySecurityPolicySchema(false, true, true, true)
func getPolicyGatewayPolicySchema(withDomain bool) map[string]*schema.Schema {
secPolicy := getPolicySecurityPolicySchema(false, true, true, withDomain)
// GW Policies don't support scope
delete(secPolicy, "scope")
secPolicy["category"].ValidateFunc = validation.StringInSlice(gatewayPolicyCategoryWritableValues, false)
// GW Policy rules require scope to be set
secPolicy["rule"] = getSecurityPolicyAndGatewayRulesSchema(true, false, true)
secPolicy["rule"] = getSecurityPolicyAndGatewayRulesSchema(withDomain, false, true)
return secPolicy
}

Expand Down
1 change: 1 addition & 0 deletions nsxt/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,7 @@ func Provider() *schema.Provider {
"nsxt_policy_compute_sub_cluster": resourceNsxtPolicyComputeSubCluster(),
"nsxt_policy_tier0_inter_vrf_routing": resourceNsxtPolicyTier0InterVRFRouting(),
"nsxt_policy_vpc_security_policy": resourceNsxtPolicyVPCSecurityPolicy(),
"nsxt_policy_vpc_gateway_policy": resourceNsxtPolicyVPCGatewayPolicy(),
},

ConfigureFunc: providerConfigure,
Expand Down
15 changes: 9 additions & 6 deletions nsxt/resource_nsxt_policy_gateway_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func resourceNsxtPolicyGatewayPolicy() *schema.Resource {
State: nsxtDomainResourceImporter,
},

Schema: getPolicyGatewayPolicySchema(),
Schema: getPolicyGatewayPolicySchema(true),
}
}

Expand Down Expand Up @@ -121,9 +121,12 @@ func getUpdatedRuleChildren(d *schema.ResourceData) ([]*data.StructValue, error)

}

func policyGatewayPolicyBuildAndPatch(d *schema.ResourceData, m interface{}, connector client.Connector, isGlobalManager bool, id string) error {
func policyGatewayPolicyBuildAndPatch(d *schema.ResourceData, m interface{}, connector client.Connector, isGlobalManager bool, id string, withDomain bool) error {

domain := d.Get("domain").(string)
domain := ""
if withDomain {
domain = d.Get("domain").(string)
}
displayName := d.Get("display_name").(string)
description := d.Get("description").(string)
tags := getPolicyTagsFromSchema(d)
Expand Down Expand Up @@ -166,7 +169,7 @@ func policyGatewayPolicyBuildAndPatch(d *schema.ResourceData, m interface{}, con
obj.Children = policyChildren
}

return gatewayPolicyInfraPatch(getSessionContext(d, m), obj, domain, m)
return gatewayPolicyInfraPatch(getSessionContext(d, m), obj, domain, m, withDomain)
}

func resourceNsxtPolicyGatewayPolicyCreate(d *schema.ResourceData, m interface{}) error {
Expand All @@ -178,7 +181,7 @@ func resourceNsxtPolicyGatewayPolicyCreate(d *schema.ResourceData, m interface{}
return err
}

err = policyGatewayPolicyBuildAndPatch(d, m, connector, isPolicyGlobalManager(m), id)
err = policyGatewayPolicyBuildAndPatch(d, m, connector, isPolicyGlobalManager(m), id, true)
if err != nil {
return handleCreateError("Gateway Policy", id, err)
}
Expand Down Expand Up @@ -229,7 +232,7 @@ func resourceNsxtPolicyGatewayPolicyUpdate(d *schema.ResourceData, m interface{}
return fmt.Errorf("Error obtaining Gateway Policy ID")
}

err := policyGatewayPolicyBuildAndPatch(d, m, connector, isPolicyGlobalManager(m), id)
err := policyGatewayPolicyBuildAndPatch(d, m, connector, isPolicyGlobalManager(m), id, true)
if err != nil {
return handleUpdateError("Gateway Policy", id, err)
}
Expand Down
33 changes: 18 additions & 15 deletions nsxt/resource_nsxt_policy_gateway_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ func TestAccResourceNsxtPolicyGatewayPolicy_multitenancy(t *testing.T) {
func testAccResourceNsxtPolicyGatewayPolicyBasic(t *testing.T, withContext bool, preCheck func()) {
name := getAccTestResourceName()
updatedName := getAccTestResourceName()
testResourceName := "nsxt_policy_gateway_policy.test"
resourceName := "nsxt_policy_gateway_policy"
testResourceName := fmt.Sprintf("%s.test", resourceName)
comments1 := "Acceptance test create"
comments2 := "Acceptance test update"
direction1 := "IN"
Expand All @@ -46,7 +47,7 @@ func testAccResourceNsxtPolicyGatewayPolicyBasic(t *testing.T, withContext bool,
},
Steps: []resource.TestStep{
{
Config: testAccNsxtPolicyGatewayPolicyBasic(name, comments1, withContext),
Config: testAccNsxtPolicyGatewayPolicyBasic(resourceName, name, comments1, withContext),
Check: resource.ComposeTestCheckFunc(
testAccNsxtPolicyGatewayPolicyExists(testResourceName, defaultDomain),
resource.TestCheckResourceAttr(testResourceName, "display_name", name),
Expand All @@ -63,7 +64,7 @@ func testAccResourceNsxtPolicyGatewayPolicyBasic(t *testing.T, withContext bool,
),
},
{
Config: testAccNsxtPolicyGatewayPolicyBasic(updatedName, comments2, withContext),
Config: testAccNsxtPolicyGatewayPolicyBasic(resourceName, updatedName, comments2, withContext),
Check: resource.ComposeTestCheckFunc(
testAccNsxtPolicyGatewayPolicyExists(testResourceName, defaultDomain),
resource.TestCheckResourceAttr(testResourceName, "display_name", updatedName),
Expand All @@ -79,7 +80,7 @@ func testAccResourceNsxtPolicyGatewayPolicyBasic(t *testing.T, withContext bool,
),
},
{
Config: testAccNsxtPolicyGatewayPolicyWithRule(updatedName, direction1, proto1, tag1, withContext),
Config: testAccNsxtPolicyGatewayPolicyWithRule(resourceName, updatedName, direction1, proto1, tag1, withContext),
Check: resource.ComposeTestCheckFunc(
testAccNsxtPolicyGatewayPolicyExists(testResourceName, defaultDomain),
resource.TestCheckResourceAttr(testResourceName, "display_name", updatedName),
Expand All @@ -102,7 +103,7 @@ func testAccResourceNsxtPolicyGatewayPolicyBasic(t *testing.T, withContext bool,
),
},
{
Config: testAccNsxtPolicyGatewayPolicyWithRule(updatedName, direction2, proto2, tag2, withContext),
Config: testAccNsxtPolicyGatewayPolicyWithRule(resourceName, updatedName, direction2, proto2, tag2, withContext),
Check: resource.ComposeTestCheckFunc(
testAccNsxtPolicyGatewayPolicyExists(testResourceName, defaultDomain),
resource.TestCheckResourceAttr(testResourceName, "display_name", updatedName),
Expand Down Expand Up @@ -208,7 +209,8 @@ func TestAccResourceNsxtPolicyGatewayPolicy_withDependencies(t *testing.T) {
}
func TestAccResourceNsxtPolicyGatewayPolicy_importBasic(t *testing.T) {
name := getAccTestResourceName()
testResourceName := "nsxt_policy_gateway_policy.test"
resourceName := "nsxt_policy_gateway_policy"
testResourceName := fmt.Sprintf("%s.test", resourceName)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -218,7 +220,7 @@ func TestAccResourceNsxtPolicyGatewayPolicy_importBasic(t *testing.T) {
},
Steps: []resource.TestStep{
{
Config: testAccNsxtPolicyGatewayPolicyBasic(name, "import", false),
Config: testAccNsxtPolicyGatewayPolicyBasic(resourceName, name, "import", false),
},
{
ResourceName: testResourceName,
Expand All @@ -231,7 +233,8 @@ func TestAccResourceNsxtPolicyGatewayPolicy_importBasic(t *testing.T) {

func TestAccResourceNsxtPolicyGatewayPolicy_importBasic_multitenancy(t *testing.T) {
name := getAccTestResourceName()
testResourceName := "nsxt_policy_gateway_policy.test"
resourceName := "nsxt_policy_gateway_policy"
testResourceName := fmt.Sprintf("%s.test", resourceName)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccOnlyMultitenancy(t) },
Expand All @@ -241,7 +244,7 @@ func TestAccResourceNsxtPolicyGatewayPolicy_importBasic_multitenancy(t *testing.
},
Steps: []resource.TestStep{
{
Config: testAccNsxtPolicyGatewayPolicyBasic(name, "import", true),
Config: testAccNsxtPolicyGatewayPolicyBasic(resourceName, name, "import", true),
},
{
ResourceName: testResourceName,
Expand Down Expand Up @@ -623,13 +626,13 @@ resource "nsxt_policy_gateway_policy" "test" {
}`, name, comments)
}

func testAccNsxtPolicyGatewayPolicyBasic(name string, comments string, withContext bool) string {
func testAccNsxtPolicyGatewayPolicyBasic(resourceName, name, comments string, withContext bool) string {
context := ""
if withContext {
context = testAccNsxtPolicyMultitenancyContext()
}
return fmt.Sprintf(`
resource "nsxt_policy_gateway_policy" "test" {
resource "%s" "test" {
%s
display_name = "%s"
description = "Acceptance Test"
Expand All @@ -645,10 +648,10 @@ resource "nsxt_policy_gateway_policy" "test" {
tag = "orange"
}
}`, context, name, comments)
}`, resourceName, context, name, comments)
}

func testAccNsxtPolicyGatewayPolicyWithRule(name string, direction string, protocol string, ruleTag string, withContext bool) string {
func testAccNsxtPolicyGatewayPolicyWithRule(resourceName, name, direction, protocol, ruleTag string, withContext bool) string {
context := ""
if withContext {
context = testAccNsxtPolicyMultitenancyContext()
Expand All @@ -660,7 +663,7 @@ resource "nsxt_policy_tier1_gateway" "gwt1test" {
description = "Acceptance Test"
}
resource "nsxt_policy_gateway_policy" "test" {
resource "%s" "test" {
%s
display_name = "%s"
description = "Acceptance Test"
Expand All @@ -687,7 +690,7 @@ resource "nsxt_policy_gateway_policy" "test" {
tag = "blue"
}
}
}`, context, context, name, name, direction, protocol, ruleTag)
}`, context, resourceName, context, name, name, direction, protocol, ruleTag)
}

// TODO: add profiles when available
Expand Down
22 changes: 19 additions & 3 deletions nsxt/resource_nsxt_policy_predefined_gateway_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
nsxt "github.com/vmware/vsphere-automation-sdk-go/services/nsxt"

"github.com/vmware/vsphere-automation-sdk-go/runtime/bindings"
"github.com/vmware/vsphere-automation-sdk-go/runtime/data"
Expand Down Expand Up @@ -263,7 +264,22 @@ func createChildDomainWithGatewayPolicy(domain string, policyID string, policy m
return dataValue.(*data.StructValue), nil
}

func gatewayPolicyInfraPatch(context utl.SessionContext, policy model.GatewayPolicy, domain string, m interface{}) error {
func gatewayPolicyInfraPatch(context utl.SessionContext, policy model.GatewayPolicy, domain string, m interface{}, withDomain bool) error {
connector := getPolicyConnector(m)
if context.ClientType == utl.VPC {
childVPC, err := createChildVPCWithGatewayPolicy(context, *policy.Id, policy)
if err != nil {
return fmt.Errorf("failed to create H-API for VPC Gateway Policy: %s", err)
}

orgRoot := model.OrgRoot{
ResourceType: strPtr("OrgRoot"),
Children: []*data.StructValue{childVPC},
}

client := nsxt.NewOrgRootClient(connector)
return client.Patch(orgRoot, nil)
}
childDomain, err := createChildDomainWithGatewayPolicy(domain, *policy.Id, policy)
if err != nil {
return fmt.Errorf("Failed to create H-API for Predefined Gateway Policy: %s", err)
Expand Down Expand Up @@ -376,7 +392,7 @@ func updatePolicyPredefinedGatewayPolicy(id string, d *schema.ResourceData, m in
predefinedPolicy.Children = childRules
}

err = gatewayPolicyInfraPatch(getSessionContext(d, m), predefinedPolicy, domain, m)
err = gatewayPolicyInfraPatch(getSessionContext(d, m), predefinedPolicy, domain, m, true)
if err != nil {
return handleUpdateError("Predefined Gateway Policy", id, err)
}
Expand Down Expand Up @@ -475,7 +491,7 @@ func resourceNsxtPolicyPredefinedGatewayPolicyDelete(d *schema.ResourceData, m i
return fmt.Errorf("Failed to revert Predefined Gateway Policy %s: %s", id, err)
}

err = gatewayPolicyInfraPatch(getSessionContext(d, m), revertedPolicy, domain, m)
err = gatewayPolicyInfraPatch(getSessionContext(d, m), revertedPolicy, domain, m, true)
if err != nil {
return handleUpdateError("Predefined Gateway Policy", id, err)
}
Expand Down
Loading

0 comments on commit ef46eec

Please sign in to comment.