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

Adding new data source for resource_monitoring_notification_channel. #5405

Merged
merged 1 commit into from
Jan 15, 2020
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
99 changes: 99 additions & 0 deletions google/data_source_monitoring_notification_channel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
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")
addOptionalFieldsToSchema(dsSchema, "project")
addOptionalFieldsToSchema(dsSchema, "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/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 = "sms"

labels = {
number = "+1555"
}
}

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/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,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