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"
|
2017-05-25 21:49:35 -04:00
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
|
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 stepConfigAlicloudPublicIP struct {
|
2018-03-13 03:07:10 -04:00
|
|
|
publicIPAddress string
|
|
|
|
RegionId string
|
2018-09-11 04:56:57 -04:00
|
|
|
SSHPrivateIp bool
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepConfigAlicloudPublicIP) 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)
|
2019-04-25 22:37:49 -04:00
|
|
|
instance := state.Get("instance").(*ecs.Instance)
|
2017-03-03 03:56:17 -05:00
|
|
|
|
2018-09-11 04:56:57 -04:00
|
|
|
if s.SSHPrivateIp {
|
|
|
|
ipaddress := instance.InnerIpAddress.IpAddress
|
|
|
|
if len(ipaddress) == 0 {
|
|
|
|
ui.Say("Failed to get private ip of instance")
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
state.Put("ipaddress", ipaddress[0])
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2019-04-25 22:37:49 -04:00
|
|
|
allocatePublicIpAddressRequest := ecs.CreateAllocatePublicIpAddressRequest()
|
|
|
|
allocatePublicIpAddressRequest.InstanceId = instance.InstanceId
|
|
|
|
ipaddress, err := client.AllocatePublicIpAddress(allocatePublicIpAddressRequest)
|
2017-03-03 03:56:17 -05:00
|
|
|
if err != nil {
|
2019-04-25 22:37:49 -04:00
|
|
|
return halt(state, err, "Error allocating public ip")
|
2017-03-03 03:56:17 -05:00
|
|
|
}
|
2019-04-25 22:37:49 -04:00
|
|
|
|
|
|
|
s.publicIPAddress = ipaddress.IpAddress
|
|
|
|
ui.Say(fmt.Sprintf("Allocated public ip address %s.", ipaddress.IpAddress))
|
|
|
|
state.Put("ipaddress", ipaddress.IpAddress)
|
2017-03-03 03:56:17 -05:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepConfigAlicloudPublicIP) Cleanup(state multistep.StateBag) {
|
|
|
|
|
|
|
|
}
|