github.phpd.cn/hashicorp/packer@v1.3.2/builder/alicloud/ecs/step_config_public_ip.go (about)

     1  package ecs
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/denverdino/aliyungo/ecs"
     8  	"github.com/hashicorp/packer/helper/multistep"
     9  	"github.com/hashicorp/packer/packer"
    10  )
    11  
    12  type stepConfigAlicloudPublicIP struct {
    13  	publicIPAddress string
    14  	RegionId        string
    15  	SSHPrivateIp    bool
    16  }
    17  
    18  func (s *stepConfigAlicloudPublicIP) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
    19  	client := state.Get("client").(*ecs.Client)
    20  	ui := state.Get("ui").(packer.Ui)
    21  	instance := state.Get("instance").(*ecs.InstanceAttributesType)
    22  
    23  	if s.SSHPrivateIp {
    24  		ipaddress := instance.InnerIpAddress.IpAddress
    25  		if len(ipaddress) == 0 {
    26  			ui.Say("Failed to get private ip of instance")
    27  			return multistep.ActionHalt
    28  		}
    29  		state.Put("ipaddress", ipaddress[0])
    30  		return multistep.ActionContinue
    31  	}
    32  
    33  	ipaddress, err := client.AllocatePublicIpAddress(instance.InstanceId)
    34  	if err != nil {
    35  		state.Put("error", err)
    36  		ui.Say(fmt.Sprintf("Error allocating public ip: %s", err))
    37  		return multistep.ActionHalt
    38  	}
    39  	s.publicIPAddress = ipaddress
    40  	ui.Say(fmt.Sprintf("Allocated public ip address %s.", ipaddress))
    41  	state.Put("ipaddress", ipaddress)
    42  	return multistep.ActionContinue
    43  }
    44  
    45  func (s *stepConfigAlicloudPublicIP) Cleanup(state multistep.StateBag) {
    46  
    47  }