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

Error: INVALID_PARAMETER: Channels ids are already in use by workflows

Open hverma95 opened this issue 2 years ago • 8 comments

  • [X] Your New Relic provider configuration (sensitive details redacted)
  • [X] A list of affected resources and/or data sources
  • [X] The configuration of the resources and/or data sources related to the bug report (i.e. from the list mentioned above)
  • [X] Description of the current behavior (the bug)
  • [X] Description of the expected behavior
  • [X] Any related log output

Terraform Version

Terraform v0.14.11

Affected Resource(s)

Please list the resources as a list, for example:

  • newrelic_workflow
  • newrelic_notification_channel

Error: Error: INVALID_PARAMETER: Channels ids are already in use by workflows [ef1f8c4c-074f-4dbc-9173-5df65faa3271, 418881be-f71d-4c1d-b32e-eebf75580617]

Description The issue is related to the mapping between the workflow and the channel. The error comes while creating the workflow resource.

When running "terraform apply", it fails on adding the same "notification channel" to some of the "workflows", it says that it is already connected to other workflows. In fact, during the terraform run the channel gets connected to some of the workflows.

Like in the above error it shows that a single channel got connected to 2 workflows but failed for the current workflow.

So the mapping scenario is like this: Set 1(shared):
Workflow1 ------> Channel1 ------> Destination1 Workflow2 ------> Channel1 ------> Destination1 Workflow3 ------> Channel1 ------> Destination1

Set 2 (dummy): Workflow4 ------> Channel2 ------> Destination2 Workflow5 ------> Channel2 ------> Destination2 Workflow6 ------> Channel2 ------> Destination2

Terraform Configuration

provider.tf

terraform {
  required_version = ">= 0.14"
  required_providers {
    newrelic = {
      source  = "newrelic/newrelic"
      version = "~> 3.5.0"
    }
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.0"
    }
  }
}

provider "newrelic" {
  account_id = "ACCOUNT_ID_GOES_HERE"
  api_key    = API_KEY_GOES_HERE
  region     = "REGION_GOES_HERE"
}

provider "aws" {
  region  = "eu-west-1"
}

main.tf

data "aws_ssm_parameter" "new_relic_api_key" {
  name = "KEY_GOES_HERE"
}

locals {
  endpoints_items = flatten([for k, v in var.platforms : [for s, t in v : {
    name              = "${s}-${var.env}"
    env               = t.env
    url               = t.url
    platform          = k
    verify_ssl        = t.verify_ssl
    workflow_enabled  = t.workflow_enabled
    location_is_private = t.location_is_private
    locations_public  = t.locations_public
    period            = t.period
    status            = t.status
    # validation_string = lookup(t, "# validation_string", null)
  }]])
}

resource "newrelic_notification_destination" "webhook-squadcast" {
    for_each            = var.squadcast_endpoints
    name              = "${each.key}"
    account_id = "ID_GOES_HERE"
    type = "WEBHOOK"
  
    property {
      key = "url"
      value = "${each.value}"
    }
  }
  
  resource "newrelic_notification_channel" "webhook-channel" {
    for_each            = var.squadcast_endpoints
    account_id = "ID_GOES_HERE"
    name              = "${each.key}"
    type = "WEBHOOK"
    destination_id = newrelic_notification_destination.webhook-squadcast[each.key].id
    product = "IINT"
  
    property {
      key = "payload"
      value = var.payload
      label = "Payload Template"
    }
  }
  
  resource "newrelic_workflow" "workflow-squadcast" {
    for_each          = { for i in local.endpoints_items : "${i.platform}-${i.name}" => i }
    account_id        = "ID_GOES_HERE"
    name              = each.value.name
    muting_rules_handling = "NOTIFY_ALL_ISSUES"
    enabled = each.value.workflow_enabled
    enrichments {
      nrql {
        name = each.value.name
  
        configuration {
         query = "SELECT count(*) from SyntheticCheck WHERE monitorName='${each.value.name}' and result='FAILED' SINCE 3 minutes ago"
        }
      }
    }
  
    issues_filter {
      name = "Shared-PolicyID"
      type = "FILTER"
  
      predicate {
        attribute = "labels.policyIds"
        operator  = "EXACTLY_MATCHES"
        values    = [ newrelic_alert_policy.alert_policy[each.value.platform].id ]
      }
    }
  
    depends_on = [
      newrelic_notification_channel.webhook-channel
    ]
  
    destination {
      channel_id = newrelic_notification_channel.webhook-channel[each.value.platform].id
    }
  }

vars.tf

env = "ENV_NAME_GOES_HERE"
email_notification = {
  shared    = "EMAIL_ID_GOES_HERE"
  dummy  = "EMAIL_ID_GOES_HERE"
}

payload = <<EOL
{
  "id": {{ json issueId }},
  "issueUrl": {{ json issuePageUrl }},
  "title": {{ json annotations.title.[0] }}
}
EOL

squadcast_endpoints = {
  shared     = "ENDPOINT1"
  dummy   = "ENDPOINT2"
}

platforms = {
  shared = {
    nextgen-keycloak = {
      env        = "ENV_NAME_GOES_HERE"
      url        = "URL_TO_BE_MONITORED"
      period     = "EVERY_5_MINUTES"
      status     = "ENABLED"
      location_is_private = true
      verify_ssl = true
      workflow_enabled = true
      locations_public = null
    }
    nextgen-thanos = {
      env        = "ENV_NAME_GOES_HERE"
      url        = "URL_TO_BE_MONITORED"
      period     = "EVERY_5_MINUTES"
      status     = "ENABLED"
      location_is_private = true
      verify_ssl = true
      workflow_enabled = true
      locations_public = null
    }
  }
  dummy = {
    dummy-payments-service = {
      env        = "ENV_NAME_GOES_HERE"
      url        = "URL_TO_BE_MONITORED"
      period     = "EVERY_MINUTE"
      status     = "ENABLED"
      location_is_private = true
      verify_ssl = true
      workflow_enabled = true
      locations_public = null
    }
    dummy-billing-profile-service = {
      env        = "ENV_NAME_GOES_HERE"
      url        = "URL_TO_BE_MONITORED"
      period     = "EVERY_MINUTE"
      status     = "DISABLED"
      location_is_private = true
      verify_ssl = true
      workflow_enabled = true
      locations_public = null
    }
  }
}

Actual Behavior

When running "terraform apply":

Error: INVALID_PARAMETER: Channels ids are already in use by workflows [ef1f8c4c-074f-4dbc-9173-5df65faa3271, 418881be-f71d-4c1d-b32e-eebf75580617]
                                              on main.tf line 82, in resource "newrelic_workflow" "workflow-squadcast":

                                              82: resource "newrelic_workflow" "workflow-squadcast" {

Expected Behavior

A single channel should get connected to multiple workflows.

Steps to Reproduce

terraform apply

Important Factoids

Manually we are able to create the workflow and attach the same channel which is already connected to other workflows BUT through terraform it fails.

hverma95 avatar Oct 21 '22 10:10 hverma95