forked from hashicorp/packer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstep_validate_template.go
269 lines (214 loc) · 6.79 KB
/
step_validate_template.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package ncloud
import (
"bytes"
"context"
"errors"
"fmt"
"strings"
ncloud "github.com/NaverCloudPlatform/ncloud-sdk-go/sdk"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/olekukonko/tablewriter"
)
//StepValidateTemplate : struct for Validation a template
type StepValidateTemplate struct {
Conn *ncloud.Conn
Validate func() error
Say func(message string)
Error func(e error)
Config *Config
zoneNo string
regionNo string
FeeSystemTypeCode string
}
// NewStepValidateTemplate : function for Validation a template
func NewStepValidateTemplate(conn *ncloud.Conn, ui packer.Ui, config *Config) *StepValidateTemplate {
var step = &StepValidateTemplate{
Conn: conn,
Say: func(message string) { ui.Say(message) },
Error: func(e error) { ui.Error(e.Error()) },
Config: config,
}
step.Validate = step.validateTemplate
return step
}
// getZoneNo : get zoneNo
func (s *StepValidateTemplate) getZoneNo() error {
if s.Config.Region == "" {
return nil
}
regionList, err := s.Conn.GetRegionList()
if err != nil {
return err
}
var regionNo string
for _, region := range regionList.RegionList {
if strings.EqualFold(region.RegionName, s.Config.Region) {
regionNo = region.RegionNo
}
}
if regionNo == "" {
return fmt.Errorf("region %s is invalid", s.Config.Region)
}
s.regionNo = regionNo
// Get ZoneNo
ZoneList, err := s.Conn.GetZoneList(regionNo)
if err != nil {
return err
}
if len(ZoneList.Zone) > 0 {
s.zoneNo = ZoneList.Zone[0].ZoneNo
}
return nil
}
func (s *StepValidateTemplate) validateMemberServerImage() error {
var serverImageName = s.Config.ServerImageName
reqParams := new(ncloud.RequestServerImageList)
reqParams.RegionNo = s.regionNo
memberServerImageList, err := s.Conn.GetMemberServerImageList(reqParams)
if err != nil {
return err
}
var isExistMemberServerImageNo = false
for _, image := range memberServerImageList.MemberServerImageList {
// Check duplicate server_image_name
if image.MemberServerImageName == serverImageName {
return fmt.Errorf("server_image_name %s is exists", serverImageName)
}
if image.MemberServerImageNo == s.Config.MemberServerImageNo {
isExistMemberServerImageNo = true
if s.Config.ServerProductCode == "" {
s.Config.ServerProductCode = image.OriginalServerProductCode
s.Say("server_product_code for member server image '" + image.OriginalServerProductCode + "' is configured automatically")
}
s.Config.ServerImageProductCode = image.OriginalServerImageProductCode
}
}
if s.Config.MemberServerImageNo != "" && !isExistMemberServerImageNo {
return fmt.Errorf("member_server_image_no %s does not exist", s.Config.MemberServerImageNo)
}
return nil
}
func (s *StepValidateTemplate) validateServerImageProduct() error {
var serverImageProductCode = s.Config.ServerImageProductCode
if serverImageProductCode == "" {
return nil
}
reqParams := new(ncloud.RequestGetServerImageProductList)
reqParams.RegionNo = s.regionNo
serverImageProductList, err := s.Conn.GetServerImageProductList(reqParams)
if err != nil {
return err
}
var isExistServerImage = false
var buf bytes.Buffer
var productName string
table := tablewriter.NewWriter(&buf)
table.SetHeader([]string{"Name", "Code"})
for _, product := range serverImageProductList.Product {
// Check exist server image product code
if product.ProductCode == serverImageProductCode {
isExistServerImage = true
productName = product.ProductName
break
}
table.Append([]string{product.ProductName, product.ProductCode})
}
if !isExistServerImage {
reqParams.BlockStorageSize = 100
serverImageProductList, err := s.Conn.GetServerImageProductList(reqParams)
if err != nil {
return err
}
for _, product := range serverImageProductList.Product {
// Check exist server image product code
if product.ProductCode == serverImageProductCode {
isExistServerImage = true
productName = product.ProductName
break
}
table.Append([]string{product.ProductName, product.ProductCode})
}
}
if !isExistServerImage {
table.Render()
s.Say(buf.String())
return fmt.Errorf("server_image_product_code %s does not exist", serverImageProductCode)
}
if strings.Contains(productName, "mssql") {
s.FeeSystemTypeCode = "FXSUM"
}
return nil
}
func (s *StepValidateTemplate) validateServerProductCode() error {
var serverImageProductCode = s.Config.ServerImageProductCode
var productCode = s.Config.ServerProductCode
reqParams := new(ncloud.RequestGetServerProductList)
reqParams.ServerImageProductCode = serverImageProductCode
reqParams.RegionNo = s.regionNo
productList, err := s.Conn.GetServerProductList(reqParams)
if err != nil {
return err
}
var isExistProductCode = false
for _, product := range productList.Product {
// Check exist server image product code
if product.ProductCode == productCode {
isExistProductCode = true
if strings.Contains(product.ProductName, "mssql") {
s.FeeSystemTypeCode = "FXSUM"
}
if product.ProductType.Code == "VDS" {
return errors.New("You cannot create my server image for VDS servers")
}
break
} else if productCode == "" && product.ProductType.Code == "STAND" {
isExistProductCode = true
s.Config.ServerProductCode = product.ProductCode
s.Say("server_product_code '" + product.ProductCode + "' is configured automatically")
break
}
}
if !isExistProductCode {
var buf bytes.Buffer
table := tablewriter.NewWriter(&buf)
table.SetHeader([]string{"Name", "Code"})
for _, product := range productList.Product {
table.Append([]string{product.ProductName, product.ProductCode})
}
table.Render()
s.Say(buf.String())
return fmt.Errorf("server_product_code %s does not exist", productCode)
}
return nil
}
// Check ImageName / Product Code / Server Image Product Code / Server Product Code...
func (s *StepValidateTemplate) validateTemplate() error {
// Get RegionNo, ZoneNo
if err := s.getZoneNo(); err != nil {
return err
}
// Validate member_server_image_no and member_server_image_no
if err := s.validateMemberServerImage(); err != nil {
return err
}
// Validate server_image_product_code
if err := s.validateServerImageProduct(); err != nil {
return err
}
// Validate server_product_code
return s.validateServerProductCode()
}
// Run : main function for validation a template
func (s *StepValidateTemplate) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
s.Say("Validating deployment template ...")
err := s.Validate()
state.Put("ZoneNo", s.zoneNo)
if s.FeeSystemTypeCode != "" {
state.Put("FeeSystemTypeCode", s.FeeSystemTypeCode)
}
return processStepResult(err, s.Error, state)
}
// Cleanup : cleanup on error
func (s *StepValidateTemplate) Cleanup(multistep.StateBag) {
}