Skip to content

Commit 9898038

Browse files
authored
fix(cockpit): add default project id in cockpit resource (#2746)
1 parent bf7c0cd commit 9898038

20 files changed

+3024
-1975
lines changed

internal/services/cockpit/cockpit.go

+17-7
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ func ResourceCockpit() *schema.Resource {
2424
"plan": {
2525
Type: schema.TypeString,
2626
Optional: true,
27+
Default: "free",
2728
Description: "Name or ID of the plan",
2829
},
2930
"plan_id": {
@@ -98,6 +99,7 @@ func ResourceCockpitCreate(ctx context.Context, d *schema.ResourceData, m interf
9899
}
99100

100101
projectID := d.Get("project_id").(string)
102+
101103
if targetPlanI, ok := d.GetOk("plan"); ok {
102104
targetPlan := targetPlanI.(string)
103105

@@ -127,7 +129,6 @@ func ResourceCockpitCreate(ctx context.Context, d *schema.ResourceData, m interf
127129
}
128130
}
129131

130-
d.SetId(projectID)
131132
return ResourceCockpitRead(ctx, d, m)
132133
}
133134

@@ -142,37 +143,46 @@ func ResourceCockpitRead(ctx context.Context, d *schema.ResourceData, m interfac
142143
return diag.FromErr(err)
143144
}
144145

146+
projectID := d.Get("project_id").(string)
147+
if projectID == "" {
148+
projectID, err = getDefaultProjectID(ctx, m)
149+
if err != nil {
150+
return diag.FromErr(err)
151+
}
152+
}
153+
145154
res, err := api.GetCurrentPlan(&cockpit.GlobalAPIGetCurrentPlanRequest{
146-
ProjectID: d.Get("project_id").(string),
155+
ProjectID: projectID,
147156
}, scw.WithContext(ctx))
148157
if err != nil {
149158
return diag.FromErr(err)
150159
}
151-
_ = d.Set("project_id", d.Get("project_id").(string))
152160
_ = d.Set("plan", res.Name.String())
153161
_ = d.Set("plan_id", res.Name.String())
154162

155163
dataSourcesRes, err := regionalAPI.ListDataSources(&cockpit.RegionalAPIListDataSourcesRequest{
156164
Region: region,
157-
ProjectID: d.Get("project_id").(string),
165+
ProjectID: projectID,
158166
Origin: "external",
159167
}, scw.WithContext(ctx), scw.WithAllPages())
160168
if err != nil {
161169
return diag.FromErr(err)
162170
}
171+
_ = d.Set("project_id", projectID)
172+
d.SetId(projectID)
163173

164174
grafana, err := api.GetGrafana(&cockpit.GlobalAPIGetGrafanaRequest{
165-
ProjectID: d.Get("project_id").(string),
175+
ProjectID: projectID,
166176
}, scw.WithContext(ctx))
167177
if err != nil {
168178
return diag.FromErr(err)
169179
}
170180
if grafana.GrafanaURL == "" {
171-
grafana.GrafanaURL = createGrafanaURL(d.Get("project_id").(string), region)
181+
grafana.GrafanaURL = createGrafanaURL(projectID, region)
172182
}
173183

174184
alertManager, err := regionalAPI.GetAlertManager(&cockpit.RegionalAPIGetAlertManagerRequest{
175-
ProjectID: d.Get("project_id").(string),
185+
ProjectID: projectID,
176186
})
177187
if err != nil {
178188
return diag.FromErr(err)

internal/services/cockpit/cockpit_data_source.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ func dataSourceCockpitRead(ctx context.Context, d *schema.ResourceData, m interf
4646
}
4747

4848
projectID := d.Get("project_id").(string)
49+
if projectID == "" {
50+
projectID, err = getDefaultProjectID(ctx, m)
51+
if err != nil {
52+
return diag.FromErr(err)
53+
}
54+
}
4955

5056
res, err := api.GetCurrentPlan(&cockpit.GlobalAPIGetCurrentPlanRequest{
5157
ProjectID: projectID,
@@ -63,25 +69,25 @@ func dataSourceCockpitRead(ctx context.Context, d *schema.ResourceData, m interf
6369

6470
dataSourcesRes, err := regionalAPI.ListDataSources(&cockpit.RegionalAPIListDataSourcesRequest{
6571
Region: region,
66-
ProjectID: d.Get("project_id").(string),
72+
ProjectID: projectID,
6773
Origin: "external",
6874
}, scw.WithContext(ctx), scw.WithAllPages())
6975
if err != nil {
7076
return diag.FromErr(err)
7177
}
7278

7379
grafana, err := api.GetGrafana(&cockpit.GlobalAPIGetGrafanaRequest{
74-
ProjectID: d.Get("project_id").(string),
80+
ProjectID: projectID,
7581
}, scw.WithContext(ctx))
7682
if err != nil {
7783
return diag.FromErr(err)
7884
}
7985
if grafana.GrafanaURL == "" {
80-
grafana.GrafanaURL = createGrafanaURL(d.Get("project_id").(string), region)
86+
grafana.GrafanaURL = createGrafanaURL(projectID, region)
8187
}
8288

8389
alertManager, err := regionalAPI.GetAlertManager(&cockpit.RegionalAPIGetAlertManagerRequest{
84-
ProjectID: d.Get("project_id").(string),
90+
ProjectID: projectID,
8591
})
8692
if err != nil {
8793
return diag.FromErr(err)

internal/services/cockpit/cockpit_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,31 @@ import (
99
"github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest"
1010
)
1111

12+
func TestAccCockpit_Simple(t *testing.T) {
13+
tt := acctest.NewTestTools(t)
14+
defer tt.Cleanup()
15+
16+
resource.ParallelTest(t, resource.TestCase{
17+
PreCheck: func() { acctest.PreCheck(t) },
18+
ProviderFactories: tt.ProviderFactories,
19+
CheckDestroy: isCockpitDestroyed(tt),
20+
Steps: []resource.TestStep{
21+
{
22+
Config: `
23+
resource scaleway_cockpit main {
24+
}
25+
`,
26+
Check: resource.ComposeTestCheckFunc(
27+
resource.TestCheckResourceAttrSet("scaleway_cockpit.main", "plan"),
28+
resource.TestCheckResourceAttrSet("scaleway_cockpit.main", "plan_id"),
29+
resource.TestCheckResourceAttr("scaleway_cockpit.main", "plan", "free"),
30+
resource.TestCheckResourceAttrSet("scaleway_cockpit.main", "endpoints.0.grafana_url"),
31+
),
32+
},
33+
},
34+
})
35+
}
36+
1237
func TestAccCockpit_Basic(t *testing.T) {
1338
tt := acctest.NewTestTools(t)
1439
defer tt.Cleanup()

internal/services/cockpit/helpers_cockpit.go

+14
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ import (
99

1010
"github.com/hashicorp/go-cty/cty"
1111
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
accountSDK "github.com/scaleway/scaleway-sdk-go/api/account/v3"
1213
"github.com/scaleway/scaleway-sdk-go/api/cockpit/v1"
1314
"github.com/scaleway/scaleway-sdk-go/scw"
1415
"github.com/scaleway/scaleway-sdk-go/validation"
1516
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
1617
"github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
18+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
19+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
1720
)
1821

1922
const (
@@ -108,3 +111,14 @@ func cockpitTokenV1UpgradeFunc(_ context.Context, rawState map[string]interface{
108111

109112
return rawState, nil
110113
}
114+
115+
func getDefaultProjectID(ctx context.Context, m interface{}) (string, error) {
116+
accountAPI := account.NewProjectAPI(m)
117+
res, err := accountAPI.ListProjects(&accountSDK.ProjectAPIListProjectsRequest{
118+
Name: types.ExpandStringPtr("default"),
119+
}, scw.WithContext(ctx))
120+
if err != nil {
121+
return "", err
122+
}
123+
return res.Projects[0].ID, nil
124+
}

0 commit comments

Comments
 (0)