/
common_elastigroup_aws_beanstalk.go
191 lines (158 loc) · 5.51 KB
/
common_elastigroup_aws_beanstalk.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
180
181
182
183
184
185
186
187
188
189
190
191
package commons
import (
"fmt"
"log"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/spotinst/spotinst-sdk-go/service/elastigroup/providers/aws"
)
const (
ElastigroupAWSBeanstalkResourceName ResourceName = "spotinst_elastigroup_aws_beanstalk"
)
var ElastigroupAWSBeanstalkResource *ElastigroupAWSBeanstalkTerraformResource
type ElastigroupAWSBeanstalkTerraformResource struct {
GenericResource
}
type ElastigroupAWSBeanstalkWrapper struct {
elastigroup *aws.Group
BeanstalkEnvironmentName string
// Load balancer states
StatusElbUpdated bool
StatusTgUpdated bool
StatusMlbUpdated bool
// Block devices states
StatusEphemeralBlockDeviceUpdated bool
StatusEbsBlockDeviceUpdated bool
}
func NewElastigroupAWSBeanstalkResource(fieldsMap map[FieldName]*GenericField) *ElastigroupAWSBeanstalkTerraformResource {
return &ElastigroupAWSBeanstalkTerraformResource{
GenericResource: GenericResource{
resourceName: ElastigroupAWSBeanstalkResourceName,
fields: NewGenericFields(fieldsMap),
},
}
}
func (res *ElastigroupAWSBeanstalkTerraformResource) OnCreate(
importedGroup *aws.Group,
resourceData *schema.ResourceData,
meta interface{}) (*aws.Group, error) {
buildEmptyElastigroupCapacity(importedGroup)
buildEmptyElastigroupInstanceTypes(importedGroup)
buildEmptyElastigroupScheduling(importedGroup)
if res.fields == nil || res.fields.fieldsMap == nil || len(res.fields.fieldsMap) == 0 {
return nil, fmt.Errorf("resource fields are nil or empty, cannot create")
}
beanstalkGroupWrapper := NewElastigroupAWSBeanstalkWrapper()
beanstalkGroupWrapper.SetElastigroupAWSBeanstalk(importedGroup)
for _, field := range res.fields.fieldsMap {
if field.onCreate == nil {
continue
}
log.Printf(string(ResourceFieldOnCreate), field.resourceAffinity, field.fieldNameStr)
if err := field.onCreate(beanstalkGroupWrapper, resourceData, meta); err != nil {
return nil, err
}
}
return beanstalkGroupWrapper.GetElastigroupAWSBeanstalk(), nil
}
func (res *ElastigroupAWSBeanstalkTerraformResource) OnRead(
elastigroup *aws.Group,
resourceData *schema.ResourceData,
meta interface{}) error {
if res.fields == nil || res.fields.fieldsMap == nil || len(res.fields.fieldsMap) == 0 {
return fmt.Errorf("resource fields are nil or empty, cannot read")
}
beanstalkWrapper := NewElastigroupAWSBeanstalkWrapper()
beanstalkWrapper.SetElastigroupAWSBeanstalk(elastigroup)
for _, field := range res.fields.fieldsMap {
if field.onRead == nil {
continue
}
log.Printf(string(ResourceFieldOnRead), field.resourceAffinity, field.fieldNameStr)
if err := field.onRead(beanstalkWrapper, resourceData, meta); err != nil {
return err
}
}
return nil
}
func (res *ElastigroupAWSBeanstalkTerraformResource) OnUpdate(
resourceData *schema.ResourceData,
meta interface{}) (bool, *aws.Group, error) {
if res.fields == nil || res.fields.fieldsMap == nil || len(res.fields.fieldsMap) == 0 {
return false, nil, fmt.Errorf("resource fields are nil or empty, cannot update")
}
beanstalkWrapper := NewElastigroupAWSBeanstalkWrapper()
hasChanged := false
for _, field := range res.fields.fieldsMap {
if field.onUpdate == nil {
continue
}
if field.hasFieldChange(resourceData, meta) {
log.Printf(string(ResourceFieldOnUpdate), field.resourceAffinity, field.fieldNameStr)
if err := field.onUpdate(beanstalkWrapper, resourceData, meta); err != nil {
return false, nil, err
}
hasChanged = true
}
}
return hasChanged, beanstalkWrapper.GetElastigroupAWSBeanstalk(), nil
}
func (res *ElastigroupAWSBeanstalkTerraformResource) MaintenanceState(
resourceData *schema.ResourceData,
meta interface{}) (string, error) {
op := "NONE"
if res.fields.fieldsMap["maintenance"] != nil {
op = resourceData.Get("maintenance").(string)
return op, nil
}
return op, nil
}
// Spotinst elastigroup must have a wrapper struct.
// Reason is that there are multiple fields who share the same elastigroup API object
// e.g. LoadBalancersConfig fields and BlockDeviceMapping fields
// Wrapper struct intended to help reflecting these fields state properly into the elastigroup object.
func NewElastigroupAWSBeanstalkWrapper() *ElastigroupAWSBeanstalkWrapper {
return &ElastigroupAWSBeanstalkWrapper{
elastigroup: &aws.Group{
Scaling: &aws.Scaling{},
Scheduling: &aws.Scheduling{},
Integration: &aws.Integration{},
Compute: &aws.Compute{
LaunchSpecification: &aws.LaunchSpecification{
LoadBalancersConfig: &aws.LoadBalancersConfig{},
},
InstanceTypes: &aws.InstanceTypes{},
},
Capacity: &aws.Capacity{},
Strategy: &aws.Strategy{
Persistence: &aws.Persistence{},
},
},
}
}
func (egWrapper *ElastigroupAWSBeanstalkWrapper) GetElastigroupAWSBeanstalk() *aws.Group {
return egWrapper.elastigroup
}
func (egWrapper *ElastigroupAWSBeanstalkWrapper) SetElastigroupAWSBeanstalk(elastigroup *aws.Group) {
egWrapper.elastigroup = elastigroup
}
func buildEmptyElastigroupCompute(group *aws.Group) {
if group != nil && group.Compute == nil {
group.SetCompute(&aws.Compute{})
}
}
func buildEmptyElastigroupScheduling(group *aws.Group) {
if group != nil && group.Scheduling == nil {
group.SetScheduling(&aws.Scheduling{})
}
}
func buildEmptyElastigroupCapacity(group *aws.Group) {
if group != nil && group.Capacity == nil {
group.SetCapacity(&aws.Capacity{})
}
}
func buildEmptyElastigroupInstanceTypes(group *aws.Group) {
buildEmptyElastigroupCompute(group)
if group.Compute.InstanceTypes == nil {
group.Compute.SetInstanceTypes(&aws.InstanceTypes{})
}
}