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

feat(cockpit): migrate grafana user to v1 #2548

Merged
merged 2 commits into from
Apr 22, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 4 additions & 5 deletions docs/resources/cockpit_grafana_user.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ For more information consult the [documentation](https://www.scaleway.com/en/doc
## Example Usage

```terraform
// Get the cockpit of the default project
data "scaleway_cockpit" "main" {}
resource "scaleway_account_project" "project" {
name = "test project grafana user"
}

// Create an editor grafana user for the cockpit
resource "scaleway_cockpit_grafana_user" "main" {
project_id = data.scaleway_cockpit.main.project_id

project_id = scaleway_account_project.project.id
login = "my-awesome-user"
role = "editor"
}
Expand Down
38 changes: 5 additions & 33 deletions internal/services/cockpit/grafana_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
cockpit "github.com/scaleway/scaleway-sdk-go/api/cockpit/v1beta1"
"github.com/scaleway/scaleway-sdk-go/api/cockpit/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
Expand Down Expand Up @@ -58,7 +58,7 @@ func ResourceCockpitGrafanaUser() *schema.Resource {
}

func ResourceCockpitGrafanaUserCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api, err := NewAPI(m)
api, err := NewGlobalAPI(m)
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -67,14 +67,7 @@ func ResourceCockpitGrafanaUserCreate(ctx context.Context, d *schema.ResourceDat
login := d.Get("login").(string)
role := cockpit.GrafanaUserRole(d.Get("role").(string))

_, err = api.WaitForCockpit(&cockpit.WaitForCockpitRequest{
ProjectID: projectID,
}, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
}

grafanaUser, err := api.CreateGrafanaUser(&cockpit.CreateGrafanaUserRequest{
grafanaUser, err := api.CreateGrafanaUser(&cockpit.GlobalAPICreateGrafanaUserRequest{
ProjectID: projectID,
Login: login,
Role: role,
Expand All @@ -94,18 +87,7 @@ func ResourceCockpitGrafanaUserRead(ctx context.Context, d *schema.ResourceData,
return diag.FromErr(err)
}

_, err = api.WaitForCockpit(&cockpit.WaitForCockpitRequest{
ProjectID: projectID,
}, scw.WithContext(ctx))
if err != nil {
if httperrors.Is404(err) {
d.SetId("")
return nil
}
return diag.FromErr(err)
}

res, err := api.ListGrafanaUsers(&cockpit.ListGrafanaUsersRequest{
res, err := api.ListGrafanaUsers(&cockpit.GlobalAPIListGrafanaUsersRequest{
ProjectID: projectID,
}, scw.WithContext(ctx), scw.WithAllPages())
if err != nil {
Expand Down Expand Up @@ -142,17 +124,7 @@ func ResourceCockpitGrafanaUserDelete(ctx context.Context, d *schema.ResourceDat
return diag.FromErr(err)
}

_, err = api.WaitForCockpit(&cockpit.WaitForCockpitRequest{
ProjectID: projectID,
}, scw.WithContext(ctx))
if err != nil {
if httperrors.Is404(err) {
return nil
}
return diag.FromErr(err)
}

err = api.DeleteGrafanaUser(&cockpit.DeleteGrafanaUserRequest{
err = api.DeleteGrafanaUser(&cockpit.GlobalAPIDeleteGrafanaUserRequest{
ProjectID: projectID,
GrafanaUserID: grafanaUserID,
}, scw.WithContext(ctx))
Expand Down
63 changes: 17 additions & 46 deletions internal/services/cockpit/grafana_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package cockpit_test

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
cockpitSDK "github.com/scaleway/scaleway-sdk-go/api/cockpit/v1beta1"
cockpitSDK "github.com/scaleway/scaleway-sdk-go/api/cockpit/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
Expand All @@ -32,24 +31,27 @@ func TestAccGrafanaUser_Basic(t *testing.T) {
name = "%[1]s"
}

resource scaleway_cockpit main {
project_id = scaleway_account_project.project.id
}

resource scaleway_cockpit_grafana_user main {
project_id = scaleway_cockpit.main.project_id
project_id = scaleway_account_project.project.id
login = "%[2]s"
role = "editor"
}
`, projectName, grafanaTestUsername),
Check: resource.ComposeTestCheckFunc(
isGrafanaUserPresent(tt, "scaleway_cockpit_grafana_user.main"),
resource.TestCheckResourceAttrPair("scaleway_cockpit_grafana_user.main", "project_id", "scaleway_cockpit.main", "project_id"),
resource.TestCheckResourceAttrPair("scaleway_cockpit_grafana_user.main", "project_id", "scaleway_account_project.project", "id"),
resource.TestCheckResourceAttr("scaleway_cockpit_grafana_user.main", "login", grafanaTestUsername),
resource.TestCheckResourceAttr("scaleway_cockpit_grafana_user.main", "role", "editor"),
resource.TestCheckResourceAttrSet("scaleway_cockpit_grafana_user.main", "password"),
),
},
{
Config: fmt.Sprintf(`
resource "scaleway_account_project" "project" {
name = "%[1]s"
}
`, projectName),
},
},
})
}
Expand All @@ -72,19 +74,15 @@ func TestAccGrafanaUser_Update(t *testing.T) {
name = "%[1]s"
}

resource scaleway_cockpit main {
project_id = scaleway_account_project.project.id
}

resource scaleway_cockpit_grafana_user main {
project_id = scaleway_cockpit.main.project_id
project_id = scaleway_account_project.project.id
login = "%[2]s"
role = "editor"
}
`, projectName, grafanaTestUsername),
Check: resource.ComposeTestCheckFunc(
isGrafanaUserPresent(tt, "scaleway_cockpit_grafana_user.main"),
resource.TestCheckResourceAttrPair("scaleway_cockpit_grafana_user.main", "project_id", "scaleway_cockpit.main", "project_id"),
resource.TestCheckResourceAttrPair("scaleway_cockpit_grafana_user.main", "project_id", "scaleway_account_project.project", "id"),
resource.TestCheckResourceAttr("scaleway_cockpit_grafana_user.main", "login", grafanaTestUsername),
resource.TestCheckResourceAttr("scaleway_cockpit_grafana_user.main", "role", "editor"),
resource.TestCheckResourceAttrSet("scaleway_cockpit_grafana_user.main", "password"),
Expand All @@ -96,53 +94,26 @@ func TestAccGrafanaUser_Update(t *testing.T) {
name = "%[1]s"
}

resource scaleway_cockpit main {
project_id = scaleway_account_project.project.id
}

resource scaleway_cockpit_grafana_user main {
project_id = scaleway_cockpit.main.project_id
project_id = scaleway_account_project.project.id
login = "%[2]s"
role = "viewer"
}
`, projectName, grafanaTestUsername),
Check: resource.ComposeTestCheckFunc(
isGrafanaUserPresent(tt, "scaleway_cockpit_grafana_user.main"),
resource.TestCheckResourceAttrPair("scaleway_cockpit_grafana_user.main", "project_id", "scaleway_cockpit.main", "project_id"),
resource.TestCheckResourceAttrPair("scaleway_cockpit_grafana_user.main", "project_id", "scaleway_account_project.project", "id"),
resource.TestCheckResourceAttr("scaleway_cockpit_grafana_user.main", "login", grafanaTestUsername),
resource.TestCheckResourceAttr("scaleway_cockpit_grafana_user.main", "role", "viewer"),
resource.TestCheckResourceAttrSet("scaleway_cockpit_grafana_user.main", "password"),
),
},
},
})
}

func TestAccGrafanaUser_NonExistentCockpit(t *testing.T) {
tt := acctest.NewTestTools(t)
defer tt.Cleanup()

projectName := "tf_tests_cockpit_grafana_user_non_existent_cockpit"
grafanaTestUsername := "testnonexistentuser"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ProviderFactories: tt.ProviderFactories,
CheckDestroy: isGrafanaUserDestroyed(tt),
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
resource "scaleway_account_project" "project" {
name = "%[1]s"
}

resource scaleway_cockpit_grafana_user main {
project_id = scaleway_account_project.project.id
login = "%[2]s"
role = "editor"
}
`, projectName, grafanaTestUsername),
ExpectError: regexp.MustCompile("not found"),
`, projectName),
},
},
})
Expand All @@ -160,7 +131,7 @@ func isGrafanaUserPresent(tt *acctest.TestTools, n string) resource.TestCheckFun
return err
}

res, err := api.ListGrafanaUsers(&cockpitSDK.ListGrafanaUsersRequest{
res, err := api.ListGrafanaUsers(&cockpitSDK.GlobalAPIListGrafanaUsersRequest{
ProjectID: projectID,
}, scw.WithAllPages())
if err != nil {
Expand Down Expand Up @@ -195,7 +166,7 @@ func isGrafanaUserDestroyed(tt *acctest.TestTools) resource.TestCheckFunc {
return err
}

err = api.DeleteGrafanaUser(&cockpitSDK.DeleteGrafanaUserRequest{
err = api.DeleteGrafanaUser(&cockpitSDK.GlobalAPIDeleteGrafanaUserRequest{
ProjectID: projectID,
GrafanaUserID: grafanaUserID,
})
Expand Down
11 changes: 9 additions & 2 deletions internal/services/cockpit/helpers_cockpit.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ func NewAPI(m interface{}) (*cockpitv1beta1.API, error) {
return api, nil
}

// NewGlobalAPI returns a new global cockpit API.
func NewGlobalAPI(m interface{}) (*cockpit.GlobalAPI, error) {
api := cockpit.NewGlobalAPI(meta.ExtractScwClient(m))

return api, nil
}

func cockpitAPIWithRegion(d *schema.ResourceData, m interface{}) (*cockpit.RegionalAPI, scw.Region, error) {
api := cockpit.NewRegionalAPI(meta.ExtractScwClient(m))

Expand All @@ -51,7 +58,7 @@ func NewAPIWithRegionAndID(m interface{}, id string) (*cockpit.RegionalAPI, scw.
}

// NewAPIGrafanaUserID returns a new cockpit API with the Grafana user ID and the project ID.
func NewAPIGrafanaUserID(m interface{}, id string) (*cockpitv1beta1.API, string, uint32, error) {
func NewAPIGrafanaUserID(m interface{}, id string) (*cockpit.GlobalAPI, string, uint32, error) {
projectID, resourceIDString, err := parseCockpitID(id)
if err != nil {
return nil, "", 0, err
Expand All @@ -62,7 +69,7 @@ func NewAPIGrafanaUserID(m interface{}, id string) (*cockpitv1beta1.API, string,
return nil, "", 0, err
}

api, err := NewAPI(m)
api, err := NewGlobalAPI(m)
if err != nil {
return nil, "", 0, err
}
Expand Down