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

fix tenant config drift and add test cases #125

Merged
merged 9 commits into from
Sep 29, 2024
Merged
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
11 changes: 3 additions & 8 deletions pulsar/resource_pulsar_tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func resourcePulsarTenant() *schema.Resource {
Elem: &schema.Schema{Type: schema.TypeString},
},
"admin_roles": {
Type: schema.TypeList,
Type: schema.TypeSet,
Optional: true,
Description: descriptions["admin_roles"],
Elem: &schema.Schema{Type: schema.TypeString},
Expand All @@ -70,7 +70,7 @@ func resourcePulsarTenantCreate(ctx context.Context, d *schema.ResourceData, met
client := getClientFromMeta(meta).Tenants()

tenant := d.Get("tenant").(string)
adminRoles := handleHCLArray(d, "admin_roles")
adminRoles := handleHCLArrayV2(d.Get("admin_roles").(*schema.Set).List())
allowedClusters := handleHCLArrayV2(d.Get("allowed_clusters").(*schema.Set).List())

input := utils.TenantData{
Expand Down Expand Up @@ -111,7 +111,7 @@ func resourcePulsarTenantUpdate(ctx context.Context, d *schema.ResourceData, met
client := getClientFromMeta(meta).Tenants()

tenant := d.Get("tenant").(string)
adminRoles := handleHCLArray(d, "admin_roles")
adminRoles := handleHCLArrayV2(d.Get("admin_roles").(*schema.Set).List())
allowedClusters := handleHCLArrayV2(d.Get("allowed_clusters").(*schema.Set).List())

input := utils.TenantData{
Expand Down Expand Up @@ -174,11 +174,6 @@ func deleteExistingNamespacesForTenant(tenant string, meta interface{}) error {
return nil
}

func handleHCLArray(d *schema.ResourceData, key string) []string {
hclArray := d.Get(key).([]interface{})
return handleHCLArrayV2(hclArray)
}

func handleHCLArrayV2(hclArray []interface{}) []string {
out := make([]string, 0)

Expand Down
104 changes: 104 additions & 0 deletions pulsar/resource_pulsar_tenant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package pulsar
import (
"fmt"
"regexp"
"strings"
"testing"

"github.com/apache/pulsar-client-go/pulsaradmin/pkg/admin"
Expand Down Expand Up @@ -189,3 +190,106 @@ resource "pulsar_tenant" "test" {
}
`, url, tname)
}

const (
testPulsarTenantWithAdminRoles1 = "pulsar-tenant-admin-role-1"
testPulsarTenantWithAdminRoles2 = "[email protected]"
)

func TestTenantWithAdminRoles(t *testing.T) {
tName := acctest.RandString(10)
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
ProviderFactories: testAccProviderFactories,
PreventPostDestroyRefresh: false,
CheckDestroy: testPulsarTenantDestroy,
Steps: []resource.TestStep{
{
Config: strings.Replace(testPulsarTenantWithAdminRoles, "thanos", tName, 1),
Check: resource.ComposeTestCheckFunc(
testPulsarTenantExists("pulsar_tenant.test"),
resource.TestCheckResourceAttr("pulsar_tenant.test", "admin_roles.#", "2"),
resource.TestCheckTypeSetElemAttr("pulsar_tenant.test", "admin_roles.*", testPulsarTenantWithAdminRoles1),
resource.TestCheckTypeSetElemAttr("pulsar_tenant.test", "admin_roles.*", testPulsarTenantWithAdminRoles2),
),
},
},
})
}

func TestTenantUpdateAdminRoles(t *testing.T) {
tName := acctest.RandString(10)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
PreventPostDestroyRefresh: false,
CheckDestroy: testPulsarTenantDestroy,
Steps: []resource.TestStep{
{
Config: strings.Replace(testPulsarTenantWithAdminRoles, "thanos", tName, 1),
Check: resource.ComposeTestCheckFunc(
testPulsarTenantExists("pulsar_tenant.test"),
),
},
{
Config: strings.Replace(testPulsarTenantWithAdminRolesUpdated, "thanos", tName, 1),
Check: resource.ComposeTestCheckFunc(
testPulsarTenantExists("pulsar_tenant.test"),
resource.TestCheckResourceAttr("pulsar_tenant.test", "admin_roles.#", "2"),
resource.TestCheckTypeSetElemAttr("pulsar_tenant.test", "admin_roles.*", testPulsarTenantWithAdminRoles1),
resource.TestCheckTypeSetElemAttr("pulsar_tenant.test", "admin_roles.*", testPulsarTenantWithAdminRoles2),
),
},
},
})
}

func TestTenantAdminRolesDrift(t *testing.T) {
tName := acctest.RandString(10)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
PreventPostDestroyRefresh: false,
CheckDestroy: testPulsarTenantDestroy,
Steps: []resource.TestStep{
{
Config: strings.Replace(testPulsarTenantWithAdminRoles, "thanos", tName, 1),
Check: resource.ComposeTestCheckFunc(
testPulsarTenantExists("pulsar_tenant.test"),
resource.TestCheckResourceAttr("pulsar_tenant.test", "admin_roles.#", "2"),
resource.TestCheckTypeSetElemAttr("pulsar_tenant.test", "admin_roles.*", testPulsarTenantWithAdminRoles1),
resource.TestCheckTypeSetElemAttr("pulsar_tenant.test", "admin_roles.*", testPulsarTenantWithAdminRoles2),
),
},
{
Config: strings.Replace(testPulsarTenantWithAdminRoles, "thanos", tName, 1),
PlanOnly: true,
ExpectNonEmptyPlan: false,
},
},
})
}

var testPulsarTenantWithAdminRoles = fmt.Sprintf(`
provider "pulsar" {
web_service_url = "%s"
}

resource "pulsar_tenant" "test" {
tenant = "thanos"
allowed_clusters = ["standalone"]
admin_roles = ["%s", "%s"]
}`, testWebServiceURL, testPulsarTenantWithAdminRoles1, testPulsarTenantWithAdminRoles2)

var testPulsarTenantWithAdminRolesUpdated = fmt.Sprintf(`
provider "pulsar" {
web_service_url = "%s"
}

resource "pulsar_tenant" "test" {
tenant = "thanos"
allowed_clusters = ["standalone"]
admin_roles = ["%s", "%s"]
}`, testWebServiceURL, testPulsarTenantWithAdminRoles2, testPulsarTenantWithAdminRoles1)
Loading