Skip to content

Commit 15ccb25

Browse files
authored
feat(cockpit): migrate grafana user to v1 (scaleway#2548)
* feat(cockpit): migrate grafana user to v1 * update test and cassettes
1 parent 3c82f2f commit 15ccb25

File tree

8 files changed

+1606
-1998
lines changed

8 files changed

+1606
-1998
lines changed

docs/resources/cockpit_grafana_user.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@ For more information consult the [documentation](https://www.scaleway.com/en/doc
1212
## Example Usage
1313

1414
```terraform
15-
// Get the cockpit of the default project
16-
data "scaleway_cockpit" "main" {}
15+
resource "scaleway_account_project" "project" {
16+
name = "test project grafana user"
17+
}
1718
18-
// Create an editor grafana user for the cockpit
1919
resource "scaleway_cockpit_grafana_user" "main" {
20-
project_id = data.scaleway_cockpit.main.project_id
21-
20+
project_id = scaleway_account_project.project.id
2221
login = "my-awesome-user"
2322
role = "editor"
2423
}

internal/services/cockpit/grafana_user.go

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
99
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1010
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
11-
cockpit "github.com/scaleway/scaleway-sdk-go/api/cockpit/v1beta1"
11+
"github.com/scaleway/scaleway-sdk-go/api/cockpit/v1"
1212
"github.com/scaleway/scaleway-sdk-go/scw"
1313
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
1414
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
@@ -58,7 +58,7 @@ func ResourceCockpitGrafanaUser() *schema.Resource {
5858
}
5959

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

70-
_, err = api.WaitForCockpit(&cockpit.WaitForCockpitRequest{
71-
ProjectID: projectID,
72-
}, scw.WithContext(ctx))
73-
if err != nil {
74-
return diag.FromErr(err)
75-
}
76-
77-
grafanaUser, err := api.CreateGrafanaUser(&cockpit.CreateGrafanaUserRequest{
70+
grafanaUser, err := api.CreateGrafanaUser(&cockpit.GlobalAPICreateGrafanaUserRequest{
7871
ProjectID: projectID,
7972
Login: login,
8073
Role: role,
@@ -94,18 +87,7 @@ func ResourceCockpitGrafanaUserRead(ctx context.Context, d *schema.ResourceData,
9487
return diag.FromErr(err)
9588
}
9689

97-
_, err = api.WaitForCockpit(&cockpit.WaitForCockpitRequest{
98-
ProjectID: projectID,
99-
}, scw.WithContext(ctx))
100-
if err != nil {
101-
if httperrors.Is404(err) {
102-
d.SetId("")
103-
return nil
104-
}
105-
return diag.FromErr(err)
106-
}
107-
108-
res, err := api.ListGrafanaUsers(&cockpit.ListGrafanaUsersRequest{
90+
res, err := api.ListGrafanaUsers(&cockpit.GlobalAPIListGrafanaUsersRequest{
10991
ProjectID: projectID,
11092
}, scw.WithContext(ctx), scw.WithAllPages())
11193
if err != nil {
@@ -142,17 +124,7 @@ func ResourceCockpitGrafanaUserDelete(ctx context.Context, d *schema.ResourceDat
142124
return diag.FromErr(err)
143125
}
144126

145-
_, err = api.WaitForCockpit(&cockpit.WaitForCockpitRequest{
146-
ProjectID: projectID,
147-
}, scw.WithContext(ctx))
148-
if err != nil {
149-
if httperrors.Is404(err) {
150-
return nil
151-
}
152-
return diag.FromErr(err)
153-
}
154-
155-
err = api.DeleteGrafanaUser(&cockpit.DeleteGrafanaUserRequest{
127+
err = api.DeleteGrafanaUser(&cockpit.GlobalAPIDeleteGrafanaUserRequest{
156128
ProjectID: projectID,
157129
GrafanaUserID: grafanaUserID,
158130
}, scw.WithContext(ctx))

internal/services/cockpit/grafana_user_test.go

Lines changed: 17 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ package cockpit_test
22

33
import (
44
"fmt"
5-
"regexp"
65
"testing"
76

87
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
98
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
10-
cockpitSDK "github.com/scaleway/scaleway-sdk-go/api/cockpit/v1beta1"
9+
cockpitSDK "github.com/scaleway/scaleway-sdk-go/api/cockpit/v1"
1110
"github.com/scaleway/scaleway-sdk-go/scw"
1211
"github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest"
1312
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
@@ -32,24 +31,27 @@ func TestAccGrafanaUser_Basic(t *testing.T) {
3231
name = "%[1]s"
3332
}
3433
35-
resource scaleway_cockpit main {
36-
project_id = scaleway_account_project.project.id
37-
}
38-
3934
resource scaleway_cockpit_grafana_user main {
40-
project_id = scaleway_cockpit.main.project_id
35+
project_id = scaleway_account_project.project.id
4136
login = "%[2]s"
4237
role = "editor"
4338
}
4439
`, projectName, grafanaTestUsername),
4540
Check: resource.ComposeTestCheckFunc(
4641
isGrafanaUserPresent(tt, "scaleway_cockpit_grafana_user.main"),
47-
resource.TestCheckResourceAttrPair("scaleway_cockpit_grafana_user.main", "project_id", "scaleway_cockpit.main", "project_id"),
42+
resource.TestCheckResourceAttrPair("scaleway_cockpit_grafana_user.main", "project_id", "scaleway_account_project.project", "id"),
4843
resource.TestCheckResourceAttr("scaleway_cockpit_grafana_user.main", "login", grafanaTestUsername),
4944
resource.TestCheckResourceAttr("scaleway_cockpit_grafana_user.main", "role", "editor"),
5045
resource.TestCheckResourceAttrSet("scaleway_cockpit_grafana_user.main", "password"),
5146
),
5247
},
48+
{
49+
Config: fmt.Sprintf(`
50+
resource "scaleway_account_project" "project" {
51+
name = "%[1]s"
52+
}
53+
`, projectName),
54+
},
5355
},
5456
})
5557
}
@@ -72,19 +74,15 @@ func TestAccGrafanaUser_Update(t *testing.T) {
7274
name = "%[1]s"
7375
}
7476
75-
resource scaleway_cockpit main {
76-
project_id = scaleway_account_project.project.id
77-
}
78-
7977
resource scaleway_cockpit_grafana_user main {
80-
project_id = scaleway_cockpit.main.project_id
78+
project_id = scaleway_account_project.project.id
8179
login = "%[2]s"
8280
role = "editor"
8381
}
8482
`, projectName, grafanaTestUsername),
8583
Check: resource.ComposeTestCheckFunc(
8684
isGrafanaUserPresent(tt, "scaleway_cockpit_grafana_user.main"),
87-
resource.TestCheckResourceAttrPair("scaleway_cockpit_grafana_user.main", "project_id", "scaleway_cockpit.main", "project_id"),
85+
resource.TestCheckResourceAttrPair("scaleway_cockpit_grafana_user.main", "project_id", "scaleway_account_project.project", "id"),
8886
resource.TestCheckResourceAttr("scaleway_cockpit_grafana_user.main", "login", grafanaTestUsername),
8987
resource.TestCheckResourceAttr("scaleway_cockpit_grafana_user.main", "role", "editor"),
9088
resource.TestCheckResourceAttrSet("scaleway_cockpit_grafana_user.main", "password"),
@@ -96,53 +94,26 @@ func TestAccGrafanaUser_Update(t *testing.T) {
9694
name = "%[1]s"
9795
}
9896
99-
resource scaleway_cockpit main {
100-
project_id = scaleway_account_project.project.id
101-
}
102-
10397
resource scaleway_cockpit_grafana_user main {
104-
project_id = scaleway_cockpit.main.project_id
98+
project_id = scaleway_account_project.project.id
10599
login = "%[2]s"
106100
role = "viewer"
107101
}
108102
`, projectName, grafanaTestUsername),
109103
Check: resource.ComposeTestCheckFunc(
110104
isGrafanaUserPresent(tt, "scaleway_cockpit_grafana_user.main"),
111-
resource.TestCheckResourceAttrPair("scaleway_cockpit_grafana_user.main", "project_id", "scaleway_cockpit.main", "project_id"),
105+
resource.TestCheckResourceAttrPair("scaleway_cockpit_grafana_user.main", "project_id", "scaleway_account_project.project", "id"),
112106
resource.TestCheckResourceAttr("scaleway_cockpit_grafana_user.main", "login", grafanaTestUsername),
113107
resource.TestCheckResourceAttr("scaleway_cockpit_grafana_user.main", "role", "viewer"),
114108
resource.TestCheckResourceAttrSet("scaleway_cockpit_grafana_user.main", "password"),
115109
),
116110
},
117-
},
118-
})
119-
}
120-
121-
func TestAccGrafanaUser_NonExistentCockpit(t *testing.T) {
122-
tt := acctest.NewTestTools(t)
123-
defer tt.Cleanup()
124-
125-
projectName := "tf_tests_cockpit_grafana_user_non_existent_cockpit"
126-
grafanaTestUsername := "testnonexistentuser"
127-
128-
resource.ParallelTest(t, resource.TestCase{
129-
PreCheck: func() { acctest.PreCheck(t) },
130-
ProviderFactories: tt.ProviderFactories,
131-
CheckDestroy: isGrafanaUserDestroyed(tt),
132-
Steps: []resource.TestStep{
133111
{
134112
Config: fmt.Sprintf(`
135113
resource "scaleway_account_project" "project" {
136114
name = "%[1]s"
137115
}
138-
139-
resource scaleway_cockpit_grafana_user main {
140-
project_id = scaleway_account_project.project.id
141-
login = "%[2]s"
142-
role = "editor"
143-
}
144-
`, projectName, grafanaTestUsername),
145-
ExpectError: regexp.MustCompile("not found"),
116+
`, projectName),
146117
},
147118
},
148119
})
@@ -160,7 +131,7 @@ func isGrafanaUserPresent(tt *acctest.TestTools, n string) resource.TestCheckFun
160131
return err
161132
}
162133

163-
res, err := api.ListGrafanaUsers(&cockpitSDK.ListGrafanaUsersRequest{
134+
res, err := api.ListGrafanaUsers(&cockpitSDK.GlobalAPIListGrafanaUsersRequest{
164135
ProjectID: projectID,
165136
}, scw.WithAllPages())
166137
if err != nil {
@@ -195,7 +166,7 @@ func isGrafanaUserDestroyed(tt *acctest.TestTools) resource.TestCheckFunc {
195166
return err
196167
}
197168

198-
err = api.DeleteGrafanaUser(&cockpitSDK.DeleteGrafanaUserRequest{
169+
err = api.DeleteGrafanaUser(&cockpitSDK.GlobalAPIDeleteGrafanaUserRequest{
199170
ProjectID: projectID,
200171
GrafanaUserID: grafanaUserID,
201172
})

internal/services/cockpit/helpers_cockpit.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ func NewAPI(m interface{}) (*cockpitv1beta1.API, error) {
3030
return api, nil
3131
}
3232

33+
// NewGlobalAPI returns a new global cockpit API.
34+
func NewGlobalAPI(m interface{}) (*cockpit.GlobalAPI, error) {
35+
api := cockpit.NewGlobalAPI(meta.ExtractScwClient(m))
36+
37+
return api, nil
38+
}
39+
3340
func cockpitAPIWithRegion(d *schema.ResourceData, m interface{}) (*cockpit.RegionalAPI, scw.Region, error) {
3441
api := cockpit.NewRegionalAPI(meta.ExtractScwClient(m))
3542

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

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

65-
api, err := NewAPI(m)
72+
api, err := NewGlobalAPI(m)
6673
if err != nil {
6774
return nil, "", 0, err
6875
}

0 commit comments

Comments
 (0)