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"
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
2020-11-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2020-11-12 17:44:02 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/uuid"
|
2017-03-03 03:56:17 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type stepConfigAlicloudSecurityGroup struct {
|
|
|
|
SecurityGroupId string
|
|
|
|
SecurityGroupName string
|
|
|
|
Description string
|
|
|
|
VpcId string
|
|
|
|
RegionId string
|
|
|
|
isCreate bool
|
|
|
|
}
|
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
var createSecurityGroupRetryErrors = []string{
|
|
|
|
"IdempotentProcessing",
|
|
|
|
}
|
|
|
|
|
|
|
|
var deleteSecurityGroupRetryErrors = []string{
|
|
|
|
"DependencyViolation",
|
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepConfigAlicloudSecurityGroup) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2019-04-25 22:37:49 -04:00
|
|
|
client := state.Get("client").(*ClientWrapper)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2017-03-03 03:56:17 -05:00
|
|
|
networkType := state.Get("networktype").(InstanceNetWork)
|
|
|
|
|
|
|
|
if len(s.SecurityGroupId) != 0 {
|
2019-04-25 22:37:49 -04:00
|
|
|
describeSecurityGroupsRequest := ecs.CreateDescribeSecurityGroupsRequest()
|
|
|
|
describeSecurityGroupsRequest.RegionId = s.RegionId
|
2019-12-23 03:50:07 -05:00
|
|
|
describeSecurityGroupsRequest.SecurityGroupId = s.SecurityGroupId
|
2019-04-25 22:37:49 -04:00
|
|
|
if networkType == InstanceNetworkVpc {
|
2017-03-03 03:56:17 -05:00
|
|
|
vpcId := state.Get("vpcid").(string)
|
2019-04-25 22:37:49 -04:00
|
|
|
describeSecurityGroupsRequest.VpcId = vpcId
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
securityGroupsResponse, err := client.DescribeSecurityGroups(describeSecurityGroupsRequest)
|
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 security group")
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
2019-04-25 22:37:49 -04:00
|
|
|
|
|
|
|
securityGroupItems := securityGroupsResponse.SecurityGroups.SecurityGroup
|
2017-03-03 03:56:17 -05:00
|
|
|
for _, securityGroupItem := range securityGroupItems {
|
|
|
|
if securityGroupItem.SecurityGroupId == s.SecurityGroupId {
|
|
|
|
state.Put("securitygroupid", s.SecurityGroupId)
|
|
|
|
s.isCreate = false
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
s.isCreate = false
|
|
|
|
err = fmt.Errorf("The specified security group {%s} doesn't exist.", s.SecurityGroupId)
|
|
|
|
return halt(state, err, "")
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
2019-04-25 22:37:49 -04:00
|
|
|
|
|
|
|
ui.Say("Creating security group...")
|
|
|
|
|
|
|
|
createSecurityGroupRequest := s.buildCreateSecurityGroupRequest(state)
|
|
|
|
securityGroupResponse, err := client.WaitForExpected(&WaitForExpectArgs{
|
|
|
|
RequestFunc: func() (responses.AcsResponse, error) {
|
|
|
|
return client.CreateSecurityGroup(createSecurityGroupRequest)
|
|
|
|
},
|
|
|
|
EvalFunc: client.EvalCouldRetryResponse(createSecurityGroupRetryErrors, 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 security group")
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
2019-04-25 22:37:49 -04:00
|
|
|
|
|
|
|
securityGroupId := securityGroupResponse.(*ecs.CreateSecurityGroupResponse).SecurityGroupId
|
|
|
|
|
|
|
|
ui.Message(fmt.Sprintf("Created security group: %s", securityGroupId))
|
2017-03-03 03:56:17 -05:00
|
|
|
state.Put("securitygroupid", securityGroupId)
|
|
|
|
s.isCreate = true
|
|
|
|
s.SecurityGroupId = securityGroupId
|
2019-04-25 22:37:49 -04:00
|
|
|
|
|
|
|
authorizeSecurityGroupEgressRequest := ecs.CreateAuthorizeSecurityGroupEgressRequest()
|
|
|
|
authorizeSecurityGroupEgressRequest.SecurityGroupId = securityGroupId
|
|
|
|
authorizeSecurityGroupEgressRequest.RegionId = s.RegionId
|
|
|
|
authorizeSecurityGroupEgressRequest.IpProtocol = IpProtocolAll
|
|
|
|
authorizeSecurityGroupEgressRequest.PortRange = DefaultPortRange
|
|
|
|
authorizeSecurityGroupEgressRequest.NicType = NicTypeInternet
|
|
|
|
authorizeSecurityGroupEgressRequest.DestCidrIp = DefaultCidrIp
|
|
|
|
|
|
|
|
if _, err := client.AuthorizeSecurityGroupEgress(authorizeSecurityGroupEgressRequest); err != nil {
|
|
|
|
return halt(state, err, "Failed authorizing security group")
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
2019-04-25 22:37:49 -04:00
|
|
|
|
|
|
|
authorizeSecurityGroupRequest := ecs.CreateAuthorizeSecurityGroupRequest()
|
|
|
|
authorizeSecurityGroupRequest.SecurityGroupId = securityGroupId
|
|
|
|
authorizeSecurityGroupRequest.RegionId = s.RegionId
|
|
|
|
authorizeSecurityGroupRequest.IpProtocol = IpProtocolAll
|
|
|
|
authorizeSecurityGroupRequest.PortRange = DefaultPortRange
|
|
|
|
authorizeSecurityGroupRequest.NicType = NicTypeInternet
|
|
|
|
authorizeSecurityGroupRequest.SourceCidrIp = DefaultCidrIp
|
|
|
|
|
|
|
|
if _, err := client.AuthorizeSecurityGroup(authorizeSecurityGroupRequest); err != nil {
|
|
|
|
return halt(state, err, "Failed authorizing security group")
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepConfigAlicloudSecurityGroup) Cleanup(state multistep.StateBag) {
|
|
|
|
if !s.isCreate {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
cleanUpMessage(state, "security group")
|
|
|
|
|
|
|
|
client := state.Get("client").(*ClientWrapper)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2017-03-03 03:56:17 -05:00
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
_, err := client.WaitForExpected(&WaitForExpectArgs{
|
|
|
|
RequestFunc: func() (responses.AcsResponse, error) {
|
|
|
|
request := ecs.CreateDeleteSecurityGroupRequest()
|
|
|
|
request.RegionId = s.RegionId
|
|
|
|
request.SecurityGroupId = s.SecurityGroupId
|
|
|
|
return client.DeleteSecurityGroup(request)
|
|
|
|
},
|
|
|
|
EvalFunc: client.EvalCouldRetryResponse(deleteSecurityGroupRetryErrors, EvalRetryErrorType),
|
|
|
|
RetryTimes: shortRetryTimes,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Failed to delete security group, 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 *stepConfigAlicloudSecurityGroup) buildCreateSecurityGroupRequest(state multistep.StateBag) *ecs.CreateSecurityGroupRequest {
|
|
|
|
networkType := state.Get("networktype").(InstanceNetWork)
|
|
|
|
|
|
|
|
request := ecs.CreateCreateSecurityGroupRequest()
|
|
|
|
request.ClientToken = uuid.TimeOrderedUUID()
|
|
|
|
request.RegionId = s.RegionId
|
|
|
|
request.SecurityGroupName = s.SecurityGroupName
|
|
|
|
|
|
|
|
if networkType == InstanceNetworkVpc {
|
|
|
|
vpcId := state.Get("vpcid").(string)
|
|
|
|
request.VpcId = vpcId
|
|
|
|
}
|
|
|
|
|
|
|
|
return request
|
|
|
|
}
|