-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathstep_verify_input.go
286 lines (267 loc) · 10.3 KB
/
step_verify_input.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package vpc
import (
"context"
"fmt"
"strings"
"github.com/IBM/go-sdk-core/v5/core"
searchv2 "github.com/IBM/platform-services-go-sdk/globalsearchv2"
"github.com/IBM/platform-services-go-sdk/resourcemanagerv2"
"github.com/IBM/vpc-go-sdk/vpcv1"
"github.com/hashicorp/packer-plugin-sdk/multistep"
"github.com/hashicorp/packer-plugin-sdk/packer"
)
type stepVerifyInput struct{}
func (s *stepVerifyInput) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
client := state.Get("client").(*IBMCloudClient)
ui := state.Get("ui").(packer.Ui)
config := state.Get("config").(Config)
// vpc service
var vpcService *vpcv1.VpcV1
if state.Get("vpcService") != nil {
vpcService = state.Get("vpcService").(*vpcv1.VpcV1)
}
// region check
getRegionOptions := &vpcv1.GetRegionOptions{
Name: &config.Region,
}
_, _, err := vpcService.GetRegion(getRegionOptions)
if err != nil {
err := fmt.Errorf("[ERROR] Error fetching region : %s: %s", config.Region, err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
// region check ends
// resource group check
if config.ResourceGroupID != "" && config.ResourceGroupName != "" {
err := fmt.Errorf("[ERROR] Either one of resource_group_name or resource_group_id can be given, both together are not supported")
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
} else if config.ResourceGroupID != "" || config.ResourceGroupName != "" {
rcUrl := config.RCEndpoint
serviceClientOptions := &resourcemanagerv2.ResourceManagerV2Options{
Authenticator: &core.IamAuthenticator{
ApiKey: client.IBMApiKey,
URL: config.IAMEndpoint,
},
URL: rcUrl,
}
serviceClient, err := resourcemanagerv2.NewResourceManagerV2(serviceClientOptions)
if err != nil {
err := fmt.Errorf("[ERROR] Error creating instance of ResourceManagerV2 for resource group: %s: %s", config.ResourceGroupID, err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
if config.ResourceGroupName != "" {
reGrpName := resourcemanagerv2.ListResourceGroupsOptions{
Name: &config.ResourceGroupName,
}
ResourceGroupName, _, errResNam := serviceClient.ListResourceGroups(&reGrpName)
if errResNam != nil {
err := fmt.Errorf("[ERROR] Error fetching resource group : %s: %s", config.ResourceGroupName, err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
if len(ResourceGroupName.Resources) == 1 {
state.Put("derived_resource_group_id", *ResourceGroupName.Resources[0].ID)
} else if len(ResourceGroupName.Resources) > 1 {
id := *ResourceGroupName.Resources[0].ID
state.Put("derived_resource_group_id", *ResourceGroupName.Resources[0].ID)
ui.Say(fmt.Sprintf("[ERROR] Multiple resource group with the provided names found, using resource group with id: %s", id))
} else {
err := fmt.Errorf("[ERROR] Error fetching resource group, no resource group found with name : %s", config.ResourceGroupName)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
} else {
result, _, err := serviceClient.GetResourceGroup(serviceClient.NewGetResourceGroupOptions(config.ResourceGroupID))
if err != nil {
err := fmt.Errorf("[ERROR] Error fetching resource group : %s: %s", config.ResourceGroupID, err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
} else if result == nil {
err := fmt.Errorf("[ERROR] Resource group not found resource_group_id : %s: %s", config.ResourceGroupID, err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
}
}
// boot volume id validation
if config.VSIBootVolumeID != "" {
getVolumeOptions := &vpcv1.GetVolumeOptions{
ID: &config.VSIBootVolumeID,
}
bootVolume, response, err := vpcService.GetVolume(getVolumeOptions)
if err != nil {
if response != nil && response.StatusCode == 404 {
err := fmt.Errorf("[ERROR] Boot volume provided is not found : %s", config.VSIBootVolumeID)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
err := fmt.Errorf("[ERROR] Error fetching volume %s", config.VSIBootVolumeID)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
if bootVolume.OperatingSystem == nil || bootVolume.OperatingSystem.Architecture == nil {
err := fmt.Errorf("[ERROR] Provided volume %s is not a bootable volume. Please provide an unattached bootable volume", config.VSIBootVolumeID)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
if bootVolume.AttachmentState != nil && *bootVolume.AttachmentState != "unattached" {
err := fmt.Errorf("[ERROR] Provided volume %s is either already attached or unusuble. Please provide an unattached bootable volume", config.VSIBootVolumeID)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
}
//boot snapshot support
if config.VSIBootSnapshotID != "" {
getSnapshotOptions := &vpcv1.GetSnapshotOptions{
ID: &config.VSIBootSnapshotID,
}
bootSnapshot, response, err := vpcService.GetSnapshot(getSnapshotOptions)
if err != nil {
if response != nil && response.StatusCode == 404 {
err := fmt.Errorf("[ERROR] Boot snapahot provided is not found %s:", config.VSIBootSnapshotID)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
err := fmt.Errorf("[ERROR] Error fetching snapshot %s", config.VSIBootSnapshotID)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
if bootSnapshot.OperatingSystem == nil || bootSnapshot.OperatingSystem.Architecture == nil {
err := fmt.Errorf("[ERROR] Provided snapshot %s is not a bootable snapshot. Please provide an unattached bootable snapshot", config.VSIBootSnapshotID)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
}
// image check
listImagesOptions := &vpcv1.ListImagesOptions{
Name: &config.ImageName,
}
// if visibility != "" {
// listImagesOptions.Visibility = &visibility
// }
availableImages, _, err := vpcService.ListImages(listImagesOptions)
if err != nil {
err := fmt.Errorf("[ERROR] Error fetching custom image %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
allrecs := availableImages.Images
if len(allrecs) != 0 {
err := fmt.Errorf("[ERROR] An Image exist with the same name :%s", config.ImageName)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
// image check ends
// usertags validation for blanks.
if len(config.ImageTags) > 0 {
for i := 0; i < len(config.ImageTags); i++ {
if config.ImageTags[i] == "" {
err := fmt.Errorf("[ERROR] Invalid user tag \"\", tags can be in `key:value` or `label` format, for example:, tags:\"my_tag\" ")
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
}
}
// security group verification
if config.SecurityGroupID != "" {
secgrpOption := &vpcv1.GetSecurityGroupOptions{
ID: &config.SecurityGroupID,
}
secGrp, _, err := vpcService.GetSecurityGroup(secgrpOption)
if err != nil {
err := fmt.Errorf("[ERROR] Error fetching security group %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
if *secGrp.ID != "" {
state.Put("user_sec_grp_vpc", *secGrp.VPC.ID) // check for vpc is done as part of subnet fetch.
}
}
// crn validation
if config.CatalogOfferingCRN != "" || config.CatalogOfferingVersionCRN != "" || config.EncryptionKeyCRN != "" {
// validate crn
searchURL := "https://api.global-search-tagging.cloud.ibm.com"
globalSearchV2Options := &searchv2.GlobalSearchV2Options{
URL: searchURL,
Authenticator: vpcService.Service.Options.Authenticator,
}
globalSearchAPIV2, err := searchv2.NewGlobalSearchV2(globalSearchV2Options)
if err != nil {
fmt.Println("GlobalSearch Service creation failed.", err)
}
// validate catalog offering crn
if config.CatalogOfferingCRN != "" {
crnToCheck := fmt.Sprintf("%s%s", strings.Split(config.CatalogOfferingCRN, ":offering")[0], "::")
query := fmt.Sprintf("crn:\"%s\"", crnToCheck)
searchOptions := &searchv2.SearchOptions{
Query: &query,
}
res, _, _ := globalSearchAPIV2.Search(searchOptions)
if len(res.Items) != 0 {
ui.Say(fmt.Sprintf("%s Catalog information successfully retrieved ...", res.Items[0].GetProperty("name")))
} else {
err := fmt.Errorf("[ERROR] Catalog crn (%s) information could not be retrieved", config.CatalogOfferingCRN)
state.Put("Catalog offering crn information could not be retrieved", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
}
// validate catalog version crn
if config.CatalogOfferingVersionCRN != "" {
crnToCheck := fmt.Sprintf("%s%s", strings.Split(config.CatalogOfferingVersionCRN, ":version")[0], "::")
query := fmt.Sprintf("crn:\"%s\"", crnToCheck)
searchOptions := &searchv2.SearchOptions{
Query: &query,
}
res, _, _ := globalSearchAPIV2.Search(searchOptions)
if len(res.Items) != 0 {
ui.Say(fmt.Sprintf("%s Catalog information successfully retrieved ...", res.Items[0].GetProperty("name")))
} else {
err := fmt.Errorf("[ERROR] Catalog version crn (%s) information could not be retrieved", config.CatalogOfferingVersionCRN)
state.Put("Catalog version crn information could not be retrieved", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
}
// validate encryption key crn
if config.EncryptionKeyCRN != "" {
crnToCheck := fmt.Sprintf("%s%s", strings.Split(config.EncryptionKeyCRN, ":key")[0], "::")
query := fmt.Sprintf("crn:\"%s\"", crnToCheck)
searchOptions := &searchv2.SearchOptions{
Query: &query,
}
res, _, _ := globalSearchAPIV2.Search(searchOptions)
if len(res.Items) != 0 {
ui.Say(fmt.Sprintf("%s Encryption information successfully retrieved ...", res.Items[0].GetProperty("name")))
} else {
err := fmt.Errorf("[ERROR] Encryption crn (%s) information could not be retrieved", config.EncryptionKeyCRN)
state.Put("Encryption information could not be retrieved", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
}
}
return multistep.ActionContinue
}
func (s *stepVerifyInput) Cleanup(state multistep.StateBag) {
}