Skip to content

Commit

Permalink
fix tenant config drift and add test cases (#125)
Browse files Browse the repository at this point in the history
* fix tenant drift and add test cases

* fix lint

* fix hcl array

* fix hcl array

* fix ci

* fix ci

* fix lint

* fix ci

* fix lint
  • Loading branch information
freeznet authored Sep 29, 2024
1 parent 9f30205 commit ded6267
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 8 deletions.
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)

0 comments on commit ded6267

Please sign in to comment.