Skip to content

Commit 3c03399

Browse files
authored
Prometheus type migration check (#2431)
* add prom mig check * update wording * tests use require * clean up
1 parent a217859 commit 3c03399

File tree

2 files changed

+121
-2
lines changed

2 files changed

+121
-2
lines changed

internal/resources/grafana/resource_data_source.go

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,13 +245,16 @@ func CreateDataSource(ctx context.Context, d *schema.ResourceData, meta any) dia
245245
return diag.FromErr(err)
246246
}
247247

248+
diags := checkDeprecatedPrometheusAuth(d)
249+
248250
resp, err := client.Datasources.AddDataSource(dataSource)
249251
if err != nil {
250252
return diag.FromErr(err)
251253
}
252254

253255
d.SetId(MakeOrgResourceID(orgID, resp.Payload.Datasource.UID))
254-
return ReadDataSource(ctx, d, meta)
256+
readDiags := ReadDataSource(ctx, d, meta)
257+
return append(diags, readDiags...)
255258
}
256259

257260
// UpdateDataSource updates a Grafana datasource
@@ -262,6 +265,9 @@ func UpdateDataSource(ctx context.Context, d *schema.ResourceData, meta any) dia
262265
if err != nil {
263266
return diag.FromErr(err)
264267
}
268+
269+
diags := checkDeprecatedPrometheusAuth(d)
270+
265271
body := models.UpdateDataSourceCommand{
266272
Access: dataSource.Access,
267273
BasicAuth: dataSource.BasicAuth,
@@ -279,7 +285,11 @@ func UpdateDataSource(ctx context.Context, d *schema.ResourceData, meta any) dia
279285
}
280286
_, err = client.Datasources.UpdateDataSourceByUID(idStr, &body)
281287

282-
return diag.FromErr(err)
288+
if err != nil {
289+
return append(diags, diag.FromErr(err)...)
290+
}
291+
292+
return diags
283293
}
284294

285295
// ReadDataSource reads a Grafana datasource
@@ -450,3 +460,42 @@ func removeHeadersFromJSONData(input map[string]any) (map[string]any, map[string
450460

451461
return jsonData, headers
452462
}
463+
464+
// checkDeprecatedPrometheusAuth checks if the data source is using deprecated authentication methods
465+
func checkDeprecatedPrometheusAuth(d *schema.ResourceData) diag.Diagnostics {
466+
var diags diag.Diagnostics
467+
468+
dsType := d.Get("type").(string)
469+
if dsType != "prometheus" {
470+
return diags
471+
}
472+
473+
jsonDataEncoded := d.Get("json_data_encoded").(string)
474+
if jsonDataEncoded == "" {
475+
return diags
476+
}
477+
478+
var jsonData map[string]any
479+
if err := json.Unmarshal([]byte(jsonDataEncoded), &jsonData); err != nil {
480+
// If we can't parse it, don't add a warning
481+
return diags
482+
}
483+
484+
if sigV4Auth, ok := jsonData["sigV4Auth"].(bool); ok && sigV4Auth {
485+
diags = append(diags, diag.Diagnostic{
486+
Severity: diag.Warning,
487+
Summary: "Deprecated authentication method",
488+
Detail: "SigV4 authentication is deprecated for the core Prometheus data source. Please install Amazon Managed Service for Prometheus found here: https://grafana.com/grafana/plugins/grafana-amazonprometheus-datasource/ and then change the type of your data source to 'grafana-amazonprometheus-datasource'.",
489+
})
490+
}
491+
492+
if azureAuth, ok := jsonData["azureCredentials"]; ok && azureAuth != nil {
493+
diags = append(diags, diag.Diagnostic{
494+
Severity: diag.Warning,
495+
Summary: "Deprecated authentication method",
496+
Detail: "Azure authentication is deprecated for the core Prometheus data source. lease install Amazon Managed Service for Prometheus found here: https://grafana.com/grafana/plugins/grafana-azureprometheus-datasource/ and then change the type of your data source to 'grafana-azureprometheus-datasource'.",
497+
})
498+
}
499+
500+
return diags
501+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package grafana
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestCheckDeprecatedPrometheusAuth_SigV4(t *testing.T) {
12+
d := schema.TestResourceDataRaw(t, resourceDataSource().Schema.Schema, map[string]interface{}{
13+
"name": "test-prometheus",
14+
"type": "prometheus",
15+
"url": "http://localhost:9090",
16+
"json_data_encoded": `{"sigV4Auth":true}`,
17+
})
18+
19+
diags := checkDeprecatedPrometheusAuth(d)
20+
21+
if len(diags) != 1 {
22+
t.Errorf("Expected 1 diagnostic, got %d", len(diags))
23+
}
24+
25+
require.Equal(t, len(diags), 1)
26+
require.Equal(t, diags[0].Severity, diag.Warning)
27+
}
28+
29+
func TestCheckDeprecatedPrometheusAuth_Azure(t *testing.T) {
30+
d := schema.TestResourceDataRaw(t, resourceDataSource().Schema.Schema, map[string]interface{}{
31+
"name": "test-prometheus",
32+
"type": "prometheus",
33+
"url": "http://localhost:9090",
34+
"json_data_encoded": `{"azureCredentials":{}}`,
35+
})
36+
37+
diags := checkDeprecatedPrometheusAuth(d)
38+
39+
if len(diags) != 1 {
40+
t.Errorf("Expected 1 diagnostic, got %d", len(diags))
41+
}
42+
43+
require.Equal(t, len(diags), 1)
44+
require.Equal(t, diags[0].Severity, diag.Warning)
45+
}
46+
47+
func TestCheckDeprecatedPrometheusAuth_NoDeprecatedAuth(t *testing.T) {
48+
d := schema.TestResourceDataRaw(t, resourceDataSource().Schema.Schema, map[string]interface{}{
49+
"name": "test-prometheus",
50+
"type": "prometheus",
51+
"url": "http://localhost:9090",
52+
})
53+
54+
diags := checkDeprecatedPrometheusAuth(d)
55+
56+
require.Equal(t, len(diags), 0)
57+
}
58+
59+
func TestCheckDeprecatedPrometheusAuth_NonPrometheusDataSource(t *testing.T) {
60+
d := schema.TestResourceDataRaw(t, resourceDataSource().Schema.Schema, map[string]interface{}{
61+
"name": "test-loki",
62+
"type": "loki",
63+
"url": "http://localhost:9090",
64+
"json_data_encoded": `{"sigV4Auth":true}`,
65+
})
66+
67+
diags := checkDeprecatedPrometheusAuth(d)
68+
69+
require.Equal(t, len(diags), 0)
70+
}

0 commit comments

Comments
 (0)