Skip to content

Commit

Permalink
Feat: add data source for getting a list of notification targets
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerHeber committed May 31, 2022
1 parent e7c0678 commit a63ae95
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 0 deletions.
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" "notification" {
for_each = toset(data.env0_notifications.all_notifications.names)
name = each.value
}

output "template1_name" {
value = data.env0_template.templates["Github Test-111"].name
}

output "template2_name" {
value = data.env0_template.templates["Github Test-222"].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
}

0 comments on commit a63ae95

Please sign in to comment.