Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run multitenancy tests in LM setups #1350

Draft
wants to merge 4 commits into
base: vpc20-feature-branch
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion nsxt/data_source_nsxt_policy_dhcp_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func testAccDataSourceNsxtPolicyDhcpServerBasic(t *testing.T, withContext bool,
func testAccNsxtPolicyDhcpServerReadTemplate(name string, withContext bool) string {
context := ""
if withContext {
context = testAccNsxtPolicyMultitenancyContext()
context = testAccNsxtProjectContext()
}
return fmt.Sprintf(`
resource "nsxt_policy_dhcp_server" "test" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func TestAccDataSourceNsxtPolicyGatewayLocaleService_multitenancy(t *testing.T)
func testAccNsxtPolicyGatewayLocaleServiceTemplate(name string, withContext bool) string {
context := ""
if withContext {
context = testAccNsxtPolicyMultitenancyContext()
context = testAccNsxtProjectContext()
}
return fmt.Sprintf(`
data "nsxt_policy_edge_cluster" "test" {
Expand Down
19 changes: 12 additions & 7 deletions nsxt/data_source_nsxt_policy_ip_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import (
"fmt"
tf_api "github.com/vmware/terraform-provider-nsxt/api/utl"

Check failure on line 8 in nsxt/data_source_nsxt_policy_ip_pool_test.go

View workflow job for this annotation

GitHub Actions / test

File is not `goimports`-ed (goimports)
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
Expand All @@ -31,17 +32,21 @@
func testAccDataSourceNsxtPolicyIPPoolBasic(t *testing.T, withContext bool, preCheck func()) {
name := getAccTestDataSourceName()
testResourceName := "data.nsxt_policy_ip_pool.test"
var clientType tf_api.ClientType = tf_api.Local
if withContext {
clientType = tf_api.Multitenancy
}

resource.ParallelTest(t, resource.TestCase{
PreCheck: preCheck,
Providers: testAccProviders,
CheckDestroy: func(state *terraform.State) error {
return testAccDataSourceNsxtPolicyIPPoolDeleteByName(name)
return testAccDataSourceNsxtPolicyIPPoolDeleteByName(name, clientType)
},
Steps: []resource.TestStep{
{
PreConfig: func() {
if err := testAccDataSourceNsxtPolicyIPPoolCreate(name); err != nil {
if err := testAccDataSourceNsxtPolicyIPPoolCreate(name, clientType); err != nil {
t.Error(err)
}
},
Expand All @@ -57,12 +62,12 @@
})
}

func testAccDataSourceNsxtPolicyIPPoolCreate(name string) error {
func testAccDataSourceNsxtPolicyIPPoolCreate(name string, clientType tf_api.ClientType) error {
connector, err := testAccGetPolicyConnector()
if err != nil {
return fmt.Errorf("Error during test client initialization: %v", err)
}
client := infra.NewIpPoolsClient(testAccGetSessionContext(), connector)
client := infra.NewIpPoolsClient(testAccGetContextByType(clientType), connector)
if client == nil {
return policyResourceNotSupportedError()
}
Expand All @@ -84,12 +89,12 @@
return nil
}

func testAccDataSourceNsxtPolicyIPPoolDeleteByName(name string) error {
func testAccDataSourceNsxtPolicyIPPoolDeleteByName(name string, clientType tf_api.ClientType) error {
connector, err := testAccGetPolicyConnector()
if err != nil {
return fmt.Errorf("Error during test client initialization: %v", err)
}
client := infra.NewIpPoolsClient(testAccGetSessionContext(), connector)
client := infra.NewIpPoolsClient(testAccGetContextByType(clientType), connector)
if client == nil {
return policyResourceNotSupportedError()
}
Expand All @@ -114,7 +119,7 @@
func testAccNsxtPolicyIPPoolReadTemplate(name string, withContext bool) string {
context := ""
if withContext {
context = testAccNsxtPolicyMultitenancyContext()
context = testAccNsxtProjectContext()
}
return fmt.Sprintf(`
data "nsxt_policy_ip_pool" "test" {
Expand Down
19 changes: 12 additions & 7 deletions nsxt/data_source_nsxt_policy_ipv6_dad_profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import (
"fmt"
tf_api "github.com/vmware/terraform-provider-nsxt/api/utl"

Check failure on line 8 in nsxt/data_source_nsxt_policy_ipv6_dad_profile_test.go

View workflow job for this annotation

GitHub Actions / test

File is not `goimports`-ed (goimports)
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
Expand All @@ -30,17 +31,21 @@
func testAccDataSourceNsxtPolicyIpv6DadProfileBasic(t *testing.T, withContext bool, preCheck func()) {
name := getAccTestDataSourceName()
testResourceName := "data.nsxt_policy_ipv6_dad_profile.test"
var clientType tf_api.ClientType = tf_api.Local
if withContext {
clientType = tf_api.Multitenancy
}

resource.ParallelTest(t, resource.TestCase{
PreCheck: preCheck,
Providers: testAccProviders,
CheckDestroy: func(state *terraform.State) error {
return testAccDataSourceNsxtPolicyIpv6DadProfileDeleteByName(name)
return testAccDataSourceNsxtPolicyIpv6DadProfileDeleteByName(name, clientType)
},
Steps: []resource.TestStep{
{
PreConfig: func() {
if err := testAccDataSourceNsxtPolicyIpv6DadProfileCreate(name); err != nil {
if err := testAccDataSourceNsxtPolicyIpv6DadProfileCreate(name, clientType); err != nil {
t.Error(err)
}
},
Expand All @@ -55,7 +60,7 @@
})
}

func testAccDataSourceNsxtPolicyIpv6DadProfileCreate(name string) error {
func testAccDataSourceNsxtPolicyIpv6DadProfileCreate(name string, clientType tf_api.ClientType) error {
connector, err := testAccGetPolicyConnector()
if err != nil {
return fmt.Errorf("Error during test client initialization: %v", err)
Expand All @@ -70,7 +75,7 @@

// Generate a random ID for the resource
id := newUUID()
client := infra.NewIpv6DadProfilesClient(testAccGetSessionContext(), connector)
client := infra.NewIpv6DadProfilesClient(testAccGetContextByType(clientType), connector)
if client == nil {
return policyResourceNotSupportedError()
}
Expand All @@ -82,13 +87,13 @@
return nil
}

func testAccDataSourceNsxtPolicyIpv6DadProfileDeleteByName(name string) error {
func testAccDataSourceNsxtPolicyIpv6DadProfileDeleteByName(name string, clientType tf_api.ClientType) error {
connector, err := testAccGetPolicyConnector()
if err != nil {
return fmt.Errorf("Error during test client initialization: %v", err)
}
// Find the object by name and delete it
client := infra.NewIpv6DadProfilesClient(testAccGetSessionContext(), connector)
client := infra.NewIpv6DadProfilesClient(testAccGetContextByType(clientType), connector)
if client == nil {
return policyResourceNotSupportedError()
}
Expand All @@ -113,7 +118,7 @@
func testAccNsxtPolicyIpv6DadProfileReadTemplate(name string, withContext bool) string {
context := ""
if withContext {
context = testAccNsxtPolicyMultitenancyContext()
context = testAccNsxtProjectContext()
}
return fmt.Sprintf(`
data "nsxt_policy_ipv6_dad_profile" "test" {
Expand Down
17 changes: 11 additions & 6 deletions nsxt/data_source_nsxt_policy_ipv6_ndra_profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import (
"fmt"
tf_api "github.com/vmware/terraform-provider-nsxt/api/utl"

Check failure on line 8 in nsxt/data_source_nsxt_policy_ipv6_ndra_profile_test.go

View workflow job for this annotation

GitHub Actions / test

File is not `goimports`-ed (goimports)
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
Expand All @@ -30,17 +31,21 @@
func testAccDataSourceNsxtPolicyIpv6NdraProfileBasic(t *testing.T, withContext bool, preCheck func()) {
name := getAccTestDataSourceName()
testResourceName := "data.nsxt_policy_ipv6_ndra_profile.test"
var clientType tf_api.ClientType = tf_api.Local
if withContext {
clientType = tf_api.Multitenancy
}

resource.ParallelTest(t, resource.TestCase{
PreCheck: preCheck,
Providers: testAccProviders,
CheckDestroy: func(state *terraform.State) error {
return testAccDataSourceNsxtPolicyIpv6NdraProfileDeleteByName(name)
return testAccDataSourceNsxtPolicyIpv6NdraProfileDeleteByName(name, clientType)
},
Steps: []resource.TestStep{
{
PreConfig: func() {
if err := testAccDataSourceNsxtPolicyIpv6NdraProfileCreate(name); err != nil {
if err := testAccDataSourceNsxtPolicyIpv6NdraProfileCreate(name, clientType); err != nil {
t.Error(err)
}
},
Expand All @@ -55,7 +60,7 @@
})
}

func testAccDataSourceNsxtPolicyIpv6NdraProfileCreate(name string) error {
func testAccDataSourceNsxtPolicyIpv6NdraProfileCreate(name string, clientType tf_api.ClientType) error {
connector, err := testAccGetPolicyConnector()
if err != nil {
return fmt.Errorf("Error during test client initialization: %v", err)
Expand All @@ -75,7 +80,7 @@
// Generate a random ID for the resource
id := newUUID()

client := infra.NewIpv6NdraProfilesClient(testAccGetSessionContext(), connector)
client := infra.NewIpv6NdraProfilesClient(testAccGetContextByType(clientType), connector)
if client == nil {
return policyResourceNotSupportedError()
}
Expand All @@ -86,13 +91,13 @@
return nil
}

func testAccDataSourceNsxtPolicyIpv6NdraProfileDeleteByName(name string) error {
func testAccDataSourceNsxtPolicyIpv6NdraProfileDeleteByName(name string, clientType tf_api.ClientType) error {
connector, err := testAccGetPolicyConnector()
if err != nil {
return fmt.Errorf("Error during test client initialization: %v", err)
}
// Find the object by name and delete it
client := infra.NewIpv6NdraProfilesClient(testAccGetSessionContext(), connector)
client := infra.NewIpv6NdraProfilesClient(testAccGetContextByType(clientType), connector)
if client == nil {
return policyResourceNotSupportedError()
}
Expand Down
19 changes: 12 additions & 7 deletions nsxt/data_source_nsxt_policy_qos_profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import (
"fmt"
tf_api "github.com/vmware/terraform-provider-nsxt/api/utl"

Check failure on line 8 in nsxt/data_source_nsxt_policy_qos_profile_test.go

View workflow job for this annotation

GitHub Actions / test

File is not `goimports`-ed (goimports)
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
Expand All @@ -30,17 +31,21 @@
func testAccDataSourceNsxtPolicyQosProfileBasic(t *testing.T, withContext bool, preCheck func()) {
name := getAccTestDataSourceName()
testResourceName := "data.nsxt_policy_qos_profile.test"
var clientType tf_api.ClientType = tf_api.Local
if withContext {
clientType = tf_api.Multitenancy
}

resource.Test(t, resource.TestCase{
PreCheck: preCheck,
Providers: testAccProviders,
CheckDestroy: func(state *terraform.State) error {
return testAccDataSourceNsxtPolicyQosProfileDeleteByName(name)
return testAccDataSourceNsxtPolicyQosProfileDeleteByName(name, clientType)
},
Steps: []resource.TestStep{
{
PreConfig: func() {
if err := testAccDataSourceNsxtPolicyQosProfileCreate(name); err != nil {
if err := testAccDataSourceNsxtPolicyQosProfileCreate(name, clientType); err != nil {
t.Error(err)
}
},
Expand All @@ -55,7 +60,7 @@
})
}

func testAccDataSourceNsxtPolicyQosProfileCreate(name string) error {
func testAccDataSourceNsxtPolicyQosProfileCreate(name string, clientType tf_api.ClientType) error {
connector, err := testAccGetPolicyConnector()
if err != nil {
return fmt.Errorf("Error during test client initialization: %v", err)
Expand All @@ -71,7 +76,7 @@
// Generate a random ID for the resource
id := newUUID()

client := infra.NewQosProfilesClient(testAccGetSessionContext(), connector)
client := infra.NewQosProfilesClient(testAccGetContextByType(clientType), connector)
if client == nil {
return policyResourceNotSupportedError()
}
Expand All @@ -83,14 +88,14 @@
return nil
}

func testAccDataSourceNsxtPolicyQosProfileDeleteByName(name string) error {
func testAccDataSourceNsxtPolicyQosProfileDeleteByName(name string, clientType tf_api.ClientType) error {
connector, err := testAccGetPolicyConnector()
if err != nil {
return fmt.Errorf("Error during test client initialization: %v", err)
}

// Find the object by name and delete it
client := infra.NewQosProfilesClient(testAccGetSessionContext(), connector)
client := infra.NewQosProfilesClient(testAccGetContextByType(clientType), connector)
if client == nil {
return policyResourceNotSupportedError()
}
Expand All @@ -113,7 +118,7 @@
func testAccNsxtPolicyQosProfileReadTemplate(name string, withContext bool) string {
context := ""
if withContext {
context = testAccNsxtPolicyMultitenancyContext()
context = testAccNsxtProjectContext()
}
return fmt.Sprintf(`
data "nsxt_policy_qos_profile" "test" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ data "nsxt_policy_segment_security_profile" "test" {
}

func testAccNsxtPolicySegmentSecurityProfileMultitenancyTemplate(name string) string {
context := testAccNsxtPolicyMultitenancyContext()
context := testAccNsxtProjectContext()
return fmt.Sprintf(`
resource "nsxt_policy_segment_security_profile" "test" {
%s
Expand Down
28 changes: 27 additions & 1 deletion nsxt/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1252,9 +1252,35 @@ func getContextDataFromSchema(d *schema.ResourceData) (string, string) {
return "", ""
}

func getContextDataFromParentPath(parentPath string) (string, string) {
segments := strings.Split(parentPath, "/")
var projectID, vpcID string
if len(segments) > 4 && segments[1] == "orgs" && segments[3] == "projects" {
projectID = segments[4]

if len(segments) > 6 && segments[5] == "vpcs" {
vpcID = segments[6]
}
}
return projectID, vpcID
}

func getSessionContext(d *schema.ResourceData, m interface{}) tf_api.SessionContext {
return getSessionContextHelper(d, m, "")
}

func getParentContext(d *schema.ResourceData, m interface{}, parentPath string) tf_api.SessionContext {
return getSessionContextHelper(d, m, parentPath)
}

func getSessionContextHelper(d *schema.ResourceData, m interface{}, parentPath string) tf_api.SessionContext {
var clientType tf_api.ClientType
projectID, vpcID := getContextDataFromSchema(d)
var projectID, vpcID string
if parentPath == "" {
projectID, vpcID = getContextDataFromSchema(d)
} else {
projectID, vpcID = getContextDataFromParentPath(parentPath)
}
if projectID != "" {
clientType = tf_api.Multitenancy
if vpcID != "" {
Expand Down
Loading
Loading