2017-03-03 03:56:17 -05:00
|
|
|
package ecs
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2019-04-25 22:37:49 -04:00
|
|
|
errorsNew "errors"
|
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
|
|
|
)
|
|
|
|
|
|
|
|
type stepConfigAlicloudVPC struct {
|
|
|
|
VpcId string
|
|
|
|
CidrBlock string //192.168.0.0/16 or 172.16.0.0/16 (default)
|
|
|
|
VpcName string
|
|
|
|
isCreate bool
|
|
|
|
}
|
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
var createVpcRetryErrors = []string{
|
|
|
|
"TOKEN_PROCESSING",
|
|
|
|
}
|
|
|
|
|
|
|
|
var deleteVpcRetryErrors = []string{
|
|
|
|
"DependencyViolation.Instance",
|
|
|
|
"DependencyViolation.RouteEntry",
|
|
|
|
"DependencyViolation.VSwitch",
|
|
|
|
"DependencyViolation.SecurityGroup",
|
|
|
|
"Forbbiden",
|
|
|
|
"TaskConflict",
|
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepConfigAlicloudVPC) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2018-09-18 09:40:57 -04:00
|
|
|
config := state.Get("config").(*Config)
|
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)
|
|
|
|
|
|
|
|
if len(s.VpcId) != 0 {
|
2019-04-25 22:37:49 -04:00
|
|
|
describeVpcsRequest := ecs.CreateDescribeVpcsRequest()
|
|
|
|
describeVpcsRequest.VpcId = s.VpcId
|
|
|
|
describeVpcsRequest.RegionId = config.AlicloudRegion
|
|
|
|
|
|
|
|
vpcsResponse, err := client.DescribeVpcs(describeVpcsRequest)
|
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 vpcs")
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
2019-04-25 22:37:49 -04:00
|
|
|
|
|
|
|
vpcs := vpcsResponse.Vpcs.Vpc
|
2017-03-03 03:56:17 -05:00
|
|
|
if len(vpcs) > 0 {
|
2019-04-25 22:37:49 -04:00
|
|
|
state.Put("vpcid", vpcs[0].VpcId)
|
2017-03-03 03:56:17 -05:00
|
|
|
s.isCreate = false
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
message := fmt.Sprintf("The specified vpc {%s} doesn't exist.", s.VpcId)
|
|
|
|
return halt(state, errorsNew.New(message), "")
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
2019-04-25 22:37:49 -04:00
|
|
|
|
|
|
|
ui.Say("Creating vpc...")
|
|
|
|
|
|
|
|
createVpcRequest := s.buildCreateVpcRequest(state)
|
|
|
|
createVpcResponse, err := client.WaitForExpected(&WaitForExpectArgs{
|
|
|
|
RequestFunc: func() (responses.AcsResponse, error) {
|
|
|
|
return client.CreateVpc(createVpcRequest)
|
|
|
|
},
|
|
|
|
EvalFunc: client.EvalCouldRetryResponse(createVpcRetryErrors, EvalRetryErrorType),
|
2017-03-03 03:56:17 -05:00
|
|
|
})
|
|
|
|
if err != nil {
|
2019-04-25 22:37:49 -04:00
|
|
|
return halt(state, err, "Failed creating vpc")
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
2019-04-25 22:37:49 -04:00
|
|
|
|
|
|
|
vpcId := createVpcResponse.(*ecs.CreateVpcResponse).VpcId
|
|
|
|
_, err = client.WaitForExpected(&WaitForExpectArgs{
|
|
|
|
RequestFunc: func() (responses.AcsResponse, error) {
|
|
|
|
request := ecs.CreateDescribeVpcsRequest()
|
|
|
|
request.RegionId = config.AlicloudRegion
|
|
|
|
request.VpcId = vpcId
|
|
|
|
return client.DescribeVpcs(request)
|
|
|
|
},
|
|
|
|
EvalFunc: func(response responses.AcsResponse, err error) WaitForExpectEvalResult {
|
|
|
|
if err != nil {
|
|
|
|
return WaitForExpectToRetry
|
|
|
|
}
|
|
|
|
|
|
|
|
vpcsResponse := response.(*ecs.DescribeVpcsResponse)
|
|
|
|
vpcs := vpcsResponse.Vpcs.Vpc
|
|
|
|
if len(vpcs) > 0 {
|
|
|
|
for _, vpc := range vpcs {
|
|
|
|
if vpc.Status == VpcStatusAvailable {
|
|
|
|
return WaitForExpectSuccess
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return WaitForExpectToRetry
|
|
|
|
},
|
|
|
|
RetryTimes: shortRetryTimes,
|
|
|
|
})
|
|
|
|
|
2017-03-03 03:56:17 -05:00
|
|
|
if err != nil {
|
2019-04-25 22:37:49 -04:00
|
|
|
return halt(state, err, "Failed waiting for vpc to become available")
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
ui.Message(fmt.Sprintf("Created vpc: %s", vpcId))
|
|
|
|
state.Put("vpcid", vpcId)
|
2017-03-03 03:56:17 -05:00
|
|
|
s.isCreate = true
|
2019-04-25 22:37:49 -04:00
|
|
|
s.VpcId = vpcId
|
2017-03-03 03:56:17 -05:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepConfigAlicloudVPC) Cleanup(state multistep.StateBag) {
|
|
|
|
if !s.isCreate {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
cleanUpMessage(state, "VPC")
|
|
|
|
|
|
|
|
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.CreateDeleteVpcRequest()
|
|
|
|
request.VpcId = s.VpcId
|
|
|
|
return client.DeleteVpc(request)
|
|
|
|
},
|
|
|
|
EvalFunc: client.EvalCouldRetryResponse(deleteVpcRetryErrors, EvalRetryErrorType),
|
|
|
|
RetryTimes: shortRetryTimes,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error deleting vpc, 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 *stepConfigAlicloudVPC) buildCreateVpcRequest(state multistep.StateBag) *ecs.CreateVpcRequest {
|
|
|
|
config := state.Get("config").(*Config)
|
|
|
|
|
|
|
|
request := ecs.CreateCreateVpcRequest()
|
|
|
|
request.ClientToken = uuid.TimeOrderedUUID()
|
|
|
|
request.RegionId = config.AlicloudRegion
|
|
|
|
request.CidrBlock = s.CidrBlock
|
|
|
|
request.VpcName = s.VpcName
|
|
|
|
|
|
|
|
return request
|
|
|
|
}
|