Skip to content

Commit

Permalink
Add monitoring uptime check ips datasource (#3320) (#6009)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

Co-authored-by: Dana Hoffman <[email protected]>
Signed-off-by: Modular Magician <[email protected]>

Co-authored-by: Dana Hoffman <[email protected]>
  • Loading branch information
modular-magician and danawillow authored Mar 30, 2020
1 parent c737a36 commit 36b2cc5
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/3320.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-datasource
monitoring: `google_monitoring_uptime_check_ips`
```
68 changes: 68 additions & 0 deletions google/data_source_google_monitoring_uptime_check_ips.go
Original file line number Diff line number Diff line change
@@ -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
}
33 changes: 33 additions & 0 deletions google/data_source_google_monitoring_uptime_check_ips_test.go
Original file line number Diff line number Diff line change
@@ -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" {
}
`
1 change: 1 addition & 0 deletions google/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
@@ -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.

3 changes: 3 additions & 0 deletions website/google.erb
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@
<li<%= sidebar_current("docs-google-datasource-monitoring-notification-channel") %>>
<a href="/docs/providers/google/d/datasource_monitoring_notification_channel.html">google_monitoring_notification_channel</a>
</li>
<li<%= sidebar_current("docs-google-datasource-google-monitoring-uptime-check-ips") %>>
<a href="/docs/providers/google/d/datasource_google_monitoring_uptime_check_ips.html">google_monitoring_uptime_check_ips</a>
</li>
<li<%= sidebar_current("docs-google-datasource-netblock-ip-ranges") %>>
<a href="/docs/providers/google/d/datasource_google_netblock_ip_ranges.html">google_netblock_ip_ranges</a>
</li>
Expand Down

0 comments on commit 36b2cc5

Please sign in to comment.