2018-12-20 11:09:44 -05:00
|
|
|
package cvm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
|
|
|
vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
|
|
|
|
)
|
|
|
|
|
|
|
|
type stepConfigVPC struct {
|
|
|
|
VpcId string
|
|
|
|
CidrBlock string
|
|
|
|
VpcName string
|
|
|
|
isCreate bool
|
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepConfigVPC) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2018-12-20 11:09:44 -05:00
|
|
|
vpcClient := state.Get("vpc_client").(*vpc.Client)
|
|
|
|
|
2019-10-21 01:21:21 -04:00
|
|
|
if len(s.VpcId) != 0 {
|
|
|
|
Say(state, s.VpcId, "Trying to use existing vpc")
|
2018-12-20 11:09:44 -05:00
|
|
|
req := vpc.NewDescribeVpcsRequest()
|
|
|
|
req.VpcIds = []*string{&s.VpcId}
|
2019-10-21 01:21:21 -04:00
|
|
|
var resp *vpc.DescribeVpcsResponse
|
|
|
|
err := Retry(ctx, func(ctx context.Context) error {
|
|
|
|
var e error
|
|
|
|
resp, e = vpcClient.DescribeVpcs(req)
|
|
|
|
return e
|
|
|
|
})
|
2018-12-20 11:09:44 -05:00
|
|
|
if err != nil {
|
2019-10-21 01:21:21 -04:00
|
|
|
return Halt(state, err, "Failed to get vpc info")
|
2018-12-20 11:09:44 -05:00
|
|
|
}
|
|
|
|
if *resp.Response.TotalCount > 0 {
|
|
|
|
s.isCreate = false
|
2019-10-21 01:21:21 -04:00
|
|
|
state.Put("vpc_id", *resp.Response.VpcSet[0].VpcId)
|
|
|
|
Message(state, *resp.Response.VpcSet[0].VpcName, "Vpc found")
|
2018-12-20 11:09:44 -05:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
2019-10-21 01:21:21 -04:00
|
|
|
return Halt(state, fmt.Errorf("The specified vpc(%s) does not exist", s.VpcId), "")
|
|
|
|
}
|
|
|
|
|
|
|
|
Say(state, "Trying to create a new vpc", "")
|
|
|
|
|
|
|
|
req := vpc.NewCreateVpcRequest()
|
|
|
|
req.VpcName = &s.VpcName
|
|
|
|
req.CidrBlock = &s.CidrBlock
|
|
|
|
var resp *vpc.CreateVpcResponse
|
|
|
|
err := Retry(ctx, func(ctx context.Context) error {
|
|
|
|
var e error
|
|
|
|
resp, e = vpcClient.CreateVpc(req)
|
|
|
|
return e
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return Halt(state, err, "Failed to create vpc")
|
2018-12-20 11:09:44 -05:00
|
|
|
}
|
2019-10-21 01:21:21 -04:00
|
|
|
|
|
|
|
s.isCreate = true
|
|
|
|
s.VpcId = *resp.Response.Vpc.VpcId
|
|
|
|
state.Put("vpc_id", s.VpcId)
|
|
|
|
Message(state, s.VpcId, "Vpc created")
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
2018-12-20 11:09:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepConfigVPC) Cleanup(state multistep.StateBag) {
|
|
|
|
if !s.isCreate {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-21 01:21:21 -04:00
|
|
|
ctx := context.TODO()
|
2018-12-20 11:09:44 -05:00
|
|
|
vpcClient := state.Get("vpc_client").(*vpc.Client)
|
|
|
|
|
2019-10-21 01:21:21 -04:00
|
|
|
SayClean(state, "vpc")
|
|
|
|
|
2018-12-20 11:09:44 -05:00
|
|
|
req := vpc.NewDeleteVpcRequest()
|
|
|
|
req.VpcId = &s.VpcId
|
2019-10-21 01:21:21 -04:00
|
|
|
err := Retry(ctx, func(ctx context.Context) error {
|
|
|
|
_, e := vpcClient.DeleteVpc(req)
|
|
|
|
return e
|
2018-12-20 11:09:44 -05:00
|
|
|
})
|
|
|
|
if err != nil {
|
2019-10-21 01:21:21 -04:00
|
|
|
Error(state, err, fmt.Sprintf("Failed to delete vpc(%s), please delete it manually", s.VpcId))
|
2018-12-20 11:09:44 -05:00
|
|
|
}
|
|
|
|
}
|