packer-cn/builder/alicloud/ecs/step_config_public_ip.go

48 lines
1.2 KiB
Go
Raw Normal View History

package ecs
import (
"context"
"fmt"
2017-05-25 21:49:35 -04:00
"github.com/denverdino/aliyungo/ecs"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
)
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
}
func (s *stepConfigAlicloudPublicIP) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
client := state.Get("client").(*ecs.Client)
ui := state.Get("ui").(packer.Ui)
instance := state.Get("instance").(*ecs.InstanceAttributesType)
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
}
ipaddress, err := client.AllocatePublicIpAddress(instance.InstanceId)
if err != nil {
state.Put("error", err)
2017-05-25 21:49:35 -04:00
ui.Say(fmt.Sprintf("Error allocating public ip: %s", err))
return multistep.ActionHalt
}
2018-03-13 03:07:10 -04:00
s.publicIPAddress = ipaddress
2017-06-01 22:33:12 -04:00
ui.Say(fmt.Sprintf("Allocated public ip address %s.", ipaddress))
state.Put("ipaddress", ipaddress)
return multistep.ActionContinue
}
func (s *stepConfigAlicloudPublicIP) Cleanup(state multistep.StateBag) {
}