From 4e17cfafe78eada64ee35b1b3822c1888e85100f Mon Sep 17 00:00:00 2001 From: jremy Date: Wed, 18 Sep 2024 16:01:37 +0200 Subject: [PATCH] fix(cockpit): add default project id in cockpit resource --- internal/services/cockpit/cockpit.go | 8 +- internal/services/cockpit/cockpit_test.go | 25 + .../testdata/cockpit-simple.cassette.yaml | 495 ++++++++++++++++++ 3 files changed, 525 insertions(+), 3 deletions(-) create mode 100644 internal/services/cockpit/testdata/cockpit-simple.cassette.yaml diff --git a/internal/services/cockpit/cockpit.go b/internal/services/cockpit/cockpit.go index 0e373bd0a..058df42bf 100644 --- a/internal/services/cockpit/cockpit.go +++ b/internal/services/cockpit/cockpit.go @@ -24,6 +24,7 @@ func ResourceCockpit() *schema.Resource { "plan": { Type: schema.TypeString, Optional: true, + Default: "free", Description: "Name or ID of the plan", }, "plan_id": { @@ -98,6 +99,7 @@ func ResourceCockpitCreate(ctx context.Context, d *schema.ResourceData, m interf } projectID := d.Get("project_id").(string) + if targetPlanI, ok := d.GetOk("plan"); ok { targetPlan := targetPlanI.(string) @@ -127,7 +129,6 @@ func ResourceCockpitCreate(ctx context.Context, d *schema.ResourceData, m interf } } - d.SetId(projectID) return ResourceCockpitRead(ctx, d, m) } @@ -148,7 +149,6 @@ func ResourceCockpitRead(ctx context.Context, d *schema.ResourceData, m interfac if err != nil { return diag.FromErr(err) } - _ = d.Set("project_id", d.Get("project_id").(string)) _ = d.Set("plan", res.Name.String()) _ = d.Set("plan_id", res.Name.String()) @@ -160,6 +160,8 @@ func ResourceCockpitRead(ctx context.Context, d *schema.ResourceData, m interfac if err != nil { return diag.FromErr(err) } + _ = d.Set("project_id", dataSourcesRes.DataSources[0].ProjectID) + d.SetId(dataSourcesRes.DataSources[0].ProjectID) grafana, err := api.GetGrafana(&cockpit.GlobalAPIGetGrafanaRequest{ ProjectID: d.Get("project_id").(string), @@ -168,7 +170,7 @@ func ResourceCockpitRead(ctx context.Context, d *schema.ResourceData, m interfac return diag.FromErr(err) } if grafana.GrafanaURL == "" { - grafana.GrafanaURL = createGrafanaURL(d.Get("project_id").(string), region) + grafana.GrafanaURL = createGrafanaURL(dataSourcesRes.DataSources[0].ProjectID, region) } alertManager, err := regionalAPI.GetAlertManager(&cockpit.RegionalAPIGetAlertManagerRequest{ diff --git a/internal/services/cockpit/cockpit_test.go b/internal/services/cockpit/cockpit_test.go index fdab48c32..4debd0b75 100644 --- a/internal/services/cockpit/cockpit_test.go +++ b/internal/services/cockpit/cockpit_test.go @@ -9,6 +9,31 @@ import ( "github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest" ) +func TestAccCockpit_Simple(t *testing.T) { + tt := acctest.NewTestTools(t) + defer tt.Cleanup() + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(t) }, + ProviderFactories: tt.ProviderFactories, + CheckDestroy: isCockpitDestroyed(tt), + Steps: []resource.TestStep{ + { + Config: ` + resource scaleway_cockpit main { + } + `, + Check: resource.ComposeTestCheckFunc( + 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"), + ), + }, + }, + }) +} + func TestAccCockpit_Basic(t *testing.T) { tt := acctest.NewTestTools(t) defer tt.Cleanup() diff --git a/internal/services/cockpit/testdata/cockpit-simple.cassette.yaml b/internal/services/cockpit/testdata/cockpit-simple.cassette.yaml new file mode 100644 index 000000000..0e0f631af --- /dev/null +++ b/internal/services/cockpit/testdata/cockpit-simple.cassette.yaml @@ -0,0 +1,495 @@ +--- +version: 2 +interactions: + - id: 0 + request: + proto: HTTP/1.1 + proto_major: 1 + proto_minor: 1 + content_length: 0 + transfer_encoding: [] + trailer: {} + host: api.scaleway.com + remote_addr: "" + request_uri: "" + body: "" + form: {} + headers: + User-Agent: + - scaleway-sdk-go/v1.0.0-beta.7+dev (go1.23.0; darwin; amd64) terraform-provider/develop terraform/terraform-tests + url: https://api.scaleway.com/cockpit/v1/plans?order_by=name_asc&page=1 + method: GET + response: + proto: HTTP/2.0 + proto_major: 2 + proto_minor: 0 + transfer_encoding: [] + trailer: {} + content_length: 754 + uncompressed: false + body: '{"plans":[{"logs_ingestion_price":35,"monthly_price":29,"name":"custom","retention_logs_interval":"2678400s","retention_metrics_interval":"31536000s","retention_traces_interval":"2678400s","sample_ingestion_price":15,"traces_ingestion_price":35},{"logs_ingestion_price":35,"monthly_price":0,"name":"free","retention_logs_interval":"604800s","retention_metrics_interval":"2678400s","retention_traces_interval":"604800s","sample_ingestion_price":15,"traces_ingestion_price":35},{"logs_ingestion_price":35,"monthly_price":29,"name":"premium","retention_logs_interval":"2678400s","retention_metrics_interval":"31536000s","retention_traces_interval":"2678400s","sample_ingestion_price":15,"traces_ingestion_price":35}],"total_count":3}' + headers: + Content-Length: + - "754" + Content-Security-Policy: + - default-src 'none'; frame-ancestors 'none' + Content-Type: + - application/json + Date: + - Wed, 18 Sep 2024 13:52:10 GMT + Server: + - Scaleway API Gateway (fr-par-2;edge03) + Strict-Transport-Security: + - max-age=63072000 + X-Content-Type-Options: + - nosniff + X-Frame-Options: + - DENY + X-Request-Id: + - 94eadd00-749f-4f9c-b026-aa75e4b6b1c3 + status: 200 OK + code: 200 + duration: 110.201361ms + - id: 1 + request: + proto: HTTP/1.1 + proto_major: 1 + proto_minor: 1 + content_length: 72 + transfer_encoding: [] + trailer: {} + host: api.scaleway.com + remote_addr: "" + request_uri: "" + body: '{"project_id":"105bdce1-64c0-48ab-899d-868455867ecf","plan_name":"free"}' + form: {} + headers: + Content-Type: + - application/json + User-Agent: + - scaleway-sdk-go/v1.0.0-beta.7+dev (go1.23.0; darwin; amd64) terraform-provider/develop terraform/terraform-tests + url: https://api.scaleway.com/cockpit/v1/plans + method: PATCH + response: + proto: HTTP/2.0 + proto_major: 2 + proto_minor: 0 + transfer_encoding: [] + trailer: {} + content_length: 236 + uncompressed: false + body: '{"logs_ingestion_price":35,"monthly_price":0,"name":"free","retention_logs_interval":"604800s","retention_metrics_interval":"2678400s","retention_traces_interval":"604800s","sample_ingestion_price":15,"traces_ingestion_price":35}' + headers: + Content-Length: + - "236" + Content-Security-Policy: + - default-src 'none'; frame-ancestors 'none' + Content-Type: + - application/json + Date: + - Wed, 18 Sep 2024 13:52:11 GMT + Server: + - Scaleway API Gateway (fr-par-2;edge03) + Strict-Transport-Security: + - max-age=63072000 + X-Content-Type-Options: + - nosniff + X-Frame-Options: + - DENY + X-Request-Id: + - 1626a7a2-587e-4cf6-8f2b-0417c0a2fc68 + status: 200 OK + code: 200 + duration: 126.324104ms + - id: 2 + request: + proto: HTTP/1.1 + proto_major: 1 + proto_minor: 1 + content_length: 0 + transfer_encoding: [] + trailer: {} + host: api.scaleway.com + remote_addr: "" + request_uri: "" + body: "" + form: {} + headers: + User-Agent: + - scaleway-sdk-go/v1.0.0-beta.7+dev (go1.23.0; darwin; amd64) terraform-provider/develop terraform/terraform-tests + url: https://api.scaleway.com/cockpit/v1/current-plan?project_id=105bdce1-64c0-48ab-899d-868455867ecf + method: GET + response: + proto: HTTP/2.0 + proto_major: 2 + proto_minor: 0 + transfer_encoding: [] + trailer: {} + content_length: 236 + uncompressed: false + body: '{"logs_ingestion_price":35,"monthly_price":0,"name":"free","retention_logs_interval":"604800s","retention_metrics_interval":"2678400s","retention_traces_interval":"604800s","sample_ingestion_price":15,"traces_ingestion_price":35}' + headers: + Content-Length: + - "236" + Content-Security-Policy: + - default-src 'none'; frame-ancestors 'none' + Content-Type: + - application/json + Date: + - Wed, 18 Sep 2024 13:52:11 GMT + Server: + - Scaleway API Gateway (fr-par-2;edge03) + Strict-Transport-Security: + - max-age=63072000 + X-Content-Type-Options: + - nosniff + X-Frame-Options: + - DENY + X-Request-Id: + - cf327949-f528-4454-9929-32b0dad4be52 + status: 200 OK + code: 200 + duration: 69.415894ms + - id: 3 + request: + proto: HTTP/1.1 + proto_major: 1 + proto_minor: 1 + content_length: 0 + transfer_encoding: [] + trailer: {} + host: api.scaleway.com + remote_addr: "" + request_uri: "" + body: "" + form: {} + headers: + User-Agent: + - scaleway-sdk-go/v1.0.0-beta.7+dev (go1.23.0; darwin; amd64) terraform-provider/develop terraform/terraform-tests + url: https://api.scaleway.com/cockpit/v1/regions/fr-par/data-sources?order_by=created_at_asc&origin=external&page=1&project_id=105bdce1-64c0-48ab-899d-868455867ecf + method: GET + response: + proto: HTTP/2.0 + proto_major: 2 + proto_minor: 0 + transfer_encoding: [] + trailer: {} + content_length: 795 + uncompressed: false + body: '{"data_sources":[{"created_at":"2023-05-17T09:46:07.224133Z","id":"b878efd7-8d6e-4486-94ed-57094273da22","name":"Metrics","origin":"external","project_id":"105bdce1-64c0-48ab-899d-868455867ecf","region":"fr-par","synchronized_with_grafana":true,"type":"metrics","updated_at":"2023-05-17T09:46:07.224133Z","url":"https://b878efd7-8d6e-4486-94ed-57094273da22.metrics.cockpit.fr-par.scw.cloud"},{"created_at":"2023-05-17T09:46:07.231274Z","id":"73918c2a-a652-4bab-a8e4-2709aae02e5e","name":"Logs","origin":"external","project_id":"105bdce1-64c0-48ab-899d-868455867ecf","region":"fr-par","synchronized_with_grafana":true,"type":"logs","updated_at":"2023-05-17T09:46:07.231274Z","url":"https://73918c2a-a652-4bab-a8e4-2709aae02e5e.logs.cockpit.fr-par.scw.cloud"}],"total_count":2}' + headers: + Content-Length: + - "795" + Content-Security-Policy: + - default-src 'none'; frame-ancestors 'none' + Content-Type: + - application/json + Date: + - Wed, 18 Sep 2024 13:52:11 GMT + Server: + - Scaleway API Gateway (fr-par-2;edge03) + Strict-Transport-Security: + - max-age=63072000 + X-Content-Type-Options: + - nosniff + X-Frame-Options: + - DENY + X-Request-Id: + - 3114fdd0-c665-4f4a-b427-3f7fa127326e + status: 200 OK + code: 200 + duration: 402.184114ms + - id: 4 + request: + proto: HTTP/1.1 + proto_major: 1 + proto_minor: 1 + content_length: 0 + transfer_encoding: [] + trailer: {} + host: api.scaleway.com + remote_addr: "" + request_uri: "" + body: "" + form: {} + headers: + User-Agent: + - scaleway-sdk-go/v1.0.0-beta.7+dev (go1.23.0; darwin; amd64) terraform-provider/develop terraform/terraform-tests + url: https://api.scaleway.com/cockpit/v1/grafana?project_id=105bdce1-64c0-48ab-899d-868455867ecf + method: GET + response: + proto: HTTP/2.0 + proto_major: 2 + proto_minor: 0 + transfer_encoding: [] + trailer: {} + content_length: 18 + uncompressed: false + body: '{"grafana_url":""}' + headers: + Content-Length: + - "18" + Content-Security-Policy: + - default-src 'none'; frame-ancestors 'none' + Content-Type: + - application/json + Date: + - Wed, 18 Sep 2024 13:52:11 GMT + Server: + - Scaleway API Gateway (fr-par-2;edge03) + Strict-Transport-Security: + - max-age=63072000 + X-Content-Type-Options: + - nosniff + X-Frame-Options: + - DENY + X-Request-Id: + - f7331a41-bd74-40ac-ac97-53ea4fff7e88 + status: 200 OK + code: 200 + duration: 66.096583ms + - id: 5 + request: + proto: HTTP/1.1 + proto_major: 1 + proto_minor: 1 + content_length: 0 + transfer_encoding: [] + trailer: {} + host: api.scaleway.com + remote_addr: "" + request_uri: "" + body: "" + form: {} + headers: + User-Agent: + - scaleway-sdk-go/v1.0.0-beta.7+dev (go1.23.0; darwin; amd64) terraform-provider/develop terraform/terraform-tests + url: https://api.scaleway.com/cockpit/v1/regions/fr-par/alert-manager?project_id=105bdce1-64c0-48ab-899d-868455867ecf + method: GET + response: + proto: HTTP/2.0 + proto_major: 2 + proto_minor: 0 + transfer_encoding: [] + trailer: {} + content_length: 186 + uncompressed: false + body: '{"alert_manager_enabled":true,"alert_manager_url":"https://53bfc50f-0420-4c11-bef8-07bf3f1c0fbb.alertmanager.cockpit.fr-par.scw.cloud","managed_alerts_enabled":true,"region":"fr-par"}' + headers: + Content-Length: + - "186" + Content-Security-Policy: + - default-src 'none'; frame-ancestors 'none' + Content-Type: + - application/json + Date: + - Wed, 18 Sep 2024 13:52:11 GMT + Server: + - Scaleway API Gateway (fr-par-2;edge03) + Strict-Transport-Security: + - max-age=63072000 + X-Content-Type-Options: + - nosniff + X-Frame-Options: + - DENY + X-Request-Id: + - ac81cce7-eec6-4a55-bbc4-0866e2663a92 + status: 200 OK + code: 200 + duration: 89.928757ms + - id: 6 + request: + proto: HTTP/1.1 + proto_major: 1 + proto_minor: 1 + content_length: 0 + transfer_encoding: [] + trailer: {} + host: api.scaleway.com + remote_addr: "" + request_uri: "" + body: "" + form: {} + headers: + User-Agent: + - scaleway-sdk-go/v1.0.0-beta.7+dev (go1.23.0; darwin; amd64) terraform-provider/develop terraform/terraform-tests + url: https://api.scaleway.com/cockpit/v1/current-plan?project_id=105bdce1-64c0-48ab-899d-868455867ecf + method: GET + response: + proto: HTTP/2.0 + proto_major: 2 + proto_minor: 0 + transfer_encoding: [] + trailer: {} + content_length: 236 + uncompressed: false + body: '{"logs_ingestion_price":35,"monthly_price":0,"name":"free","retention_logs_interval":"604800s","retention_metrics_interval":"2678400s","retention_traces_interval":"604800s","sample_ingestion_price":15,"traces_ingestion_price":35}' + headers: + Content-Length: + - "236" + Content-Security-Policy: + - default-src 'none'; frame-ancestors 'none' + Content-Type: + - application/json + Date: + - Wed, 18 Sep 2024 13:52:12 GMT + Server: + - Scaleway API Gateway (fr-par-2;edge03) + Strict-Transport-Security: + - max-age=63072000 + X-Content-Type-Options: + - nosniff + X-Frame-Options: + - DENY + X-Request-Id: + - bff37a5b-35f5-4cd0-8412-ebdefb0dbaaa + status: 200 OK + code: 200 + duration: 93.981768ms + - id: 7 + request: + proto: HTTP/1.1 + proto_major: 1 + proto_minor: 1 + content_length: 0 + transfer_encoding: [] + trailer: {} + host: api.scaleway.com + remote_addr: "" + request_uri: "" + body: "" + form: {} + headers: + User-Agent: + - scaleway-sdk-go/v1.0.0-beta.7+dev (go1.23.0; darwin; amd64) terraform-provider/develop terraform/terraform-tests + url: https://api.scaleway.com/cockpit/v1/regions/fr-par/data-sources?order_by=created_at_asc&origin=external&page=1&project_id=105bdce1-64c0-48ab-899d-868455867ecf + method: GET + response: + proto: HTTP/2.0 + proto_major: 2 + proto_minor: 0 + transfer_encoding: [] + trailer: {} + content_length: 795 + uncompressed: false + body: '{"data_sources":[{"created_at":"2023-05-17T09:46:07.224133Z","id":"b878efd7-8d6e-4486-94ed-57094273da22","name":"Metrics","origin":"external","project_id":"105bdce1-64c0-48ab-899d-868455867ecf","region":"fr-par","synchronized_with_grafana":true,"type":"metrics","updated_at":"2023-05-17T09:46:07.224133Z","url":"https://b878efd7-8d6e-4486-94ed-57094273da22.metrics.cockpit.fr-par.scw.cloud"},{"created_at":"2023-05-17T09:46:07.231274Z","id":"73918c2a-a652-4bab-a8e4-2709aae02e5e","name":"Logs","origin":"external","project_id":"105bdce1-64c0-48ab-899d-868455867ecf","region":"fr-par","synchronized_with_grafana":true,"type":"logs","updated_at":"2023-05-17T09:46:07.231274Z","url":"https://73918c2a-a652-4bab-a8e4-2709aae02e5e.logs.cockpit.fr-par.scw.cloud"}],"total_count":2}' + headers: + Content-Length: + - "795" + Content-Security-Policy: + - default-src 'none'; frame-ancestors 'none' + Content-Type: + - application/json + Date: + - Wed, 18 Sep 2024 13:52:12 GMT + Server: + - Scaleway API Gateway (fr-par-2;edge03) + Strict-Transport-Security: + - max-age=63072000 + X-Content-Type-Options: + - nosniff + X-Frame-Options: + - DENY + X-Request-Id: + - 4d3f65cf-55b4-49d5-b0bd-e7763e6ec6b4 + status: 200 OK + code: 200 + duration: 254.722151ms + - id: 8 + request: + proto: HTTP/1.1 + proto_major: 1 + proto_minor: 1 + content_length: 0 + transfer_encoding: [] + trailer: {} + host: api.scaleway.com + remote_addr: "" + request_uri: "" + body: "" + form: {} + headers: + User-Agent: + - scaleway-sdk-go/v1.0.0-beta.7+dev (go1.23.0; darwin; amd64) terraform-provider/develop terraform/terraform-tests + url: https://api.scaleway.com/cockpit/v1/grafana?project_id=105bdce1-64c0-48ab-899d-868455867ecf + method: GET + response: + proto: HTTP/2.0 + proto_major: 2 + proto_minor: 0 + transfer_encoding: [] + trailer: {} + content_length: 18 + uncompressed: false + body: '{"grafana_url":""}' + headers: + Content-Length: + - "18" + Content-Security-Policy: + - default-src 'none'; frame-ancestors 'none' + Content-Type: + - application/json + Date: + - Wed, 18 Sep 2024 13:52:12 GMT + Server: + - Scaleway API Gateway (fr-par-2;edge03) + Strict-Transport-Security: + - max-age=63072000 + X-Content-Type-Options: + - nosniff + X-Frame-Options: + - DENY + X-Request-Id: + - 197a9ddd-926c-4573-9756-3cb3ee772698 + status: 200 OK + code: 200 + duration: 70.89669ms + - id: 9 + request: + proto: HTTP/1.1 + proto_major: 1 + proto_minor: 1 + content_length: 0 + transfer_encoding: [] + trailer: {} + host: api.scaleway.com + remote_addr: "" + request_uri: "" + body: "" + form: {} + headers: + User-Agent: + - scaleway-sdk-go/v1.0.0-beta.7+dev (go1.23.0; darwin; amd64) terraform-provider/develop terraform/terraform-tests + url: https://api.scaleway.com/cockpit/v1/regions/fr-par/alert-manager?project_id=105bdce1-64c0-48ab-899d-868455867ecf + method: GET + response: + proto: HTTP/2.0 + proto_major: 2 + proto_minor: 0 + transfer_encoding: [] + trailer: {} + content_length: 186 + uncompressed: false + body: '{"alert_manager_enabled":true,"alert_manager_url":"https://53bfc50f-0420-4c11-bef8-07bf3f1c0fbb.alertmanager.cockpit.fr-par.scw.cloud","managed_alerts_enabled":true,"region":"fr-par"}' + headers: + Content-Length: + - "186" + Content-Security-Policy: + - default-src 'none'; frame-ancestors 'none' + Content-Type: + - application/json + Date: + - Wed, 18 Sep 2024 13:52:12 GMT + Server: + - Scaleway API Gateway (fr-par-2;edge03) + Strict-Transport-Security: + - max-age=63072000 + X-Content-Type-Options: + - nosniff + X-Frame-Options: + - DENY + X-Request-Id: + - 9302a6ea-ee8b-4fff-84a5-d09de49cf436 + status: 200 OK + code: 200 + duration: 86.250083ms