newrelic / terraform-provider-newrelic

Terraform provider for New Relic
https://registry.terraform.io/providers/newrelic/newrelic/latest/docs
Mozilla Public License 2.0
200 stars 244 forks source link

`newrelic_entity` Data Source: The data source does not return entity tags #2666

Closed Hasgaroth closed 1 month ago

Hasgaroth commented 1 month ago

Hi, We are having an issue with the NewRelic data source newrelic_entity which is not returning any tags associated with the resources that are found through the use of the data source.

Affected Resource

Version information

Terraform v1.5.5
on darwin_arm64
+ provider registry.terraform.io/hashicorp/aws v5.50.0
+ provider registry.terraform.io/newrelic/newrelic v3.36.0

Current Behaviour

Expected Behaviour

Full Description

The documentation for this data source gives the option of providing tags as part of the search criteria, and for the output attributes that are exported, it states In addition to all arguments above, the following attributes are exported. So we would expect that all the tags that are assigned to the discovered entity will be exported with the rest of the attributes.

Unfortunately, there are no tags exported as part of the object attribute data.

newrelic_entity = {
  account_id                 = 1234567
  application_id             = null
  domain                     = "AIOPS"
  guid                       = "redacted_guid_value"
  id                         = "redacted_guid_value"
  ignore_case                = false
  ignore_not_found           = true
  name                       = "Entity name"
  serving_apm_application_id = null
  tag                        = null
  type                       = "CONDITION"
}

This is now causing a perpetual diff in our Terraform plan results, as we have an automated process that adds tags to resources based on other tag information that is present. The NewRelic provider does not give us the ability to ignore a set of these added tags, so decides that the tags need to be removed.

Running the apply for the resources then removes these tags, but the automated process re-adds them the next time it runs, meaning that the Terraform plan output will again show the drift in the tags.

The AWS provider gives the ability to provide a set of default tags for all resources, but also the ability to ignore certain tags so that they do not conflict with the Terraform controlled tags.

provider "aws" {
  # Other provider config items

  ignore_tags {
    key_prefixes = ["prefix_to_ignore"]
  }

  default_tags {
    tags = { set of tags to add to all resources }
  }
}

What I was trying to do to get around this issue, is to extract all the tags associated with an entity using the newrelic_entity data source, and then merge those extracted tags with the ones I want to maintain, so that Terraform places all the tags under control, even though they are not part of my configuration.

resource "newrelic_nrql_alert_condition" "alert_condition" {
  name               = var.name
  # other configuration items
}

data "newrelic_entity" "alert_condition" {
  name             = var.name
  type             = "CONDITION"
  ignore_not_found = true
}

resource "newrelic_entity_tags" "tags" {
  guid = newrelic_nrql_alert_condition.alert_condition.entity_guid

  dynamic "tag" {
    for_each = merge(data.newrelic_entity.alert_condition.tags, var.tags)
    content {
      key    = tag.key
      values = flatten([tag.value])
    }
  }
}
pranav-new-relic commented 1 month ago

Thanks for creating this issue, @Hasgaroth. We'll take a look at this sometime soon based on our current priority queue, and revert with some findings to share.

abasha1234 commented 1 month ago

Hey @Hasgaroth Support for retrieving tags of an entity using this data source has been added with 3.38.0 of the Terraform provider, please see this example; this might not entirely suffice your usecase since some tags associated with the entity are reserved, which when managed by the newrelic_entity_tags resource or the underlying NerdGraph mutation would thrown an error, but we'd suggest you to give this a try.

Hasgaroth commented 3 weeks ago

This resolves our issues with extracting tags via the newrelic_entity data source, thanks!