Skip to content

Commit d766990

Browse files
Sébastien GLONrosbo
Sébastien GLON
authored andcommitted
Add new google_compute_regions (#1603)
* Add new google_compute_regions * Add docs * correct doc
1 parent 1bd1577 commit d766990

5 files changed

+187
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package google
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"sort"
7+
"time"
8+
9+
"github.com/hashicorp/terraform/helper/schema"
10+
"github.com/hashicorp/terraform/helper/validation"
11+
"google.golang.org/api/compute/v1"
12+
)
13+
14+
func dataSourceGoogleComputeRegions() *schema.Resource {
15+
return &schema.Resource{
16+
Read: dataSourceGoogleComputeRegionsRead,
17+
Schema: map[string]*schema.Schema{
18+
"project": &schema.Schema{
19+
Type: schema.TypeString,
20+
Optional: true,
21+
Computed: true,
22+
},
23+
"names": {
24+
Type: schema.TypeList,
25+
Computed: true,
26+
Elem: &schema.Schema{Type: schema.TypeString},
27+
},
28+
"status": {
29+
Type: schema.TypeString,
30+
Optional: true,
31+
ValidateFunc: validation.StringInSlice([]string{"UP", "DOWN"}, false),
32+
},
33+
},
34+
}
35+
}
36+
37+
func dataSourceGoogleComputeRegionsRead(d *schema.ResourceData, meta interface{}) error {
38+
config := meta.(*Config)
39+
40+
project, err := getProject(d, config)
41+
if err != nil {
42+
return err
43+
}
44+
filter := ""
45+
if s, ok := d.GetOk("status"); ok {
46+
filter = fmt.Sprintf(" (status eq %s)", s)
47+
}
48+
49+
call := config.clientCompute.Regions.List(project).Filter(filter)
50+
51+
resp, err := call.Do()
52+
if err != nil {
53+
return err
54+
}
55+
56+
regions := flattenRegions(resp.Items)
57+
log.Printf("[DEBUG] Received Google Compute Regions: %q", regions)
58+
59+
d.Set("names", regions)
60+
d.Set("project", project)
61+
d.SetId(time.Now().UTC().String())
62+
63+
return nil
64+
}
65+
66+
func flattenRegions(regions []*compute.Region) []string {
67+
result := make([]string, len(regions), len(regions))
68+
for i, region := range regions {
69+
result[i] = region.Name
70+
}
71+
sort.Strings(result)
72+
return result
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package google
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"strconv"
7+
"testing"
8+
9+
"github.com/hashicorp/terraform/helper/resource"
10+
"github.com/hashicorp/terraform/terraform"
11+
)
12+
13+
func TestAccComputeRegions_basic(t *testing.T) {
14+
t.Parallel()
15+
16+
resource.Test(t, resource.TestCase{
17+
PreCheck: func() { testAccPreCheck(t) },
18+
Providers: testAccProviders,
19+
Steps: []resource.TestStep{
20+
{
21+
Config: testAccCheckGoogleComputeRegionsConfig,
22+
Check: resource.ComposeTestCheckFunc(
23+
testAccCheckGoogleComputeRegionsMeta("data.google_compute_regions.available"),
24+
),
25+
},
26+
},
27+
})
28+
}
29+
30+
func testAccCheckGoogleComputeRegionsMeta(n string) resource.TestCheckFunc {
31+
return func(s *terraform.State) error {
32+
rs, ok := s.RootModule().Resources[n]
33+
if !ok {
34+
return fmt.Errorf("Can't find regions data source: %s", n)
35+
}
36+
37+
if rs.Primary.ID == "" {
38+
return errors.New("regions data source ID not set.")
39+
}
40+
41+
count, ok := rs.Primary.Attributes["names.#"]
42+
if !ok {
43+
return errors.New("can't find 'names' attribute")
44+
}
45+
46+
noOfNames, err := strconv.Atoi(count)
47+
if err != nil {
48+
return errors.New("failed to read number of regions")
49+
}
50+
if noOfNames < 2 {
51+
return fmt.Errorf("expected at least 2 regions, received %d, this is most likely a bug",
52+
noOfNames)
53+
}
54+
55+
for i := 0; i < noOfNames; i++ {
56+
idx := "names." + strconv.Itoa(i)
57+
v, ok := rs.Primary.Attributes[idx]
58+
if !ok {
59+
return fmt.Errorf("region list is corrupt (%q not found), this is definitely a bug", idx)
60+
}
61+
if len(v) < 1 {
62+
return fmt.Errorf("Empty region name (%q), this is definitely a bug", idx)
63+
}
64+
}
65+
66+
return nil
67+
}
68+
}
69+
70+
var testAccCheckGoogleComputeRegionsConfig = `
71+
data "google_compute_regions" "available" {}
72+
`

google/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ func Provider() terraform.ResourceProvider {
9494
"google_storage_object_signed_url": dataSourceGoogleSignedUrl(),
9595
"google_storage_project_service_account": dataSourceGoogleStorageProjectServiceAccount(),
9696
"google_compute_backend_service": dataSourceGoogleComputeBackendService(),
97+
"google_compute_regions": dataSourceGoogleComputeRegions(),
9798
},
9899

99100
ResourcesMap: mergeResourceMaps(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
layout: "google"
3+
page_title: "Google: google_compute_regions"
4+
sidebar_current: "docs-google-datasource-compute-regions"
5+
description: |-
6+
Provides a list of available Google Compute regions
7+
---
8+
9+
# google\_compute\_regions
10+
11+
Provides access to available Google Compute regions for a given project.
12+
See more about [regions and regions](https://cloud.google.com/compute/docs/regions-zones/) in the upstream docs.
13+
14+
```
15+
data "google_compute_regions" "available" {}
16+
17+
resource "google_compute_subnetwork" "cluster" {
18+
count = "${length(data.google_compute_regions.available.names)}"
19+
name = "my-network"
20+
ip_cidr_range = "10.36.${count.index}.0/24"
21+
network = "my-network"
22+
region = "${data.google_compute_regions.available.names[count.index]}"
23+
}
24+
```
25+
26+
## Argument Reference
27+
28+
The following arguments are supported:
29+
30+
* `project` (Optional) - Project from which to list available regions. Defaults to project declared in the provider.
31+
* `status` (Optional) - Allows to filter list of regions based on their current status. Status can be either `UP` or `DOWN`.
32+
Defaults to no filtering (all available regions - both `UP` and `DOWN`).
33+
34+
## Attributes Reference
35+
36+
The following attribute is exported:
37+
38+
* `names` - A list of regions available in the given project

website/google.erb

+3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@
4343
<li<%= sidebar_current("docs-google-datasource-project") %>>
4444
<a href="/docs/providers/google/d/google_project.html">google_project</a>
4545
</li>
46+
<li<%= sidebar_current("docs-google-datasource-compute-regions") %>>
47+
<a href="/docs/providers/google/d/google_compute_regions.html">google_compute_regions</a>
48+
</li>
4649
<li<%= sidebar_current("docs-google-datasource-compute-ssl-policy") %>>
4750
<a href="/docs/providers/google/d/datasource_compute_ssl_policy.html">google_compute_ssl_policy</a>
4851
</li>

0 commit comments

Comments
 (0)