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

Add user labels and labels to monitoring notification channel DS #3005

Merged
merged 2 commits into from
Jan 22, 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
2 changes: 1 addition & 1 deletion build/terraform
2 changes: 1 addition & 1 deletion build/terraform-beta
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package google

import (
"errors"
"fmt"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
Expand All @@ -14,6 +14,8 @@ func dataSourceMonitoringNotificationChannel() *schema.Resource {
addOptionalFieldsToSchema(dsSchema, "display_name")
addOptionalFieldsToSchema(dsSchema, "project")
addOptionalFieldsToSchema(dsSchema, "type")
addOptionalFieldsToSchema(dsSchema, "labels")
addOptionalFieldsToSchema(dsSchema, "user_labels")

return &schema.Resource{
Read: dataSourceMonitoringNotificationChannelRead,
Expand All @@ -33,26 +35,41 @@ func dataSourceMonitoringNotificationChannelRead(d *schema.ResourceData, meta in
channelType := d.Get("type").(string)

if displayName == "" && channelType == "" {
return errors.New("Must at least provide either `display_name` or `type`")
return fmt.Errorf("At least one of display_name or type must be provided")
}

filter := ""
labels, err := expandMonitoringNotificationChannelLabels(d.Get("labels"), d, config)
if err != nil {
return err
}

userLabels, err := expandMonitoringNotificationChannelLabels(d.Get("user_labels"), d, config)
if err != nil {
return err
}

filters := make([]string, 0, len(labels)+2)

if displayName != "" {
filter = fmt.Sprintf("display_name=\"%s\"", displayName)
filters = append(filters, 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
}
filters = append(filters, fmt.Sprintf(`type="%s"`, channelType))
}

for k, v := range labels {
filters = append(filters, fmt.Sprintf(`labels.%s="%s"`, k, v))
}

params := make(map[string]string)
params["filter"] = filter
for k, v := range userLabels {
filters = append(filters, fmt.Sprintf(`user_labels.%s="%s"`, k, v))
}

filter := strings.Join(filters, " AND ")
params := map[string]string{
"filter": filter,
}
url, err = addQueryParams(url, params)
if err != nil {
return err
Expand All @@ -68,32 +85,21 @@ func dataSourceMonitoringNotificationChannelRead(d *schema.ResourceData, meta in
return fmt.Errorf("Error retrieving NotificationChannels: %s", err)
}

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

if len(pageMonitoringNotificationChannels) == 0 {
return fmt.Errorf("No NotificationChannel found using filter=%s", filter)
if len(channels) == 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)
if len(channels) > 1 {
return fmt.Errorf("Found more than one 1 NotificationChannel matching specified filter: %s", filter)
}

res := pageMonitoringNotificationChannels[0].(map[string]interface{})
res := channels[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
return resourceMonitoringNotificationChannelRead(d, meta)
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,42 @@ func TestAccDataSourceGoogleMonitoringNotificationChannel_byDisplayName(t *testi
Config: testAccDataSourceGoogleMonitoringNotificationChannel_byDisplayName(acctest.RandomWithPrefix("tf-test")),
Check: resource.ComposeTestCheckFunc(
checkDataSourceStateMatchesResourceState(
"data.google_monitoring_notification_channel.my",
"google_monitoring_notification_channel.my"),
"data.google_monitoring_notification_channel.default",
"google_monitoring_notification_channel.default"),
),
},
},
})
}

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

func TestAccDataSourceGoogleMonitoringNotificationChannel_UserLabel(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceGoogleMonitoringNotificationChannel_byTypeAndUserLabel(acctest.RandomWithPrefix("tf-test")),
Check: resource.ComposeTestCheckFunc(
checkDataSourceStateMatchesResourceState(
"data.google_monitoring_notification_channel.default",
"google_monitoring_notification_channel.default"),
),
},
},
Expand All @@ -52,141 +69,189 @@ func TestAccDataSourceGoogleMonitoringNotificationChannel_byDisplayNameAndType(t
Config: testAccDataSourceGoogleMonitoringNotificationChannel_byDisplayNameAndType(acctest.RandomWithPrefix("tf-test")),
Check: resource.ComposeTestCheckFunc(
checkDataSourceStateMatchesResourceState(
"data.google_monitoring_notification_channel.my",
"google_monitoring_notification_channel.myemail"),
"data.google_monitoring_notification_channel.email",
"google_monitoring_notification_channel.email"),
),
},
},
})
}

func TestAccDataSourceGoogleMonitoringNotificationChannel_NotFound(t *testing.T) {
func TestAccDataSourceGoogleMonitoringNotificationChannel_ErrorNoDisplayNameOrType(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceGoogleMonitoringNotificationChannel_NoDisplayNameOrType(),
ExpectError: regexp.MustCompile("At least one of display_name or type must be provided"),
},
},
})
}

func TestAccDataSourceGoogleMonitoringNotificationChannel_ErrorNotFound(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)),
ExpectError: regexp.MustCompile(fmt.Sprintf(`No NotificationChannel found using filter: display_name="%s"`, displayName)),
},
},
})
}

func TestAccDataSourceGoogleMonitoringNotificationChannel_NotUnique(t *testing.T) {
func TestAccDataSourceGoogleMonitoringNotificationChannel_ErrorNotUnique(t *testing.T) {
displayName := acctest.RandomWithPrefix("tf-test")
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
PreventPostDestroyRefresh: true,
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
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)),
Config: testAccDataSourceGoogleMonitoringNotificationChannel_NotUniqueWithData(displayName),
ExpectError: regexp.MustCompile(fmt.Sprintf(
`Found more than one 1 NotificationChannel matching specified filter: display_name="%s"`, displayName)),
},
},
})
}

func testAccDataSourceGoogleMonitoringNotificationChannel_byDisplayName(displayName string) string {
return fmt.Sprintf(`
resource "google_monitoring_notification_channel" "my" {
resource "google_monitoring_notification_channel" "default" {
display_name = "%s"
type = "webhook_tokenauth"

labels = {
url = "http://www.acme.org"
url = "http://www.google.com"
}
}

data "google_monitoring_notification_channel" "my" {
display_name = google_monitoring_notification_channel.my.display_name
data "google_monitoring_notification_channel" "default" {
display_name = google_monitoring_notification_channel.default.display_name
}
`, displayName)
}

func testAccDataSourceGoogleMonitoringNotificationChannel_byType(displayName string) string {
// Include label so we don't fail on dangling resources
func testAccDataSourceGoogleMonitoringNotificationChannel_byTypeAndLabel(displayName string) string {
return fmt.Sprintf(`
resource "google_monitoring_notification_channel" "my" {
resource "google_monitoring_notification_channel" "default" {
display_name = "%s"
type = "sms"
type = "email"

labels = {
number = "+1555"
email_address = "%[email protected]"
}
}

data "google_monitoring_notification_channel" "my" {
type = google_monitoring_notification_channel.my.type
data "google_monitoring_notification_channel" "default" {
type = google_monitoring_notification_channel.default.type
labels = google_monitoring_notification_channel.default.labels
}
`, displayName)
`, displayName, displayName)
}

func testAccDataSourceGoogleMonitoringNotificationChannel_byTypeAndUserLabel(displayName string) string {
return fmt.Sprintf(`
resource "google_monitoring_notification_channel" "default" {
display_name = "%s"
type = "email"

user_labels = {
testname = "%s"
}
}

data "google_monitoring_notification_channel" "default" {
type = google_monitoring_notification_channel.default.type
user_labels = google_monitoring_notification_channel.default.user_labels
}
`, displayName, displayName)
}

func testAccDataSourceGoogleMonitoringNotificationChannel_byDisplayNameAndType(displayName string) string {
return fmt.Sprintf(`
resource "google_monitoring_notification_channel" "mywebhook" {
resource "google_monitoring_notification_channel" "webhook" {
display_name = "%s"
type = "webhook_tokenauth"

labels = {
url = "http://www.acme.org"
url = "http://www.google.com"
}
}

resource "google_monitoring_notification_channel" "myemail" {
display_name = google_monitoring_notification_channel.mywebhook.display_name
resource "google_monitoring_notification_channel" "email" {
display_name = "%s"
type = "email"

labels = {
email_address = "[email protected]"
email_address = "%[email protected]"
}
}

data "google_monitoring_notification_channel" "my" {
display_name = google_monitoring_notification_channel.myemail.display_name
type = google_monitoring_notification_channel.myemail.type
data "google_monitoring_notification_channel" "email" {
display_name = google_monitoring_notification_channel.email.display_name
type = google_monitoring_notification_channel.email.type
}
`, displayName)
`, displayName, displayName, displayName)
}

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

func testAccDataSourceGoogleMonitoringNotificationChannel_NoDisplayNameOrType() string {
return `
data "google_monitoring_notification_channel" "default" {
labels = {
email = "[email protected]'"
}
user_labels = {
foo = "bar"
}
}
`
}

func testAccDataSourceGoogleMonitoringNotificationChannel_NotUnique(displayName string) string {
return fmt.Sprintf(`
resource "google_monitoring_notification_channel" "default" {
display_name = "%s"
resource "google_monitoring_notification_channel" "channel-1" {
display_name = "%[1]s"
type = "webhook_tokenauth"

labels = {
url = "http://www.acme1.org"
url = "http://%[1]s.google.com"
}
}

resource "google_monitoring_notification_channel" "default2" {
display_name = google_monitoring_notification_channel.default.display_name
resource "google_monitoring_notification_channel" "channel-2" {
display_name = google_monitoring_notification_channel.channel-1.display_name
type = "webhook_tokenauth"

labels = {
url = "http://www.acme2.org"
url = "http://%[1]s-copy.google.org"
}
}
`, displayName)
}

func testAccDataSourceGoogleMonitoringNotificationChannel_NotUniqueDS(displayName string) string {
return fmt.Sprintf(`
data "google_monitoring_notification_channel" "my" {
display_name = "%s"
func testAccDataSourceGoogleMonitoringNotificationChannel_NotUniqueWithData(displayName string) string {
return testAccDataSourceGoogleMonitoringNotificationChannel_NotUnique(displayName) + `

data "google_monitoring_notification_channel" "ds" {
display_name = google_monitoring_notification_channel.channel-2.display_name
}
`, displayName)
`
}
Loading