/
common_elastigroup_azure_v3.go
132 lines (109 loc) · 3.8 KB
/
common_elastigroup_azure_v3.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
package commons
import (
"fmt"
"log"
azurev3 "github.com/spotinst/spotinst-sdk-go/service/elastigroup/providers/azure/v3"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
const (
ElastigroupAzureV3ResourceName ResourceName = "spotinst_elastigroup_azure_v3"
)
var ElastigroupAzureV3Resource *ElastigroupAzureV3TerraformResource
type ElastigroupAzureV3TerraformResource struct {
GenericResource
}
type ElastigroupAzureV3Wrapper struct {
elastigroup *azurev3.Group
}
func NewElastigroupAzureV3Resource(fieldsMap map[FieldName]*GenericField) *ElastigroupAzureV3TerraformResource {
return &ElastigroupAzureV3TerraformResource{
GenericResource: GenericResource{
resourceName: ElastigroupAzureV3ResourceName,
fields: NewGenericFields(fieldsMap),
},
}
}
func (res *ElastigroupAzureV3TerraformResource) OnRead(
elastigroup *azurev3.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")
}
egWrapper := NewElastigroupAzureV3Wrapper()
egWrapper.SetElastigroup(elastigroup)
for _, field := range res.fields.fieldsMap {
if field.onRead == nil {
continue
}
log.Printf(string(ResourceFieldOnRead), field.resourceAffinity, field.fieldNameStr)
if err := field.onRead(egWrapper, resourceData, meta); err != nil {
return err
}
}
return nil
}
func (res *ElastigroupAzureV3TerraformResource) OnCreate(
resourceData *schema.ResourceData,
meta interface{}) (*azurev3.Group, error) {
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")
}
egWrapper := NewElastigroupAzureV3Wrapper()
for _, field := range res.fields.fieldsMap {
if field.onCreate == nil {
continue
}
log.Printf(string(ResourceFieldOnCreate), field.resourceAffinity, field.fieldNameStr)
if err := field.onCreate(egWrapper, resourceData, meta); err != nil {
return nil, err
}
}
return egWrapper.GetElastigroup(), nil
}
func (res *ElastigroupAzureV3TerraformResource) OnUpdate(
resourceData *schema.ResourceData,
meta interface{}) (bool, *azurev3.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")
}
egWrapper := NewElastigroupAzureV3Wrapper()
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(egWrapper, resourceData, meta); err != nil {
return false, nil, err
}
hasChanged = true
}
}
return hasChanged, egWrapper.GetElastigroup(), 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 NewElastigroupAzureV3Wrapper() *ElastigroupAzureV3Wrapper {
return &ElastigroupAzureV3Wrapper{
elastigroup: &azurev3.Group{
Compute: &azurev3.Compute{
LaunchSpecification: &azurev3.LaunchSpecification{
//LoadBalancersConfig: &v3.LoadBalancersConfig{},
},
VMSizes: &azurev3.VMSizes{},
},
Capacity: &azurev3.Capacity{},
Strategy: &azurev3.Strategy{},
},
}
}
func (egWrapper *ElastigroupAzureV3Wrapper) GetElastigroup() *azurev3.Group {
return egWrapper.elastigroup
}
func (egWrapper *ElastigroupAzureV3Wrapper) SetElastigroup(elastigroup *azurev3.Group) {
egWrapper.elastigroup = elastigroup
}