-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathutils.go
More file actions
96 lines (86 loc) · 2.83 KB
/
utils.go
File metadata and controls
96 lines (86 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package upcloud
import (
"context"
"errors"
"fmt"
"time"
"github.com/hashicorp/packer-plugin-sdk/multistep"
"github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud"
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
)
// wraps error logic.
func stepHaltWithError(state multistep.StateBag, err error) multistep.StepAction {
uiRaw := state.Get("ui")
if ui, ok := uiRaw.(packer.Ui); ok {
ui.Error(err.Error())
}
state.Put("error", err)
return multistep.ActionHalt
}
// Find IP address by type from list of IP addresses.
func findIPAddressByType(addrs upcloud.IPAddressSlice, infType InterfaceType) (*IPAddress, error) {
var ipv6 *IPAddress
for _, ipAddress := range addrs {
if ipAddress.Access == string(infType) {
switch ipAddress.Family {
case upcloud.IPAddressFamilyIPv4:
// prefer IPv4 over IPv6 - return first matching IPv4 interface if found
return &IPAddress{Address: ipAddress.Address, Family: ipAddress.Family}, nil
case upcloud.IPAddressFamilyIPv6:
// not returning IPv6 because there might be IPv4 address coming up in the slice
ipv6 = &IPAddress{Address: ipAddress.Address, Family: ipAddress.Family}
}
}
}
// return IPv6 if found
if ipv6 != nil {
return ipv6, nil
}
return nil, fmt.Errorf("unable to find '%s' IP address", infType)
}
func getNowString() string {
return time.Now().Format("20060102-150405")
}
// sshHostCallback returns server's IP addresss.
// Note that IPv6 address needs to be enclosed in square brackets.
func sshHostCallback(state multistep.StateBag) (string, error) {
addr, ok := state.Get("server_ip_address").(*IPAddress)
if !ok || addr == nil {
return "", errors.New("unable to get server_ip_address from state")
}
if addr.Family == upcloud.IPAddressFamilyIPv6 {
return fmt.Sprintf("[%s]", addr.Address), nil
}
return addr.Address, nil
}
func convertNetworkTypes(rawNetworking []NetworkInterface) []request.CreateServerInterface {
networking := make([]request.CreateServerInterface, 0, len(rawNetworking))
for _, iface := range rawNetworking {
ips := make([]request.CreateServerIPAddress, 0, len(iface.IPAddresses))
for _, ip := range iface.IPAddresses {
ips = append(ips, request.CreateServerIPAddress{Family: ip.Family, Address: ip.Address})
}
networking = append(networking, request.CreateServerInterface{
IPAddresses: ips,
Type: string(iface.Type),
Network: iface.Network,
})
}
return networking
}
func contextWithDefaultTimeout() (context.Context, context.CancelFunc) {
return context.WithTimeout(context.Background(), defaultTimeout)
}
func defaultNetworking() []request.CreateServerInterface {
return []request.CreateServerInterface{
{
IPAddresses: []request.CreateServerIPAddress{
{
Family: upcloud.IPAddressFamilyIPv4,
},
},
Type: upcloud.IPAddressAccessPublic,
},
}
}