This repository has been archived by the owner on Jun 9, 2023. It is now read-only.
forked from hetznercloud/terraform-provider-hcloud
/
resource_hcloud_floatingip_assignment.go
124 lines (105 loc) · 3.2 KB
/
resource_hcloud_floatingip_assignment.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
117
118
119
120
121
122
123
124
package hcloud
import (
"context"
"log"
"strconv"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hetznercloud/hcloud-go/hcloud"
)
func resourceFloatingIPAssignment() *schema.Resource {
return &schema.Resource{
Create: resourceFloatingIPAssignmentCreate,
Read: resourceFloatingIPAssignmentRead,
Delete: resourceFloatingIPAssignmentDelete,
Schema: map[string]*schema.Schema{
"floating_ip_id": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
},
"server_id": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
},
},
}
}
func resourceFloatingIPAssignmentCreate(d *schema.ResourceData, m interface{}) error {
client := m.(*hcloud.Client)
ctx := context.Background()
floatingIPID := d.Get("floating_ip_id")
floatingIP := &hcloud.FloatingIP{ID: floatingIPID.(int)}
serverID := d.Get("server_id")
server := &hcloud.Server{ID: serverID.(int)}
_, _, err := client.FloatingIP.Assign(ctx, floatingIP, server)
if err != nil {
return err
}
// Since a floating ip can only be assigned to one server
// we can use the floating ip id as floating ip association id.
d.SetId(strconv.Itoa(floatingIP.ID))
return resourceFloatingIPAssignmentRead(d, m)
}
func resourceFloatingIPAssignmentRead(d *schema.ResourceData, m interface{}) error {
client := m.(*hcloud.Client)
ctx := context.Background()
floatingIPID, err := strconv.Atoi(d.Id())
if err != nil {
log.Printf("[WARN] Floating IP ID (%s) not found, removing from state: %v", d.Id(), err)
d.SetId("")
return nil
}
// 'floating_ip_id' and 'server_id' is 'Required' and 'TypeInt'
// therefore the cast should always work
floatingIP, _, err := client.FloatingIP.GetByID(ctx, floatingIPID)
if err != nil {
return err
}
if floatingIP == nil {
log.Printf("[WARN] Floating IP ID (%v) not found, removing Floating IP Association from state", d.Get("floating_ip_id"))
d.SetId("")
return nil
}
server, _, err := client.Server.GetByID(ctx, d.Get("server_id").(int))
if err != nil {
return err
}
if server == nil {
log.Printf("[WARN] Server ID (%v) not found, removing Floating IP Association from state", d.Get("server_id"))
d.SetId("")
return nil
}
// check if floating api is assigned to any server
if floatingIP.Server == nil {
log.Printf("[WARN] Floating IP (%v) is not associated to a server, removing Floating IP Association from state", d.Get("floating_ip_id"))
d.SetId("")
return nil
}
d.Set("server_id", floatingIP.Server.ID)
d.Set("floating_ip_id", floatingIP.ID)
return nil
}
func resourceFloatingIPAssignmentDelete(d *schema.ResourceData, m interface{}) error {
client := m.(*hcloud.Client)
ctx := context.Background()
floatingIPID, err := strconv.Atoi(d.Id())
if err != nil {
log.Printf("[WARN] Invalid id (%s), removing from state: %v", d.Id(), err)
d.SetId("")
return nil
}
floatingIP, _, err := client.FloatingIP.GetByID(ctx, floatingIPID)
if floatingIP == nil {
log.Printf("[WARN] Floating IP ID (%v) not found, removing Floating IP Association from state", d.Get("floating_ip_id"))
d.SetId("")
return nil
}
if floatingIP.Server != nil {
_, _, err = client.FloatingIP.Unassign(ctx, floatingIP)
if err != nil {
return err
}
}
return nil
}