-
Notifications
You must be signed in to change notification settings - Fork 55
/
fields_spotinst_ocean_gke_strategy.go
144 lines (125 loc) · 3.93 KB
/
fields_spotinst_ocean_gke_strategy.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
package ocean_gke_import_strategy
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/spotinst/spotinst-sdk-go/service/ocean/providers/gcp"
"github.com/spotinst/spotinst-sdk-go/spotinst"
"github.com/spotinst/terraform-provider-spotinst/spotinst/commons"
)
func Setup(fieldsMap map[commons.FieldName]*commons.GenericField) {
fieldsMap[Strategy] = commons.NewGenericField(
commons.OceanGKEImportStrategy,
Strategy,
&schema.Schema{
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
string(DrainingTimeout): {
Type: schema.TypeInt,
Optional: true,
},
string(ProvisioningModel): {
Type: schema.TypeString,
Optional: true,
},
string(PreemptiblePercentage): {
Type: schema.TypeInt,
Optional: true,
Default: -1,
ValidateFunc: validation.IntAtLeast(-1),
},
},
},
},
func(resourceObject interface{}, resourceData *schema.ResourceData, meta interface{}) error {
clusterWrapper := resourceObject.(*commons.GKEImportClusterWrapper)
cluster := clusterWrapper.GetCluster()
var result []interface{} = nil
if cluster.Strategy != nil {
strategy := cluster.Strategy
result = flattenStrategy(strategy)
}
if result != nil {
if err := resourceData.Set(string(Strategy), result); err != nil {
return fmt.Errorf(string(commons.FailureFieldReadPattern), string(Strategy), err)
}
}
return nil
},
func(resourceObject interface{}, resourceData *schema.ResourceData, meta interface{}) error {
clusterWrapper := resourceObject.(*commons.GKEImportClusterWrapper)
cluster := clusterWrapper.GetCluster()
if v, ok := resourceData.GetOk(string(Strategy)); ok {
if strategy, err := expandStrategy(v); err != nil {
return err
} else {
cluster.SetStrategy(strategy)
}
}
return nil
},
func(resourceObject interface{}, resourceData *schema.ResourceData, meta interface{}) error {
clusterWrapper := resourceObject.(*commons.GKEImportClusterWrapper)
cluster := clusterWrapper.GetCluster()
var value *gcp.Strategy = nil
if v, ok := resourceData.GetOk(string(Strategy)); ok {
if strategy, err := expandStrategy(v); err != nil {
return err
} else {
value = strategy
}
}
cluster.SetStrategy(value)
return nil
},
nil,
)
}
func flattenStrategy(strategy *gcp.Strategy) []interface{} {
var out []interface{}
if strategy != nil {
result := make(map[string]interface{})
if strategy.DrainingTimeout != nil {
result[string(DrainingTimeout)] = spotinst.IntValue(strategy.DrainingTimeout)
}
if strategy.ProvisioningModel != nil {
result[string(ProvisioningModel)] = spotinst.StringValue(strategy.ProvisioningModel)
}
preemptiblePercentage := spotinst.Int(-1)
if strategy.PreemptiblePercentage != nil {
preemptiblePercentage = strategy.PreemptiblePercentage
}
result[string(PreemptiblePercentage)] = spotinst.IntValue(preemptiblePercentage)
if len(result) > 0 {
out = append(out, result)
}
}
return out
}
func expandStrategy(data interface{}) (*gcp.Strategy, error) {
if list := data.([]interface{}); len(list) > 0 {
strategy := &gcp.Strategy{}
if list[0] != nil {
m := list[0].(map[string]interface{})
if v, ok := m[string(DrainingTimeout)].(int); ok && v > 0 {
strategy.SetDrainingTimeout(spotinst.Int(v))
} else {
strategy.SetDrainingTimeout(nil)
}
if v, ok := m[string(ProvisioningModel)].(string); ok && v != "" {
strategy.SetProvisioningModel(spotinst.String(v))
} else {
strategy.SetProvisioningModel(nil)
}
if v, ok := m[string(PreemptiblePercentage)].(int); ok && v >= 0 {
strategy.SetPreemptiblePercentage(spotinst.Int(v))
} else {
strategy.SetPreemptiblePercentage(nil)
}
}
return strategy, nil
}
return nil, nil
}