-
Notifications
You must be signed in to change notification settings - Fork 55
/
fields_spotinst_stateful_node_azure_health.go
179 lines (154 loc) · 4.45 KB
/
fields_spotinst_stateful_node_azure_health.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package stateful_node_azure_health
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/spotinst/spotinst-sdk-go/service/stateful/providers/azure"
"github.com/spotinst/spotinst-sdk-go/spotinst"
"github.com/spotinst/terraform-provider-spotinst/spotinst/commons"
)
func Setup(fieldsMap map[commons.FieldName]*commons.GenericField) {
fieldsMap[Health] = commons.NewGenericField(
commons.StatefulNodeAzureHealth,
Health,
&schema.Schema{
Type: schema.TypeList,
Optional: true,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
string(HealthCheckTypes): {
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString},
Required: true,
},
string(GracePeriod): {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
string(UnhealthyDuration): {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
string(AutoHealing): {
Type: schema.TypeBool,
Required: true,
},
},
},
},
func(resourceObject interface{}, resourceData *schema.ResourceData, meta interface{}) error {
snWrapper := resourceObject.(*commons.StatefulNodeAzureV3Wrapper)
statefulNode := snWrapper.GetStatefulNode()
var result []interface{} = nil
if statefulNode.Health != nil {
health := statefulNode.Health
result = flattenHealth(health)
}
if result != nil {
if err := resourceData.Set(string(Health), result); err != nil {
return fmt.Errorf(string(commons.FailureFieldReadPattern), string(Health), err)
}
}
return nil
},
func(resourceObject interface{}, resourceData *schema.ResourceData, meta interface{}) error {
snWrapper := resourceObject.(*commons.StatefulNodeAzureV3Wrapper)
statefulNode := snWrapper.GetStatefulNode()
if v, ok := resourceData.GetOk(string(Health)); ok {
if health, err := expandHealth(v); err != nil {
return err
} else {
statefulNode.SetHealth(health)
}
}
return nil
},
func(resourceObject interface{}, resourceData *schema.ResourceData, meta interface{}) error {
snWrapper := resourceObject.(*commons.StatefulNodeAzureV3Wrapper)
statefulNode := snWrapper.GetStatefulNode()
var value *azure.Health = nil
if v, ok := resourceData.GetOk(string(Health)); ok {
if health, err := expandHealth(v); err != nil {
return err
} else {
value = health
}
}
statefulNode.SetHealth(value)
return nil
},
nil,
)
}
func flattenHealth(health *azure.Health) []interface{} {
var out []interface{}
if health != nil {
result := make(map[string]interface{})
if health.HealthCheckTypes != nil {
result[string(HealthCheckTypes)] = spotinst.StringSlice(health.HealthCheckTypes)
}
if health.GracePeriod != nil {
result[string(GracePeriod)] = spotinst.IntValue(health.GracePeriod)
}
if health.UnhealthyDuration != nil {
result[string(UnhealthyDuration)] = spotinst.IntValue(health.UnhealthyDuration)
}
if health.AutoHealing != nil {
result[string(AutoHealing)] = spotinst.BoolValue(health.AutoHealing)
}
if len(result) > 0 {
out = append(out, result)
}
}
return out
}
func expandHealth(data interface{}) (*azure.Health, error) {
if list := data.([]interface{}); len(list) > 0 {
health := &azure.Health{}
if list[0] != nil {
m := list[0].(map[string]interface{})
if v, ok := m[string(HealthCheckTypes)]; ok {
htc, err := expandHealthCheckTypes(v)
if err != nil {
return nil, err
}
if htc != nil {
health.SetHealthCheckTypes(htc)
}
} else {
health.SetHealthCheckTypes(nil)
}
if v, ok := m[string(GracePeriod)].(int); ok && v >= 0 {
health.SetGracePeriod(spotinst.Int(v))
} else {
health.SetGracePeriod(nil)
}
if v, ok := m[string(UnhealthyDuration)].(int); ok && v >= 0 {
health.SetUnhealthyDuration(spotinst.Int(v))
} else {
health.SetUnhealthyDuration(nil)
}
if v, ok := m[string(AutoHealing)].(bool); ok {
health.SetAutoHealing(spotinst.Bool(v))
} else {
health.SetAutoHealing(nil)
}
}
return health, nil
}
return nil, nil
}
func expandHealthCheckTypes(data interface{}) ([]string, error) {
list := data.([]interface{})
result := make([]string, 0, len(list))
for _, v := range list {
if healthCheckType, ok := v.(string); ok && healthCheckType != "" {
result = append(result, healthCheckType)
}
}
return result, nil
}