Skip to content

Commit

Permalink
Remove context block from shared resource
Browse files Browse the repository at this point in the history
Context is determined fromthe parent share_path
For consistency with other resources, we don't expose the
context block (which provides redundant info) when parent
path is present and required.

Signed-off-by: Anna Khmelnitsky <[email protected]>
  • Loading branch information
annakhm committed Sep 11, 2024
1 parent e9e70bb commit 32340df
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 15 deletions.
68 changes: 68 additions & 0 deletions nsxt/resource_nsxt_policy_share_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package nsxt

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
Expand Down Expand Up @@ -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 {

Expand Down Expand Up @@ -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"])
}
21 changes: 12 additions & 9 deletions nsxt/resource_nsxt_policy_shared_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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)

Expand Down
65 changes: 63 additions & 2 deletions nsxt/resource_nsxt_policy_shared_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -64,14 +64,56 @@ 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"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccOnlyLocalManager(t)
testAccNotGlobalManager(t)
testAccNSXVersion(t, "4.1.1")
},
Providers: testAccProviders,
Expand Down Expand Up @@ -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"])
}
9 changes: 7 additions & 2 deletions nsxt/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -845,13 +851,12 @@ resource "nsxt_policy_share" "test" {
}
resource "nsxt_policy_shared_resource" "test" {
%s
display_name = "%s"
share_path = nsxt_policy_share.test.path
resource_object {
resource_path = %s
include_children = true
}
}`, context, name, projectPath, context, name, sharedResourcePath)
}`, context, name, projectPath, name, sharedResourcePath)
}
2 changes: 0 additions & 2 deletions website/docs/r/policy_shared_resource.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 32340df

Please sign in to comment.