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: add data source for getting a list of notification targets #403

Merged
merged 1 commit into from
May 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions env0/data_notifications.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package env0

import (
"context"

"github.com/env0/terraform-provider-env0/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataNotifications() *schema.Resource {
return &schema.Resource{
ReadContext: dataNotificationsRead,

Schema: map[string]*schema.Schema{
"names": {
Type: schema.TypeList,
Description: "list of all notifications (by name)",
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
Description: "the notification name",
},
},
},
}
}

func dataNotificationsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
apiClient := meta.(client.ApiClientInterface)
notifications, err := apiClient.Notifications()
if err != nil {
return diag.Errorf("could not get notifications: %v", err)
}

names := []string{}

for _, notification := range notifications {
names = append(names, notification.Name)
}

d.Set("names", names)
d.SetId("all_notification_names")

return nil
}
73 changes: 73 additions & 0 deletions env0/data_notifications_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package env0

import (
"errors"
"regexp"
"testing"

"github.com/env0/terraform-provider-env0/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestNotificationsDataSource(t *testing.T) {
notification1 := client.Notification{
Id: "id0",
Name: "name0",
Type: client.NotificationTypeSlack,
Value: "http://my-notification-0.com",
}

notification2 := client.Notification{
Id: "id1",
Name: "name10",
Type: client.NotificationTypeTeams,
Value: "http://my-notification-0.com",
}

resourceType := "env0_notifications"
resourceName := "test_notifications"
accessor := dataSourceAccessor(resourceType, resourceName)

getTestCase := func() resource.TestCase {
return resource.TestCase{
Steps: []resource.TestStep{
{
Config: dataSourceConfigCreate(resourceType, resourceName, map[string]interface{}{}),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(accessor, "names.0", notification1.Name),
resource.TestCheckResourceAttr(accessor, "names.1", notification2.Name),
),
},
},
}
}

mockNotifications := func(returnValue []client.Notification) func(mockFunc *client.MockApiClientInterface) {
return func(mock *client.MockApiClientInterface) {
mock.EXPECT().Notifications().AnyTimes().Return(returnValue, nil)
}
}

t.Run("Success", func(t *testing.T) {
runUnitTest(t,
getTestCase(),
mockNotifications([]client.Notification{notification1, notification2}),
)
})

t.Run("API Call Error", func(t *testing.T) {
runUnitTest(t,
resource.TestCase{
Steps: []resource.TestStep{
{
Config: dataSourceConfigCreate(resourceType, resourceName, map[string]interface{}{}),
ExpectError: regexp.MustCompile("error"),
},
},
},
func(mock *client.MockApiClientInterface) {
mock.EXPECT().Notifications().AnyTimes().Return(nil, errors.New("error"))
},
)
})
}
1 change: 1 addition & 0 deletions env0/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func Provider(version string) plugin.ProviderFunc {
"env0_environment": dataEnvironment(),
"env0_workflow_triggers": dataWorkflowTriggers(),
"env0_notification": dataNotification(),
"env0_notifications": dataNotifications(),
"env0_module": dataModule(),
"env0_git_token": dataGitToken(),
"env0_api_key": dataApiKey(),
Expand Down
14 changes: 14 additions & 0 deletions examples/data-sources/env0_notifications/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
data "env0_notifications" "all_notifications" {}

data "env0_notification" "notifications" {
for_each = toset(data.env0_notifications.all_notifications.names)
name = each.value
}

output "notification1_name" {
value = data.env0_notification.notifications["my notification 345"].name
}

output "notification2_name" {
value = data.env0_notification.notifications["my notification 123"].name
}
15 changes: 15 additions & 0 deletions tests/integration/025_notifications/conf.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
terraform {
backend "local" {
}
required_providers {
env0 = {
source = "terraform-registry.env0.com/env0/env0"
}
}
}

provider "env0" {}

variable "second_run" {
default = false
}
1 change: 1 addition & 0 deletions tests/integration/025_notifications/expected_outputs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
27 changes: 27 additions & 0 deletions tests/integration/025_notifications/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
provider "random" {}

resource "random_string" "random" {
length = 8
special = false
min_lower = 8
}


resource "env0_notification" "test_notification_1" {
name = "notification-${random_string.random.result}-1"
type = "Slack"
value = "https://someurl1.com"
}

resource "env0_notification" "test_notification_2" {
name = "notification-${random_string.random.result}-2"
type = "Teams"
value = "https://someurl2.com"
}

data "env0_notifications" "all_notifications" {}

data "env0_notification" "notification" {
for_each = toset(data.env0_notifications.all_notifications.names)
name = each.value
}