Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(data_source_newrelic_notifications_destination): Support name in destination data source #2395

Merged
merged 10 commits into from
Jun 15, 2023
Merged
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/mitchellh/go-homedir v1.1.0
github.com/newrelic/go-agent/v3 v3.20.3
github.com/newrelic/go-insights v1.0.3
github.com/newrelic/newrelic-client-go/v2 v2.19.1
github.com/newrelic/newrelic-client-go/v2 v2.19.2
github.com/stretchr/testify v1.8.2
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,8 @@ github.com/newrelic/go-agent/v3 v3.20.3 h1:hUBAMq/Y2Y9as5/yxQbf0zNde/X7w58cWZkm2
github.com/newrelic/go-agent/v3 v3.20.3/go.mod h1:rT6ZUxJc5rQbWLyCtjqQCOcfb01lKRFbc1yMQkcboWM=
github.com/newrelic/go-insights v1.0.3 h1:zSNp1CEZnXktzSIEsbHJk8v6ZihdPFP2WsO/fzau3OQ=
github.com/newrelic/go-insights v1.0.3/go.mod h1:A20BoT8TNkqPGX2nS/Z2fYmKl3Cqa3iKZd4whzedCY4=
github.com/newrelic/newrelic-client-go/v2 v2.19.1 h1:3JZ92M/i73HC+cmWoaKZuDF/T7OWusRoheVYT8cnWPQ=
github.com/newrelic/newrelic-client-go/v2 v2.19.1/go.mod h1:AX08IIL08pYVbnowsR05EqOdfN1Dz+ZXdIwqS0dkT2c=
github.com/newrelic/newrelic-client-go/v2 v2.19.2 h1:+ftufW63uuLtZl/Jk50Ibavr6fhePOU4wGqgZyGD0P8=
github.com/newrelic/newrelic-client-go/v2 v2.19.2/go.mod h1:AX08IIL08pYVbnowsR05EqOdfN1Dz+ZXdIwqS0dkT2c=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw=
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
Expand Down
32 changes: 21 additions & 11 deletions newrelic/data_source_newrelic_notifications_destination.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,23 @@ func dataSourceNewRelicNotificationDestination() *schema.Resource {
ReadContext: dataSourceNewRelicNotificationDestinationRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Required: true,
Description: "The ID of the destination.",
Type: schema.TypeString,
Optional: true,
ExactlyOneOf: []string{"id", "name"},
Description: "The ID of the destination.",
},
"name": {
Type: schema.TypeString,
Optional: true,
ExactlyOneOf: []string{"id", "name"},
Description: "The name of the destination.",
},
"account_id": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: "The account ID under which to put the destination.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "The name of the destination.",
},
"type": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -67,10 +69,18 @@ func dataSourceNewRelicNotificationDestinationRead(ctx context.Context, d *schem
providerConfig := meta.(*ProviderConfig)
accountID := selectAccountID(providerConfig, d)
updatedContext := updateContextWithAccountID(ctx, accountID)
id := d.Get("id").(string)
filters := ai.AiNotificationsDestinationFilter{ID: id}
var filters ai.AiNotificationsDestinationFilter
sorter := notifications.AiNotificationsDestinationSorter{}

nameValue, nameOk := d.Get("name").(string)
if nameOk && nameValue != "" {
filters = ai.AiNotificationsDestinationFilter{Name: nameValue}
}
idValue, idOk := d.Get("id").(string)
if idOk && idValue != "" {
filters = ai.AiNotificationsDestinationFilter{ID: idValue}
}

pranav-new-relic marked this conversation as resolved.
Show resolved Hide resolved
destinationResponse, err := client.Notifications.GetDestinationsWithContext(updatedContext, accountID, "", filters, sorter)
if err != nil {
if _, ok := err.(*errors.NotFound); ok {
Expand All @@ -83,7 +93,7 @@ func dataSourceNewRelicNotificationDestinationRead(ctx context.Context, d *schem

if len(destinationResponse.Entities) == 0 {
d.SetId("")
return diag.FromErr(fmt.Errorf("the id '%s' does not match any New Relic notification destination", id))
return diag.FromErr(fmt.Errorf("the id or name you provided does not match any New Relic notification destination"))
lzaga-newrelic marked this conversation as resolved.
Show resolved Hide resolved
}

errors := buildAiNotificationsResponseErrors(destinationResponse.Errors)
Expand Down
46 changes: 43 additions & 3 deletions newrelic/data_source_newrelic_notifications_destination_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestAccNewRelicNotificationDestinationDataSource_Basic(t *testing.T) {
func TestAccNewRelicNotificationDestinationDataSource_ById(t *testing.T) {
resourceName := "newrelic_notification_destination.foo"
rand := acctest.RandString(5)
rName := fmt.Sprintf("tf-notifications-test-%s", rand)
Expand All @@ -22,7 +22,7 @@ func TestAccNewRelicNotificationDestinationDataSource_Basic(t *testing.T) {
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccNewRelicNotificationsDestinationDataSourceConfig(rName),
Config: testAccNewRelicNotificationsDestinationDataSourceConfigById(rName),
Check: resource.ComposeTestCheckFunc(
testAccNewRelicNotificationDestination("data.newrelic_notification_destination.foo"),
resource.TestCheckResourceAttr(resourceName, "name", rName),
Expand All @@ -33,7 +33,28 @@ func TestAccNewRelicNotificationDestinationDataSource_Basic(t *testing.T) {
})
}

func testAccNewRelicNotificationsDestinationDataSourceConfig(name string) string {
func TestAccNewRelicNotificationDestinationDataSource_ByName(t *testing.T) {
resourceName := "newrelic_notification_destination.foo"
rand := acctest.RandString(5)
rName := fmt.Sprintf("tf-notifications-test-%s", rand)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheckEnvVars(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccNewRelicNotificationsDestinationDataSourceConfigByName(rName),
Check: resource.ComposeTestCheckFunc(
testAccNewRelicNotificationDestination("data.newrelic_notification_destination.foo"),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "type", "WEBHOOK"),
),
},
},
})
}

func testAccNewRelicNotificationsDestinationDataSourceConfigById(name string) string {
return fmt.Sprintf(`
resource "newrelic_notification_destination" "foo" {
name = "%s"
Expand All @@ -52,6 +73,25 @@ data "newrelic_notification_destination" "foo" {
`, name)
}

func testAccNewRelicNotificationsDestinationDataSourceConfigByName(name string) string {
return fmt.Sprintf(`
resource "newrelic_notification_destination" "foo" {
name = "%s"
type = "WEBHOOK"
active = true

property {
key = "url"
value = "https://webhook.site/"
}
}

data "newrelic_notification_destination" "foo" {
name = newrelic_notification_destination.foo.name
}
pranav-new-relic marked this conversation as resolved.
Show resolved Hide resolved
`, name)
}

func testAccNewRelicNotificationDestination(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
r := s.RootModule().Resources[n]
Expand Down
30 changes: 28 additions & 2 deletions website/docs/d/notification_destination.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ description: |-

Use this data source to get information about a specific notification destination in New Relic that already exists. More information on Terraform's data sources can be found [here](https://www.terraform.io/language/data-sources).

## Example Usage
## Id Example Usage

```hcl
# Data source
Expand All @@ -33,18 +33,44 @@ resource "newrelic_notification_channel" "foo-channel" {
}
```

## Name Example Usage

```hcl
# Data source
data "newrelic_notification_destination" "foo" {
name = "webhook-destination"
}

# Resource
resource "newrelic_notification_channel" "foo-channel" {
name = "webhook-example"
type = "WEBHOOK"
destination_id = data.newrelic_notification_destination.foo.id
product = "IINT"

property {
key = "payload"
value = "{\n\t\"name\": \"foo\"\n}"
label = "Payload Template"
}
}
```
pranav-new-relic marked this conversation as resolved.
Show resolved Hide resolved

## Argument Reference

The following arguments are supported:

Required **one of these two** arguments:
* `id` - (Required) The id of the notification destination in New Relic.
* `name` - (Required) The name of the notification destination.
lzaga-newrelic marked this conversation as resolved.
Show resolved Hide resolved

Optional:
* `account_id` - (Optional) The New Relic account ID to operate on. This allows you to override the `account_id` attribute set on the provider. Defaults to the environment variable `NEW_RELIC_ACCOUNT_ID`.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

* `name` - The name of the notification destination.
lzaga-newrelic marked this conversation as resolved.
Show resolved Hide resolved
* `type` - The notification destination type, either: `EMAIL`, `SERVICE_NOW`, `WEBHOOK`, `JIRA`, `MOBILE_PUSH`, `EVENT_BRIDGE`, `PAGERDUTY_ACCOUNT_INTEGRATION` or `PAGERDUTY_SERVICE_INTEGRATION`, `SLACK` and `SLACK_COLLABORATION`.
* `property` - A nested block that describes a notification destination property.
* `active` - An indication whether the notification destination is active or not.
Expand Down