diff --git a/nsxt/resource_nsxt_policy_share_test.go b/nsxt/resource_nsxt_policy_share_test.go index 129d2bb92..bedff8915 100644 --- a/nsxt/resource_nsxt_policy_share_test.go +++ b/nsxt/resource_nsxt_policy_share_test.go @@ -5,6 +5,7 @@ package nsxt import ( "fmt" + "os" "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" @@ -91,6 +92,48 @@ func TestAccResourceNsxtPolicyShare_importBasic(t *testing.T) { }) } +func TestAccResourceNsxtPolicyShare_multitenancy(t *testing.T) { + testResourceName := "nsxt_policy_share.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheck(t) + testAccOnlyMultitenancy(t) + testAccNSXVersion(t, "4.1.1") + }, + Providers: testAccProviders, + CheckDestroy: func(state *terraform.State) error { + return testAccNsxtPolicyShareCheckDestroy(state, accTestPolicyShareUpdateAttributes["display_name"]) + }, + Steps: []resource.TestStep{ + { + Config: testAccNsxtPolicyShareWithMyselfTemplate(true), + Check: resource.ComposeTestCheckFunc( + testAccNsxtPolicyShareExists(accTestPolicyShareCreateAttributes["display_name"], testResourceName), + resource.TestCheckResourceAttr(testResourceName, "display_name", accTestPolicyShareCreateAttributes["display_name"]), + resource.TestCheckResourceAttr(testResourceName, "description", accTestPolicyShareCreateAttributes["description"]), + resource.TestCheckResourceAttrSet(testResourceName, "nsx_id"), + resource.TestCheckResourceAttrSet(testResourceName, "path"), + resource.TestCheckResourceAttrSet(testResourceName, "revision"), + resource.TestCheckResourceAttr(testResourceName, "tag.#", "0"), + ), + }, + { + Config: testAccNsxtPolicyShareWithMyselfTemplate(false), + Check: resource.ComposeTestCheckFunc( + testAccNsxtPolicyShareExists(accTestPolicyShareUpdateAttributes["display_name"], testResourceName), + resource.TestCheckResourceAttr(testResourceName, "display_name", accTestPolicyShareUpdateAttributes["display_name"]), + resource.TestCheckResourceAttr(testResourceName, "description", accTestPolicyShareUpdateAttributes["description"]), + resource.TestCheckResourceAttrSet(testResourceName, "nsx_id"), + resource.TestCheckResourceAttrSet(testResourceName, "path"), + resource.TestCheckResourceAttrSet(testResourceName, "revision"), + resource.TestCheckResourceAttr(testResourceName, "tag.#", "0"), + ), + }, + }, + }) +} + func testAccNsxtPolicyShareExists(displayName string, resourceName string) resource.TestCheckFunc { return func(state *terraform.State) error { @@ -157,3 +200,28 @@ resource "nsxt_policy_share" "test" { } }`, attrMap["display_name"], attrMap["description"]) } + +func testAccNsxtPolicyShareWithMyselfTemplate(createFlow bool) string { + var attrMap map[string]string + if createFlow { + attrMap = accTestPolicyShareCreateAttributes + } else { + attrMap = accTestPolicyShareUpdateAttributes + } + projectID := os.Getenv("NSXT_PROJECT_ID") + return fmt.Sprintf(` +data "nsxt_policy_project" "test" { + id = "%s" +} + +resource "nsxt_policy_share" "test" { + context { + project_id = data.nsxt_policy_project.test.id + } + display_name = "%s" + description = "%s" + + sharing_strategy = "ALL_DESCENDANTS" + shared_with = [data.nsxt_policy_project.test.path] +}`, projectID, attrMap["display_name"], attrMap["description"]) +} diff --git a/nsxt/resource_nsxt_policy_shared_resource.go b/nsxt/resource_nsxt_policy_shared_resource.go index d2f3ac5f8..1b357d660 100644 --- a/nsxt/resource_nsxt_policy_shared_resource.go +++ b/nsxt/resource_nsxt_policy_shared_resource.go @@ -28,7 +28,6 @@ func resourceNsxtPolicySharedResource() *schema.Resource { "description": getDescriptionSchema(), "revision": getRevisionSchema(), "tag": getTagsSchema(), - "context": getContextSchema(false, false, false), "share_path": { Type: schema.TypeString, Required: true, @@ -64,7 +63,9 @@ func resourceNsxtPolicySharedResourceCreate(d *schema.ResourceData, m interface{ id := newUUID() connector := getPolicyConnector(m) - shareID := getPolicyIDFromPath(d.Get("share_path").(string)) + sharePath := d.Get("share_path").(string) + shareID := getPolicyIDFromPath(sharePath) + context := getParentContext(d, m, sharePath) displayName := d.Get("display_name").(string) description := d.Get("description").(string) tags := getPolicyTagsFromSchema(d) @@ -85,7 +86,6 @@ func resourceNsxtPolicySharedResourceCreate(d *schema.ResourceData, m interface{ Tags: tags, ResourceObjects: resourceObjects, } - context := getSessionContext(d, m) client := shares.NewResourcesClient(context, connector) err := client.Patch(shareID, id, obj) if err != nil { @@ -106,8 +106,9 @@ func resourceNsxtPolicySharedResourceRead(d *schema.ResourceData, m interface{}) return fmt.Errorf("error obtaining Shared Resource ID") } - shareID := getPolicyIDFromPath(d.Get("share_path").(string)) - context := getSessionContext(d, m) + sharePath := d.Get("share_path").(string) + shareID := getPolicyIDFromPath(sharePath) + context := getParentContext(d, m, sharePath) client := shares.NewResourcesClient(context, connector) obj, err := client.Get(shareID, id) if err != nil { @@ -137,7 +138,9 @@ func resourceNsxtPolicySharedResourceUpdate(d *schema.ResourceData, m interface{ id := d.Id() connector := getPolicyConnector(m) - shareID := getPolicyIDFromPath(d.Get("share_path").(string)) + sharePath := d.Get("share_path").(string) + shareID := getPolicyIDFromPath(sharePath) + context := getParentContext(d, m, sharePath) displayName := d.Get("display_name").(string) description := d.Get("description").(string) tags := getPolicyTagsFromSchema(d) @@ -158,7 +161,6 @@ func resourceNsxtPolicySharedResourceUpdate(d *schema.ResourceData, m interface{ Tags: tags, ResourceObjects: resourceObjects, } - context := getSessionContext(d, m) client := shares.NewResourcesClient(context, connector) err := client.Patch(shareID, id, obj) if err != nil { @@ -176,10 +178,11 @@ func resourceNsxtPolicySharedResourceDelete(d *schema.ResourceData, m interface{ if id == "" { return fmt.Errorf("error obtaining Shared Resource ID") } - shareID := getPolicyIDFromPath(d.Get("share_path").(string)) + sharePath := d.Get("share_path").(string) + shareID := getPolicyIDFromPath(sharePath) connector := getPolicyConnector(m) - context := getSessionContext(d, m) + context := getParentContext(d, m, sharePath) client := shares.NewResourcesClient(context, connector) err := client.Delete(shareID, id) diff --git a/nsxt/resource_nsxt_policy_shared_resource_test.go b/nsxt/resource_nsxt_policy_shared_resource_test.go index ab0a60e9d..cc73fa004 100644 --- a/nsxt/resource_nsxt_policy_shared_resource_test.go +++ b/nsxt/resource_nsxt_policy_shared_resource_test.go @@ -28,7 +28,7 @@ func TestAccResourceNsxtPolicySharedResource_basic(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) - testAccOnlyLocalManager(t) + testAccNotGlobalManager(t) testAccNSXVersion(t, "4.1.1") }, Providers: testAccProviders, @@ -64,6 +64,48 @@ func TestAccResourceNsxtPolicySharedResource_basic(t *testing.T) { }) } +func TestAccResourceNsxtPolicySharedResource_multitenancy(t *testing.T) { + testResourceName := "nsxt_policy_shared_resource.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheck(t) + testAccOnlyMultitenancy(t) + testAccNSXVersion(t, "4.1.1") + }, + Providers: testAccProviders, + CheckDestroy: func(state *terraform.State) error { + return testAccNsxtPolicySharedResourceCheckDestroy(state, accTestPolicySharedResourceUpdateAttributes["display_name"]) + }, + Steps: []resource.TestStep{ + { + Config: testAccNsxtPolicySharedResourceMultitenancyTemplate(true), + Check: resource.ComposeTestCheckFunc( + testAccNsxtPolicySharedResourceExists(accTestPolicySharedResourceCreateAttributes["display_name"], testResourceName), + resource.TestCheckResourceAttr(testResourceName, "display_name", accTestPolicySharedResourceCreateAttributes["display_name"]), + resource.TestCheckResourceAttr(testResourceName, "description", accTestPolicySharedResourceCreateAttributes["description"]), + resource.TestCheckResourceAttrSet(testResourceName, "nsx_id"), + resource.TestCheckResourceAttrSet(testResourceName, "path"), + resource.TestCheckResourceAttrSet(testResourceName, "revision"), + resource.TestCheckResourceAttr(testResourceName, "tag.#", "0"), + ), + }, + { + Config: testAccNsxtPolicySharedResourceMultitenancyTemplate(false), + Check: resource.ComposeTestCheckFunc( + testAccNsxtPolicySharedResourceExists(accTestPolicySharedResourceUpdateAttributes["display_name"], testResourceName), + resource.TestCheckResourceAttr(testResourceName, "display_name", accTestPolicySharedResourceUpdateAttributes["display_name"]), + resource.TestCheckResourceAttr(testResourceName, "description", accTestPolicySharedResourceUpdateAttributes["description"]), + resource.TestCheckResourceAttrSet(testResourceName, "nsx_id"), + resource.TestCheckResourceAttrSet(testResourceName, "path"), + resource.TestCheckResourceAttrSet(testResourceName, "revision"), + resource.TestCheckResourceAttr(testResourceName, "tag.#", "0"), + ), + }, + }, + }) +} + func TestAccResourceNsxtPolicySharedResource_importBasic(t *testing.T) { name := getAccTestResourceName() testResourceName := "nsxt_policy_shared_resource.test" @@ -71,7 +113,7 @@ func TestAccResourceNsxtPolicySharedResource_importBasic(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) - testAccOnlyLocalManager(t) + testAccNotGlobalManager(t) testAccNSXVersion(t, "4.1.1") }, Providers: testAccProviders, @@ -169,3 +211,22 @@ resource "nsxt_policy_shared_resource" "test" { } }`, attrMap["display_name"], attrMap["description"]) } + +func testAccNsxtPolicySharedResourceMultitenancyTemplate(createFlow bool) string { + var attrMap map[string]string + if createFlow { + attrMap = accTestPolicySharedResourceCreateAttributes + } else { + attrMap = accTestPolicySharedResourceUpdateAttributes + } + return testAccNsxtPolicyShareWithMyselfTemplate(true) + testAccNsxtPolicyContextProfileReadTemplate("AMQP") + fmt.Sprintf(` +resource "nsxt_policy_shared_resource" "test" { + display_name = "%s" + description = "%s" + + share_path = nsxt_policy_share.test.path + resource_object { + resource_path = data.nsxt_policy_context_profile.test.path + } +}`, attrMap["display_name"], attrMap["description"]) +} diff --git a/nsxt/utils_test.go b/nsxt/utils_test.go index c8b579f67..b00aec437 100644 --- a/nsxt/utils_test.go +++ b/nsxt/utils_test.go @@ -287,6 +287,12 @@ func testAccIsGlobalManager2() tf_api.ClientType { return tf_api.Local } +func testAccNotGlobalManager(t *testing.T) { + if testAccIsGlobalManager() { + t.Skipf("This test requires a global manager environment") + } +} + func testAccOnlyGlobalManager(t *testing.T) { if !testAccIsGlobalManager() { t.Skipf("This test requires a global manager environment") @@ -845,7 +851,6 @@ resource "nsxt_policy_share" "test" { } resource "nsxt_policy_shared_resource" "test" { -%s display_name = "%s" share_path = nsxt_policy_share.test.path @@ -853,5 +858,5 @@ resource "nsxt_policy_shared_resource" "test" { resource_path = %s include_children = true } -}`, context, name, projectPath, context, name, sharedResourcePath) +}`, context, name, projectPath, name, sharedResourcePath) } diff --git a/website/docs/r/policy_shared_resource.html.markdown b/website/docs/r/policy_shared_resource.html.markdown index daeed89e8..c0b733657 100644 --- a/website/docs/r/policy_shared_resource.html.markdown +++ b/website/docs/r/policy_shared_resource.html.markdown @@ -33,8 +33,6 @@ The following arguments are supported: * `description` - (Optional) Description of the resource. * `tag` - (Optional) A list of scope + tag pairs to associate with this resource. * `nsx_id` - (Optional) The NSX ID of this resource. If set, this ID will be used to create the resource. -* `context` - (Optional) The context which the object belongs to - * `project_id` - (Required) The ID of the project which the object belongs to * `share_path` - (Required) Share policy path to associate the resource to. * `resource_object` _ (Required) List of resources to be shared. * `include_children` - (Optional) Denotes if the children of the shared path are also shared.