2018-12-20 11:09:44 -05:00
|
|
|
package cvm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
2018-12-20 11:09:44 -05:00
|
|
|
vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
|
|
|
|
)
|
|
|
|
|
|
|
|
type stepConfigSubnet struct {
|
|
|
|
SubnetId string
|
|
|
|
SubnetCidrBlock string
|
|
|
|
SubnetName string
|
|
|
|
Zone string
|
|
|
|
isCreate bool
|
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepConfigSubnet) 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
|
|
|
|
2018-12-20 11:09:44 -05:00
|
|
|
vpcId := state.Get("vpc_id").(string)
|
|
|
|
|
2019-10-21 01:21:21 -04:00
|
|
|
if len(s.SubnetId) != 0 {
|
|
|
|
Say(state, s.SubnetId, "Trying to use existing subnet")
|
2018-12-20 11:09:44 -05:00
|
|
|
req := vpc.NewDescribeSubnetsRequest()
|
|
|
|
req.SubnetIds = []*string{&s.SubnetId}
|
2019-10-21 01:21:21 -04:00
|
|
|
var resp *vpc.DescribeSubnetsResponse
|
|
|
|
err := Retry(ctx, func(ctx context.Context) error {
|
|
|
|
var e error
|
|
|
|
resp, e = vpcClient.DescribeSubnets(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 subnet 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
|
|
|
if *resp.Response.SubnetSet[0].VpcId != vpcId {
|
|
|
|
return Halt(state, fmt.Errorf("The specified subnet(%s) does not belong to the specified vpc(%s)", s.SubnetId, vpcId), "")
|
|
|
|
}
|
|
|
|
state.Put("subnet_id", *resp.Response.SubnetSet[0].SubnetId)
|
|
|
|
Message(state, *resp.Response.SubnetSet[0].SubnetName, "Subnet 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 subnet(%s) does not exist", s.SubnetId), "")
|
|
|
|
}
|
|
|
|
|
|
|
|
Say(state, "Trying to create a new subnet", "")
|
|
|
|
|
|
|
|
req := vpc.NewCreateSubnetRequest()
|
|
|
|
req.VpcId = &vpcId
|
|
|
|
req.SubnetName = &s.SubnetName
|
|
|
|
req.CidrBlock = &s.SubnetCidrBlock
|
|
|
|
req.Zone = &s.Zone
|
|
|
|
var resp *vpc.CreateSubnetResponse
|
|
|
|
err := Retry(ctx, func(ctx context.Context) error {
|
|
|
|
var e error
|
|
|
|
resp, e = vpcClient.CreateSubnet(req)
|
|
|
|
return e
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return Halt(state, err, "Failed to create subnet")
|
2018-12-20 11:09:44 -05:00
|
|
|
}
|
2019-10-21 01:21:21 -04:00
|
|
|
|
|
|
|
s.isCreate = true
|
|
|
|
s.SubnetId = *resp.Response.Subnet.SubnetId
|
|
|
|
state.Put("subnet_id", s.SubnetId)
|
|
|
|
Message(state, s.SubnetId, "Subnet created")
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
2018-12-20 11:09:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepConfigSubnet) 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, "subnet")
|
|
|
|
|
2018-12-20 11:09:44 -05:00
|
|
|
req := vpc.NewDeleteSubnetRequest()
|
|
|
|
req.SubnetId = &s.SubnetId
|
2019-10-21 01:21:21 -04:00
|
|
|
err := Retry(ctx, func(ctx context.Context) error {
|
|
|
|
_, e := vpcClient.DeleteSubnet(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 subnet(%s), please delete it manually", s.SubnetId))
|
2018-12-20 11:09:44 -05:00
|
|
|
}
|
|
|
|
}
|