Skip to content

Commit c02687d

Browse files
authored
feat:(cockpit): add push urls to cockpit datasources (#2700)
* t add push url in ressource source * handle error in createPushURL * fix test
1 parent a7e77ec commit c02687d

24 files changed

+2944
-1891
lines changed

docs/resources/cockpit_source.md

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ In addition to all arguments above, the following attributes are exported:
3939
~> **Important:** cockpit data sources' IDs are [regional](../guides/regions_and_zones.md#resource-ids), which means they are of the form `{region}/{id}`, e.g. `fr-par/11111111-1111-1111-1111-111111111111
4040

4141
- `url` - The URL of the cockpit data source.
42+
- `push_url` - The URL endpoint used for pushing data to the cockpit data source.
4243
- `origin` - The origin of the cockpit data source.
4344
- `synchronized_with_grafana` - Indicates whether the data source is synchronized with Grafana.
4445
- `created_at` - Date and time of the cockpit data source's creation (RFC 3339 format).

internal/services/cockpit/cockpit.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func ResourceCockpitRead(ctx context.Context, d *schema.ResourceData, m interfac
182182
endpoints := flattenCockpitEndpoints(dataSourcesRes.DataSources, grafana.GrafanaURL, alertManagerURL)
183183

184184
_ = d.Set("endpoints", endpoints)
185-
_ = d.Set("push_url", createCockpitPushURL(endpoints))
185+
_ = d.Set("push_url", createCockpitPushURLList(endpoints))
186186

187187
return nil
188188
}

internal/services/cockpit/cockpit_data_source.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func dataSourceCockpitRead(ctx context.Context, d *schema.ResourceData, m interf
9191
endpoints := flattenCockpitEndpoints(dataSourcesRes.DataSources, grafana.GrafanaURL, alertManagerURL)
9292

9393
_ = d.Set("endpoints", endpoints)
94-
_ = d.Set("push_url", createCockpitPushURL(endpoints))
94+
_ = d.Set("push_url", createCockpitPushURLList(endpoints))
9595
d.SetId(projectID)
9696
return nil
9797
}

internal/services/cockpit/cockpit_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func TestAccCockpit_WithSourceEndpoints(t *testing.T) {
9595
9696
resource "scaleway_cockpit_grafana_user" "main" {
9797
project_id = scaleway_account_project.project.id
98-
login = "cockpit_test"
98+
login = "cockpit_test_endpoint"
9999
role = "editor"
100100
}
101101

internal/services/cockpit/helpers_cockpit.go

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const (
2020
DefaultCockpitTimeout = 5 * time.Minute
2121
pathMetricsURL = "/api/v1/push"
2222
pathLogsURL = "/loki/api/v1/push"
23+
pathTracesURL = "/otlp/v1/traces"
2324
)
2425

2526
// NewGlobalAPI returns a new global cockpit API.

internal/services/cockpit/source.go

+11
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ func ResourceCockpitSource() *schema.Resource {
6868
Computed: true,
6969
Description: "The date and time of the last update of the cockpit datasource",
7070
},
71+
"push_url": {
72+
Type: schema.TypeString,
73+
Computed: true,
74+
Description: "The URL endpoint used for pushing data to the cockpit data source.",
75+
},
7176
"project_id": account.ProjectIDSchema(),
7277
"region": regional.Schema(),
7378
},
@@ -112,6 +117,11 @@ func ResourceCockpitSourceRead(ctx context.Context, d *schema.ResourceData, meta
112117
return diag.FromErr(err)
113118
}
114119

120+
pushURL, err := createCockpitPushURL(res.Type, res.URL)
121+
if err != nil {
122+
return diag.FromErr(err)
123+
}
124+
115125
_ = d.Set("name", res.Name)
116126
_ = d.Set("type", res.Type.String())
117127
_ = d.Set("url", res.URL)
@@ -121,6 +131,7 @@ func ResourceCockpitSourceRead(ctx context.Context, d *schema.ResourceData, meta
121131
_ = d.Set("created_at", types.FlattenTime(res.CreatedAt))
122132
_ = d.Set("updated_at", types.FlattenTime(res.UpdatedAt))
123133
_ = d.Set("project_id", res.ProjectID)
134+
_ = d.Set("push_url", pushURL)
124135

125136
return nil
126137
}

internal/services/cockpit/source_test.go

+41-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/cockpit"
1313
)
1414

15-
func TestAccCockpitSource_Basic(t *testing.T) {
15+
func TestAccCockpitSource_Basic_metrics(t *testing.T) {
1616
tt := acctest.NewTestTools(t)
1717
defer tt.Cleanup()
1818

@@ -39,6 +39,46 @@ func TestAccCockpitSource_Basic(t *testing.T) {
3939
resource.TestCheckResourceAttr("scaleway_cockpit_source.main", "type", "metrics"),
4040
resource.TestCheckResourceAttr("scaleway_cockpit_source.main", "region", "fr-par"),
4141
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "url"),
42+
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "push_url"),
43+
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "origin"),
44+
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "created_at"),
45+
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "updated_at"),
46+
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "synchronized_with_grafana"),
47+
resource.TestCheckResourceAttrPair("scaleway_cockpit_source.main", "project_id", "scaleway_account_project.project", "id"),
48+
),
49+
},
50+
},
51+
})
52+
}
53+
54+
func TestAccCockpitSource_Basic_logs(t *testing.T) {
55+
tt := acctest.NewTestTools(t)
56+
defer tt.Cleanup()
57+
58+
resource.ParallelTest(t, resource.TestCase{
59+
PreCheck: func() { acctest.PreCheck(t) },
60+
ProviderFactories: tt.ProviderFactories,
61+
CheckDestroy: isSourceDestroyed(tt),
62+
Steps: []resource.TestStep{
63+
{
64+
Config: `
65+
resource "scaleway_account_project" "project" {
66+
name = "tf_tests_cockpit_datasource_basic"
67+
}
68+
69+
resource "scaleway_cockpit_source" "main" {
70+
project_id = scaleway_account_project.project.id
71+
name = "my-source"
72+
type = "logs"
73+
}
74+
`,
75+
Check: resource.ComposeTestCheckFunc(
76+
isSourcePresent(tt, "scaleway_cockpit_source.main"),
77+
resource.TestCheckResourceAttr("scaleway_cockpit_source.main", "name", "my-source"),
78+
resource.TestCheckResourceAttr("scaleway_cockpit_source.main", "type", "logs"),
79+
resource.TestCheckResourceAttr("scaleway_cockpit_source.main", "region", "fr-par"),
80+
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "url"),
81+
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "push_url"),
4282
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "origin"),
4383
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "created_at"),
4484
resource.TestCheckResourceAttrSet("scaleway_cockpit_source.main", "updated_at"),

0 commit comments

Comments
 (0)