Skip to content

Commit 20c2f9b

Browse files
jremy42remyleone
andauthored
feat(cockpit): migrate legacy cockpit resources to v1 API (#2679)
* feat/cockpit migrate api v1 * fix error 500 * add comment for unknow_type for alert_manager * add doc * fix alert_manager_url * delete 404 * delele timeout * fix lint * fix cassette * fix indentation --------- Co-authored-by: Rémy Léone <[email protected]>
1 parent d6965c4 commit 20c2f9b

26 files changed

+9139
-5574
lines changed

docs/data-sources/cockpit.md

+9-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ page_title: "Scaleway: scaleway_cockpit"
44
---
55
# scaleway_cockpit
66

7+
8+
~> **Important:** The data source `scaleway_cockpit` has been deprecated and will no longer be supported. Instead, use resource `scaleway_cockpit`.
9+
710
-> **Note:**
811
As of April 2024, Cockpit has introduced regionalization to offer more flexibility and resilience.
912
If you have customized dashboards in Grafana for monitoring Scaleway resources, please update your queries to accommodate the new regionalized [data sources](../resources/cockpit_source.md).
@@ -35,9 +38,9 @@ data "scaleway_cockpit" "main" {
3538

3639
In addition to all arguments above, the following attributes are exported:
3740

38-
- `plan_id` - The ID of the current plan
39-
- `endpoints` - Endpoints
40-
- `metrics_url` - The metrics URL
41-
- `logs_url` - The logs URL
42-
- `alertmanager_url` - The alertmanager URL
43-
- `grafana_url` - The grafana URL
41+
- `plan_id` - (Deprecated) The ID of the current plan
42+
- `endpoints` - (Deprecated) Endpoints
43+
- `metrics_url` - (Deprecated) The metrics URL
44+
- `logs_url` - (Deprecated) The logs URL
45+
- `alertmanager_url` - (Deprecated) The alertmanager URL
46+
- `grafana_url` - (Deprecated) The grafana URL

docs/resources/cockpit.md

+20-11
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,29 @@ For more information consult the [documentation](https://www.scaleway.com/en/doc
1515

1616
## Example Usage
1717

18-
### Activate Cockpit in the default project
18+
### Manage Cockpit in the default project
1919

2020
```terraform
2121
resource "scaleway_cockpit" "main" {}
2222
```
2323

24-
### Activate Cockpit in a specific project
24+
### Manage Cockpit in a specific project
2525

2626
```terraform
2727
resource "scaleway_cockpit" "main" {
2828
project_id = "11111111-1111-1111-1111-111111111111"
2929
}
3030
```
3131

32+
### Choose a specific plan for Cockpit
33+
34+
```terraform
35+
resource "scaleway_cockpit" "main" {
36+
project_id = "11111111-1111-1111-1111-111111111111"
37+
plan = "premium"
38+
}
39+
```
40+
3241
### Use the Grafana Terraform provider
3342

3443
```terraform
@@ -53,25 +62,25 @@ resource "grafana_folder" "test_folder" {
5362
## Argument Reference
5463

5564
- `project_id` - (Defaults to [provider](../index.md#project_id) `project_id`) The ID of the project the cockpit is associated with.
56-
- `plan` - (Optional) Name or ID of the plan to use.
65+
- `plan` - (Optional) Name of the plan to use. Available plans are free, premium, and custom.
5766

5867

5968
## Attributes Reference
6069

6170
In addition to all arguments above, the following attributes are exported:
6271

63-
- `plan_id` - The ID of the current plan.
64-
- `endpoints` - Endpoints.
65-
- `metrics_url` - The metrics URL.
66-
- `logs_url` - The logs URL.
67-
- `alertmanager_url` - The alertmanager URL.
68-
- `grafana_url` - The grafana URL.
69-
- `traces_url` - The traces URL.
72+
- `plan_id` - (Deprecated) The ID of the current plan. Please use plan instead.
73+
- `endpoints` - (Deprecated) Endpoints. Please use scaleway_cockpit_source instead.
74+
- `metrics_url` - (Deprecated) The metrics URL.
75+
- `logs_url` - (Deprecated) The logs URL.
76+
- `alertmanager_url` - (Deprecated) The alertmanager URL.
77+
- `grafana_url` - (Deprecated) The grafana URL.
78+
- `traces_url` - (Deprecated) The traces URL.
7079

7180
## Import
7281

7382
Cockpits can be imported using the `{project_id}`, e.g.
7483

7584
```bash
7685
terraform import scaleway_cockpit.main 11111111-1111-1111-1111-111111111111
77-
```
86+
```

internal/services/cockpit/cockpit.go

+65-74
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ import (
55

66
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
77
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8-
cockpit "github.com/scaleway/scaleway-sdk-go/api/cockpit/v1beta1"
8+
"github.com/scaleway/scaleway-sdk-go/api/cockpit/v1"
99
"github.com/scaleway/scaleway-sdk-go/scw"
10-
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
1110
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
1211
)
1312

@@ -17,12 +16,6 @@ func ResourceCockpit() *schema.Resource {
1716
ReadContext: ResourceCockpitRead,
1817
UpdateContext: ResourceCockpitUpdate,
1918
DeleteContext: ResourceCockpitDelete,
20-
Timeouts: &schema.ResourceTimeout{
21-
Create: schema.DefaultTimeout(DefaultCockpitTimeout),
22-
Read: schema.DefaultTimeout(DefaultCockpitTimeout),
23-
Delete: schema.DefaultTimeout(DefaultCockpitTimeout),
24-
Default: schema.DefaultTimeout(DefaultCockpitTimeout),
25-
},
2619
Importer: &schema.ResourceImporter{
2720
StateContext: schema.ImportStatePassthroughContext,
2821
},
@@ -37,11 +30,13 @@ func ResourceCockpit() *schema.Resource {
3730
Type: schema.TypeString,
3831
Computed: true,
3932
Description: "The plan ID of the cockpit",
33+
Deprecated: "Please use Name only",
4034
},
4135
"endpoints": {
4236
Type: schema.TypeList,
4337
Computed: true,
4438
Description: "Endpoints",
39+
Deprecated: "Please use `scaleway_cockpit_source` instead",
4540
Elem: &schema.Resource{
4641
Schema: map[string]*schema.Schema{
4742
"metrics_url": {
@@ -76,6 +71,7 @@ func ResourceCockpit() *schema.Resource {
7671
Type: schema.TypeList,
7772
Computed: true,
7873
Description: "Push_url",
74+
Deprecated: "Please use `scaleway_cockpit_source` instead",
7975
Elem: &schema.Resource{
8076
Schema: map[string]*schema.Schema{
8177
"push_metrics_url": {
@@ -96,114 +92,135 @@ func ResourceCockpit() *schema.Resource {
9692
}
9793

9894
func ResourceCockpitCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
99-
api, err := NewAPI(m)
95+
api, err := NewGlobalAPI(m)
10096
if err != nil {
10197
return diag.FromErr(err)
10298
}
10399

104100
projectID := d.Get("project_id").(string)
105-
106-
res, err := api.ActivateCockpit(&cockpit.ActivateCockpitRequest{
107-
ProjectID: projectID,
108-
}, scw.WithContext(ctx))
109-
if err != nil {
110-
return diag.FromErr(err)
111-
}
112-
113101
if targetPlanI, ok := d.GetOk("plan"); ok {
114102
targetPlan := targetPlanI.(string)
115103

116-
plans, err := api.ListPlans(&cockpit.ListPlansRequest{}, scw.WithContext(ctx), scw.WithAllPages())
104+
plans, err := api.ListPlans(&cockpit.GlobalAPIListPlansRequest{}, scw.WithContext(ctx), scw.WithAllPages())
117105
if err != nil {
118106
return diag.FromErr(err)
119107
}
120108

121-
var planID string
109+
var planName string
122110
for _, plan := range plans.Plans {
123-
if plan.Name.String() == targetPlan || plan.ID == targetPlan {
124-
planID = plan.ID
111+
if plan.Name.String() == targetPlan {
112+
planName = plan.Name.String()
125113
break
126114
}
127115
}
128116

129-
if planID == "" {
117+
if planName == "" {
130118
return diag.Errorf("plan %s not found", targetPlan)
131119
}
132120

133-
_, err = api.SelectPlan(&cockpit.SelectPlanRequest{
121+
_, err = api.SelectPlan(&cockpit.GlobalAPISelectPlanRequest{
134122
ProjectID: projectID,
135-
PlanID: planID,
123+
PlanName: cockpit.PlanName(planName),
136124
}, scw.WithContext(ctx))
137125
if err != nil {
138126
return diag.FromErr(err)
139127
}
140128
}
141129

142-
d.SetId(res.ProjectID)
130+
d.SetId(projectID)
143131
return ResourceCockpitRead(ctx, d, m)
144132
}
145133

146134
func ResourceCockpitRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
147-
api, err := NewAPI(m)
135+
api, err := NewGlobalAPI(m)
148136
if err != nil {
149137
return diag.FromErr(err)
150138
}
151139

152-
res, err := waitForCockpit(ctx, api, d.Id(), d.Timeout(schema.TimeoutRead))
140+
regionalAPI, region, err := cockpitAPIWithRegion(d, m)
141+
if err != nil {
142+
return diag.FromErr(err)
143+
}
144+
145+
res, err := api.GetCurrentPlan(&cockpit.GlobalAPIGetCurrentPlanRequest{
146+
ProjectID: d.Get("project_id").(string),
147+
}, scw.WithContext(ctx))
148+
if err != nil {
149+
return diag.FromErr(err)
150+
}
151+
_ = d.Set("project_id", d.Get("project_id").(string))
152+
_ = d.Set("plan", res.Name.String())
153+
_ = d.Set("plan_id", res.Name.String())
154+
155+
dataSourcesRes, err := regionalAPI.ListDataSources(&cockpit.RegionalAPIListDataSourcesRequest{
156+
Region: region,
157+
ProjectID: d.Get("project_id").(string),
158+
Origin: "external",
159+
}, scw.WithContext(ctx), scw.WithAllPages())
160+
if err != nil {
161+
return diag.FromErr(err)
162+
}
163+
164+
grafana, err := api.GetGrafana(&cockpit.GlobalAPIGetGrafanaRequest{
165+
ProjectID: d.Get("project_id").(string),
166+
}, scw.WithContext(ctx))
167+
if err != nil {
168+
return diag.FromErr(err)
169+
}
170+
171+
alertManager, err := regionalAPI.GetAlertManager(&cockpit.RegionalAPIGetAlertManagerRequest{
172+
ProjectID: d.Get("project_id").(string),
173+
})
153174
if err != nil {
154-
if httperrors.Is404(err) {
155-
d.SetId("")
156-
return nil
157-
}
158175
return diag.FromErr(err)
159176
}
177+
alertManagerURL := ""
178+
if alertManager.AlertManagerURL != nil {
179+
alertManagerURL = *alertManager.AlertManagerURL
180+
}
160181

161-
_ = d.Set("project_id", res.ProjectID)
162-
_ = d.Set("plan_id", res.Plan.ID)
163-
_ = d.Set("endpoints", flattenCockpitEndpoints(res.Endpoints))
164-
_ = d.Set("push_url", createCockpitPushURL(res.Endpoints))
182+
endpoints := flattenCockpitEndpoints(dataSourcesRes.DataSources, grafana.GrafanaURL, alertManagerURL)
183+
184+
_ = d.Set("endpoints", endpoints)
185+
_ = d.Set("push_url", createCockpitPushURL(endpoints))
165186

166187
return nil
167188
}
168189

169190
func ResourceCockpitUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
170-
api, err := NewAPI(m)
191+
api, err := NewGlobalAPI(m)
171192
if err != nil {
172193
return diag.FromErr(err)
173194
}
174195

175196
projectID := d.Id()
176-
_, err = waitForCockpit(ctx, api, projectID, d.Timeout(schema.TimeoutDelete))
177-
if err != nil {
178-
return diag.FromErr(err)
179-
}
180197

181198
if d.HasChange("plan") {
182199
targetPlan := cockpit.PlanNameFree.String()
183200
if targetPlanI, ok := d.GetOk("plan"); ok {
184201
targetPlan = targetPlanI.(string)
185202
}
186203

187-
plans, err := api.ListPlans(&cockpit.ListPlansRequest{}, scw.WithContext(ctx), scw.WithAllPages())
204+
plans, err := api.ListPlans(&cockpit.GlobalAPIListPlansRequest{}, scw.WithContext(ctx), scw.WithAllPages())
188205
if err != nil {
189206
return diag.FromErr(err)
190207
}
191208

192-
var planID string
209+
var planName string
193210
for _, plan := range plans.Plans {
194-
if plan.Name.String() == targetPlan || plan.ID == targetPlan {
195-
planID = plan.ID
211+
if plan.Name.String() == targetPlan {
212+
planName = plan.Name.String()
196213
break
197214
}
198215
}
199216

200-
if planID == "" {
217+
if planName == "" {
201218
return diag.Errorf("plan %s not found", targetPlan)
202219
}
203220

204-
_, err = api.SelectPlan(&cockpit.SelectPlanRequest{
221+
_, err = api.SelectPlan(&cockpit.GlobalAPISelectPlanRequest{
205222
ProjectID: projectID,
206-
PlanID: planID,
223+
PlanName: cockpit.PlanName(planName),
207224
}, scw.WithContext(ctx))
208225
if err != nil {
209226
return diag.FromErr(err)
@@ -213,32 +230,6 @@ func ResourceCockpitUpdate(ctx context.Context, d *schema.ResourceData, m interf
213230
return ResourceCockpitRead(ctx, d, m)
214231
}
215232

216-
func ResourceCockpitDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
217-
api, err := NewAPI(m)
218-
if err != nil {
219-
return diag.FromErr(err)
220-
}
221-
222-
_, err = waitForCockpit(ctx, api, d.Id(), d.Timeout(schema.TimeoutDelete))
223-
if err != nil {
224-
if httperrors.Is404(err) {
225-
d.SetId("")
226-
return nil
227-
}
228-
return diag.FromErr(err)
229-
}
230-
231-
_, err = api.DeactivateCockpit(&cockpit.DeactivateCockpitRequest{
232-
ProjectID: d.Id(),
233-
}, scw.WithContext(ctx))
234-
if err != nil && !httperrors.Is404(err) {
235-
return diag.FromErr(err)
236-
}
237-
238-
_, err = waitForCockpit(ctx, api, d.Id(), d.Timeout(schema.TimeoutDelete))
239-
if err != nil && !httperrors.Is404(err) {
240-
return diag.FromErr(err)
241-
}
242-
233+
func ResourceCockpitDelete(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics {
243234
return nil
244235
}

0 commit comments

Comments
 (0)