Skip to content

Commit aeb0c42

Browse files
authored
feat(cockpit): add plan selection (#1941)
* feat(cockpit): add plan selection * fix tests * update cassettes * update cassettes
1 parent 696f376 commit aeb0c42

17 files changed

+5507
-1717
lines changed

docs/data-sources/cockpit.md

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ data "scaleway_cockpit" "main" {
3131

3232
In addition to all arguments above, the following attributes are exported:
3333

34+
- `plan_id` - The ID of the current plan
3435
- `endpoints` - Endpoints
3536
- `metrics_url` - The metrics URL
3637
- `logs_url` - The logs URL

docs/resources/cockpit.md

+2
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,14 @@ resource "grafana_folder" "test_folder" {
4949
## Arguments Reference
5050

5151
- `project_id` - (Defaults to [provider](../index.md#project_id) `project_id`) The ID of the project the cockpit is associated with.
52+
- `plan` - (Optional) Name or ID of the plan to use.
5253

5354

5455
## Attributes Reference
5556

5657
In addition to all arguments above, the following attributes are exported:
5758

59+
- `plan_id` - The ID of the current plan
5860
- `endpoints` - Endpoints
5961
- `metrics_url` - The metrics URL
6062
- `logs_url` - The logs URL

scaleway/data_source_cockpit.go

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ func dataSourceScalewayCockpit() *schema.Resource {
1717
Optional: true,
1818
ValidateFunc: validationUUID(),
1919
}
20+
delete(dsSchema, "plan")
2021

2122
return &schema.Resource{
2223
ReadContext: dataSourceScalewayCockpitRead,
@@ -39,6 +40,7 @@ func dataSourceScalewayCockpitRead(ctx context.Context, d *schema.ResourceData,
3940

4041
d.SetId(res.ProjectID)
4142
_ = d.Set("project_id", res.ProjectID)
43+
_ = d.Set("plan_id", res.Plan.ID)
4244
_ = d.Set("endpoints", flattenCockpitEndpoints(res.Endpoints))
4345

4446
return nil

scaleway/data_source_cockpit_test.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,23 @@ func TestAccScalewayDataSourceCockpit_Basic(t *testing.T) {
2121
name = "tf_tests_datasource_cockpit_project_basic"
2222
}
2323
24-
resource scaleway_cockpit main {
24+
resource "scaleway_cockpit" "main" {
2525
project_id = scaleway_account_project.project.id
2626
}
2727
28-
data scaleway_cockpit selected {
28+
data "scaleway_cockpit" "selected" {
2929
project_id = scaleway_cockpit.main.project_id
3030
}
31+
32+
data "scaleway_cockpit_plan" "free" {
33+
name = "free"
34+
}
3135
`,
3236
Check: resource.ComposeTestCheckFunc(
3337
testAccCheckScalewayCockpitExists(tt, "scaleway_cockpit.main"),
3438
testAccCheckScalewayCockpitExists(tt, "data.scaleway_cockpit.selected"),
3539

40+
resource.TestCheckResourceAttrPair("data.scaleway_cockpit.selected", "plan_id", "data.scaleway_cockpit_plan.free", "id"),
3641
resource.TestCheckResourceAttrSet("data.scaleway_cockpit.selected", "endpoints.0.metrics_url"),
3742
resource.TestCheckResourceAttrSet("data.scaleway_cockpit.selected", "endpoints.0.metrics_url"),
3843
resource.TestCheckResourceAttrSet("data.scaleway_cockpit.selected", "endpoints.0.logs_url"),

scaleway/resource_cockpit.go

+91-6
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ import (
66
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
77
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
88
cockpit "github.com/scaleway/scaleway-sdk-go/api/cockpit/v1beta1"
9+
"github.com/scaleway/scaleway-sdk-go/scw"
910
)
1011

1112
func resourceScalewayCockpit() *schema.Resource {
1213
return &schema.Resource{
1314
CreateContext: resourceScalewayCockpitCreate,
1415
ReadContext: resourceScalewayCockpitRead,
16+
UpdateContext: resourceScalewayCockpitUpdate,
1517
DeleteContext: resourceScalewayCockpitDelete,
1618
Timeouts: &schema.ResourceTimeout{
1719
Create: schema.DefaultTimeout(defaultCockpitTimeout),
@@ -24,6 +26,16 @@ func resourceScalewayCockpit() *schema.Resource {
2426
},
2527
Schema: map[string]*schema.Schema{
2628
"project_id": projectIDSchema(),
29+
"plan": {
30+
Type: schema.TypeString,
31+
Optional: true,
32+
Description: "Name or ID of the plan",
33+
},
34+
"plan_id": {
35+
Type: schema.TypeString,
36+
Computed: true,
37+
Description: "The plan ID of the cockpit",
38+
},
2739
"endpoints": {
2840
Type: schema.TypeList,
2941
Computed: true,
@@ -67,11 +79,40 @@ func resourceScalewayCockpitCreate(ctx context.Context, d *schema.ResourceData,
6779

6880
res, err := api.ActivateCockpit(&cockpit.ActivateCockpitRequest{
6981
ProjectID: projectID,
70-
})
82+
}, scw.WithContext(ctx))
7183
if err != nil {
7284
return diag.FromErr(err)
7385
}
7486

87+
if targetPlanI, ok := d.GetOk("plan"); ok {
88+
targetPlan := targetPlanI.(string)
89+
90+
plans, err := api.ListPlans(&cockpit.ListPlansRequest{}, scw.WithContext(ctx), scw.WithAllPages())
91+
if err != nil {
92+
return diag.FromErr(err)
93+
}
94+
95+
var planID string
96+
for _, plan := range plans.Plans {
97+
if plan.Name.String() == targetPlan || plan.ID == targetPlan {
98+
planID = plan.ID
99+
break
100+
}
101+
}
102+
103+
if planID == "" {
104+
return diag.Errorf("plan %s not found", targetPlan)
105+
}
106+
107+
_, err = api.SelectPlan(&cockpit.SelectPlanRequest{
108+
ProjectID: projectID,
109+
PlanID: planID,
110+
}, scw.WithContext(ctx))
111+
if err != nil {
112+
return diag.FromErr(err)
113+
}
114+
}
115+
75116
d.SetId(res.ProjectID)
76117
return resourceScalewayCockpitRead(ctx, d, meta)
77118
}
@@ -84,19 +125,63 @@ func resourceScalewayCockpitRead(ctx context.Context, d *schema.ResourceData, me
84125

85126
res, err := waitForCockpit(ctx, api, d.Id(), d.Timeout(schema.TimeoutRead))
86127
if err != nil {
87-
if is404Error(err) {
88-
d.SetId("")
89-
return nil
90-
}
91128
return diag.FromErr(err)
92129
}
93130

94131
_ = d.Set("project_id", res.ProjectID)
132+
_ = d.Set("plan_id", res.Plan.ID)
95133
_ = d.Set("endpoints", flattenCockpitEndpoints(res.Endpoints))
96134

97135
return nil
98136
}
99137

138+
func resourceScalewayCockpitUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
139+
api, err := cockpitAPI(meta)
140+
if err != nil {
141+
return diag.FromErr(err)
142+
}
143+
144+
projectID := d.Id()
145+
_, err = waitForCockpit(ctx, api, projectID, d.Timeout(schema.TimeoutDelete))
146+
if err != nil {
147+
return diag.FromErr(err)
148+
}
149+
150+
if d.HasChange("plan") {
151+
targetPlan := cockpit.PlanNameFree.String()
152+
if targetPlanI, ok := d.GetOk("plan"); ok {
153+
targetPlan = targetPlanI.(string)
154+
}
155+
156+
plans, err := api.ListPlans(&cockpit.ListPlansRequest{}, scw.WithContext(ctx), scw.WithAllPages())
157+
if err != nil {
158+
return diag.FromErr(err)
159+
}
160+
161+
var planID string
162+
for _, plan := range plans.Plans {
163+
if plan.Name.String() == targetPlan || plan.ID == targetPlan {
164+
planID = plan.ID
165+
break
166+
}
167+
}
168+
169+
if planID == "" {
170+
return diag.Errorf("plan %s not found", targetPlan)
171+
}
172+
173+
_, err = api.SelectPlan(&cockpit.SelectPlanRequest{
174+
ProjectID: projectID,
175+
PlanID: planID,
176+
}, scw.WithContext(ctx))
177+
if err != nil {
178+
return diag.FromErr(err)
179+
}
180+
}
181+
182+
return resourceScalewayCockpitRead(ctx, d, meta)
183+
}
184+
100185
func resourceScalewayCockpitDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
101186
api, err := cockpitAPI(meta)
102187
if err != nil {
@@ -110,7 +195,7 @@ func resourceScalewayCockpitDelete(ctx context.Context, d *schema.ResourceData,
110195

111196
_, err = api.DeactivateCockpit(&cockpit.DeactivateCockpitRequest{
112197
ProjectID: d.Id(),
113-
})
198+
}, scw.WithContext(ctx))
114199
if err != nil {
115200
return diag.FromErr(err)
116201
}

scaleway/resource_cockpit_test.go

+105
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ func TestAccScalewayCockpit_Basic(t *testing.T) {
7979
`,
8080
Check: resource.ComposeTestCheckFunc(
8181
testAccCheckScalewayCockpitExists(tt, "scaleway_cockpit.main"),
82+
resource.TestCheckResourceAttrSet("scaleway_cockpit.main", "plan_id"),
8283
resource.TestCheckResourceAttrSet("scaleway_cockpit.main", "endpoints.0.metrics_url"),
8384
resource.TestCheckResourceAttrSet("scaleway_cockpit.main", "endpoints.0.metrics_url"),
8485
resource.TestCheckResourceAttrSet("scaleway_cockpit.main", "endpoints.0.logs_url"),
@@ -87,6 +88,110 @@ func TestAccScalewayCockpit_Basic(t *testing.T) {
8788
resource.TestCheckResourceAttrPair("scaleway_cockpit.main", "project_id", "scaleway_account_project.project", "id"),
8889
),
8990
},
91+
{
92+
Config: `
93+
resource "scaleway_account_project" "project" {
94+
name = "tf_tests_cockpit_project_basic"
95+
}
96+
97+
data "scaleway_cockpit_plan" "premium" {
98+
name = "premium"
99+
}
100+
101+
resource "scaleway_cockpit" "main" {
102+
project_id = scaleway_account_project.project.id
103+
plan = data.scaleway_cockpit_plan.premium.id
104+
}
105+
`,
106+
Check: resource.ComposeTestCheckFunc(
107+
testAccCheckScalewayCockpitExists(tt, "scaleway_cockpit.main"),
108+
resource.TestCheckResourceAttrSet("scaleway_cockpit.main", "plan_id"),
109+
),
110+
},
111+
},
112+
})
113+
}
114+
115+
func TestAccScalewayCockpit_PremiumPlanByID(t *testing.T) {
116+
tt := NewTestTools(t)
117+
defer tt.Cleanup()
118+
119+
resource.ParallelTest(t, resource.TestCase{
120+
PreCheck: func() { testAccPreCheck(t) },
121+
ProviderFactories: tt.ProviderFactories,
122+
CheckDestroy: testAccCheckScalewayCockpitDestroy(tt),
123+
Steps: []resource.TestStep{
124+
{
125+
Config: `
126+
resource "scaleway_account_project" "project" {
127+
name = "tf_tests_cockpit_project_premium"
128+
}
129+
130+
data "scaleway_cockpit_plan" "premium" {
131+
name = "premium"
132+
}
133+
134+
resource scaleway_cockpit main {
135+
project_id = scaleway_account_project.project.id
136+
plan = data.scaleway_cockpit_plan.premium.id
137+
}
138+
`,
139+
Check: resource.ComposeTestCheckFunc(
140+
testAccCheckScalewayCockpitExists(tt, "scaleway_cockpit.main"),
141+
),
142+
},
143+
{
144+
Config: `
145+
resource "scaleway_account_project" "project" {
146+
name = "tf_tests_cockpit_project_premium"
147+
}
148+
149+
data "scaleway_cockpit_plan" "free" {
150+
name = "free"
151+
}
152+
153+
resource scaleway_cockpit main {
154+
project_id = scaleway_account_project.project.id
155+
}
156+
`,
157+
Check: resource.ComposeTestCheckFunc(
158+
testAccCheckScalewayCockpitExists(tt, "scaleway_cockpit.main"),
159+
resource.TestCheckResourceAttrPair("scaleway_cockpit.main", "plan_id", "data.scaleway_cockpit_plan.free", "id"),
160+
),
161+
},
162+
},
163+
})
164+
}
165+
166+
func TestAccScalewayCockpit_PremiumPlanByName(t *testing.T) {
167+
tt := NewTestTools(t)
168+
defer tt.Cleanup()
169+
170+
resource.ParallelTest(t, resource.TestCase{
171+
PreCheck: func() { testAccPreCheck(t) },
172+
ProviderFactories: tt.ProviderFactories,
173+
CheckDestroy: testAccCheckScalewayCockpitDestroy(tt),
174+
Steps: []resource.TestStep{
175+
{
176+
Config: `
177+
resource "scaleway_account_project" "project" {
178+
name = "tf_tests_cockpit_project_premium"
179+
}
180+
181+
data "scaleway_cockpit_plan" "premium" {
182+
name = "premium"
183+
}
184+
185+
resource "scaleway_cockpit" "main" {
186+
project_id = scaleway_account_project.project.id
187+
plan = "premium"
188+
}
189+
`,
190+
Check: resource.ComposeTestCheckFunc(
191+
testAccCheckScalewayCockpitExists(tt, "scaleway_cockpit.main"),
192+
resource.TestCheckResourceAttrPair("scaleway_cockpit.main", "plan_id", "data.scaleway_cockpit_plan.premium", "id"),
193+
),
194+
},
90195
},
91196
})
92197
}

0 commit comments

Comments
 (0)