mirror of
https://github.com/letic/terraform-provider-google.git
synced 2024-10-03 01:01:06 +00:00
provider/google: Add google_compute_zones data source
This commit is contained in:
parent
5a3eb9394e
commit
f353843c2f
80
data_source_google_compute_zones.go
Normal file
80
data_source_google_compute_zones.go
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
package google
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"sort"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/hashicorp/terraform/helper/schema"
|
||||||
|
compute "google.golang.org/api/compute/v1"
|
||||||
|
)
|
||||||
|
|
||||||
|
func dataSourceGoogleComputeZones() *schema.Resource {
|
||||||
|
return &schema.Resource{
|
||||||
|
Read: dataSourceGoogleComputeZonesRead,
|
||||||
|
Schema: map[string]*schema.Schema{
|
||||||
|
"region": {
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Optional: true,
|
||||||
|
},
|
||||||
|
"names": {
|
||||||
|
Type: schema.TypeList,
|
||||||
|
Computed: true,
|
||||||
|
Elem: &schema.Schema{Type: schema.TypeString},
|
||||||
|
},
|
||||||
|
"status": {
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Optional: true,
|
||||||
|
ValidateFunc: func(v interface{}, k string) (ws []string, es []error) {
|
||||||
|
value := v.(string)
|
||||||
|
if value != "UP" && value != "DOWN" {
|
||||||
|
es = append(es, fmt.Errorf("%q can only be 'UP' or 'DOWN' (%q given)", k, value))
|
||||||
|
}
|
||||||
|
return
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func dataSourceGoogleComputeZonesRead(d *schema.ResourceData, meta interface{}) error {
|
||||||
|
config := meta.(*Config)
|
||||||
|
|
||||||
|
region := config.Region
|
||||||
|
if r, ok := d.GetOk("region"); ok {
|
||||||
|
region = r.(string)
|
||||||
|
}
|
||||||
|
|
||||||
|
regionUrl := fmt.Sprintf("https://www.googleapis.com/compute/v1/projects/%s/regions/%s",
|
||||||
|
config.Project, region)
|
||||||
|
filter := fmt.Sprintf("(region eq %s)", regionUrl)
|
||||||
|
|
||||||
|
if s, ok := d.GetOk("status"); ok {
|
||||||
|
filter += fmt.Sprintf(" (status eq %s)", s)
|
||||||
|
}
|
||||||
|
|
||||||
|
call := config.clientCompute.Zones.List(config.Project).Filter(filter)
|
||||||
|
|
||||||
|
resp, err := call.Do()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
zones := flattenZones(resp.Items)
|
||||||
|
log.Printf("[DEBUG] Received Google Compute Zones: %q", zones)
|
||||||
|
|
||||||
|
d.Set("names", zones)
|
||||||
|
d.SetId(time.Now().UTC().String())
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func flattenZones(zones []*compute.Zone) []string {
|
||||||
|
result := make([]string, len(zones), len(zones))
|
||||||
|
for i, zone := range zones {
|
||||||
|
result[i] = zone.Name
|
||||||
|
}
|
||||||
|
sort.Strings(result)
|
||||||
|
return result
|
||||||
|
}
|
70
data_source_google_compute_zones_test.go
Normal file
70
data_source_google_compute_zones_test.go
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
package google
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/hashicorp/terraform/helper/resource"
|
||||||
|
"github.com/hashicorp/terraform/terraform"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestAccGoogleComputeZones_basic(t *testing.T) {
|
||||||
|
resource.Test(t, resource.TestCase{
|
||||||
|
PreCheck: func() { testAccPreCheck(t) },
|
||||||
|
Providers: testAccProviders,
|
||||||
|
Steps: []resource.TestStep{
|
||||||
|
{
|
||||||
|
Config: testAccCheckGoogleComputeZonesConfig,
|
||||||
|
Check: resource.ComposeTestCheckFunc(
|
||||||
|
testAccCheckGoogleComputeZonesMeta("data.google_compute_zones.available"),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func testAccCheckGoogleComputeZonesMeta(n string) resource.TestCheckFunc {
|
||||||
|
return func(s *terraform.State) error {
|
||||||
|
rs, ok := s.RootModule().Resources[n]
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("Can't find zones data source: %s", n)
|
||||||
|
}
|
||||||
|
|
||||||
|
if rs.Primary.ID == "" {
|
||||||
|
return errors.New("zones data source ID not set.")
|
||||||
|
}
|
||||||
|
|
||||||
|
count, ok := rs.Primary.Attributes["names.#"]
|
||||||
|
if !ok {
|
||||||
|
return errors.New("can't find 'names' attribute")
|
||||||
|
}
|
||||||
|
|
||||||
|
noOfNames, err := strconv.Atoi(count)
|
||||||
|
if err != nil {
|
||||||
|
return errors.New("failed to read number of zones")
|
||||||
|
}
|
||||||
|
if noOfNames < 2 {
|
||||||
|
return fmt.Errorf("expected at least 2 zones, received %d, this is most likely a bug",
|
||||||
|
noOfNames)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < noOfNames; i++ {
|
||||||
|
idx := "names." + strconv.Itoa(i)
|
||||||
|
v, ok := rs.Primary.Attributes[idx]
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("zone list is corrupt (%q not found), this is definitely a bug", idx)
|
||||||
|
}
|
||||||
|
if len(v) < 1 {
|
||||||
|
return fmt.Errorf("Empty zone name (%q), this is definitely a bug", idx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var testAccCheckGoogleComputeZonesConfig = `
|
||||||
|
data "google_compute_zones" "available" {}
|
||||||
|
`
|
@ -58,6 +58,7 @@ func Provider() terraform.ResourceProvider {
|
|||||||
|
|
||||||
DataSourcesMap: map[string]*schema.Resource{
|
DataSourcesMap: map[string]*schema.Resource{
|
||||||
"google_iam_policy": dataSourceGoogleIamPolicy(),
|
"google_iam_policy": dataSourceGoogleIamPolicy(),
|
||||||
|
"google_compute_zones": dataSourceGoogleComputeZones(),
|
||||||
},
|
},
|
||||||
|
|
||||||
ResourcesMap: map[string]*schema.Resource{
|
ResourcesMap: map[string]*schema.Resource{
|
||||||
|
Loading…
Reference in New Issue
Block a user