From 7d10b69982214a35f35c954c068c8fe9d0525d37 Mon Sep 17 00:00:00 2001 From: pranav-new-relic Date: Wed, 27 Mar 2024 15:13:18 +0530 Subject: [PATCH 1/3] fix(entity): add argument ignore_not_found to ignore not found error --- newrelic/data_source_newrelic_entity.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/newrelic/data_source_newrelic_entity.go b/newrelic/data_source_newrelic_entity.go index 1273c89db..fd16dbbe5 100644 --- a/newrelic/data_source_newrelic_entity.go +++ b/newrelic/data_source_newrelic_entity.go @@ -45,6 +45,12 @@ func dataSourceNewRelicEntity() *schema.Resource { return strings.EqualFold(old, new) // Case fold this attribute when diffing }, }, + "ignore_not_found": { + Type: schema.TypeBool, + Default: false, + Optional: true, + Description: "A boolean attribute which when set to true, does not throw an error if the queried entity is not found.", + }, "tag": { Type: schema.TypeList, Optional: true, @@ -144,7 +150,21 @@ func dataSourceNewRelicEntityRead(ctx context.Context, d *schema.ResourceData, m } if entity == nil { - return diag.FromErr(fmt.Errorf("no entities found for the provided search parameters, please ensure your schema attributes are valid")) + if d.Get("ignore_not_found").(bool) { + log.Printf("[INFO] Entity not found, ignoring error") + d.SetId("") + var diags diag.Diagnostics + diags = append(diags, diag.Diagnostic{ + Severity: diag.Warning, + Summary: "no entities found for the provided search parameters, please ensure your schema attributes are valid.\n" + + "This message is being displayed as a warning and not as an error as `ignore_not_found` has been set to true.\n" + + "Ignoring the 'not found' error can lead to downstream errors if the values exported by this data source are\n" + + "used elsewhere, since the value of all exported attributes would be null. Please use this attribute at your own risk.\n", + }) + return diags + } else { + return diag.FromErr(fmt.Errorf("no entities found for the provided search parameters, please ensure your schema attributes are valid")) + } } return diag.FromErr(flattenEntityData(entity, d)) From 4e1169f5c7369d3344311fbb9d8309a5bce1ba2e Mon Sep 17 00:00:00 2001 From: pranav-new-relic Date: Wed, 27 Mar 2024 15:50:02 +0530 Subject: [PATCH 2/3] chore: lint fix --- newrelic/data_source_newrelic_entity.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/newrelic/data_source_newrelic_entity.go b/newrelic/data_source_newrelic_entity.go index fd16dbbe5..99f838671 100644 --- a/newrelic/data_source_newrelic_entity.go +++ b/newrelic/data_source_newrelic_entity.go @@ -162,9 +162,9 @@ func dataSourceNewRelicEntityRead(ctx context.Context, d *schema.ResourceData, m "used elsewhere, since the value of all exported attributes would be null. Please use this attribute at your own risk.\n", }) return diags - } else { - return diag.FromErr(fmt.Errorf("no entities found for the provided search parameters, please ensure your schema attributes are valid")) } + return diag.FromErr(fmt.Errorf("no entities found for the provided search parameters, please ensure your schema attributes are valid")) + } return diag.FromErr(flattenEntityData(entity, d)) From 43214e80d379aa2b1a2662989f63ce2651b89da0 Mon Sep 17 00:00:00 2001 From: pranav-new-relic Date: Wed, 27 Mar 2024 16:12:56 +0530 Subject: [PATCH 3/3] docs: add change to docs and edit error message --- newrelic/data_source_newrelic_entity.go | 4 ++-- website/docs/d/entity.html.markdown | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/newrelic/data_source_newrelic_entity.go b/newrelic/data_source_newrelic_entity.go index 99f838671..77aa86c18 100644 --- a/newrelic/data_source_newrelic_entity.go +++ b/newrelic/data_source_newrelic_entity.go @@ -158,8 +158,8 @@ func dataSourceNewRelicEntityRead(ctx context.Context, d *schema.ResourceData, m Severity: diag.Warning, Summary: "no entities found for the provided search parameters, please ensure your schema attributes are valid.\n" + "This message is being displayed as a warning and not as an error as `ignore_not_found` has been set to true.\n" + - "Ignoring the 'not found' error can lead to downstream errors if the values exported by this data source are\n" + - "used elsewhere, since the value of all exported attributes would be null. Please use this attribute at your own risk.\n", + "Ignoring the 'not found' error can lead to downstream errors if the values of attributes exported by this\n" + + "data source are used elsewhere, since all of these values would be null. Please use this attribute at your own risk.\n", }) return diags } diff --git a/website/docs/d/entity.html.markdown b/website/docs/d/entity.html.markdown index 240029383..64b9a44f0 100644 --- a/website/docs/d/entity.html.markdown +++ b/website/docs/d/entity.html.markdown @@ -147,6 +147,9 @@ The following arguments are supported: * `type` - (Optional) The entity's type. Valid values are APPLICATION, DASHBOARD, HOST, MONITOR, WORKLOAD, AWSLAMBDAFUNCTION, SERVICE_LEVEL, and KEY_TRANSACTION. Note: Other entity types may also be queryable as the list of entity types may fluctuate over time. * `domain` - (Optional) The entity's domain. Valid values are APM, BROWSER, INFRA, MOBILE, SYNTH, and EXT. If not specified, all domains are searched. * `tag` - (Optional) A tag applied to the entity. See [Nested tag blocks](#nested-`tag`-blocks) below for details. +* `ignore_not_found`- (Optional) A boolean argument that, when set to true, prevents an error from being thrown when the queried entity is not found. Instead, a warning is displayed. Defaults to `false`. + +-> **WARNING:** Setting the `ignore_not_found` argument to `true` will display an 'entity not found' warning instead of throwing an error. This can lead to downstream errors if the values of attributes exported by this data source are used elsewhere, as all of these values would be null. Please use this argument at your own risk. ### Nested `tag` blocks