Skip to content

Commit

Permalink
Adding new data source for resource_monitoring_notification_channel.
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
ffung authored and modular-magician committed Jan 14, 2020
1 parent de73a65 commit fdabc61
Show file tree
Hide file tree
Showing 5 changed files with 397 additions and 0 deletions.
97 changes: 97 additions & 0 deletions google-beta/data_source_monitoring_notification_channel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package google

import (
"errors"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func dataSourceMonitoringNotificationChannel() *schema.Resource {
dsSchema := datasourceSchemaFromResourceSchema(resourceMonitoringNotificationChannel().Schema)

// Set 'Optional' schema elements
addOptionalFieldsToSchema(dsSchema, "display_name", "type")

return &schema.Resource{
Read: dataSourceMonitoringNotificationChannelRead,
Schema: dsSchema,
}
}

func dataSourceMonitoringNotificationChannelRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

url, err := replaceVars(d, config, "{{MonitoringBasePath}}projects/{{project}}/notificationChannels")
if err != nil {
return err
}

displayName := d.Get("display_name").(string)
channelType := d.Get("type").(string)

if displayName == "" && channelType == "" {
return errors.New("Must at least provide either `display_name` or `type`")
}

filter := ""
if displayName != "" {
filter = fmt.Sprintf("display_name=\"%s\"", displayName)
}

if channelType != "" {
channelFilter := fmt.Sprintf("type=\"%s\"", channelType)
if filter != "" {
filter += fmt.Sprintf(" AND %s", channelFilter)
} else {
filter = channelFilter
}
}

params := make(map[string]string)
params["filter"] = filter

url, err = addQueryParams(url, params)
if err != nil {
return err
}

project, err := getProject(d, config)
if err != nil {
return err
}

response, err := sendRequest(config, "GET", project, url, nil)
if err != nil {
return fmt.Errorf("Error retrieving NotificationChannels: %s", err)
}

var pageMonitoringNotificationChannels []interface{}
if v, ok := response["notificationChannels"]; ok {
pageMonitoringNotificationChannels = v.([]interface{})
}

if len(pageMonitoringNotificationChannels) == 0 {
return fmt.Errorf("No NotificationChannel found using filter=%s", filter)
}

if len(pageMonitoringNotificationChannels) > 1 {
return fmt.Errorf("More than one matching NotificationChannel found using filter=%s", filter)
}

res := pageMonitoringNotificationChannels[0].(map[string]interface{})

name := flattenMonitoringNotificationChannelName(res["name"], d).(string)
d.Set("name", name)
d.Set("project", project)
d.Set("labels", flattenMonitoringNotificationChannelLabels(res["labels"], d))
d.Set("verification_status", flattenMonitoringNotificationChannelVerificationStatus(res["verificationStatus"], d))
d.Set("type", flattenMonitoringNotificationChannelType(res["type"], d))
d.Set("user_labels", flattenMonitoringNotificationChannelUserLabels(res["userLabels"], d))
d.Set("description", flattenMonitoringNotificationChannelDescription(res["descriptionx"], d))
d.Set("display_name", flattenMonitoringNotificationChannelDisplayName(res["displayName"], d))
d.Set("enabled", flattenMonitoringNotificationChannelEnabled(res["enabled"], d))
d.SetId(name)

return nil
}
192 changes: 192 additions & 0 deletions google-beta/data_source_monitoring_notification_channel_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
package google

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccDataSourceGoogleMonitoringNotificationChannel_byDisplayName(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceGoogleMonitoringNotificationChannel_byDisplayName(acctest.RandomWithPrefix("tf-test")),
Check: resource.ComposeTestCheckFunc(
checkDataSourceStateMatchesResourceState(
"data.google_monitoring_notification_channel.my",
"google_monitoring_notification_channel.my"),
),
},
},
})
}

func TestAccDataSourceGoogleMonitoringNotificationChannel_byType(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceGoogleMonitoringNotificationChannel_byType(acctest.RandomWithPrefix("tf-test")),
Check: resource.ComposeTestCheckFunc(
checkDataSourceStateMatchesResourceState(
"data.google_monitoring_notification_channel.my",
"google_monitoring_notification_channel.my"),
),
},
},
})
}

func TestAccDataSourceGoogleMonitoringNotificationChannel_byDisplayNameAndType(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceGoogleMonitoringNotificationChannel_byDisplayNameAndType(acctest.RandomWithPrefix("tf-test")),
Check: resource.ComposeTestCheckFunc(
checkDataSourceStateMatchesResourceState(
"data.google_monitoring_notification_channel.my",
"google_monitoring_notification_channel.myemail"),
),
},
},
})
}

func TestAccDataSourceGoogleMonitoringNotificationChannel_NotFound(t *testing.T) {
displayName := acctest.RandomWithPrefix("tf-test")
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceGoogleMonitoringNotificationChannel_NotFound(displayName),
ExpectError: regexp.MustCompile(fmt.Sprintf("No NotificationChannel found using filter=display_name=\"%s\"", displayName)),
},
},
})
}

func TestAccDataSourceGoogleMonitoringNotificationChannel_NotUnique(t *testing.T) {
displayName := acctest.RandomWithPrefix("tf-test")
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
PreventPostDestroyRefresh: true,
Steps: []resource.TestStep{
{
Config: testAccDataSourceGoogleMonitoringNotificationChannel_NotUnique(displayName),
},
{
Config: testAccDataSourceGoogleMonitoringNotificationChannel_NotUniqueDS(displayName),
ExpectError: regexp.MustCompile(fmt.Sprintf("More than one matching NotificationChannel found using filter=display_name=\"%s\"", displayName)),
},
},
})
}

func testAccDataSourceGoogleMonitoringNotificationChannel_byDisplayName(displayName string) string {
return fmt.Sprintf(`
resource "google_monitoring_notification_channel" "my" {
display_name = "%s"
type = "webhook_tokenauth"
labels = {
url = "http://www.acme.org"
}
}
data "google_monitoring_notification_channel" "my" {
display_name = google_monitoring_notification_channel.my.display_name
}
`, displayName)
}

func testAccDataSourceGoogleMonitoringNotificationChannel_byType(displayName string) string {
return fmt.Sprintf(`
resource "google_monitoring_notification_channel" "my" {
display_name = "%s"
type = "email"
labels = {
email_address = "mailme@acme.org"
}
}
data "google_monitoring_notification_channel" "my" {
type = google_monitoring_notification_channel.my.type
}
`, displayName)
}

func testAccDataSourceGoogleMonitoringNotificationChannel_byDisplayNameAndType(displayName string) string {
return fmt.Sprintf(`
resource "google_monitoring_notification_channel" "mywebhook" {
display_name = "%s"
type = "webhook_tokenauth"
labels = {
url = "http://www.acme.org"
}
}
resource "google_monitoring_notification_channel" "myemail" {
display_name = google_monitoring_notification_channel.mywebhook.display_name
type = "email"
labels = {
email_address = "mailme@acme.org"
}
}
data "google_monitoring_notification_channel" "my" {
display_name = google_monitoring_notification_channel.myemail.display_name
type = google_monitoring_notification_channel.myemail.type
}
`, displayName)
}

func testAccDataSourceGoogleMonitoringNotificationChannel_NotFound(displayName string) string {
return fmt.Sprintf(`
data "google_monitoring_notification_channel" "my" {
display_name = "%s"
}
`, displayName)
}

func testAccDataSourceGoogleMonitoringNotificationChannel_NotUnique(displayName string) string {
return fmt.Sprintf(`
resource "google_monitoring_notification_channel" "default" {
display_name = "%s"
type = "webhook_tokenauth"
labels = {
url = "http://www.acme1.org"
}
}
resource "google_monitoring_notification_channel" "default2" {
display_name = google_monitoring_notification_channel.default.display_name
type = "webhook_tokenauth"
labels = {
url = "http://www.acme2.org"
}
}
`, displayName)
}

func testAccDataSourceGoogleMonitoringNotificationChannel_NotUniqueDS(displayName string) string {
return fmt.Sprintf(`
data "google_monitoring_notification_channel" "my" {
display_name = "%s"
}
`, displayName)
}
1 change: 1 addition & 0 deletions google-beta/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ func Provider() terraform.ResourceProvider {
"google_kms_secret_ciphertext": dataSourceGoogleKmsSecretCiphertext(),
"google_folder": dataSourceGoogleFolder(),
"google_folder_organization_policy": dataSourceGoogleFolderOrganizationPolicy(),
"google_monitoring_notification_channel": dataSourceMonitoringNotificationChannel(),
"google_netblock_ip_ranges": dataSourceGoogleNetblockIpRanges(),
"google_organization": dataSourceGoogleOrganization(),
"google_project": dataSourceGoogleProject(),
Expand Down
Loading

0 comments on commit fdabc61

Please sign in to comment.