Skip to content

Commit 27bf677

Browse files
authored
Prevent a provider panic when the referenced snapshot repo does not exist (#758)
* Prevent a provider panic when the referenced snapshot repo does not exist * Changelog
1 parent 7b9f454 commit 27bf677

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
- Fix a provider panic when `elasticstack_kibana_action_connector` reads a non-existant connector ([#729](https://github.com/elastic/terraform-provider-elasticstack/pull/729))
66
- Add support for `remote_indicies` to `elasticstack_elasticsearch_security_role` & `elasticstack_kibana_security_role` (#723)[https://github.com/elastic/terraform-provider-elasticstack/pull/723]
77
- Fix error handling in `elasticstack_kibana_import_saved_objects` ([#738](https://github.com/elastic/terraform-provider-elasticstack/pull/738))
8-
- Remove `space_id` parameter from private locations to fix inconsistent state for `elasticstack_kibana_synthetics_private_location` `space_id` ([#733](https://github.com/elastic/terraform-provider-elasticstack/pull/733))
8+
- Remove `space_id` parameter from private locations to fix inconsistent state for `elasticstack_kibana_synthetics_private_location` `space_id` ([#733](https://github.com/elastic/terraform-provider-elasticstack/pull/733))
9+
- Prevent a provider panic when the repository referenced in an `elasticstack_elasticsearch_snapshot_repository` does not exist ([#758](https://github.com/elastic/terraform-provider-elasticstack/pull/758))
910

1011
## [0.11.6] - 2024-08-20
1112

internal/elasticsearch/cluster/snapshot_repository_data_source.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,16 @@ func dataSourceSnapRepoRead(ctx context.Context, d *schema.ResourceData, meta in
272272
return diags
273273
}
274274

275+
d.SetId(id.String())
276+
if currentRepo == nil {
277+
return diag.Diagnostics{
278+
diag.Diagnostic{
279+
Severity: diag.Warning,
280+
Summary: fmt.Sprintf("Could not find snapshot repository [%s]", repoName),
281+
},
282+
}
283+
}
284+
275285
// get the schema of the Elem of the current repo type
276286
schemaSettings := DataSourceSnapshotRespository().Schema[currentRepo.Type].Elem.(*schema.Resource).Schema
277287
settings, err := flattenRepoSettings(currentRepo, schemaSettings)
@@ -291,6 +301,5 @@ func dataSourceSnapRepoRead(ctx context.Context, d *schema.ResourceData, meta in
291301
return diag.FromErr(err)
292302
}
293303

294-
d.SetId(id.String())
295304
return diags
296305
}

internal/elasticsearch/cluster/snapshot_repository_data_source_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,27 @@ import (
99
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1010
)
1111

12+
func TestAccDataSourceSnapRepoMissing(t *testing.T) {
13+
name := sdkacctest.RandStringFromCharSet(10, sdkacctest.CharSetAlphaNum)
14+
15+
resource.Test(t, resource.TestCase{
16+
PreCheck: func() { acctest.PreCheck(t) },
17+
ProtoV6ProviderFactories: acctest.Providers,
18+
Steps: []resource.TestStep{
19+
{
20+
Config: fmt.Sprintf(`
21+
provider "elasticstack" {
22+
elasticsearch {}
23+
}
24+
25+
data "elasticstack_elasticsearch_snapshot_repository" "test_fs_repo" {
26+
name = "%s"
27+
}`, name),
28+
},
29+
},
30+
})
31+
}
32+
1233
func TestAccDataSourceSnapRepoFs(t *testing.T) {
1334
name := sdkacctest.RandStringFromCharSet(10, sdkacctest.CharSetAlphaNum)
1435

0 commit comments

Comments
 (0)