From 77fb2da19fc73126bd55f845094f8d37568bf042 Mon Sep 17 00:00:00 2001 From: Modular Magician Date: Mon, 30 Mar 2020 17:38:51 +0000 Subject: [PATCH] Add monitoring uptime check ips datasource (#3320) * Add monitoring uptime check ips datasource * Remove project * Update third_party/terraform/website/docs/d/datasource_google_monitoring_uptime_check_ips.html.markdown Co-Authored-By: Dana Hoffman Co-authored-by: Dana Hoffman Signed-off-by: Modular Magician --- .changelog/3320.txt | 3 + ...urce_google_monitoring_uptime_check_ips.go | 68 +++++++++++++++++++ ...google_monitoring_uptime_check_ips_test.go | 33 +++++++++ google/provider.go | 1 + ..._monitoring_uptime_check_ips.html.markdown | 38 +++++++++++ website/google.erb | 3 + 6 files changed, 146 insertions(+) create mode 100644 .changelog/3320.txt create mode 100644 google/data_source_google_monitoring_uptime_check_ips.go create mode 100644 google/data_source_google_monitoring_uptime_check_ips_test.go create mode 100644 website/docs/d/datasource_google_monitoring_uptime_check_ips.html.markdown diff --git a/.changelog/3320.txt b/.changelog/3320.txt new file mode 100644 index 00000000000..eb5599841e6 --- /dev/null +++ b/.changelog/3320.txt @@ -0,0 +1,3 @@ +```release-note:new-datasource +monitoring: `google_monitoring_uptime_check_ips` +``` diff --git a/google/data_source_google_monitoring_uptime_check_ips.go b/google/data_source_google_monitoring_uptime_check_ips.go new file mode 100644 index 00000000000..39ba4966b77 --- /dev/null +++ b/google/data_source_google_monitoring_uptime_check_ips.go @@ -0,0 +1,68 @@ +package google + +import ( + "fmt" + "time" + + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" +) + +func dataSourceGoogleMonitoringUptimeCheckIps() *schema.Resource { + return &schema.Resource{ + Read: dataSourceGoogleMonitoringUptimeCheckIpsRead, + + Schema: map[string]*schema.Schema{ + "uptime_check_ips": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "region": { + Type: schema.TypeString, + Computed: true, + }, + "location": { + Type: schema.TypeString, + Computed: true, + }, + "ip_address": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + }, + } +} + +func dataSourceGoogleMonitoringUptimeCheckIpsRead(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + + url := "https://monitoring.googleapis.com/v3/uptimeCheckIps" + + uptimeCheckIps, err := paginatedListRequest("", url, config, flattenUptimeCheckIpsList) + if err != nil { + return fmt.Errorf("Error retrieving monitoring uptime check ips: %s", err) + } + + if err := d.Set("uptime_check_ips", uptimeCheckIps); err != nil { + return fmt.Errorf("Error retrieving monitoring uptime check ips: %s", err) + } + d.SetId(time.Now().UTC().String()) + return nil +} + +func flattenUptimeCheckIpsList(resp map[string]interface{}) []interface{} { + ipObjList := resp["uptimeCheckIps"].([]interface{}) + uptimeCheckIps := make([]interface{}, len(ipObjList)) + for i, u := range ipObjList { + ipObj := u.(map[string]interface{}) + uptimeCheckIps[i] = map[string]interface{}{ + "region": ipObj["region"], + "location": ipObj["location"], + "ip_address": ipObj["ipAddress"], + } + } + return uptimeCheckIps +} diff --git a/google/data_source_google_monitoring_uptime_check_ips_test.go b/google/data_source_google_monitoring_uptime_check_ips_test.go new file mode 100644 index 00000000000..bab24f78003 --- /dev/null +++ b/google/data_source_google_monitoring_uptime_check_ips_test.go @@ -0,0 +1,33 @@ +package google + +import ( + "regexp" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" +) + +func TestAccDataSourceGoogleMonitoringUptimeCheckIps_basic(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceGoogleMonitoringUptimeCheckIps_basic, + Check: resource.ComposeTestCheckFunc( + resource.TestMatchResourceAttr("data.google_monitoring_uptime_check_ips.foobar", + "uptime_check_ips.0.ip_address", regexp.MustCompile("^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$")), + resource.TestMatchResourceAttr("data.google_monitoring_uptime_check_ips.foobar", + "uptime_check_ips.0.location", regexp.MustCompile("^[A-Z][a-z-]+$")), + resource.TestMatchResourceAttr("data.google_monitoring_uptime_check_ips.foobar", + "uptime_check_ips.0.region", regexp.MustCompile("^[A-Z_]+$")), + ), + }, + }, + }) +} + +const testAccDataSourceGoogleMonitoringUptimeCheckIps_basic = ` +data "google_monitoring_uptime_check_ips" "foobar" { +} +` diff --git a/google/provider.go b/google/provider.go index 4c309398cbd..4751a18fc9d 100644 --- a/google/provider.go +++ b/google/provider.go @@ -488,6 +488,7 @@ func Provider() terraform.ResourceProvider { "google_folder": dataSourceGoogleFolder(), "google_folder_organization_policy": dataSourceGoogleFolderOrganizationPolicy(), "google_monitoring_notification_channel": dataSourceMonitoringNotificationChannel(), + "google_monitoring_uptime_check_ips": dataSourceGoogleMonitoringUptimeCheckIps(), "google_netblock_ip_ranges": dataSourceGoogleNetblockIpRanges(), "google_organization": dataSourceGoogleOrganization(), "google_project": dataSourceGoogleProject(), diff --git a/website/docs/d/datasource_google_monitoring_uptime_check_ips.html.markdown b/website/docs/d/datasource_google_monitoring_uptime_check_ips.html.markdown new file mode 100644 index 00000000000..753aaeed9e5 --- /dev/null +++ b/website/docs/d/datasource_google_monitoring_uptime_check_ips.html.markdown @@ -0,0 +1,38 @@ +--- +subcategory: "Stackdriver Monitoring" +layout: "google" +page_title: "Google: google_monitoring_uptime_check_ips" +sidebar_current: "docs-google-datasource-google-monitoring-uptime-check-ips" +description: |- + Returns the list of IP addresses Stackdriver Monitoring uses for uptime checking. +--- + +# google\_monitoring\_uptime\_check\_ips + +Returns the list of IP addresses that checkers run from. For more information see +the [official documentation](https://cloud.google.com/monitoring/uptime-checks#get-ips). + +## Example Usage + +```hcl +data "google_monitoring_uptime_check_ips" "ips" { +} + +output "ip_list" { + value = data.google_monitoring_uptime_check_ips.ips.uptime_check_ips +} +``` + +## Attributes Reference + +The following computed attributes are exported: + +* `uptime_check_ips` - A list of uptime check IPs used by Stackdriver Monitoring. Each `uptime_check_ip` contains: + * `region` - A broad region category in which the IP address is located. + * `location` - A more specific location within the region that typically encodes a particular city/town/metro + (and its containing state/province or country) within the broader umbrella region category. + * `ip_address` - The IP address from which the Uptime check originates. This is a fully specified IP address + (not an IP address range). Most IP addresses, as of this publication, are in IPv4 format; however, one should not + rely on the IP addresses being in IPv4 format indefinitely, and should support interpreting this field in either + IPv4 or IPv6 format. + diff --git a/website/google.erb b/website/google.erb index d160574531f..fd42885aff1 100644 --- a/website/google.erb +++ b/website/google.erb @@ -168,6 +168,9 @@ > google_monitoring_notification_channel + > + google_monitoring_uptime_check_ips + > google_netblock_ip_ranges