Skip to content

Commit 7bc5d34

Browse files
Deny importing read-only datasource (#1717)
Closes #1682
1 parent d390b07 commit 7bc5d34

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

internal/resources/grafana/resource_data_source.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,20 @@ source selected (via the 'type' argument).
3636
SchemaVersion: 1,
3737

3838
Importer: &schema.ResourceImporter{
39-
StateContext: schema.ImportStatePassthroughContext,
39+
StateContext: func(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
40+
client, _, idStr := OAPIClientFromExistingOrgResource(meta, d.Id())
41+
42+
resp, err := client.Datasources.GetDataSourceByUID(idStr)
43+
if err != nil {
44+
return nil, err
45+
}
46+
47+
if resp.Payload.ReadOnly {
48+
return nil, fmt.Errorf("this Grafana data source is read-only. It cannot be imported as a resource. Use the `data_grafana_data_source` data source instead")
49+
}
50+
51+
return schema.ImportStatePassthroughContext(ctx, d, meta)
52+
},
4053
},
4154

4255
Schema: map[string]*schema.Schema{

internal/resources/grafana/resource_data_source_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,24 @@ func TestAccDataSource_SeparateConfig(t *testing.T) {
426426
})
427427
}
428428

429+
func TestAccDataSource_ImportReadOnly(t *testing.T) {
430+
testutils.CheckCloudInstanceTestsEnabled(t)
431+
432+
resource.ParallelTest(t, resource.TestCase{
433+
ProtoV5ProviderFactories: testutils.ProtoV5ProviderFactories,
434+
Steps: []resource.TestStep{
435+
{
436+
Config: `resource "grafana_data_source" "prometheus" {}`,
437+
ImportState: true,
438+
ResourceName: "grafana_data_source.prometheus",
439+
ImportStateVerify: true,
440+
ImportStateId: "grafanacloud-prom",
441+
ExpectError: regexp.MustCompile("this Grafana data source is read-only. It cannot be imported as a resource. Use the `data_grafana_data_source` data source instead"),
442+
},
443+
},
444+
})
445+
}
446+
429447
func testAccDatasourceInOrganization(orgName string) string {
430448
return fmt.Sprintf(`
431449
resource "grafana_organization" "test" {

0 commit comments

Comments
 (0)