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/sdk/responses"
|
|
|
|
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
|
|
|
|
"github.com/hashicorp/packer/common/uuid"
|
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
|
|
|
)
|
|
|
|
|
2017-03-04 05:06:32 -05:00
|
|
|
type stepConfigAlicloudVSwitch struct {
|
2017-03-03 03:56:17 -05:00
|
|
|
VSwitchId string
|
|
|
|
ZoneId string
|
|
|
|
isCreate bool
|
|
|
|
CidrBlock string
|
|
|
|
VSwitchName string
|
|
|
|
}
|
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
var createVSwitchRetryErrors = []string{
|
|
|
|
"TOKEN_PROCESSING",
|
|
|
|
}
|
|
|
|
|
|
|
|
var deleteVSwitchRetryErrors = []string{
|
|
|
|
"IncorrectVSwitchStatus",
|
|
|
|
"DependencyViolation",
|
|
|
|
"DependencyViolation.HaVip",
|
|
|
|
"IncorrectRouteEntryStatus",
|
|
|
|
"TaskConflict",
|
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepConfigAlicloudVSwitch) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2019-04-25 22:37:49 -04:00
|
|
|
client := state.Get("client").(*ClientWrapper)
|
2017-03-03 03:56:17 -05:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
vpcId := state.Get("vpcid").(string)
|
2018-09-18 09:40:57 -04:00
|
|
|
config := state.Get("config").(*Config)
|
2017-03-03 03:56:17 -05:00
|
|
|
|
|
|
|
if len(s.VSwitchId) != 0 {
|
2019-04-25 22:37:49 -04:00
|
|
|
describeVSwitchesRequest := ecs.CreateDescribeVSwitchesRequest()
|
|
|
|
describeVSwitchesRequest.VpcId = vpcId
|
|
|
|
describeVSwitchesRequest.VSwitchId = s.VSwitchId
|
|
|
|
describeVSwitchesRequest.ZoneId = s.ZoneId
|
|
|
|
|
|
|
|
vswitchesResponse, err := client.DescribeVSwitches(describeVSwitchesRequest)
|
2017-03-03 03:56:17 -05:00
|
|
|
if err != nil {
|
2019-04-25 22:37:49 -04:00
|
|
|
return halt(state, err, "Failed querying vswitch")
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
2019-04-25 22:37:49 -04:00
|
|
|
|
|
|
|
vswitch := vswitchesResponse.VSwitches.VSwitch
|
|
|
|
if len(vswitch) > 0 {
|
|
|
|
state.Put("vswitchid", vswitch[0].VSwitchId)
|
2017-03-03 03:56:17 -05:00
|
|
|
s.isCreate = false
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
s.isCreate = false
|
|
|
|
return halt(state, fmt.Errorf("The specified vswitch {%s} doesn't exist.", s.VSwitchId), "")
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
2019-04-25 22:37:49 -04:00
|
|
|
|
2017-03-03 03:56:17 -05:00
|
|
|
if s.ZoneId == "" {
|
2019-04-25 22:37:49 -04:00
|
|
|
describeZonesRequest := ecs.CreateDescribeZonesRequest()
|
|
|
|
describeZonesRequest.RegionId = config.AlicloudRegion
|
2017-03-03 03:56:17 -05:00
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
zonesResponse, err := client.DescribeZones(describeZonesRequest)
|
2017-03-03 03:56:17 -05:00
|
|
|
if err != nil {
|
2019-04-25 22:37:49 -04:00
|
|
|
return halt(state, err, "Query for available zones failed")
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
2019-04-25 22:37:49 -04:00
|
|
|
|
2017-03-03 03:56:17 -05:00
|
|
|
var instanceTypes []string
|
2019-04-25 22:37:49 -04:00
|
|
|
zones := zonesResponse.Zones.Zone
|
2017-03-03 03:56:17 -05:00
|
|
|
for _, zone := range zones {
|
|
|
|
isVSwitchSupported := false
|
|
|
|
for _, resourceType := range zone.AvailableResourceCreation.ResourceTypes {
|
2019-04-25 22:37:49 -04:00
|
|
|
if resourceType == "VSwitch" {
|
2017-03-03 03:56:17 -05:00
|
|
|
isVSwitchSupported = true
|
|
|
|
}
|
|
|
|
}
|
2019-04-25 22:37:49 -04:00
|
|
|
|
2017-03-03 03:56:17 -05:00
|
|
|
if isVSwitchSupported {
|
|
|
|
for _, instanceType := range zone.AvailableInstanceTypes.InstanceTypes {
|
|
|
|
if instanceType == config.InstanceType {
|
|
|
|
s.ZoneId = zone.ZoneId
|
|
|
|
break
|
|
|
|
}
|
|
|
|
instanceTypes = append(instanceTypes, instanceType)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.ZoneId == "" {
|
|
|
|
if len(instanceTypes) > 0 {
|
2017-03-04 05:06:32 -05:00
|
|
|
ui.Say(fmt.Sprintf("The instance type %s isn't available in this region."+
|
|
|
|
"\n You can either change the instance to one of following: %v \n"+
|
2017-06-01 22:33:12 -04:00
|
|
|
"or choose another region.", config.InstanceType, instanceTypes))
|
2017-03-03 03:56:17 -05:00
|
|
|
|
2017-03-04 05:06:32 -05:00
|
|
|
state.Put("error", fmt.Errorf("The instance type %s isn't available in this region."+
|
|
|
|
"\n You can either change the instance to one of following: %v \n"+
|
2017-06-01 22:33:12 -04:00
|
|
|
"or choose another region.", config.InstanceType, instanceTypes))
|
2017-03-03 03:56:17 -05:00
|
|
|
return multistep.ActionHalt
|
|
|
|
} else {
|
2017-03-04 05:06:32 -05:00
|
|
|
ui.Say(fmt.Sprintf("The instance type %s isn't available in this region."+
|
2017-06-01 22:33:12 -04:00
|
|
|
"\n You can change to other regions.", config.InstanceType))
|
2017-03-03 03:56:17 -05:00
|
|
|
|
2017-03-04 05:06:32 -05:00
|
|
|
state.Put("error", fmt.Errorf("The instance type %s isn't available in this region."+
|
2017-06-01 22:33:12 -04:00
|
|
|
"\n You can change to other regions.", config.InstanceType))
|
2017-03-03 03:56:17 -05:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-25 22:37:49 -04:00
|
|
|
|
2017-03-03 03:56:17 -05:00
|
|
|
if config.CidrBlock == "" {
|
2019-04-25 22:37:49 -04:00
|
|
|
s.CidrBlock = DefaultCidrBlock //use the default CirdBlock
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
2019-04-25 22:37:49 -04:00
|
|
|
|
2017-06-01 22:33:12 -04:00
|
|
|
ui.Say("Creating vswitch...")
|
2019-04-25 22:37:49 -04:00
|
|
|
|
|
|
|
createVSwitchRequest := s.buildCreateVSwitchRequest(state)
|
|
|
|
createVSwitchResponse, err := client.WaitForExpected(&WaitForExpectArgs{
|
|
|
|
RequestFunc: func() (responses.AcsResponse, error) {
|
|
|
|
return client.CreateVSwitch(createVSwitchRequest)
|
|
|
|
},
|
|
|
|
EvalFunc: client.EvalCouldRetryResponse(createVSwitchRetryErrors, EvalRetryErrorType),
|
2017-03-03 03:56:17 -05:00
|
|
|
})
|
|
|
|
if err != nil {
|
2019-04-25 22:37:49 -04:00
|
|
|
return halt(state, err, "Error Creating vswitch")
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
2019-04-25 22:37:49 -04:00
|
|
|
|
|
|
|
vSwitchId := createVSwitchResponse.(*ecs.CreateVSwitchResponse).VSwitchId
|
|
|
|
|
|
|
|
describeVSwitchesRequest := ecs.CreateDescribeVSwitchesRequest()
|
|
|
|
describeVSwitchesRequest.VpcId = vpcId
|
|
|
|
describeVSwitchesRequest.VSwitchId = vSwitchId
|
|
|
|
|
|
|
|
_, err = client.WaitForExpected(&WaitForExpectArgs{
|
|
|
|
RequestFunc: func() (responses.AcsResponse, error) {
|
|
|
|
return client.DescribeVSwitches(describeVSwitchesRequest)
|
|
|
|
},
|
|
|
|
EvalFunc: func(response responses.AcsResponse, err error) WaitForExpectEvalResult {
|
|
|
|
if err != nil {
|
|
|
|
return WaitForExpectToRetry
|
|
|
|
}
|
|
|
|
|
|
|
|
vSwitchesResponse := response.(*ecs.DescribeVSwitchesResponse)
|
|
|
|
vSwitches := vSwitchesResponse.VSwitches.VSwitch
|
|
|
|
if len(vSwitches) > 0 {
|
|
|
|
for _, vSwitch := range vSwitches {
|
|
|
|
if vSwitch.Status == VSwitchStatusAvailable {
|
|
|
|
return WaitForExpectSuccess
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return WaitForExpectToRetry
|
|
|
|
},
|
|
|
|
RetryTimes: shortRetryTimes,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return halt(state, err, "Timeout waiting for vswitch to become available")
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
2019-04-25 22:37:49 -04:00
|
|
|
|
|
|
|
ui.Message(fmt.Sprintf("Created vswitch: %s", vSwitchId))
|
|
|
|
state.Put("vswitchid", vSwitchId)
|
2017-03-03 03:56:17 -05:00
|
|
|
s.isCreate = true
|
2019-04-25 22:37:49 -04:00
|
|
|
s.VSwitchId = vSwitchId
|
2017-03-03 03:56:17 -05:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2017-03-04 05:06:32 -05:00
|
|
|
func (s *stepConfigAlicloudVSwitch) Cleanup(state multistep.StateBag) {
|
2017-03-03 03:56:17 -05:00
|
|
|
if !s.isCreate {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
cleanUpMessage(state, "vSwitch")
|
|
|
|
|
|
|
|
client := state.Get("client").(*ClientWrapper)
|
2017-03-03 03:56:17 -05:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2019-04-25 22:37:49 -04:00
|
|
|
|
|
|
|
_, err := client.WaitForExpected(&WaitForExpectArgs{
|
|
|
|
RequestFunc: func() (responses.AcsResponse, error) {
|
|
|
|
request := ecs.CreateDeleteVSwitchRequest()
|
|
|
|
request.VSwitchId = s.VSwitchId
|
|
|
|
return client.DeleteVSwitch(request)
|
|
|
|
},
|
|
|
|
EvalFunc: client.EvalCouldRetryResponse(deleteVSwitchRetryErrors, EvalRetryErrorType),
|
|
|
|
RetryTimes: shortRetryTimes,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error deleting vswitch, it may still be around: %s", err))
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
}
|
2019-04-25 22:37:49 -04:00
|
|
|
|
|
|
|
func (s *stepConfigAlicloudVSwitch) buildCreateVSwitchRequest(state multistep.StateBag) *ecs.CreateVSwitchRequest {
|
|
|
|
vpcId := state.Get("vpcid").(string)
|
|
|
|
|
|
|
|
request := ecs.CreateCreateVSwitchRequest()
|
|
|
|
request.ClientToken = uuid.TimeOrderedUUID()
|
|
|
|
request.CidrBlock = s.CidrBlock
|
|
|
|
request.ZoneId = s.ZoneId
|
|
|
|
request.VpcId = vpcId
|
|
|
|
request.VSwitchName = s.VSwitchName
|
|
|
|
|
|
|
|
return request
|
|
|
|
}
|