-
Notifications
You must be signed in to change notification settings - Fork 55
/
fields_spotinst_elastigroup_azure_health_check.go
108 lines (98 loc) · 3.35 KB
/
fields_spotinst_elastigroup_azure_health_check.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
package elastigroup_azure_health_check
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/spotinst/spotinst-sdk-go/service/elastigroup/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[HealthCheck] = commons.NewGenericField(
commons.ElastigroupAzureHealthCheck,
HealthCheck,
&schema.Schema{
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
string(AutoHealing): {
Type: schema.TypeBool,
Optional: true,
},
string(HealthCheckType): {
Type: schema.TypeString,
Required: true,
},
string(GracePeriod): {
Type: schema.TypeInt,
Optional: true,
},
},
},
},
func(resourceObject interface{}, resourceData *schema.ResourceData, meta interface{}) error {
egWrapper := resourceObject.(*commons.ElastigroupAzureWrapper)
elastigroup := egWrapper.GetElastigroup()
var value []interface{} = nil
if elastigroup.Compute != nil && elastigroup.Compute.Health != nil {
value = flattenAzureGroupHealthCheck(elastigroup.Compute.Health)
}
if err := resourceData.Set(string(HealthCheck), value); err != nil {
return fmt.Errorf(string(commons.FailureFieldReadPattern), string(HealthCheck), err)
}
return nil
},
func(resourceObject interface{}, resourceData *schema.ResourceData, meta interface{}) error {
egWrapper := resourceObject.(*commons.ElastigroupAzureWrapper)
elastigroup := egWrapper.GetElastigroup()
if v, ok := resourceData.GetOk(string(HealthCheck)); ok {
if healthcheck, err := expandAzureGroupHealthCheck(v); err != nil {
return err
} else {
elastigroup.Compute.SetHealth(healthcheck)
}
}
return nil
},
func(resourceObject interface{}, resourceData *schema.ResourceData, meta interface{}) error {
egWrapper := resourceObject.(*commons.ElastigroupAzureWrapper)
elastigroup := egWrapper.GetElastigroup()
var value *azure.Health = nil
if v, ok := resourceData.GetOk(string(HealthCheck)); ok {
if healthcheck, err := expandAzureGroupHealthCheck(v); err != nil {
return err
} else {
value = healthcheck
}
}
elastigroup.Compute.SetHealth(value)
return nil
},
nil,
)
}
func flattenAzureGroupHealthCheck(healthcheck *azure.Health) []interface{} {
result := make(map[string]interface{})
result[string(AutoHealing)] = spotinst.BoolValue(healthcheck.AutoHealing)
result[string(HealthCheckType)] = spotinst.StringValue(healthcheck.HealthCheckType)
result[string(GracePeriod)] = spotinst.IntValue(healthcheck.GracePeriod)
return []interface{}{result}
}
func expandAzureGroupHealthCheck(data interface{}) (*azure.Health, error) {
healthcheck := &azure.Health{}
list := data.([]interface{})
if list != nil && list[0] != nil {
m := list[0].(map[string]interface{})
if v, ok := m[string(AutoHealing)].(bool); ok {
healthcheck.SetAutoHealing(spotinst.Bool(v))
}
if v, ok := m[string(HealthCheckType)].(string); ok && v != "" {
healthcheck.SetHealthCheckType(spotinst.String(v))
}
if v, ok := m[string(GracePeriod)].(int); ok && v >= 0 {
healthcheck.SetGracePeriod(spotinst.Int(v))
}
}
return healthcheck, nil
}