github.phpd.cn/hashicorp/packer@v1.3.2/builder/azure/arm/step_get_ip_address.go (about)

     1  package arm
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/hashicorp/packer/builder/azure/common/constants"
     8  	"github.com/hashicorp/packer/helper/multistep"
     9  	"github.com/hashicorp/packer/packer"
    10  )
    11  
    12  type EndpointType int
    13  
    14  const (
    15  	PublicEndpoint EndpointType = iota
    16  	PrivateEndpoint
    17  	PublicEndpointInPrivateNetwork
    18  )
    19  
    20  var (
    21  	EndpointCommunicationText = map[EndpointType]string{
    22  		PublicEndpoint:                 "PublicEndpoint",
    23  		PrivateEndpoint:                "PrivateEndpoint",
    24  		PublicEndpointInPrivateNetwork: "PublicEndpointInPrivateNetwork",
    25  	}
    26  )
    27  
    28  type StepGetIPAddress struct {
    29  	client   *AzureClient
    30  	endpoint EndpointType
    31  	get      func(ctx context.Context, resourceGroupName string, ipAddressName string, interfaceName string) (string, error)
    32  	say      func(message string)
    33  	error    func(e error)
    34  }
    35  
    36  func NewStepGetIPAddress(client *AzureClient, ui packer.Ui, endpoint EndpointType) *StepGetIPAddress {
    37  	var step = &StepGetIPAddress{
    38  		client:   client,
    39  		endpoint: endpoint,
    40  		say:      func(message string) { ui.Say(message) },
    41  		error:    func(e error) { ui.Error(e.Error()) },
    42  	}
    43  
    44  	switch endpoint {
    45  	case PrivateEndpoint:
    46  		step.get = step.getPrivateIP
    47  	case PublicEndpoint:
    48  		step.get = step.getPublicIP
    49  	case PublicEndpointInPrivateNetwork:
    50  		step.get = step.getPublicIPInPrivateNetwork
    51  	}
    52  
    53  	return step
    54  }
    55  
    56  func (s *StepGetIPAddress) getPrivateIP(ctx context.Context, resourceGroupName string, ipAddressName string, interfaceName string) (string, error) {
    57  	resp, err := s.client.InterfacesClient.Get(ctx, resourceGroupName, interfaceName, "")
    58  	if err != nil {
    59  		s.say(s.client.LastError.Error())
    60  		return "", err
    61  	}
    62  
    63  	return *(*resp.IPConfigurations)[0].PrivateIPAddress, nil
    64  }
    65  
    66  func (s *StepGetIPAddress) getPublicIP(ctx context.Context, resourceGroupName string, ipAddressName string, interfaceName string) (string, error) {
    67  	resp, err := s.client.PublicIPAddressesClient.Get(ctx, resourceGroupName, ipAddressName, "")
    68  	if err != nil {
    69  		return "", err
    70  	}
    71  
    72  	return *resp.IPAddress, nil
    73  }
    74  
    75  func (s *StepGetIPAddress) getPublicIPInPrivateNetwork(ctx context.Context, resourceGroupName string, ipAddressName string, interfaceName string) (string, error) {
    76  	s.getPrivateIP(ctx, resourceGroupName, ipAddressName, interfaceName)
    77  	return s.getPublicIP(ctx, resourceGroupName, ipAddressName, interfaceName)
    78  }
    79  
    80  func (s *StepGetIPAddress) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
    81  	s.say("Getting the VM's IP address ...")
    82  
    83  	var resourceGroupName = state.Get(constants.ArmResourceGroupName).(string)
    84  	var ipAddressName = state.Get(constants.ArmPublicIPAddressName).(string)
    85  	var nicName = state.Get(constants.ArmNicName).(string)
    86  
    87  	s.say(fmt.Sprintf(" -> ResourceGroupName   : '%s'", resourceGroupName))
    88  	s.say(fmt.Sprintf(" -> PublicIPAddressName : '%s'", ipAddressName))
    89  	s.say(fmt.Sprintf(" -> NicName             : '%s'", nicName))
    90  	s.say(fmt.Sprintf(" -> Network Connection  : '%s'", EndpointCommunicationText[s.endpoint]))
    91  
    92  	address, err := s.get(ctx, resourceGroupName, ipAddressName, nicName)
    93  	if err != nil {
    94  		state.Put(constants.Error, err)
    95  		s.error(err)
    96  
    97  		return multistep.ActionHalt
    98  	}
    99  
   100  	state.Put(constants.SSHHost, address)
   101  	s.say(fmt.Sprintf(" -> IP Address          : '%s'", address))
   102  
   103  	return multistep.ActionContinue
   104  }
   105  
   106  func (*StepGetIPAddress) Cleanup(multistep.StateBag) {
   107  }