packer-cn/builder/azure/arm/step_get_ip_address.go

108 lines
3.3 KiB
Go
Raw Normal View History

package arm
import (
"context"
"fmt"
2020-12-17 16:29:25 -05:00
"github.com/hashicorp/packer-plugin-sdk/multistep"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
2017-04-04 16:39:01 -04:00
"github.com/hashicorp/packer/builder/azure/common/constants"
)
type EndpointType int
const (
PublicEndpoint EndpointType = iota
PrivateEndpoint
2017-08-06 18:32:44 -04:00
PublicEndpointInPrivateNetwork
)
var (
EndpointCommunicationText = map[EndpointType]string{
2017-08-06 18:32:44 -04:00
PublicEndpoint: "PublicEndpoint",
PrivateEndpoint: "PrivateEndpoint",
PublicEndpointInPrivateNetwork: "PublicEndpointInPrivateNetwork",
}
)
type StepGetIPAddress struct {
client *AzureClient
endpoint EndpointType
2018-04-06 04:12:58 -04:00
get func(ctx context.Context, resourceGroupName string, ipAddressName string, interfaceName string) (string, error)
say func(message string)
error func(e error)
}
func NewStepGetIPAddress(client *AzureClient, ui packersdk.Ui, endpoint EndpointType) *StepGetIPAddress {
var step = &StepGetIPAddress{
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
2017-08-06 18:32:44 -04:00
case PublicEndpointInPrivateNetwork:
step.get = step.getPublicIPInPrivateNetwork
}
return step
}
2018-04-06 04:12:58 -04:00
func (s *StepGetIPAddress) getPrivateIP(ctx context.Context, resourceGroupName string, ipAddressName string, interfaceName string) (string, error) {
resp, err := s.client.InterfacesClient.Get(ctx, resourceGroupName, interfaceName, "")
if err != nil {
2017-06-08 20:57:59 -04:00
s.say(s.client.LastError.Error())
return "", err
}
return *(*resp.IPConfigurations)[0].PrivateIPAddress, nil
}
2018-04-06 04:12:58 -04:00
func (s *StepGetIPAddress) getPublicIP(ctx context.Context, resourceGroupName string, ipAddressName string, interfaceName string) (string, error) {
resp, err := s.client.PublicIPAddressesClient.Get(ctx, resourceGroupName, ipAddressName, "")
if err != nil {
return "", err
}
return *resp.IPAddress, nil
}
2018-04-06 04:12:58 -04:00
func (s *StepGetIPAddress) getPublicIPInPrivateNetwork(ctx context.Context, resourceGroupName string, ipAddressName string, interfaceName string) (string, error) {
s.getPrivateIP(ctx, resourceGroupName, ipAddressName, interfaceName)
return s.getPublicIP(ctx, resourceGroupName, ipAddressName, interfaceName)
2017-08-06 18:32:44 -04:00
}
2018-04-06 04:12:58 -04:00
func (s *StepGetIPAddress) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
s.say("Getting the VM's IP address ...")
var resourceGroupName = state.Get(constants.ArmResourceGroupName).(string)
var ipAddressName = state.Get(constants.ArmPublicIPAddressName).(string)
var nicName = state.Get(constants.ArmNicName).(string)
s.say(fmt.Sprintf(" -> ResourceGroupName : '%s'", resourceGroupName))
s.say(fmt.Sprintf(" -> PublicIPAddressName : '%s'", ipAddressName))
s.say(fmt.Sprintf(" -> NicName : '%s'", nicName))
s.say(fmt.Sprintf(" -> Network Connection : '%s'", EndpointCommunicationText[s.endpoint]))
2018-04-06 04:12:58 -04:00
address, err := s.get(ctx, resourceGroupName, ipAddressName, nicName)
if err != nil {
state.Put(constants.Error, err)
s.error(err)
return multistep.ActionHalt
}
state.Put(constants.SSHHost, address)
s.say(fmt.Sprintf(" -> IP Address : '%s'", address))
return multistep.ActionContinue
}
func (*StepGetIPAddress) Cleanup(multistep.StateBag) {
}