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

Workflow support slack notifications

Open huzie opened this issue 2 years ago • 9 comments

Feature Description

Currently it doesn't look like the Terraform provider supports creating a workflow and sending notifications to Slack.

I want to be able to configure the below via Terraform and link it to an existing policy.

image

huzie avatar Sep 29 '22 11:09 huzie

Hi @huzie I believe that because Slack requires interactive authentication through OAuth, it can't be created by Terraform. The solution is to use the UI to create a Slack destination and import into Terraform. I'll also tag @lzaga-newrelic who may have some more context.

mbazhlekova avatar Sep 30 '22 14:09 mbazhlekova

Hi @huzie , @mbazhlekova is correct. Slack requires interactive authentication through OAuth, therefore Slack destinations can only be imported to terraform after creating through the UI. Slack channels can be created via Terraform.

lzaga-newrelic avatar Oct 01 '22 00:10 lzaga-newrelic

Hi @mbazhlekova and @lzaga-newrelic, thank you for your advice. We have imported the Slack destination and then followed the approach outlined here: https://registry.terraform.io/providers/newrelic/newrelic/latest/docs/resources/workflow however seeing " Error: CONNECTION_ERROR: Unable to connect to external service to perform this action" It seems to be a problem with the new relic notification channel resource.

xvella97 avatar Oct 04 '22 01:10 xvella97

Hi @mbazhlekova @lzaga-newrelic, further to @xvella97 comment, we want to be able to create an alert policy which has an alert condition which is then linked to a workflow which has the slack destination. Is this possible?

huzie avatar Oct 04 '22 22:10 huzie

Hi.

I am not sure I understand the situation. We are already creating Slack Channels in Terraform using the newrelic_notification_channel resource.

I am not sure I understand why we are now not able to do this in the new newrelic_notification_destination resource?

For us this appears to be a step backwards in functionality.

andrewhooker79 avatar Oct 07 '22 08:10 andrewhooker79

Hi @huzie , @mbazhlekova is correct. Slack requires interactive authentication through OAuth, therefore Slack destinations can only be imported to terraform after creating through the UI. Slack channels can be created via Terraform.

i'm struggling to get anywhere with workflows using the terraform provider. the documentation gives examples of using channel_id which would be ok for now until we can get a data resource but when i export the destination id from the destinations tab as below i get an error

image

Error: INVALID_PARAMETER: A channel with id ***** was not found

i've also tried using an id from within alerts classic as below

image

and also using the UUID from the URL

image

clearly i'm being a dafty and would really appreciate some help :slightly_smiling_face:

tormodmacleod avatar Oct 11 '22 14:10 tormodmacleod

Hi @tormodmacleod, the channel ID mentioned in the docs is the notifications channel but the channel ID you are trying to use is from the alerts channel. You can find the notifications channels as a section in workflows. At the moment you can get the ID of notifications channels from Nerd Graph. Screenshot 2022-10-12 at 7 04 58 PM

NSSPKrishna avatar Oct 12 '22 13:10 NSSPKrishna

We struggle too with the new workflow with Slack.

I imported the slack destination as described.

In the newrelic UI, "destination" are set for Slack Workspace and for "workflow" you specifiy the slack channel according the destination.

But in the workflow resource in Terraform, you can only put channel id who is the slack workspace.

How do you specify the Slack channel in terraform?

gdubroeucq avatar Oct 13 '22 12:10 gdubroeucq

Hi @tormodmacleod, the channel ID mentioned in the docs is the notifications channel but the channel ID you are trying to use is from the alerts channel. You can find the notifications channels as a section in workflows. At the moment you can get the ID of notifications channels from Nerd Graph. Screenshot 2022-10-12 at 7 04 58 PM

If I'm creating the workflow & notification channel via terraform how will this work? The Nerd Graph link you sent requires a workflow to exist already right?

ayk33 avatar Oct 14 '22 19:10 ayk33

@gdubroeucq You can get the Slack channel id from this NerdGraph query. You can add filters as you wish, like the channel name etc.

Also, if you imported the destination to your terraform, you can use a reference as described here: Screen Shot 2022-10-30 at 11 21 29

lzaga-newrelic avatar Oct 30 '22 09:10 lzaga-newrelic

Hi @huzie, @xvella97, @trustthewhiterabbit, @tormodmacleod, @gdubroeucq, @ayk33

Looks like there are several misunderstandings on several different topics in this issue. I'll try to explain how to create a Slack destination and channel and connect it to an alert policy and to a workflow.

First, as I mentioned here, There is no option to create a Slack destination via New Relic Terraform provider. Slack requires interactive authentication through OAuth, therefore Slack destinations can only be imported to terraform after creating through the UI.

Second, Slack channels can be created via Terraform. To create a Slack channel you'll have to provide a Slack channel id. We are working on adding to our docs more information on how to export the Slack channel id from Slack API.


Steps to create all resources mentioned in this issue:

  1. Create an Alert Policy:
resource "newrelic_alert_policy" "my-policy" {
  name = "my_policy"
}
  1. Create a Slack destination via the UI and authenticate: In New Relic One -> Alerts & AI Tab -> Under Destinations -> Choose Slack: Screen Shot 2022-11-22 at 16 04 13

  2. Import the Slack notification destination into terraform:

  • Get the destination id from the UI Destinations Table: In New Relic One -> Alerts & AI Tab -> Under Destinations -> Destinations Tab (attached photo) -> Filter Slack and pick your Slack destination -> Copy the destination Id in the 3 dots menu to the right: Screen Shot 2022-11-22 at 16 07 46 Screen Shot 2022-11-22 at 16 08 41

  • Add an empty resource to your terraform file:

resource "newrelic_notification_destination" "slack-imported-destination" {
}
  • Run import command: terraform import newrelic_notification_destination.foo <destination_id>. The destination Id is the one you picked in the previous step.
  1. Create a Slack notification channel via terraform:
resource "newrelic_notification_channel" "my-channel" {
  account_id = 12345678
  name = "slack-example"
  type = "SLACK"
  destination_id = "00b6bd1d-ac06-4d3d-bd72-49551e70f7a8"
  product = "IINT"

  property {
    key = "channelId"
    value = "123456"
  }
}

Notice: you have to provide the Slack channel id here!

  1. Create workflow via terraform and connect it to the Slack notification channel:
resource "newrelic_workflow" "my-workflow" {
  name = "workflow-example"
  muting_rules_handling = "NOTIFY_ALL_ISSUES"

  issues_filter {
    name = "Filter-name"
    type = "FILTER"

    predicate {
      attribute = "labels.policyIds"
      operator = "EXACTLY_MATCHES"
      values = [ newrelic_alert_policy.my-policy.id ] # Here is the policy you created in step #1
    }
  }

  destination {
    channel_id = newrelic_notification_channel.my-channel.id # Here is the notification channel you created in step #4
  }
}

Notice: if you want to use another existing notification channel, that is not created via Terraform, you can look for it in NerdGraph API here.

Hope this helps to answer all of your questions.

lzaga-newrelic avatar Nov 22 '22 14:11 lzaga-newrelic

you cannot create an empty newrelic_notification_destination block, it's not a valid resource.

resource "newrelic_notification_destination" "slack-imported-destination" {}

Too few blocks specified for "property": At least 1 block(s) are expected for "property"Terraform

At least that's what my IDE is telling me.

My resource look like this:

resource "newrelic_notification_destination" "slack" {
  name = "Slack Integration"
  type = "SLACK"

  property {
    key   = "scope"
    label = "Permissions"
    value = "app_mentions:read,channels:join,channels:read,chat:write,chat:write.public,commands,groups:read,links:read,links:write,team:read,users:read"
  }

  property {
    key   = "teamName"
    label = "Team Name"
    value = "xxx"
  }

  lifecycle {
    ignore_changes = [auth_token]
  }
}

I'm importing this resource using the import directive

import {
  id = "xxxxxx-xxxxxx-xxxxx-xxxxx-xxxxxx"
  to = newrelic_notification_destination.slack
}

and I still get this error

╷
│ Error: CONNECTION_ERROR: Unable to connect to external service to perform this action
│ 
│   with newrelic_notification_destination.slack,
│   on newrelic.tf line 1, in resource "newrelic_notification_destination" "slack":
│    1: resource "newrelic_notification_destination" "slack" {
│ 
╵

Maxwell2022 avatar Feb 07 '24 00:02 Maxwell2022

It seems that you cannot rename a slack destination. The import seems to have worked, but applying new name is failing with the error above.

I wonder what is the point of having a resource if you cannot update it, the documentation should recommend to use data instead for slack. Note that the plan is happy with renaming the destination, and it's not suggesting that it will re-create it either.

Maxwell2022 avatar Feb 07 '24 01:02 Maxwell2022

I'm having the same issue with renaming after import. @Maxwell2022 did you find any workaround?

csiepka avatar Feb 07 '24 15:02 csiepka

I believe the workaround is to create a new slack destination manually in the UI with the name you want and import it again. If you don't want to delete the existing destination you can remove it from your terraform state

terraform state rm newrelic_notification_destination.slack

Maxwell2022 avatar Feb 07 '24 21:02 Maxwell2022