diff --git a/env0/data_notifications.go b/env0/data_notifications.go new file mode 100644 index 00000000..d3316b9f --- /dev/null +++ b/env0/data_notifications.go @@ -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 +} diff --git a/env0/data_notifications_test.go b/env0/data_notifications_test.go new file mode 100644 index 00000000..48c1ff43 --- /dev/null +++ b/env0/data_notifications_test.go @@ -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")) + }, + ) + }) +} diff --git a/env0/provider.go b/env0/provider.go index 4834c79e..75272f5f 100644 --- a/env0/provider.go +++ b/env0/provider.go @@ -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(), diff --git a/examples/data-sources/env0_notifications/data-source.tf b/examples/data-sources/env0_notifications/data-source.tf new file mode 100644 index 00000000..e15fb10a --- /dev/null +++ b/examples/data-sources/env0_notifications/data-source.tf @@ -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 +} diff --git a/tests/integration/025_notifications/conf.tf b/tests/integration/025_notifications/conf.tf new file mode 100644 index 00000000..8d6d2954 --- /dev/null +++ b/tests/integration/025_notifications/conf.tf @@ -0,0 +1,15 @@ +terraform { + backend "local" { + } + required_providers { + env0 = { + source = "terraform-registry.env0.com/env0/env0" + } + } +} + +provider "env0" {} + +variable "second_run" { + default = false +} diff --git a/tests/integration/025_notifications/expected_outputs.json b/tests/integration/025_notifications/expected_outputs.json new file mode 100644 index 00000000..0967ef42 --- /dev/null +++ b/tests/integration/025_notifications/expected_outputs.json @@ -0,0 +1 @@ +{} diff --git a/tests/integration/025_notifications/main.tf b/tests/integration/025_notifications/main.tf new file mode 100644 index 00000000..a04059ad --- /dev/null +++ b/tests/integration/025_notifications/main.tf @@ -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 +}