forked from hashicorp/terraform-provider-azurerm
/
batch_pool.go
243 lines (192 loc) · 6.93 KB
/
batch_pool.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package azure
import (
"fmt"
"log"
"regexp"
"github.com/Azure/azure-sdk-for-go/services/batch/mgmt/2017-09-01/batch"
)
// FlattenBatchPoolAutoScaleSettings flattens the auto scale settings for a Batch pool
func FlattenBatchPoolAutoScaleSettings(settings *batch.AutoScaleSettings) []interface{} {
results := make([]interface{}, 0)
if settings == nil {
log.Printf("[DEBUG] settings is nil")
return results
}
result := make(map[string]interface{})
if settings.EvaluationInterval != nil {
result["evaluation_interval"] = *settings.EvaluationInterval
}
if settings.Formula != nil {
result["formula"] = *settings.Formula
}
return append(results, result)
}
// FlattenBatchPoolFixedScaleSettings flattens the fixed scale settings for a Batch pool
func FlattenBatchPoolFixedScaleSettings(settings *batch.FixedScaleSettings) []interface{} {
results := make([]interface{}, 0)
if settings == nil {
log.Printf("[DEBUG] settings is nil")
return results
}
result := make(map[string]interface{})
if settings.TargetDedicatedNodes != nil {
result["target_dedicated_nodes"] = *settings.TargetDedicatedNodes
}
if settings.TargetLowPriorityNodes != nil {
result["target_low_priority_nodes"] = *settings.TargetLowPriorityNodes
}
if settings.ResizeTimeout != nil {
result["resize_timeout"] = *settings.ResizeTimeout
}
return append(results, result)
}
// FlattenBatchPoolImageReference flattens the Batch pool image reference
func FlattenBatchPoolImageReference(image *batch.ImageReference) []interface{} {
results := make([]interface{}, 0)
if image == nil {
log.Printf("[DEBUG] image is nil")
return results
}
result := make(map[string]interface{})
if image.Publisher != nil {
result["publisher"] = *image.Publisher
}
if image.Offer != nil {
result["offer"] = *image.Offer
}
if image.Sku != nil {
result["sku"] = *image.Sku
}
if image.Version != nil {
result["version"] = *image.Version
}
if image.ID != nil {
result["id"] = *image.ID
}
return append(results, result)
}
// FlattenBatchPoolStartTask flattens a Batch pool start task
func FlattenBatchPoolStartTask(startTask *batch.StartTask) []interface{} {
results := make([]interface{}, 0)
if startTask == nil {
log.Printf("[DEBUG] startTask is nil")
return results
}
result := make(map[string]interface{})
if startTask.CommandLine != nil {
result["command_line"] = *startTask.CommandLine
}
if startTask.WaitForSuccess != nil {
result["wait_for_success"] = *startTask.WaitForSuccess
}
if startTask.MaxTaskRetryCount != nil {
result["max_task_retry_count"] = *startTask.MaxTaskRetryCount
}
if startTask.UserIdentity != nil {
userIdentity := make(map[string]interface{})
if startTask.UserIdentity.AutoUser != nil {
autoUser := make(map[string]interface{})
elevationLevel := string(startTask.UserIdentity.AutoUser.ElevationLevel)
scope := string(startTask.UserIdentity.AutoUser.Scope)
autoUser["elevation_level"] = elevationLevel
autoUser["scope"] = scope
userIdentity["auto_user"] = []interface{}{autoUser}
} else {
userIdentity["user_name"] = *startTask.UserIdentity.UserName
}
result["user_identity"] = []interface{}{userIdentity}
}
if startTask.EnvironmentSettings != nil {
environment := make(map[string]interface{})
for _, envSetting := range *startTask.EnvironmentSettings {
environment[*envSetting.Name] = *envSetting.Value
}
result["environment"] = environment
}
return append(results, result)
}
// ExpandBatchPoolImageReference expands Batch pool image reference
func ExpandBatchPoolImageReference(list []interface{}) (*batch.ImageReference, error) {
if len(list) == 0 {
return nil, fmt.Errorf("Error: storage image reference should be defined")
}
storageImageRef := list[0].(map[string]interface{})
storageImageRefOffer := storageImageRef["offer"].(string)
storageImageRefPublisher := storageImageRef["publisher"].(string)
storageImageRefSku := storageImageRef["sku"].(string)
storageImageRefVersion := storageImageRef["version"].(string)
imageRef := &batch.ImageReference{
Offer: &storageImageRefOffer,
Publisher: &storageImageRefPublisher,
Sku: &storageImageRefSku,
Version: &storageImageRefVersion,
}
return imageRef, nil
}
// ExpandBatchPoolStartTask expands Batch pool start task
func ExpandBatchPoolStartTask(list []interface{}) (*batch.StartTask, error) {
if len(list) == 0 {
return nil, fmt.Errorf("Error: batch pool start task should be defined")
}
startTaskValue := list[0].(map[string]interface{})
startTaskCmdLine := startTaskValue["command_line"].(string)
maxTaskRetryCount := int32(startTaskValue["max_task_retry_count"].(int))
waitForSuccess := startTaskValue["wait_for_success"].(bool)
userIdentityList := startTaskValue["user_identity"].([]interface{})
if len(userIdentityList) == 0 {
return nil, fmt.Errorf("Error: batch pool start task user identity should be defined")
}
userIdentityValue := userIdentityList[0].(map[string]interface{})
userIdentity := batch.UserIdentity{}
if autoUserValue, ok := userIdentityValue["auto_user"]; ok {
autoUser := autoUserValue.([]interface{})
if len(autoUser) != 0 {
autoUserMap := autoUser[0].(map[string]interface{})
userIdentity.AutoUser = &batch.AutoUserSpecification{
ElevationLevel: batch.ElevationLevel(autoUserMap["elevation_level"].(string)),
Scope: batch.AutoUserScope(autoUserMap["scope"].(string)),
}
}
} else if userNameValue, ok := userIdentityValue["username"]; ok {
userName := userNameValue.(string)
userIdentity.UserName = &userName
} else {
return nil, fmt.Errorf("Error: either auto_user or user_name should be speicfied for Batch pool start task")
}
startTask := &batch.StartTask{
CommandLine: &startTaskCmdLine,
MaxTaskRetryCount: &maxTaskRetryCount,
WaitForSuccess: &waitForSuccess,
UserIdentity: &userIdentity,
}
// populate environment settings, if defined
if environment := startTaskValue["environment"]; environment != nil {
envMap := environment.(map[string]interface{})
envSettings := make([]batch.EnvironmentSetting, 0)
for k, v := range envMap {
envValue := v.(string)
envSetting := batch.EnvironmentSetting{
Name: &k,
Value: &envValue,
}
envSettings = append(envSettings, envSetting)
}
startTask.EnvironmentSettings = &envSettings
}
return startTask, nil
}
// ValidateAzureRMBatchPoolName validates the name of a Batch pool
func ValidateAzureRMBatchPoolName(v interface{}, k string) (warnings []string, errors []error) {
value := v.(string)
if !regexp.MustCompile(`^[a-zA-Z0-9_-]+$`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"any combination of alphanumeric characters including hyphens and underscores are allowed in %q: %q", k, value))
}
if 1 > len(value) {
errors = append(errors, fmt.Errorf("%q cannot be less than 1 character: %q", k, value))
}
if len(value) > 64 {
errors = append(errors, fmt.Errorf("%q cannot be longer than 64 characters: %q %d", k, value, len(value)))
}
return warnings, errors
}