2016-03-04 05:14:55 -05:00
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
2016-05-06 23:32:18 -04:00
|
|
|
// Licensed under the MIT License. See the LICENSE file in builder/azure for license information.
|
2016-03-04 05:14:55 -05:00
|
|
|
|
|
|
|
package arm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/builder/azure/common/constants"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
2016-04-21 19:50:03 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
2016-03-04 05:14:55 -05:00
|
|
|
)
|
|
|
|
|
2016-06-30 19:51:52 -04:00
|
|
|
type EndpointType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
PublicEndpoint EndpointType = iota
|
|
|
|
PrivateEndpoint
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
EndpointCommunicationText = map[EndpointType]string{
|
|
|
|
PublicEndpoint: "PublicEndpoint",
|
|
|
|
PrivateEndpoint: "PrivateEndpoint",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2016-03-04 05:14:55 -05:00
|
|
|
type StepGetIPAddress struct {
|
2016-06-30 19:51:52 -04:00
|
|
|
client *AzureClient
|
|
|
|
endpoint EndpointType
|
|
|
|
get func(resourceGroupName string, ipAddressName string, interfaceName string) (string, error)
|
|
|
|
say func(message string)
|
|
|
|
error func(e error)
|
2016-03-04 05:14:55 -05:00
|
|
|
}
|
|
|
|
|
2016-06-30 19:51:52 -04:00
|
|
|
func NewStepGetIPAddress(client *AzureClient, ui packer.Ui, endpoint EndpointType) *StepGetIPAddress {
|
2016-03-04 05:14:55 -05:00
|
|
|
var step = &StepGetIPAddress{
|
2016-06-30 19:51:52 -04:00
|
|
|
client: client,
|
|
|
|
endpoint: endpoint,
|
|
|
|
say: func(message string) { ui.Say(message) },
|
|
|
|
error: func(e error) { ui.Error(e.Error()) },
|
|
|
|
}
|
|
|
|
|
|
|
|
switch endpoint {
|
|
|
|
case PrivateEndpoint:
|
|
|
|
step.get = step.getPrivateIP
|
|
|
|
case PublicEndpoint:
|
|
|
|
step.get = step.getPublicIP
|
2016-03-04 05:14:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return step
|
|
|
|
}
|
|
|
|
|
2016-06-30 19:51:52 -04:00
|
|
|
func (s *StepGetIPAddress) getPrivateIP(resourceGroupName string, ipAddressName string, interfaceName string) (string, error) {
|
|
|
|
resp, err := s.client.InterfacesClient.Get(resourceGroupName, interfaceName, "")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2017-05-28 03:38:45 -04:00
|
|
|
return *(*resp.IPConfigurations)[0].PrivateIPAddress, nil
|
2016-06-30 19:51:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepGetIPAddress) getPublicIP(resourceGroupName string, ipAddressName string, interfaceName string) (string, error) {
|
|
|
|
resp, err := s.client.PublicIPAddressesClient.Get(resourceGroupName, ipAddressName, "")
|
2016-03-04 05:14:55 -05:00
|
|
|
if err != nil {
|
2016-06-30 19:51:52 -04:00
|
|
|
return "", err
|
2016-03-04 05:14:55 -05:00
|
|
|
}
|
|
|
|
|
2017-05-28 03:38:45 -04:00
|
|
|
return *resp.IPAddress, nil
|
2016-03-04 05:14:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepGetIPAddress) Run(state multistep.StateBag) multistep.StepAction {
|
2016-06-30 19:51:52 -04:00
|
|
|
s.say("Getting the VM's IP address ...")
|
2016-03-04 05:14:55 -05:00
|
|
|
|
|
|
|
var resourceGroupName = state.Get(constants.ArmResourceGroupName).(string)
|
|
|
|
var ipAddressName = state.Get(constants.ArmPublicIPAddressName).(string)
|
2016-06-30 19:51:52 -04:00
|
|
|
var nicName = state.Get(constants.ArmNicName).(string)
|
2016-03-04 05:14:55 -05:00
|
|
|
|
|
|
|
s.say(fmt.Sprintf(" -> ResourceGroupName : '%s'", resourceGroupName))
|
|
|
|
s.say(fmt.Sprintf(" -> PublicIPAddressName : '%s'", ipAddressName))
|
2016-06-30 19:51:52 -04:00
|
|
|
s.say(fmt.Sprintf(" -> NicName : '%s'", nicName))
|
|
|
|
s.say(fmt.Sprintf(" -> Network Connection : '%s'", EndpointCommunicationText[s.endpoint]))
|
2016-03-04 05:14:55 -05:00
|
|
|
|
2016-06-30 19:51:52 -04:00
|
|
|
address, err := s.get(resourceGroupName, ipAddressName, nicName)
|
2016-03-04 05:14:55 -05:00
|
|
|
if err != nil {
|
|
|
|
state.Put(constants.Error, err)
|
|
|
|
s.error(err)
|
|
|
|
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
state.Put(constants.SSHHost, address)
|
2016-06-30 19:51:52 -04:00
|
|
|
s.say(fmt.Sprintf(" -> IP Address : '%s'", address))
|
2016-03-04 05:14:55 -05:00
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*StepGetIPAddress) Cleanup(multistep.StateBag) {
|
|
|
|
}
|