-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathresource_helper.go
75 lines (61 loc) · 1.71 KB
/
resource_helper.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
package main
import (
"github.com/hashicorp/terraform/helper/schema"
)
// resourcePropertyHelper provides commonly-used functionality for working with Terraform's schema.ResourceData.
type resourcePropertyHelper struct {
data *schema.ResourceData
}
func propertyHelper(data *schema.ResourceData) resourcePropertyHelper {
return resourcePropertyHelper{data}
}
func (helper resourcePropertyHelper) GetStringList(key string) (elements []string) {
value, ok := helper.data.GetOk(key)
if !ok {
return
}
untypedElements := value.([]interface{})
elements = make([]string, len(untypedElements))
for index, untypedElement := range untypedElements {
elements[index] = untypedElement.(string)
}
return
}
func (helper resourcePropertyHelper) SetStringList(key string, elements []string) {
untypedElements := make([]interface{}, len(elements))
for index, element := range elements {
var untypedElement interface{}
untypedElement = element
untypedElements[index] = untypedElement
}
helper.data.Set(key, untypedElements)
}
func (helper resourcePropertyHelper) GetOptionalString(key string, allowEmpty bool) *string {
value := helper.data.Get(key)
switch typedValue := value.(type) {
case string:
if len(typedValue) > 0 || allowEmpty {
return &typedValue
}
}
return nil
}
func (helper resourcePropertyHelper) GetOptionalInt(key string, allowZero bool) *int {
value := helper.data.Get(key)
switch typedValue := value.(type) {
case int:
if typedValue != 0 || allowZero {
return &typedValue
}
}
return nil
}
func (helper resourcePropertyHelper) GetOptionalBool(key string) *bool {
value := helper.data.Get(key)
switch typedValue := value.(type) {
case bool:
return &typedValue
default:
return nil
}
}