github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/builder/azure/arm/step_get_ip_address.go (about) 1 // Copyright (c) Microsoft Corporation. All rights reserved. 2 // Licensed under the MIT License. See the LICENSE file in builder/azure for license information. 3 4 package arm 5 6 import ( 7 "fmt" 8 9 "github.com/mitchellh/multistep" 10 "github.com/mitchellh/packer/builder/azure/common/constants" 11 "github.com/mitchellh/packer/packer" 12 ) 13 14 type EndpointType int 15 16 const ( 17 PublicEndpoint EndpointType = iota 18 PrivateEndpoint 19 ) 20 21 var ( 22 EndpointCommunicationText = map[EndpointType]string{ 23 PublicEndpoint: "PublicEndpoint", 24 PrivateEndpoint: "PrivateEndpoint", 25 } 26 ) 27 28 type StepGetIPAddress struct { 29 client *AzureClient 30 endpoint EndpointType 31 get func(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 } 50 51 return step 52 } 53 54 func (s *StepGetIPAddress) getPrivateIP(resourceGroupName string, ipAddressName string, interfaceName string) (string, error) { 55 resp, err := s.client.InterfacesClient.Get(resourceGroupName, interfaceName, "") 56 if err != nil { 57 return "", err 58 } 59 60 return *(*resp.Properties.IPConfigurations)[0].Properties.PrivateIPAddress, nil 61 } 62 63 func (s *StepGetIPAddress) getPublicIP(resourceGroupName string, ipAddressName string, interfaceName string) (string, error) { 64 resp, err := s.client.PublicIPAddressesClient.Get(resourceGroupName, ipAddressName, "") 65 if err != nil { 66 return "", err 67 } 68 69 return *resp.Properties.IPAddress, nil 70 } 71 72 func (s *StepGetIPAddress) Run(state multistep.StateBag) multistep.StepAction { 73 s.say("Getting the VM's IP address ...") 74 75 var resourceGroupName = state.Get(constants.ArmResourceGroupName).(string) 76 var ipAddressName = state.Get(constants.ArmPublicIPAddressName).(string) 77 var nicName = state.Get(constants.ArmNicName).(string) 78 79 s.say(fmt.Sprintf(" -> ResourceGroupName : '%s'", resourceGroupName)) 80 s.say(fmt.Sprintf(" -> PublicIPAddressName : '%s'", ipAddressName)) 81 s.say(fmt.Sprintf(" -> NicName : '%s'", nicName)) 82 s.say(fmt.Sprintf(" -> Network Connection : '%s'", EndpointCommunicationText[s.endpoint])) 83 84 address, err := s.get(resourceGroupName, ipAddressName, nicName) 85 if err != nil { 86 state.Put(constants.Error, err) 87 s.error(err) 88 89 return multistep.ActionHalt 90 } 91 92 state.Put(constants.SSHHost, address) 93 s.say(fmt.Sprintf(" -> IP Address : '%s'", address)) 94 95 return multistep.ActionContinue 96 } 97 98 func (*StepGetIPAddress) Cleanup(multistep.StateBag) { 99 }