44 "errors"
55 "fmt"
66 "strconv"
7+ "strings"
78 "testing"
89
910 "github.com/hashicorp/terraform-plugin-sdk/helper/resource"
@@ -18,7 +19,7 @@ func TestAccComputeZones_basic(t *testing.T) {
1819 Providers : testAccProviders ,
1920 Steps : []resource.TestStep {
2021 {
21- Config : testAccCheckGoogleComputeZonesConfig ,
22+ Config : testAccComputeZones_basic ,
2223 Check : resource .ComposeTestCheckFunc (
2324 testAccCheckGoogleComputeZonesMeta ("data.google_compute_zones.available" ),
2425 ),
@@ -27,6 +28,24 @@ func TestAccComputeZones_basic(t *testing.T) {
2728 })
2829}
2930
31+ func TestAccComputeZones_filter (t * testing.T ) {
32+ t .Parallel ()
33+ region := "us-central1"
34+
35+ vcrTest (t , resource.TestCase {
36+ PreCheck : func () { testAccPreCheck (t ) },
37+ Providers : testAccProviders ,
38+ Steps : []resource.TestStep {
39+ {
40+ Config : testAccComputeZones_filter (region ),
41+ Check : resource .ComposeTestCheckFunc (
42+ testAccCheckGoogleComputeZonesRegion ("data.google_compute_zones.available" , region ),
43+ ),
44+ },
45+ },
46+ })
47+ }
48+
3049func testAccCheckGoogleComputeZonesMeta (n string ) resource.TestCheckFunc {
3150 return func (s * terraform.State ) error {
3251 rs , ok := s .RootModule ().Resources [n ]
@@ -67,6 +86,51 @@ func testAccCheckGoogleComputeZonesMeta(n string) resource.TestCheckFunc {
6786 }
6887}
6988
70- var testAccCheckGoogleComputeZonesConfig = `
89+ func testAccCheckGoogleComputeZonesRegion (n , region string ) resource.TestCheckFunc {
90+ return func (s * terraform.State ) error {
91+ rs , ok := s .RootModule ().Resources [n ]
92+ if ! ok {
93+ return fmt .Errorf ("Can't find zones data source: %s" , n )
94+ }
95+
96+ if rs .Primary .ID == "" {
97+ return errors .New ("zones data source ID not set." )
98+ }
99+
100+ count , ok := rs .Primary .Attributes ["names.#" ]
101+ if ! ok {
102+ return errors .New ("can't find 'names' attribute" )
103+ }
104+
105+ noOfNames , err := strconv .Atoi (count )
106+ if err != nil {
107+ return errors .New ("failed to read number of zones" )
108+ }
109+
110+ for i := 0 ; i < noOfNames ; i ++ {
111+ idx := "names." + strconv .Itoa (i )
112+ v , ok := rs .Primary .Attributes [idx ]
113+ if ! ok {
114+ return fmt .Errorf ("zone list is corrupt (%q not found), this is definitely a bug" , idx )
115+ }
116+ if ! strings .Contains (v , region ) {
117+ return fmt .Errorf ("zone name %q does not contain region %q" , v , region )
118+ }
119+ }
120+
121+ return nil
122+ }
123+ }
124+
125+ var testAccComputeZones_basic = `
71126data "google_compute_zones" "available" {}
72127`
128+
129+ func testAccComputeZones_filter (region string ) string {
130+ return fmt .Sprintf (`
131+ data "google_compute_zones" "available" {
132+ region = "%s"
133+ status = "UP"
134+ }
135+ ` , region )
136+ }
0 commit comments