Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cockpit): add grafana url manual creation #2741

Merged
merged 2 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions docs/resources/cockpit.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ page_title: "Scaleway: scaleway_cockpit"
# Resource: scaleway_cockpit

-> **Note:**
As of April 2024, Cockpit has introduced [regionalization](https://www.scaleway.com/en/docs/observability/cockpit/concepts/#region) to offer more flexibility and resilience.
As of September 2024, Cockpit has introduced [regionalization](https://www.scaleway.com/en/docs/observability/cockpit/concepts/#region) to offer more flexibility and resilience.
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).

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.

The `scaleway_cockpit` resource allows you to create and manage Scaleway Cockpit instances.

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.
Expand Down Expand Up @@ -44,14 +46,15 @@ resource "scaleway_cockpit" "main" {

```terraform
// Use the Grafana Terraform provider to create a Grafana user and a Grafana folder in the default Project's Cockpit
resource "scaleway_cockpit" "main" {}

resource "scaleway_cockpit_grafana_user" "main" {
project_id = scaleway_cockpit.main.project_id
login = "example"
role = "editor"
project_id = scaleway_cockpit.main.project_id
login = "example"
role = "editor"
}

resource "scaleway_cockpit" "main" {}

provider "grafana" {
url = scaleway_cockpit.main.endpoints.0.grafana_url
auth = "${scaleway_cockpit_grafana_user.main.login}:${scaleway_cockpit_grafana_user.main.password}"
Expand Down
3 changes: 3 additions & 0 deletions internal/services/cockpit/cockpit.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ func ResourceCockpitRead(ctx context.Context, d *schema.ResourceData, m interfac
if err != nil {
return diag.FromErr(err)
}
if grafana.GrafanaURL == "" {
grafana.GrafanaURL = createGrafanaURL(d.Get("project_id").(string), region)
}

alertManager, err := regionalAPI.GetAlertManager(&cockpit.RegionalAPIGetAlertManagerRequest{
ProjectID: d.Get("project_id").(string),
Expand Down
3 changes: 3 additions & 0 deletions internal/services/cockpit/cockpit_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ func dataSourceCockpitRead(ctx context.Context, d *schema.ResourceData, m interf
if err != nil {
return diag.FromErr(err)
}
if grafana.GrafanaURL == "" {
grafana.GrafanaURL = createGrafanaURL(d.Get("project_id").(string), region)
}

alertManager, err := regionalAPI.GetAlertManager(&cockpit.RegionalAPIGetAlertManagerRequest{
ProjectID: d.Get("project_id").(string),
Expand Down
17 changes: 17 additions & 0 deletions internal/services/cockpit/cockpit_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cockpit_test

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
Expand Down Expand Up @@ -33,6 +34,8 @@ func TestAccCockpit_Basic(t *testing.T) {
resource.TestCheckResourceAttrSet("scaleway_cockpit.main", "plan"),
resource.TestCheckResourceAttrSet("scaleway_cockpit.main", "plan_id"),
resource.TestCheckResourceAttr("scaleway_cockpit.main", "plan", "free"),
resource.TestCheckResourceAttrSet("scaleway_cockpit.main", "endpoints.0.grafana_url"),
checkGrafanaURL("scaleway_cockpit.main", "scaleway_account_project.project"),
),
},
{
Expand Down Expand Up @@ -128,6 +131,20 @@ func TestAccCockpit_WithSourceEndpoints(t *testing.T) {
})
}

func checkGrafanaURL(resourceName, projectResource string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[projectResource]
if !ok {
return fmt.Errorf("Not found: %s", projectResource)
}

projectID := rs.Primary.ID
expectedURL := fmt.Sprintf("https://%s.dashboards.obs.fr-par.scw.cloud", projectID)

return resource.TestCheckResourceAttr(resourceName, "endpoints.0.grafana_url", expectedURL)(s)
}
}

func isCockpitDestroyed(_ *acctest.TestTools) resource.TestCheckFunc {
return func(_ *terraform.State) error {
return nil
Expand Down
5 changes: 5 additions & 0 deletions internal/services/cockpit/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/scaleway/scaleway-sdk-go/api/cockpit/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
)

var scopeMapping = map[string]cockpit.TokenScope{
Expand All @@ -18,6 +19,10 @@ var scopeMapping = map[string]cockpit.TokenScope{
"write_traces": cockpit.TokenScopeWriteOnlyTraces,
}

func createGrafanaURL(projectID string, region scw.Region) string {
return fmt.Sprintf("https://%s.dashboards.obs.%s.scw.cloud", projectID, region)
}

func flattenCockpitEndpoints(dataSources []*cockpit.DataSource, grafanaURL string, alertManagerURL string) []map[string]interface{} {
endpointMap := map[string]string{}

Expand Down
Loading