generated from hashicorp/packer-plugin-scaffolding
/
step_link_server_ip.go
116 lines (111 loc) · 3.23 KB
/
step_link_server_ip.go
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package gridscale
import (
"context"
"errors"
"fmt"
"github.com/gridscale/gsclient-go/v3"
"github.com/hashicorp/packer-plugin-sdk/multistep"
"github.com/hashicorp/packer-plugin-sdk/packer"
)
type stepLinkServerIPAddr struct {
client gsclient.ServerIPRelationOperator
config *Config
ui packer.Ui
}
func (s *stepLinkServerIPAddr) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
client := s.client
ui := s.ui
// Get IP address UUID
ipAddrUUID, ok := state.Get("ip_addr_uuid").(string)
if !ok {
err := errors.New("cannot convert ip_addr_uuid to string")
ui.Error(err.Error())
state.Put("error", err)
return multistep.ActionHalt
}
if ipAddrUUID == "" {
err := errors.New("ip_addr_uuid is empty")
ui.Error(err.Error())
state.Put("error", err)
return multistep.ActionHalt
}
// Get server UUID
serverUUID, ok := state.Get("server_uuid").(string)
if !ok {
err := errors.New("cannot convert server_uuid to string")
ui.Error(err.Error())
state.Put("error", err)
return multistep.ActionHalt
}
if serverUUID == "" {
err := errors.New("serverUUID is empty")
ui.Error(err.Error())
state.Put("error", err)
return multistep.ActionHalt
}
// Link server with IP address
ui.Say(fmt.Sprintf("Linking the server (%s) and the IP address (%s)...", serverUUID, ipAddrUUID))
err := client.LinkIP(context.Background(), serverUUID, ipAddrUUID)
if err != nil {
ui.Error(fmt.Sprintf(
"Error linking Server with IP address: %s", err))
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
state.Put("server_ip_addr_linked", true)
ui.Say(fmt.Sprintf("Linked the server (%s) and the IP address (%s)", serverUUID, ipAddrUUID))
return multistep.ActionContinue
}
func (s *stepLinkServerIPAddr) Cleanup(state multistep.StateBag) {
client := s.client
ui := s.ui
// Get server UUID
serverUUID, ok := state.Get("server_uuid").(string)
if !ok {
err := errors.New("cannot convert server_uuid to string")
ui.Error(err.Error())
state.Put("error", err)
return
}
if serverUUID == "" {
err := errors.New("serverUUID is empty")
ui.Error(err.Error())
state.Put("error", err)
return
}
// Get IP address UUID
ipAddrUUID, ok := state.Get("ip_addr_uuid").(string)
if !ok {
err := errors.New("cannot convert ip_addr_uuid to string")
ui.Error(err.Error())
state.Put("error", err)
return
}
if ipAddrUUID == "" {
err := errors.New("ip_addr_uuid is empty")
ui.Error(err.Error())
state.Put("error", err)
return
}
// Unlink server and IP address
serverIPAddrLinked, ok := state.Get("server_ip_addr_linked").(bool)
if !ok {
err := errors.New("cannot convert server_ip_addr_linked to boolean")
ui.Error(err.Error())
state.Put("error", err)
return
}
if !serverIPAddrLinked {
ui.Say("the server is not linked with the IP address.")
return
}
ui.Say(fmt.Sprintf("Unlinking the server (%s) and the IP address (%s)...", serverUUID, ipAddrUUID))
err := client.UnlinkIP(context.Background(), serverUUID, ipAddrUUID)
if err != nil {
ui.Error(fmt.Sprintf(
"Error unlink server (%s) and IP address (%s). Please unlink them manually: %s", serverUUID, ipAddrUUID, err))
return
}
ui.Say(fmt.Sprintf("Unlinked the server (%s) and the IP address (%s)", serverUUID, ipAddrUUID))
}