-
Notifications
You must be signed in to change notification settings - Fork 55
/
common_ocean_gke_launch_spec.go
152 lines (125 loc) · 4.19 KB
/
common_ocean_gke_launch_spec.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
package commons
import (
"fmt"
"log"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/spotinst/spotinst-sdk-go/service/ocean/providers/gcp"
)
const (
OceanGKELaunchSpecResourceName ResourceName = "spotinst_ocean_gke_launch_spec"
)
var OceanGKELaunchSpecResource *OceanGKELaunchSpecTerraformResource
type OceanGKELaunchSpecTerraformResource struct {
GenericResource
}
type LaunchSpecGKEWrapper struct {
launchSpec *gcp.LaunchSpec
}
func NewOceanGKELaunchSpecResource(fieldsMap map[FieldName]*GenericField) *OceanGKELaunchSpecTerraformResource {
return &OceanGKELaunchSpecTerraformResource{
GenericResource: GenericResource{
resourceName: OceanGKELaunchSpecResourceName,
fields: NewGenericFields(fieldsMap),
},
}
}
func (res *OceanGKELaunchSpecTerraformResource) OnCreate(
importedLaunchSpec *gcp.LaunchSpec,
resourceData *schema.ResourceData,
meta interface{}) (*gcp.LaunchSpec, 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")
}
launchSpecWrapper := NewGKELaunchSpecWrapper()
if importedLaunchSpec != nil {
buildEmptyGKELaunchSpecRequirements(importedLaunchSpec)
launchSpecWrapper.SetLaunchSpec(importedLaunchSpec)
}
for _, field := range res.fields.fieldsMap {
if field.onCreate == nil {
continue
}
log.Printf(string(ResourceFieldOnCreate), field.resourceAffinity, field.fieldNameStr)
if err := field.onCreate(launchSpecWrapper, resourceData, meta); err != nil {
return nil, err
}
}
return launchSpecWrapper.GetLaunchSpec(), nil
}
func (res *OceanGKELaunchSpecTerraformResource) OnRead(
launchSpec *gcp.LaunchSpec,
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")
}
launchSpecWrapper := NewGKELaunchSpecWrapper()
launchSpecWrapper.SetLaunchSpec(launchSpec)
for _, field := range res.fields.fieldsMap {
if field.onRead == nil {
continue
}
log.Printf(string(ResourceFieldOnRead), field.resourceAffinity, field.fieldNameStr)
if err := field.onRead(launchSpecWrapper, resourceData, meta); err != nil {
return err
}
}
return nil
}
func (res *OceanGKELaunchSpecTerraformResource) OnUpdate(
resourceData *schema.ResourceData,
meta interface{}) (bool, *gcp.LaunchSpec, 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")
}
launchSpecWrapper := NewGKELaunchSpecWrapper()
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(launchSpecWrapper, resourceData, meta); err != nil {
return false, nil, err
}
hasChanged = true
}
}
return hasChanged, launchSpecWrapper.GetLaunchSpec(), nil
}
func NewGKELaunchSpecWrapper() *LaunchSpecGKEWrapper {
return &LaunchSpecGKEWrapper{
launchSpec: &gcp.LaunchSpec{
Metadata: []*gcp.Metadata{},
Labels: []*gcp.Label{},
Taints: []*gcp.Taint{},
AutoScale: &gcp.AutoScale{
Headrooms: []*gcp.AutoScaleHeadroom{},
},
ShieldedInstanceConfig: &gcp.ShieldedInstanceConfig{},
Storage: &gcp.Storage{},
LaunchSpecScheduling: &gcp.GKELaunchSpecScheduling{},
},
}
}
func (launchSpecWrapper *LaunchSpecGKEWrapper) GetLaunchSpec() *gcp.LaunchSpec {
return launchSpecWrapper.launchSpec
}
func (launchSpecWrapper *LaunchSpecGKEWrapper) SetLaunchSpec(launchSpec *gcp.LaunchSpec) {
launchSpecWrapper.launchSpec = launchSpec
}
func buildEmptyGKELaunchSpecRequirements(launchSpec *gcp.LaunchSpec) {
if launchSpec == nil {
return
}
if launchSpec.AutoScale == nil {
launchSpec.SetAutoScale(&gcp.AutoScale{})
}
if launchSpec.AutoScale.Headrooms == nil {
launchSpec.AutoScale.SetHeadrooms([]*gcp.AutoScaleHeadroom{})
}
if launchSpec.LaunchSpecScheduling == nil {
launchSpec.SetScheduling(&gcp.GKELaunchSpecScheduling{})
}
}