@@ -2,6 +2,7 @@ package main
22
33import (
44 "fmt"
5+ "strconv"
56
67 "github.com/hashicorp/terraform/helper/schema"
78)
@@ -19,12 +20,34 @@ func resourceIsValid() *schema.Resource {
1920 Required : true ,
2021 },
2122 "test" : & schema.Schema {
22- Type : schema .TypeBool ,
23+ Type : schema .TypeMap ,
2324 Required : true ,
2425 ValidateFunc : func (val interface {}, key string ) (warns []string , errs []error ) {
25- v := val .(bool )
26- if v != true {
27- errs = append (errs , fmt .Errorf ("Not Valid" ))
26+ v := val .(map [string ]interface {})
27+ var errorMessage = "Not Valid"
28+
29+ // Get assertValue (string)
30+ assertValue , assertKeyExists := v ["assert" ]
31+ if ! assertKeyExists {
32+ errs = append (errs , fmt .Errorf ("'test' map must contain an 'assert' key" ))
33+ return
34+ }
35+
36+ // Parse assert value (string -> bool)
37+ assert , err := strconv .ParseBool (assertValue .(string ))
38+ if err != nil {
39+ errs = append (errs , fmt .Errorf ("Your assert must be a bool: %v" , assertValue ))
40+ return
41+ }
42+
43+ // Get optional error message
44+ if x , ok := v ["error_message" ]; ok {
45+ errorMessage = x .(string )
46+ }
47+
48+ // Check assertion
49+ if ! assert {
50+ errs = append (errs , fmt .Errorf (errorMessage ))
2851 }
2952 return
3053 },
@@ -43,7 +66,7 @@ func resourceIsValidCreate(d *schema.ResourceData, m interface{}) error {
4366}
4467
4568func resourceIsValidRead (d * schema.ResourceData , m interface {}) error {
46- return nil
69+ return nil
4770}
4871
4972func resourceIsValidUpdate (d * schema.ResourceData , m interface {}) error {
0 commit comments