-
Notifications
You must be signed in to change notification settings - Fork 55
/
fields_spotinst_ocean_aks_os_disk.go
114 lines (99 loc) · 3.19 KB
/
fields_spotinst_ocean_aks_os_disk.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
package ocean_aks_os_disk
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/spotinst/spotinst-sdk-go/service/ocean/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[OSDisk] = commons.NewGenericField(
commons.OceanAKSLaunchSpecification,
OSDisk,
&schema.Schema{
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
string(SizeGB): {
Type: schema.TypeInt,
Required: true,
},
string(Type): {
Type: schema.TypeString,
Optional: true,
},
},
},
},
func(resourceObject interface{}, resourceData *schema.ResourceData, meta interface{}) error {
clusterWrapper := resourceObject.(*commons.AKSClusterWrapper)
cluster := clusterWrapper.GetCluster()
var value interface{} = nil
if cluster.VirtualNodeGroupTemplate != nil && cluster.VirtualNodeGroupTemplate.LaunchSpecification != nil &&
cluster.VirtualNodeGroupTemplate.LaunchSpecification.OSDisk != nil {
value = flattenOSDisk(cluster.VirtualNodeGroupTemplate.LaunchSpecification.OSDisk)
}
if err := resourceData.Set(string(OSDisk), value); err != nil {
return fmt.Errorf(string(commons.FailureFieldReadPattern), string(OSDisk), err)
}
return nil
},
func(resourceObject interface{}, resourceData *schema.ResourceData, meta interface{}) error {
clusterWrapper := resourceObject.(*commons.AKSClusterWrapper)
cluster := clusterWrapper.GetCluster()
var value *azure.OSDisk = nil
if v, ok := resourceData.GetOk(string(OSDisk)); ok {
if osDisk, err := expandOSDisk(v); err != nil {
return err
} else {
value = osDisk
}
}
cluster.VirtualNodeGroupTemplate.LaunchSpecification.SetOSDisk(value)
return nil
},
func(resourceObject interface{}, resourceData *schema.ResourceData, meta interface{}) error {
clusterWrapper := resourceObject.(*commons.AKSClusterWrapper)
cluster := clusterWrapper.GetCluster()
var value *azure.OSDisk = nil
if v, ok := resourceData.GetOk(string(OSDisk)); ok {
if osDisk, err := expandOSDisk(v); err != nil {
return err
} else {
value = osDisk
}
}
cluster.VirtualNodeGroupTemplate.LaunchSpecification.SetOSDisk(value)
return nil
},
nil,
)
}
func flattenOSDisk(osd *azure.OSDisk) interface{} {
osDisk := make(map[string]interface{})
osDisk[string(SizeGB)] = spotinst.IntValue(osd.SizeGB)
osDisk[string(Type)] = spotinst.StringValue(osd.Type)
return []interface{}{osDisk}
}
func expandOSDisk(data interface{}) (*azure.OSDisk, error) {
if list := data.([]interface{}); len(list) > 0 {
osDisk := &azure.OSDisk{}
if list[0] != nil {
m := list[0].(map[string]interface{})
var sizeGB *int = nil
var osType *string = nil
if v, ok := m[string(SizeGB)].(int); ok && v > 0 {
sizeGB = spotinst.Int(v)
}
osDisk.SetSizeGB(sizeGB)
if v, ok := m[string(Type)].(string); ok && v != "" {
osType = spotinst.String(v)
}
osDisk.SetType(osType)
}
return osDisk, nil
}
return nil, nil
}