Skip to content

Commit

Permalink
Role tests: Use OpenAPI client
Browse files Browse the repository at this point in the history
Gets rid of the manually written checkers in favor of the new ones that use the OpenAPI client
  • Loading branch information
julienduchesne committed Nov 9, 2023
1 parent 392523e commit 7f445a7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 62 deletions.
9 changes: 9 additions & 0 deletions internal/resources/grafana/common_check_exists_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

gapi "github.com/grafana/grafana-api-golang-client"
goapi "github.com/grafana/grafana-openapi-client-go/client"
"github.com/grafana/grafana-openapi-client-go/client/access_control"
"github.com/grafana/grafana-openapi-client-go/client/annotations"
"github.com/grafana/grafana-openapi-client-go/client/datasources"
"github.com/grafana/grafana-openapi-client-go/client/folders"
Expand Down Expand Up @@ -56,6 +57,14 @@ var (
return payloadOrError(resp, err)
},
)
roleCheckExists = newCheckExistsHelper(
func(r *models.RoleDTO) string { return r.UID },
func(client *goapi.GrafanaHTTPAPI, id string) (*models.RoleDTO, error) {
params := access_control.NewGetRoleParams().WithRoleUID(id)
resp, err := client.AccessControl.GetRole(params, nil)
return payloadOrError(resp, err)
},
)
serviceAccountCheckExists = newCheckExistsHelper(
func(t *models.ServiceAccountDTO) string { return strconv.FormatInt(t.ID, 10) },
func(client *goapi.GrafanaHTTPAPI, id string) (*models.ServiceAccountDTO, error) {
Expand Down
8 changes: 4 additions & 4 deletions internal/resources/grafana/data_source_role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ package grafana_test
import (
"testing"

gapi "github.com/grafana/grafana-api-golang-client"
"github.com/grafana/grafana-openapi-client-go/models"
"github.com/grafana/terraform-provider-grafana/internal/testutils"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccDatasourceRole_basic(t *testing.T) {
testutils.CheckEnterpriseTestsEnabled(t)

var role gapi.Role
var role models.RoleDTO
checks := []resource.TestCheckFunc{
testAccRoleCheckExists("grafana_role.test", &role),
roleCheckExists.exists("grafana_role.test", &role),
resource.TestCheckResourceAttr("data.grafana_role.from_name", "name", "test-role"),
resource.TestCheckResourceAttr("data.grafana_role.from_name", "description", "test-role description"),
resource.TestCheckResourceAttr("data.grafana_role.from_name", "uid", "test-ds-role-uid"),
Expand All @@ -30,7 +30,7 @@ func TestAccDatasourceRole_basic(t *testing.T) {

resource.ParallelTest(t, resource.TestCase{
ProviderFactories: testutils.ProviderFactories,
CheckDestroy: testAccRoleCheckDestroy(&role),
CheckDestroy: roleCheckExists.destroyed(&role, nil),
Steps: []resource.TestStep{
{
Config: testutils.TestAccExample(t, "data-sources/grafana_role/data-source.tf"),
Expand Down
69 changes: 11 additions & 58 deletions internal/resources/grafana/resource_role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,24 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"

gapi "github.com/grafana/grafana-api-golang-client"
"github.com/grafana/terraform-provider-grafana/internal/common"
"github.com/grafana/terraform-provider-grafana/internal/resources/grafana"
"github.com/grafana/grafana-openapi-client-go/models"
"github.com/grafana/terraform-provider-grafana/internal/testutils"
)

func TestAccRole_basic(t *testing.T) {
testutils.CheckEnterpriseTestsEnabled(t)

var role gapi.Role
var role models.RoleDTO

resource.ParallelTest(t, resource.TestCase{
ProviderFactories: testutils.ProviderFactories,
CheckDestroy: testAccRoleCheckDestroy(&role),
CheckDestroy: roleCheckExists.destroyed(&role, nil),
Steps: []resource.TestStep{
{
Config: roleConfigBasic,
Check: resource.ComposeTestCheckFunc(
testAccRoleCheckExists("grafana_role.test", &role),
roleCheckExists.exists("grafana_role.test", &role),
resource.TestCheckResourceAttr("grafana_role.test", "name", "terraform-acc-test"),
resource.TestCheckResourceAttr("grafana_role.test", "description", "test desc"),
resource.TestCheckResourceAttr("grafana_role.test", "display_name", "testdisplay"),
Expand All @@ -40,7 +37,7 @@ func TestAccRole_basic(t *testing.T) {
{
Config: roleConfigWithPermissions,
Check: resource.ComposeTestCheckFunc(
testAccRoleCheckExists("grafana_role.test", &role),
roleCheckExists.exists("grafana_role.test", &role),
resource.TestCheckResourceAttr("grafana_role.test", "name", "terraform-acc-test"),
resource.TestCheckResourceAttr("grafana_role.test", "description", "test desc"),
resource.TestCheckResourceAttr("grafana_role.test", "display_name", "testdisplay"),
Expand All @@ -62,12 +59,12 @@ func TestAccRole_basic(t *testing.T) {
func TestAccRoleVersioning(t *testing.T) {
testutils.CheckEnterpriseTestsEnabled(t)

var role gapi.Role
var role models.RoleDTO
name := acctest.RandomWithPrefix("versioning-")

resource.ParallelTest(t, resource.TestCase{
ProviderFactories: testutils.ProviderFactories,
CheckDestroy: testAccRoleCheckDestroy(&role),
CheckDestroy: roleCheckExists.destroyed(&role, nil),
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
Expand All @@ -77,7 +74,7 @@ func TestAccRoleVersioning(t *testing.T) {
auto_increment_version = true
}`, name),
Check: resource.ComposeTestCheckFunc(
testAccRoleCheckExists("grafana_role.test", &role),
roleCheckExists.exists("grafana_role.test", &role),
resource.TestCheckResourceAttr("grafana_role.test", "name", name),
resource.TestCheckResourceAttr("grafana_role.test", "version", "1"),
),
Expand All @@ -90,7 +87,7 @@ func TestAccRoleVersioning(t *testing.T) {
version = 5
}`, name),
Check: resource.ComposeTestCheckFunc(
testAccRoleCheckExists("grafana_role.test", &role),
roleCheckExists.exists("grafana_role.test", &role),
resource.TestCheckResourceAttr("grafana_role.test", "name", name),
resource.TestCheckResourceAttr("grafana_role.test", "version", "5"),
),
Expand All @@ -103,7 +100,7 @@ func TestAccRoleVersioning(t *testing.T) {
auto_increment_version = true
}`, name),
Check: resource.ComposeTestCheckFunc(
testAccRoleCheckExists("grafana_role.test", &role),
roleCheckExists.exists("grafana_role.test", &role),
resource.TestCheckResourceAttr("grafana_role.test", "name", name),
resource.TestCheckResourceAttr("grafana_role.test", "version", "6"),
),
Expand All @@ -116,7 +113,7 @@ func TestAccRoleVersioning(t *testing.T) {
auto_increment_version = true
}`, name),
Check: resource.ComposeTestCheckFunc(
testAccRoleCheckExists("grafana_role.test", &role),
roleCheckExists.exists("grafana_role.test", &role),
resource.TestCheckResourceAttr("grafana_role.test", "name", name),
resource.TestCheckResourceAttr("grafana_role.test", "version", "7"),
),
Expand All @@ -125,50 +122,6 @@ func TestAccRoleVersioning(t *testing.T) {
})
}

func testAccRoleCheckExists(rn string, r *gapi.Role) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[rn]
if !ok {
return fmt.Errorf("resource not found: %s", rn)
}

if rs.Primary.ID == "" {
return fmt.Errorf("resource id not set")
}
orgID, roleUID := grafana.SplitOrgResourceID(rs.Primary.ID)
client := testutils.Provider.Meta().(*common.Client).GrafanaAPI

// If the org ID is set, check that the report doesn't exist in the default org
if orgID > 1 {
role, err := client.GetRole(roleUID)
if err == nil || role != nil {
return fmt.Errorf("expected no role with ID %s in default org but found one", roleUID)
}
client = client.WithOrgID(orgID)
}

role, err := client.GetRole(roleUID)
if err != nil {
return fmt.Errorf("error getting role: %s", err)
}

*r = *role

return nil
}
}

func testAccRoleCheckDestroy(r *gapi.Role) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := testutils.Provider.Meta().(*common.Client).GrafanaAPI
role, err := client.GetRole(r.UID)
if err == nil && role.Name != "" {
return fmt.Errorf("role still exists")
}
return nil
}
}

const roleConfigBasic = `
resource "grafana_role" "test" {
name = "terraform-acc-test"
Expand Down

0 comments on commit 7f445a7

Please sign in to comment.