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

Deny importing read-only datasource #1717

Merged
merged 1 commit into from
Jul 26, 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
15 changes: 14 additions & 1 deletion internal/resources/grafana/resource_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,20 @@ source selected (via the 'type' argument).
SchemaVersion: 1,

Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
StateContext: func(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
client, _, idStr := OAPIClientFromExistingOrgResource(meta, d.Id())

resp, err := client.Datasources.GetDataSourceByUID(idStr)
if err != nil {
return nil, err
}

if resp.Payload.ReadOnly {
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")
}

return schema.ImportStatePassthroughContext(ctx, d, meta)
},
},

Schema: map[string]*schema.Schema{
Expand Down
18 changes: 18 additions & 0 deletions internal/resources/grafana/resource_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,24 @@ func TestAccDataSource_SeparateConfig(t *testing.T) {
})
}

func TestAccDataSource_ImportReadOnly(t *testing.T) {
testutils.CheckCloudInstanceTestsEnabled(t)

resource.ParallelTest(t, resource.TestCase{
ProtoV5ProviderFactories: testutils.ProtoV5ProviderFactories,
Steps: []resource.TestStep{
{
Config: `resource "grafana_data_source" "prometheus" {}`,
ImportState: true,
ResourceName: "grafana_data_source.prometheus",
ImportStateVerify: true,
ImportStateId: "grafanacloud-prom",
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"),
},
},
})
}

func testAccDatasourceInOrganization(orgName string) string {
return fmt.Sprintf(`
resource "grafana_organization" "test" {
Expand Down
Loading