forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_clc_public_ip.go
193 lines (180 loc) · 5.1 KB
/
resource_clc_public_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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package clc
import (
"fmt"
"log"
"strconv"
clc "github.com/CenturyLinkCloud/clc-sdk"
"github.com/CenturyLinkCloud/clc-sdk/server"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceCLCPublicIP() *schema.Resource {
return &schema.Resource{
Create: resourceCLCPublicIPCreate,
Read: resourceCLCPublicIPRead,
Update: resourceCLCPublicIPUpdate,
Delete: resourceCLCPublicIPDelete,
Schema: map[string]*schema.Schema{
"server_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"internal_ip_address": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
Default: nil,
},
"ports": &schema.Schema{
Type: schema.TypeList,
Required: true,
Elem: &schema.Schema{Type: schema.TypeMap},
},
"source_restrictions": &schema.Schema{
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeMap},
},
},
}
}
func resourceCLCPublicIPCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clc.Client)
sid := d.Get("server_id").(string)
priv := d.Get("internal_ip_address").(string)
ports, sources := parseIPSpec(d)
req := server.PublicIP{
Ports: *ports,
SourceRestrictions: *sources,
}
// since the API doesn't tell us the public IP it allocated,
// track what was added after the call.
ips := make(map[string]string)
prev, err := client.Server.Get(sid)
if err != nil {
return fmt.Errorf("Failed finding server %v: %v", sid, err)
}
for _, i := range prev.Details.IPaddresses {
ips[i.Internal] = i.Public
}
if priv != "" {
// use existing private ip
if _, present := ips[priv]; !present {
return fmt.Errorf("Failed finding internal ip to use %v", priv)
}
req.InternalIP = priv
}
// execute the request
resp, err := client.Server.AddPublicIP(sid, req)
if err != nil {
return fmt.Errorf("Failed reserving public ip: %v", err)
}
err = waitStatus(client, resp.ID)
if err != nil {
return err
}
server, err := client.Server.Get(sid)
if err != nil {
return fmt.Errorf("Failed refreshing server for public ip: %v", err)
}
for _, i := range server.Details.IPaddresses {
if priv != "" && i.Internal == priv {
// bind
log.Printf("[DEBUG] Public IP bound on existing internal:%v - %v", i.Internal, i.Public)
d.SetId(i.Public)
break
} else if ips[i.Internal] == "" && i.Public != "" {
// allocate
log.Printf("[DEBUG] Public IP allocated on new internal:%v - %v", i.Internal, i.Public)
d.SetId(i.Public)
break
}
}
return resourceCLCPublicIPRead(d, meta)
}
func resourceCLCPublicIPRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clc.Client)
pip := d.Id()
s := d.Get("server_id").(string)
resp, err := client.Server.GetPublicIP(s, pip)
if err != nil {
log.Printf("[INFO] Failed finding public ip: %v. Marking destroyed", d.Id())
d.SetId("")
return nil
}
d.Set("internal_ip_address", resp.InternalIP)
d.Set("ports", resp.Ports)
d.Set("source_restrictions", resp.SourceRestrictions)
return nil
}
func resourceCLCPublicIPUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clc.Client)
ip := d.Id()
sid := d.Get("server_id").(string)
if d.HasChange("ports") || d.HasChange("source_restrictions") {
ports, sources := parseIPSpec(d)
req := server.PublicIP{
Ports: *ports,
SourceRestrictions: *sources,
}
resp, err := client.Server.UpdatePublicIP(sid, ip, req)
if err != nil {
return fmt.Errorf("Failed updating public ip: %v", err)
}
err = waitStatus(client, resp.ID)
if err != nil {
return err
}
log.Printf("[INFO] Successfully updated %v with %v", ip, req)
}
return nil
}
func resourceCLCPublicIPDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clc.Client)
s := d.Get("server_id").(string)
ip := d.Id()
log.Printf("[INFO] Deleting public ip %v", ip)
resp, err := client.Server.DeletePublicIP(s, ip)
if err != nil {
return fmt.Errorf("Failed deleting public ip: %v", err)
}
err = waitStatus(client, resp.ID)
if err != nil {
return err
}
log.Printf("[INFO] Public IP sucessfully deleted: %v", ip)
return nil
}
func parseIPSpec(d *schema.ResourceData) (*[]server.Port, *[]server.SourceRestriction) {
var ports []server.Port
var sources []server.SourceRestriction
if v := d.Get("ports"); v != nil {
for _, v := range v.([]interface{}) {
m := v.(map[string]interface{})
p := server.Port{}
port, err := strconv.Atoi(m["port"].(string))
if err != nil {
log.Printf("[WARN] Failed parsing port '%v'. skipping", m["port"])
continue
}
p.Protocol = m["protocol"].(string)
p.Port = port
through := -1
if to := m["port_to"]; to != nil {
through, _ = strconv.Atoi(to.(string))
log.Printf("[DEBUG] port range: %v-%v", port, through)
p.PortTo = through
}
ports = append(ports, p)
}
}
if v := d.Get("source_restrictions"); v != nil {
for _, v := range v.([]interface{}) {
m := v.(map[string]interface{})
r := server.SourceRestriction{}
r.CIDR = m["cidr"].(string)
sources = append(sources, r)
}
}
return &ports, &sources
}