Skip to content

Commit 1406903

Browse files
committed
fix(cockpit): add grafana url manual creation
1 parent ed1abf5 commit 1406903

File tree

5 files changed

+36
-6
lines changed

5 files changed

+36
-6
lines changed

docs/resources/cockpit.md

+8-5
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ page_title: "Scaleway: scaleway_cockpit"
66
# Resource: scaleway_cockpit
77

88
-> **Note:**
9-
As of April 2024, Cockpit has introduced [regionalization](https://www.scaleway.com/en/docs/observability/cockpit/concepts/#region) to offer more flexibility and resilience.
9+
As of September 2024, Cockpit has introduced [regionalization](https://www.scaleway.com/en/docs/observability/cockpit/concepts/#region) to offer more flexibility and resilience.
1010
If you have created customized dashboards with data for your Scaleway resources before April 2024, you will need to update your queries in Grafana, with the new regionalized [data sources](../resources/cockpit_source.md).
1111

12+
Please note that even if you provide the grafana_url, it will only be active if a [Grafana user](../resources/cockpit_grafana_user.md) is created first. Make sure to create a Grafana user in your Cockpit instance to enable full access to Grafana.
13+
1214
The `scaleway_cockpit` resource allows you to create and manage Scaleway Cockpit instances.
1315

1416
Refer to Cockpit's [product documentation](https://www.scaleway.com/en/docs/observability/cockpit/concepts/) and [API documentation](https://www.scaleway.com/en/developers/api/cockpit/regional-api) for more information.
@@ -44,14 +46,15 @@ resource "scaleway_cockpit" "main" {
4446

4547
```terraform
4648
// Use the Grafana Terraform provider to create a Grafana user and a Grafana folder in the default Project's Cockpit
47-
resource "scaleway_cockpit" "main" {}
4849
4950
resource "scaleway_cockpit_grafana_user" "main" {
50-
project_id = scaleway_cockpit.main.project_id
51-
login = "example"
52-
role = "editor"
51+
project_id = scaleway_cockpit.main.project_id
52+
login = "example"
53+
role = "editor"
5354
}
5455
56+
resource "scaleway_cockpit" "main" {}
57+
5558
provider "grafana" {
5659
url = scaleway_cockpit.main.endpoints.0.grafana_url
5760
auth = "${scaleway_cockpit_grafana_user.main.login}:${scaleway_cockpit_grafana_user.main.password}"

internal/services/cockpit/cockpit.go

+3
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ func ResourceCockpitRead(ctx context.Context, d *schema.ResourceData, m interfac
167167
if err != nil {
168168
return diag.FromErr(err)
169169
}
170+
if grafana.GrafanaURL == "" {
171+
grafana.GrafanaURL = createGrafanaUrl(d.Get("project_id").(string))
172+
}
170173

171174
alertManager, err := regionalAPI.GetAlertManager(&cockpit.RegionalAPIGetAlertManagerRequest{
172175
ProjectID: d.Get("project_id").(string),

internal/services/cockpit/cockpit_data_source.go

+3
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ func dataSourceCockpitRead(ctx context.Context, d *schema.ResourceData, m interf
7676
if err != nil {
7777
return diag.FromErr(err)
7878
}
79+
if grafana.GrafanaURL == "" {
80+
grafana.GrafanaURL = createGrafanaUrl(d.Get("project_id").(string))
81+
}
7982

8083
alertManager, err := regionalAPI.GetAlertManager(&cockpit.RegionalAPIGetAlertManagerRequest{
8184
ProjectID: d.Get("project_id").(string),

internal/services/cockpit/cockpit_test.go

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cockpit_test
22

33
import (
4+
"fmt"
45
"testing"
56

67
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
@@ -33,6 +34,8 @@ func TestAccCockpit_Basic(t *testing.T) {
3334
resource.TestCheckResourceAttrSet("scaleway_cockpit.main", "plan"),
3435
resource.TestCheckResourceAttrSet("scaleway_cockpit.main", "plan_id"),
3536
resource.TestCheckResourceAttr("scaleway_cockpit.main", "plan", "free"),
37+
resource.TestCheckResourceAttrSet("scaleway_cockpit.main", "endpoints.0.grafana_url"),
38+
checkGrafanaURL("scaleway_cockpit.main", "scaleway_account_project.project"),
3639
),
3740
},
3841
{
@@ -56,7 +59,7 @@ func TestAccCockpit_Basic(t *testing.T) {
5659
}
5760

5861
func TestAccCockpit_WithSourceEndpoints(t *testing.T) {
59-
t.Skip("TestAccCockpit_WithSourceEndpoints skipped: encountered repeated HTTP 500 errors from the Scaleway Cockpit API.")
62+
//t.Skip("TestAccCockpit_WithSourceEndpoints skipped: encountered repeated HTTP 500 errors from the Scaleway Cockpit API.")
6063
tt := acctest.NewTestTools(t)
6164
defer tt.Cleanup()
6265

@@ -128,6 +131,20 @@ func TestAccCockpit_WithSourceEndpoints(t *testing.T) {
128131
})
129132
}
130133

134+
func checkGrafanaURL(resourceName, projectResource string) resource.TestCheckFunc {
135+
return func(s *terraform.State) error {
136+
rs, ok := s.RootModule().Resources[projectResource]
137+
if !ok {
138+
return fmt.Errorf("Not found: %s", projectResource)
139+
}
140+
141+
projectID := rs.Primary.ID
142+
expectedURL := fmt.Sprintf("https://%s.dashboards.obs.fr-par.scw.cloud", projectID)
143+
144+
return resource.TestCheckResourceAttr(resourceName, "endpoints.0.grafana_url", expectedURL)(s)
145+
}
146+
}
147+
131148
func isCockpitDestroyed(_ *acctest.TestTools) resource.TestCheckFunc {
132149
return func(_ *terraform.State) error {
133150
return nil

internal/services/cockpit/types.go

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ var scopeMapping = map[string]cockpit.TokenScope{
1818
"write_traces": cockpit.TokenScopeWriteOnlyTraces,
1919
}
2020

21+
func createGrafanaUrl(projectID string) string {
22+
return fmt.Sprintf("https://%s.dashboards.obs.fr-par.scw.cloud", projectID)
23+
}
24+
2125
func flattenCockpitEndpoints(dataSources []*cockpit.DataSource, grafanaURL string, alertManagerURL string) []map[string]interface{} {
2226
endpointMap := map[string]string{}
2327

0 commit comments

Comments
 (0)