-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: add data source for getting a list of notification targets
- Loading branch information
1 parent
e7c0678
commit a63ae95
Showing
7 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
}, | ||
) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |