2017-03-03 03:56:17 -05:00
|
|
|
package ecs
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2017-03-03 03:56:17 -05:00
|
|
|
"fmt"
|
2019-04-26 03:12:07 -04:00
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-17 09:04:52 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2017-03-03 03:56:17 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type stepCheckAlicloudSourceImage struct {
|
|
|
|
SourceECSImageId string
|
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepCheckAlicloudSourceImage) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2019-04-25 22:37:49 -04:00
|
|
|
client := state.Get("client").(*ClientWrapper)
|
2018-09-18 09:40:57 -04:00
|
|
|
config := state.Get("config").(*Config)
|
2017-03-03 03:56:17 -05:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2019-04-25 22:37:49 -04:00
|
|
|
|
|
|
|
describeImagesRequest := ecs.CreateDescribeImagesRequest()
|
|
|
|
describeImagesRequest.RegionId = config.AlicloudRegion
|
|
|
|
describeImagesRequest.ImageId = config.AlicloudSourceImage
|
|
|
|
imagesResponse, err := client.DescribeImages(describeImagesRequest)
|
2017-03-03 03:56:17 -05:00
|
|
|
if err != nil {
|
2019-04-25 22:37:49 -04:00
|
|
|
return halt(state, err, "Error querying alicloud image")
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
images := imagesResponse.Images.Image
|
|
|
|
|
2018-08-14 12:14:02 -04:00
|
|
|
// Describe markerplace image
|
2019-04-25 22:37:49 -04:00
|
|
|
describeImagesRequest.ImageOwnerAlias = "marketplace"
|
|
|
|
marketImagesResponse, err := client.DescribeImages(describeImagesRequest)
|
2018-08-14 12:14:02 -04:00
|
|
|
if err != nil {
|
2019-04-25 22:37:49 -04:00
|
|
|
return halt(state, err, "Error querying alicloud marketplace image")
|
2018-08-14 12:14:02 -04:00
|
|
|
}
|
2019-04-25 22:37:49 -04:00
|
|
|
|
|
|
|
marketImages := marketImagesResponse.Images.Image
|
|
|
|
if len(marketImages) > 0 {
|
|
|
|
images = append(images, marketImages...)
|
2018-08-14 12:14:02 -04:00
|
|
|
}
|
|
|
|
|
2017-03-03 03:56:17 -05:00
|
|
|
if len(images) == 0 {
|
|
|
|
err := fmt.Errorf("No alicloud image was found matching filters: %v", config.AlicloudSourceImage)
|
2019-04-25 22:37:49 -04:00
|
|
|
return halt(state, err, "")
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
ui.Message(fmt.Sprintf("Found image ID: %s", images[0].ImageId))
|
|
|
|
|
|
|
|
state.Put("source_image", &images[0])
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepCheckAlicloudSourceImage) Cleanup(multistep.StateBag) {}
|