Skip to content

Commit 33e5e5a

Browse files
authored
purge(cockpit): remove plan from cockpit (#2968)
* fix(cockpit): purge plan * fix(cockpit): fix lint
1 parent e5ae8ab commit 33e5e5a

File tree

6 files changed

+382
-1628
lines changed

6 files changed

+382
-1628
lines changed

internal/services/cockpit/cockpit.go

Lines changed: 38 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -25,53 +25,54 @@ func ResourceCockpit() *schema.Resource {
2525
Type: schema.TypeString,
2626
Optional: true,
2727
Default: "free",
28-
Description: "Name or ID of the plan",
28+
Description: "[DEPRECATED] The plan field is deprecated. Any modification or selection will have no effect.",
29+
Deprecated: "The 'plan' attribute is deprecated and no longer has any effect. Future updates will remove this attribute entirely.",
2930
},
3031
"plan_id": {
3132
Type: schema.TypeString,
3233
Computed: true,
33-
Description: "The plan ID of the cockpit",
34-
Deprecated: "Please use Name only",
34+
Description: "[DEPRECATED] The plan ID of the cockpit. This field is no longer relevant.",
35+
Deprecated: "The 'plan_id' attribute is deprecated and will be removed in a future release.",
3536
},
3637
"endpoints": {
3738
Type: schema.TypeList,
3839
Computed: true,
39-
Description: "Endpoints",
40-
Deprecated: "Please use `scaleway_cockpit_source` instead",
40+
Description: "[DEPRECATED] Endpoints list. Please use 'scaleway_cockpit_source' instead.",
41+
Deprecated: "Use 'scaleway_cockpit_source' instead of 'endpoints'. This field will be removed in future releases.",
4142
Elem: &schema.Resource{
4243
Schema: map[string]*schema.Schema{
4344
"metrics_url": {
4445
Type: schema.TypeString,
4546
Computed: true,
46-
Description: "The metrics URL",
47+
Description: "The metrics URL.",
4748
},
4849
"logs_url": {
4950
Type: schema.TypeString,
5051
Computed: true,
51-
Description: "The logs URL",
52+
Description: "The logs URL.",
5253
},
5354
"alertmanager_url": {
5455
Type: schema.TypeString,
5556
Computed: true,
56-
Description: "The alertmanager URL",
57+
Description: "The alertmanager URL.",
5758
},
5859
"grafana_url": {
5960
Type: schema.TypeString,
6061
Computed: true,
61-
Description: "The grafana URL",
62+
Description: "The grafana URL.",
6263
},
6364
"traces_url": {
6465
Type: schema.TypeString,
6566
Computed: true,
66-
Description: "The traces URL",
67+
Description: "The traces URL.",
6768
},
6869
},
6970
},
7071
},
7172
"push_url": {
7273
Type: schema.TypeList,
7374
Computed: true,
74-
Description: "Push_url",
75+
Description: "[DEPRECATED] Push_url",
7576
Deprecated: "Please use `scaleway_cockpit_source` instead",
7677
Elem: &schema.Resource{
7778
Schema: map[string]*schema.Schema{
@@ -93,48 +94,22 @@ func ResourceCockpit() *schema.Resource {
9394
}
9495

9596
func ResourceCockpitCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
96-
api, err := NewGlobalAPI(m)
97-
if err != nil {
98-
return diag.FromErr(err)
99-
}
100-
10197
projectID := d.Get("project_id").(string)
102-
103-
if targetPlanI, ok := d.GetOk("plan"); ok {
104-
targetPlan := targetPlanI.(string)
105-
106-
plans, err := api.ListPlans(&cockpit.GlobalAPIListPlansRequest{}, scw.WithContext(ctx), scw.WithAllPages()) //nolint:staticcheck
107-
if err != nil {
108-
return diag.FromErr(err)
109-
}
110-
111-
var planName string
112-
113-
for _, plan := range plans.Plans {
114-
if plan.Name.String() == targetPlan {
115-
planName = plan.Name.String()
116-
117-
break
118-
}
119-
}
120-
121-
if planName == "" {
122-
return diag.Errorf("plan %s not found", targetPlan)
123-
}
124-
125-
_, err = api.SelectPlan(&cockpit.GlobalAPISelectPlanRequest{ //nolint:staticcheck
126-
ProjectID: projectID,
127-
PlanName: cockpit.PlanName(planName),
128-
}, scw.WithContext(ctx))
98+
if projectID == "" {
99+
_, err := getDefaultProjectID(ctx, m)
129100
if err != nil {
130101
return diag.FromErr(err)
131102
}
132103
}
133104

105+
d.SetId(projectID)
106+
134107
return ResourceCockpitRead(ctx, d, m)
135108
}
136109

137110
func ResourceCockpitRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
111+
var diags diag.Diagnostics
112+
138113
api, err := NewGlobalAPI(m)
139114
if err != nil {
140115
return diag.FromErr(err)
@@ -153,15 +128,14 @@ func ResourceCockpitRead(ctx context.Context, d *schema.ResourceData, m interfac
153128
}
154129
}
155130

156-
res, err := api.GetCurrentPlan(&cockpit.GlobalAPIGetCurrentPlanRequest{ //nolint:staticcheck
157-
ProjectID: projectID,
158-
}, scw.WithContext(ctx))
159-
if err != nil {
160-
return diag.FromErr(err)
161-
}
131+
diags = append(diags, diag.Diagnostic{
132+
Severity: diag.Warning,
133+
Summary: "Deprecated attribute: 'plan'",
134+
Detail: "The 'plan' attribute is deprecated and will be removed in a future version. Any changes to this attribute will have no effect.",
135+
})
162136

163-
_ = d.Set("plan", res.Name.String())
164-
_ = d.Set("plan_id", res.Name.String())
137+
_ = d.Set("plan", d.Get("plan"))
138+
_ = d.Set("plan_id", "")
165139

166140
dataSourcesRes, err := regionalAPI.ListDataSources(&cockpit.RegionalAPIListDataSourcesRequest{
167141
Region: region,
@@ -203,54 +177,26 @@ func ResourceCockpitRead(ctx context.Context, d *schema.ResourceData, m interfac
203177
_ = d.Set("endpoints", endpoints)
204178
_ = d.Set("push_url", createCockpitPushURLList(endpoints))
205179

206-
return nil
180+
return diags
207181
}
208182

209183
func ResourceCockpitUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
210-
api, err := NewGlobalAPI(m)
211-
if err != nil {
212-
return diag.FromErr(err)
213-
}
214-
215-
projectID := d.Id()
216-
184+
diags := diag.Diagnostics{}
217185
if d.HasChange("plan") {
218-
targetPlan := cockpit.PlanNameFree.String()
219-
if targetPlanI, ok := d.GetOk("plan"); ok {
220-
targetPlan = targetPlanI.(string)
221-
}
222-
223-
plans, err := api.ListPlans(&cockpit.GlobalAPIListPlansRequest{}, scw.WithContext(ctx), scw.WithAllPages()) //nolint:staticcheck
224-
if err != nil {
225-
return diag.FromErr(err)
226-
}
227-
228-
var planName string
229-
230-
for _, plan := range plans.Plans {
231-
if plan.Name.String() == targetPlan {
232-
planName = plan.Name.String()
233-
234-
break
235-
}
236-
}
237-
238-
if planName == "" {
239-
return diag.Errorf("plan %s not found", targetPlan)
240-
}
241-
242-
_, err = api.SelectPlan(&cockpit.GlobalAPISelectPlanRequest{ //nolint:staticcheck
243-
ProjectID: projectID,
244-
PlanName: cockpit.PlanName(planName),
245-
}, scw.WithContext(ctx))
246-
if err != nil {
247-
return diag.FromErr(err)
248-
}
186+
diags = append(diags, diag.Diagnostic{
187+
Severity: diag.Warning,
188+
Summary: "Deprecated attribute update: 'plan'",
189+
Detail: "Updating 'plan' has no effect as it is deprecated and will be removed in a future version.",
190+
})
249191
}
250192

251-
return ResourceCockpitRead(ctx, d, m)
193+
diags = append(diags, ResourceCockpitRead(ctx, d, m)...)
194+
195+
return diags
252196
}
253197

254-
func ResourceCockpitDelete(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics {
198+
func ResourceCockpitDelete(_ context.Context, d *schema.ResourceData, _ interface{}) diag.Diagnostics {
199+
d.SetId("")
200+
255201
return nil
256202
}

internal/services/cockpit/cockpit_data_source.go

Lines changed: 11 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,12 @@ import (
55

66
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
77
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8-
"github.com/scaleway/scaleway-sdk-go/api/cockpit/v1"
9-
"github.com/scaleway/scaleway-sdk-go/scw"
108
"github.com/scaleway/terraform-provider-scaleway/v2/internal/datasource"
11-
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
129
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
1310
)
1411

1512
func DataSourceCockpit() *schema.Resource {
16-
// Generate datasource schema from resource
1713
dsSchema := datasource.SchemaFromResourceSchema(ResourceCockpit().Schema)
18-
1914
dsSchema["project_id"] = &schema.Schema{
2015
Type: schema.TypeString,
2116
Description: "The project_id you want to attach the resource to",
@@ -25,7 +20,8 @@ func DataSourceCockpit() *schema.Resource {
2520
dsSchema["plan"] = &schema.Schema{
2621
Type: schema.TypeString,
2722
Computed: true,
28-
Description: "The current plan of the cockpit project",
23+
Description: "[DEPRECATED] The current plan of the cockpit project.",
24+
Deprecated: "The 'plan' attribute is deprecated and will be removed in a future version. Any changes to this attribute will have no effect.",
2925
}
3026

3127
return &schema.Resource{
@@ -36,78 +32,25 @@ func DataSourceCockpit() *schema.Resource {
3632
}
3733

3834
func dataSourceCockpitRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
39-
api, err := NewGlobalAPI(m)
40-
if err != nil {
41-
return diag.FromErr(err)
42-
}
43-
44-
regionalAPI, region, err := cockpitAPIWithRegion(d, m)
45-
if err != nil {
46-
return diag.FromErr(err)
47-
}
48-
4935
projectID := d.Get("project_id").(string)
5036
if projectID == "" {
51-
projectID, err = getDefaultProjectID(ctx, m)
37+
_, err := getDefaultProjectID(ctx, m)
5238
if err != nil {
5339
return diag.FromErr(err)
5440
}
5541
}
5642

57-
res, err := api.GetCurrentPlan(&cockpit.GlobalAPIGetCurrentPlanRequest{ //nolint:staticcheck
58-
ProjectID: projectID,
59-
}, scw.WithContext(ctx))
60-
if err != nil {
61-
if httperrors.Is404(err) {
62-
d.SetId("")
63-
64-
return nil
65-
}
66-
67-
return diag.FromErr(err)
68-
}
69-
70-
_ = d.Set("project_id", d.Get("project_id").(string))
71-
_ = d.Set("plan", res.Name)
72-
_ = d.Set("plan_id", res.Name)
43+
diags := diag.Diagnostics{}
7344

74-
dataSourcesRes, err := regionalAPI.ListDataSources(&cockpit.RegionalAPIListDataSourcesRequest{
75-
Region: region,
76-
ProjectID: projectID,
77-
Origin: "external",
78-
}, scw.WithContext(ctx), scw.WithAllPages())
79-
if err != nil {
80-
return diag.FromErr(err)
81-
}
82-
83-
grafana, err := api.GetGrafana(&cockpit.GlobalAPIGetGrafanaRequest{
84-
ProjectID: projectID,
85-
}, scw.WithContext(ctx))
86-
if err != nil {
87-
return diag.FromErr(err)
88-
}
89-
90-
if grafana.GrafanaURL == "" {
91-
grafana.GrafanaURL = createGrafanaURL(projectID, region)
92-
}
93-
94-
alertManager, err := regionalAPI.GetAlertManager(&cockpit.RegionalAPIGetAlertManagerRequest{
95-
ProjectID: projectID,
45+
diags = append(diags, diag.Diagnostic{
46+
Severity: diag.Warning,
47+
Summary: "Deprecated attribute: 'plan'",
48+
Detail: "The 'plan' attribute is deprecated and will be removed in a future version. Any changes to this attribute will have no effect.",
9649
})
97-
if err != nil {
98-
return diag.FromErr(err)
99-
}
100-
101-
alertManagerURL := ""
102-
if alertManager.AlertManagerURL != nil {
103-
alertManagerURL = *alertManager.AlertManagerURL
104-
}
105-
106-
endpoints := flattenCockpitEndpoints(dataSourcesRes.DataSources, grafana.GrafanaURL, alertManagerURL)
10750

108-
_ = d.Set("endpoints", endpoints)
109-
_ = d.Set("push_url", createCockpitPushURLList(endpoints))
51+
_ = d.Set("plan", d.Get("plan"))
52+
_ = d.Set("project_id", projectID)
11053
d.SetId(projectID)
11154

112-
return nil
55+
return diags
11356
}

internal/services/cockpit/cockpit_data_source_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ func TestAccDataSourceCockpit_Basic(t *testing.T) {
6767
`,
6868
Check: resource.ComposeTestCheckFunc(
6969
resource.TestCheckResourceAttr("scaleway_cockpit.main", "plan", "free"),
70-
resource.TestCheckResourceAttr("scaleway_cockpit.main", "plan_id", "free"),
7170
resource.TestCheckResourceAttrSet("scaleway_cockpit.main", "endpoints.0.metrics_url"),
7271
resource.TestCheckResourceAttrSet("scaleway_cockpit.main", "endpoints.0.logs_url"),
7372
resource.TestCheckResourceAttrSet("scaleway_cockpit.main", "endpoints.0.alertmanager_url"),

internal/services/cockpit/cockpit_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ func TestAccCockpit_Simple(t *testing.T) {
2525
`,
2626
Check: resource.ComposeTestCheckFunc(
2727
resource.TestCheckResourceAttrSet("scaleway_cockpit.main", "plan"),
28-
resource.TestCheckResourceAttrSet("scaleway_cockpit.main", "plan_id"),
2928
resource.TestCheckResourceAttr("scaleway_cockpit.main", "plan", "free"),
3029
resource.TestCheckResourceAttrSet("scaleway_cockpit.main", "endpoints.0.grafana_url"),
3130
),
@@ -57,7 +56,6 @@ func TestAccCockpit_Basic(t *testing.T) {
5756
Check: resource.ComposeTestCheckFunc(
5857
resource.TestCheckResourceAttrPair("scaleway_cockpit.main", "project_id", "scaleway_account_project.project", "id"),
5958
resource.TestCheckResourceAttrSet("scaleway_cockpit.main", "plan"),
60-
resource.TestCheckResourceAttrSet("scaleway_cockpit.main", "plan_id"),
6159
resource.TestCheckResourceAttr("scaleway_cockpit.main", "plan", "free"),
6260
resource.TestCheckResourceAttrSet("scaleway_cockpit.main", "endpoints.0.grafana_url"),
6361
checkGrafanaURL("scaleway_cockpit.main", "scaleway_account_project.project"),
@@ -127,7 +125,6 @@ func TestAccCockpit_WithSourceEndpoints(t *testing.T) {
127125
`,
128126
Check: resource.ComposeTestCheckFunc(
129127
resource.TestCheckResourceAttr("scaleway_cockpit.main", "plan", "premium"),
130-
resource.TestCheckResourceAttr("scaleway_cockpit.main", "plan_id", "premium"),
131128
resource.TestCheckResourceAttrSet("scaleway_cockpit.main", "endpoints.0.metrics_url"),
132129
resource.TestCheckResourceAttrSet("scaleway_cockpit.main", "endpoints.0.logs_url"),
133130
resource.TestCheckResourceAttrSet("scaleway_cockpit.main", "endpoints.0.alertmanager_url"),

0 commit comments

Comments
 (0)