terraform-provider-newrelic icon indicating copy to clipboard operation
terraform-provider-newrelic copied to clipboard

newrelic_entity_tags right after creating a resource

Open trunet opened this issue 4 years ago ā€¢ 27 comments

Feature Description

To be able to use newrelic_entity_tags, we need to have the entity GUID. However, the resource is not yet created when data is acquired. We need to have a way to get GUID and/or resource name from the resource itself so data will auto depend on it during apply.

Describe Alternatives

Using depends_on on the data (that is not recommended on terraform documentation) and it will fail once and succeed on second run.

Additional context

From https://discuss.newrelic.com/t/synthetic-labels-end-of-life-for-tf-new-relic-provider/104468/6

Iā€™m having problems adding tags to a newrelic_synthetics_monitor.

The example below returns an error because during refresh phase, data is not available because newrelic_synthetics_monitor is not created yet.

If I add a depends_on to data, it will fail on first run, and run successfully on second.

How do I manage to have this running successfully in one run? Is it possible to implement on newrelic_synthetics_monitor to return its entity GUID in addition to ID?

resource "newrelic_synthetics_monitor" "ssl" {
  name          = var.name
  type          = "SCRIPT_API"
  frequency     = var.frequency
  status        = var.status
  locations     = var.locations
  sla_threshold = var.sla_threshold
}
data "newrelic_entity" "ssl" {
  name   = var.name
  type = "MONITOR"
  domain = "SYNTH"
}
resource "newrelic_entity_tags" "ssl" {
  guid = data.newrelic_entity.ssl.guid

  dynamic "tag" {
    for_each = merge(var.tags, local.common_tags)
    content {
      key    = tag.key
      values = list(tag.value)
    }
  }
}

trunet avatar Jul 08 '20 12:07 trunet

Hi @trunet !

We are in the middle of a large transition that involves moving most of our resources from legacy REST-based APIs to the NerdGraph GraphQL API. Synthetics is a domain that has not been moved yet, and for this reason we can't surface entity GUIDs directly after the monitor's creation. Once synthetic monitors surface in NerdGraph, we should be able to do exactly that.

In the meantime, I wonder if the backend just needs a few seconds to index the new resource. Can you try adding a 10-30s sleep after monitor creation and see if the helps?

https://registry.terraform.io/providers/hashicorp/time/latest/docs/resources/sleep

I know this is not ideal but it should be able to get you moving until Synthetics is moved into NerdGraph.

ctrombley avatar Jul 08 '20 16:07 ctrombley

@ctrombley this workaround works fine. I added a 10s sleep in between and it works flawless.

For future reference if anyone face this problem:

resource "newrelic_synthetics_monitor" "ssl" {
  name          = var.name
  type          = "SCRIPT_API"
  frequency     = var.frequency
  status        = var.status
  locations     = var.locations
  sla_threshold = var.sla_threshold
}

resource "time_sleep" "wait_10_seconds" {
  depends_on = [newrelic_synthetics_monitor.ssl]

  create_duration = "10s"
}

data "newrelic_entity" "ssl" {
  name   = var.name
  domain = "SYNTH"

  depends_on = [time_sleep.wait_10_seconds]
}

resource "newrelic_entity_tags" "ssl" {
  guid = data.newrelic_entity.ssl.guid

  dynamic "tag" {
    for_each = merge(var.tags, local.common_tags)
    content {
      key    = tag.key
      values = list(tag.value)
    }
  }
}

trunet avatar Jul 10 '20 09:07 trunet

I'm still having issues. The sleep 10 is not enough, and in my case, I got some synthetics that took 15 minutes to be able to have a GUID available.

trunet avatar Jul 10 '20 14:07 trunet

There's another problem, not sure if you want another ticket for this. If the data name is a constant (in my case var.name), it will ALWAYS try to get on refresh phase and trigger an error if you try to rename one monitor. If newrelic_synthetics_monitor had a name attribute exported, than it would depends and would work I guess.

resource "newrelic_synthetics_monitor" "[REDACTED]_endtoend" {
  name          = var.name
  type          = "SCRIPT_BROWSER"
  frequency     = var.frequency
  status        = var.status
  locations     = var.locations
  sla_threshold = var.sla_threshold
}

resource "time_sleep" "wait_10_seconds" {
  depends_on = [newrelic_synthetics_monitor.reaxys_endtoend]

  create_duration = "10s"
}

data "newrelic_entity" "[REDACTED]_endtoend" {
  name   = var.name
  type   = "MONITOR"
  domain = "SYNTH"

  depends_on = [time_sleep.wait_10_seconds, newrelic_synthetics_monitor.reaxys_endtoend]
}

Error:

Error: the name '[REDACTED]' does not match any New Relic One entity for the given search parameters

trunet avatar Jul 29 '20 12:07 trunet

I manage to workaround this (once again), doing this:

data "null_data_source" "name" {
  inputs = {
    name = var.name
  }

  depends_on = [time_sleep.wait_10_seconds]
}

data "newrelic_entity" "[REDACTED]_endtoend" {
  name   = data.null_data_source.name
  type   = "MONITOR"
  domain = "SYNTH"
}

trunet avatar Jul 29 '20 13:07 trunet

@ctrombley - Is there updates on when NerdGraph GraphQL APIs will be available to fix this issue? Thanks for your reply.

MAN98 avatar Sep 02 '20 18:09 MAN98

Is this still an issue with the latest release? I can't tell if this was the same issue as the service-defined tags, or slightly different.

zlesnr avatar Sep 21 '20 20:09 zlesnr

@zlesnr - This is still an issue since this was related to the fact that the newrelic_synthetics_monitor resource does not output an entity_guid after create. I think that's because the APIs for synthetics are still not in NERDGraph.

Note: I did try using the latest provider version creating a new synthetic and assign a tag right away and still same error as above

MAN98 avatar Sep 22 '20 17:09 MAN98

This issue has been automatically marked as stale because it has not had any recent activity. It will be closed if no further activity occurs.

stale[bot] avatar Oct 06 '20 22:10 stale[bot]

This issue has been automatically marked as stale because it has not had any recent activity. It will be closed if no further activity occurs.

stale[bot] avatar Oct 20 '20 23:10 stale[bot]

This issue has been automatically closed due to a lack of activity for an extended period of time.

stale[bot] avatar Oct 28 '20 01:10 stale[bot]

This is definitely still an issue and should not be closed.

MAN98 avatar Oct 28 '20 02:10 MAN98

I'm hearing that the synthetics resources may be available in Nerdgraph soon.

zlesnr avatar Oct 30 '20 18:10 zlesnr

That's great news @zlesnr. Please do keep us updated.

MAN98 avatar Oct 30 '20 18:10 MAN98

Now using the data component looking for guid is creating a chaos in terraform plan. Plan is reporting a change for all our Synthetics, with the message "# module.[monitor_name].data.newrelic_entity.browser will be read during apply". It is getting very difficult to look for the actual change due to these false messages. These data bugs are reported in AWS terraform providers too and they got it fixed. Check here

image

imsathyakumar avatar Nov 12 '20 14:11 imsathyakumar

That looks like a new issue to me @imsathyakumar. If so, please file a new issue.

zlesnr avatar Nov 12 '20 18:11 zlesnr

Just a note for anyone that's trying to implement this with for_each looped resource definitions. I found that even using @trunet's workaround with the time_sleep dependency I was still getting the error:

Error: the name '[REDACTED]' does not match any New Relic One entity for the given search parameters (ignore_case: false)

Works on first run but succeeds on subsequent runs. It look like the time_sleep dependency was not working properly, at least not for all resources in the loop. The following configuration seems to work:

data "newrelic_entity" "synthetic_loop" {

  for_each = {
    for synthetic in local.synthetic:
    synthetic.name => synthetic
  }

  name = time_sleep.wait_10_seconds_synthetic[each.value.name].triggers["name"]
  
  domain = "SYNTH"
  type = "MONITOR"

}

resource "time_sleep" "wait_10_seconds_synthetic" {

  for_each = {
    for synthetic in local.synthetic:
    synthetic.name => synthetic
  }
  
  create_duration = "10s"
  
  triggers = {
    name = newrelic_synthetics_monitor.synthetic_loop[each.value.name].name
  }

}

resource "newrelic_synthetics_monitor" "synthetic_loop" {

  for_each = {
    for synthetic in local.synthetic:
    synthetic.name => synthetic
  }

  name      = join(" ", [local.tags.policy_name, each.value.name])
  type      = each.value.type
  frequency = each.value.frequency
  status    = each.value.enabled ? "ENABLED": "DISABLED"
  locations = each.value.location
  uri       = each.value.uri

}

resource "newrelic_entity_tags" "synthetic_loop" {

    for_each = {
    for synthetic in local.synthetic:
    synthetic.name => synthetic
  }

  guid = data.newrelic_entity.synthetic_loop[each.value.name].guid

  dynamic "tag" {
    for_each = local.tags
    content {
      key = tag.key
      values = [tag.value]
    }
  }

}

The changes being:

  • Using implicit dependencies via the triggers attribute instead of the depends_on attribute to establish the relationship between the newrelic_entity and time_sleep resources
  • Looping the time_sleep resource so that there is one for each synthetic monitor. Note that they appear to run in parallel so this does not add an unwanted additional delay.

joshuaakelly avatar Sep 24 '21 00:09 joshuaakelly

Is there any timeline when would this be resolved?

MAN98 avatar Nov 16 '21 17:11 MAN98

The new API's for synthetics should be available soon, and will plan in the work to migrate synthetics resources to the new API. No timeline at the moment.

kidk avatar Nov 17 '21 13:11 kidk

Just adding another voice of support for this issue - we ran into this exact same problem today.

arhill05 avatar Mar 11 '22 17:03 arhill05

I would also like to vote for this issue. We ran into this same problem today.

burck1 avatar Mar 29 '22 21:03 burck1

Running into same issue :(

nikonovak avatar Apr 01 '22 19:04 nikonovak

Just to add another voice to the queue to keep this active - our team is also looking for a way to manage tags on synthetics and plan on waiting for this fix before we do.

lizgene avatar Apr 12 '22 18:04 lizgene

Update from the team: We are currently in the process of migrating Synthetics to the new API's. So you can soon expect a better solution than sleep šŸ˜… Thanks everyone for your patience.

kidk avatar Apr 25 '22 07:04 kidk

Update from the team: We are currently in the process of migrating Synthetics to the new API's. So you can soon expect a better solution than sleep šŸ˜… Thanks everyone for your patience.

That was the update from November 2021. Is there an ETA of when we can expect this migration to be complete?

zeffron avatar Apr 25 '22 18:04 zeffron

The update was indeed similar, at that time the new API was not yet available, now it is. We hope to have this all done before the summer.

kidk avatar Apr 26 '22 08:04 kidk

+1 for this issue hoping for a solution soon

dconnolly-sfdc avatar Aug 08 '22 13:08 dconnolly-sfdc

We've released V3 version of our provider which includes all of the new Synthetics features: https://registry.terraform.io/providers/newrelic/newrelic/latest/docs/guides/migration_guide_v3

The new resources now include a tags attribute which will allow you to set the right tags from the resource without the need for newrelic_entity_tags. The monitor now also returns the GUID (id attribute) which should help with use of newrelic_entity_tags.

Please let us know if you have any questions. If you encounter any issues please don't hesitate to create a new ticket.

I'll close this ticket as the work has been delivered. Feel free to continue the discussion if needed.

kidk avatar Aug 29 '22 09:08 kidk