-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathresource_isValid.go
78 lines (66 loc) · 1.84 KB
/
resource_isValid.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"fmt"
"strconv"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceIsValid() *schema.Resource {
return &schema.Resource{
Create: resourceIsValidCreate,
Read: resourceIsValidRead,
Update: resourceIsValidUpdate,
Delete: resourceIsValidDelete,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"test": &schema.Schema{
Type: schema.TypeMap,
Required: true,
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) {
v := val.(map[string]interface{})
var errorMessage = "Not Valid"
// Get assertValue (string)
assertValue, assertKeyExists := v["assert"]
if !assertKeyExists {
errs = append(errs, fmt.Errorf("'test' map must contain an 'assert' key"))
return
}
// Parse assert value (string -> bool)
assert, err := strconv.ParseBool(assertValue.(string))
if err != nil {
errs = append(errs, fmt.Errorf("Your assert must be a bool: %v", assertValue))
return
}
// Get optional error message
if x, ok := v["error_message"]; ok {
errorMessage = x.(string)
}
// Check assertion
if !assert {
errs = append(errs, fmt.Errorf(errorMessage))
}
return
},
},
},
}
}
func resourceIsValidCreate(d *schema.ResourceData, m interface{}) error {
name := d.Get("name").(string)
test := d.Get("test").(map[string]interface{})
d.SetId(name)
d.Set("name", name)
d.Set("test", test)
return resourceIsValidRead(d, m)
}
func resourceIsValidRead(d *schema.ResourceData, m interface{}) error {
return nil
}
func resourceIsValidUpdate(d *schema.ResourceData, m interface{}) error {
return resourceIsValidRead(d, m)
}
func resourceIsValidDelete(d *schema.ResourceData, m interface{}) error {
return nil
}