diff --git a/google/resource_container_cluster.go b/google/resource_container_cluster.go index 6f6581abaa2..d9b21af3cde 100644 --- a/google/resource_container_cluster.go +++ b/google/resource_container_cluster.go @@ -936,7 +936,6 @@ func flattenMasterAuthorizedNetworksConfig(c *container.MasterAuthorizedNetworks "display_name": v.DisplayName, }) } - // now does this adhere to the schema? result["cidr_blocks"] = cidrBlocks } return []map[string]interface{}{result} diff --git a/google/resource_container_cluster_test.go b/google/resource_container_cluster_test.go index 38dab58ebd1..5201b9b8121 100644 --- a/google/resource_container_cluster_test.go +++ b/google/resource_container_cluster_test.go @@ -137,8 +137,8 @@ func TestAccContainerCluster_withMasterAuthorizedNetworksConfig(t *testing.T) { Config: testAccContainerCluster_withMasterAuthorizedNetworksConfig(clusterName, []string{"8.8.8.8/32"}), Check: resource.ComposeTestCheckFunc( testAccCheckContainerCluster("google_container_cluster.with_master_authorized_networks"), - resource.TestCheckNoResourceAttr("google_container_cluster.with_master_authorized_networks", - "master_authorized_networks_config.0.cidr_blocks"), + resource.TestCheckResourceAttr("google_container_cluster.with_master_authorized_networks", + "master_authorized_networks_config.0.cidr_blocks.#", "1"), ), }, }, @@ -943,7 +943,7 @@ resource "google_container_cluster" "with_master_authorized_networks" { master_authorized_networks_config { %s - } + } }`, clusterName, cidrBlocks) } diff --git a/google/validation_test.go b/google/validation_test.go index e76b954e022..4b9a5c91236 100644 --- a/google/validation_test.go +++ b/google/validation_test.go @@ -28,12 +28,45 @@ func TestValidateGCPName(t *testing.T) { } } +func TestValidateRFC1918Network(t *testing.T) { + x := []RFC1918NetworkTestCase{ + // No errors + {TestName: "valid 10.x", CIDR: "10.0.0.0/8", MinPrefix: 0, MaxPrefix: 32}, + {TestName: "valid 172.x", CIDR: "172.16.0.0/16", MinPrefix: 0, MaxPrefix: 32}, + {TestName: "valid 192.x", CIDR: "192.168.0.0/32", MinPrefix: 0, MaxPrefix: 32}, + {TestName: "valid, bounded 10.x CIDR", CIDR: "10.0.0.0/8", MinPrefix: 8, MaxPrefix: 32}, + {TestName: "valid, bounded 172.x CIDR", CIDR: "172.16.0.0/16", MinPrefix: 12, MaxPrefix: 32}, + {TestName: "valid, bounded 192.x CIDR", CIDR: "192.168.0.0/32", MinPrefix: 16, MaxPrefix: 32}, + + // With errors + {TestName: "empty CIDR", CIDR: "", MinPrefix: 0, MaxPrefix: 32, ExpectError: true}, + {TestName: "missing mask", CIDR: "10.0.0.0", MinPrefix: 0, MaxPrefix: 32, ExpectError: true}, + {TestName: "invalid CIDR", CIDR: "10.1.0.0/8", MinPrefix: 0, MaxPrefix: 32, ExpectError: true}, + {TestName: "valid 10.x CIDR with lower bound violation", CIDR: "10.0.0.0/8", MinPrefix: 16, MaxPrefix: 32, ExpectError: true}, + {TestName: "valid 10.x CIDR with upper bound violation", CIDR: "10.0.0.0/24", MinPrefix: 8, MaxPrefix: 16, ExpectError: true}, + {TestName: "valid public CIDR", CIDR: "8.8.8.8/32", MinPrefix: 0, MaxPrefix: 32, ExpectError: true}, + } + + es := testRFC1918Networks(x) + if len(es) > 0 { + t.Errorf("Failed to validate RFC1918 Networks: %v", es) + } +} + type GCPNameTestCase struct { TestName string Value string ExpectError bool } +type RFC1918NetworkTestCase struct { + TestName string + CIDR string + MinPrefix int + MaxPrefix int + ExpectError bool +} + func testGCPNames(cases []GCPNameTestCase) []error { es := make([]error, 0) for _, c := range cases { @@ -55,3 +88,26 @@ func testGCPName(testCase GCPNameTestCase) []error { return es } + +func testRFC1918Networks(cases []RFC1918NetworkTestCase) []error { + es := make([]error, 0) + for _, c := range cases { + es = append(es, testRFC1918Network(c)...) + } + + return es +} + +func testRFC1918Network(testCase RFC1918NetworkTestCase) []error { + f := validateRFC1918Network(testCase.MinPrefix, testCase.MaxPrefix) + _, es := f(testCase.CIDR, testCase.TestName) + if testCase.ExpectError { + if len(es) > 0 { + return nil + } + return []error{fmt.Errorf("Didn't see expected error in case \"%s\" with CIDR=\"%s\" MinPrefix=%v MaxPrefix=%v", + testCase.TestName, testCase.CIDR, testCase.MinPrefix, testCase.MaxPrefix)} + } + + return es +}