-
Notifications
You must be signed in to change notification settings - Fork 55
/
fields_spotinst_ocean_aks_np_health.go
112 lines (99 loc) · 2.87 KB
/
fields_spotinst_ocean_aks_np_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
package ocean_aks_np_health
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/spotinst/spotinst-sdk-go/service/ocean/providers/azure_np"
"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.OceanAKSNPHealth,
Health,
&schema.Schema{
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
string(GracePeriod): {
Type: schema.TypeInt,
Optional: true,
Default: -1,
},
},
},
},
func(resourceObject interface{}, resourceData *schema.ResourceData, meta interface{}) error {
clusterWrapper := resourceObject.(*commons.AKSNPClusterWrapper)
cluster := clusterWrapper.GetNPCluster()
var result []interface{} = nil
if cluster != nil && cluster.Health != nil {
result = flattenHealth(cluster.Health)
}
if len(result) > 0 {
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 {
clusterWrapper := resourceObject.(*commons.AKSNPClusterWrapper)
cluster := clusterWrapper.GetNPCluster()
var value *azure_np.Health = nil
if v, ok := resourceData.GetOk(string(Health)); ok {
if health, err := expandHealth(v); err != nil {
return err
} else {
value = health
}
}
cluster.SetHealth(value)
return nil
},
func(resourceObject interface{}, resourceData *schema.ResourceData, meta interface{}) error {
clusterWrapper := resourceObject.(*commons.AKSNPClusterWrapper)
cluster := clusterWrapper.GetNPCluster()
var value *azure_np.Health = nil
if v, ok := resourceData.GetOk(string(Health)); ok {
if health, err := expandHealth(v); err != nil {
return err
} else {
value = health
}
}
cluster.SetHealth(value)
return nil
},
nil,
)
}
func expandHealth(data interface{}) (*azure_np.Health, error) {
if list := data.([]interface{}); len(list) > 0 {
health := &azure_np.Health{}
if list[0] != nil {
m := list[0].(map[string]interface{})
if v, ok := m[string(GracePeriod)].(int); ok && v > -1 {
health.SetGracePeriod(spotinst.Int(v))
} else {
health.SetGracePeriod(nil)
}
}
return health, nil
}
return nil, nil
}
func flattenHealth(health *azure_np.Health) []interface{} {
var out []interface{}
if health != nil {
result := make(map[string]interface{})
if health.GracePeriod != nil {
result[string(GracePeriod)] = spotinst.IntValue(health.GracePeriod)
}
if len(result) > 0 {
out = append(out, result)
}
}
return out
}