Skip to content

Commit

Permalink
Merge pull request #135 from terraform-providers/b-label-vals-invalid
Browse files Browse the repository at this point in the history
resource/*: Treat non-string label values as invalid
  • Loading branch information
radeksimko authored Mar 5, 2018
2 parents 6fe21b4 + c70df4c commit 2a4d31e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
30 changes: 30 additions & 0 deletions kubernetes/resource_kubernetes_namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,22 @@ func TestAccKubernetesNamespace_basic(t *testing.T) {
})
}

func TestAccKubernetesNamespace_invalidLabelValueType(t *testing.T) {
nsName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckKubernetesNamespaceDestroy,
Steps: []resource.TestStep{
{
Config: testAccKubernetesNamespaceConfig_invalidLabelValueType(nsName),
ExpectError: regexp.MustCompile("Expected value to be string"),
},
},
})
}

func TestAccKubernetesNamespace_importBasic(t *testing.T) {
resourceName := "kubernetes_namespace.test"
nsName := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
Expand Down Expand Up @@ -400,3 +416,17 @@ resource "kubernetes_namespace" "test" {
}
}`, nsName)
}

func testAccKubernetesNamespaceConfig_invalidLabelValueType(nsName string) string {
return fmt.Sprintf(`
resource "kubernetes_namespace" "test" {
metadata {
labels {
"first" = "one"
"integer" = 2
"bool" = true
}
name = "%s"
}
}`, nsName)
}
2 changes: 2 additions & 0 deletions kubernetes/schema_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func metadataFields(objectName string) map[string]*schema.Schema {
Type: schema.TypeMap,
Description: fmt.Sprintf("An unstructured key value map stored with the %s that may be used to store arbitrary metadata. More info: http://kubernetes.io/docs/user-guide/annotations", objectName),
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
ValidateFunc: validateAnnotations,
},
"generation": {
Expand All @@ -23,6 +24,7 @@ func metadataFields(objectName string) map[string]*schema.Schema {
Type: schema.TypeMap,
Description: fmt.Sprintf("Map of string keys and values that can be used to organize and categorize (scope and select) the %s. May match selectors of replication controllers and services. More info: http://kubernetes.io/docs/user-guide/labels", objectName),
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
ValidateFunc: validateLabels,
},
"name": {
Expand Down
6 changes: 5 additions & 1 deletion kubernetes/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ func validateLabels(value interface{}, key string) (ws []string, es []error) {
for _, msg := range utilValidation.IsQualifiedName(k) {
es = append(es, fmt.Errorf("%s (%q) %s", key, k, msg))
}
val := v.(string)
val, isString := v.(string)
if !isString {
es = append(es, fmt.Errorf("%s.%s (%#v): Expected value to be string", key, k, v))
return
}
for _, msg := range utilValidation.IsValidLabelValue(val) {
es = append(es, fmt.Errorf("%s (%q) %s", key, val, msg))
}
Expand Down

0 comments on commit 2a4d31e

Please sign in to comment.