-
Notifications
You must be signed in to change notification settings - Fork 33
/
resource_xenorchestra_network.go
256 lines (230 loc) · 6.77 KB
/
resource_xenorchestra_network.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package xoa
import (
"errors"
"fmt"
"log"
"github.com/vatesfr/terraform-provider-xenorchestra/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
var netDefaultDesc string = "Created with Xen Orchestra"
func resourceXoaNetwork() *schema.Resource {
return &schema.Resource{
Create: resourceNetworkCreate,
Delete: resourceNetworkDelete,
Read: resourceNetworkRead,
Update: resourceNetworkUpdate,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"automatic": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"default_is_locked": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "This argument controls whether the network should enforce VIF locking. This defaults to `false` which means that no filtering rules are applied.",
},
"name_label": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The name label of the network.",
},
"name_description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: netDefaultDesc,
},
"source_pif_device": &schema.Schema{
Type: schema.TypeString,
Optional: true,
RequiredWith: []string{
"vlan",
},
ForceNew: true,
Description: "The PIF device (eth0, eth1, etc) that will be used as an input during network creation. This parameter is required if a vlan is specified.",
},
"vlan": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Default: 0,
RequiredWith: []string{
"source_pif_device",
},
ForceNew: true,
Description: "The vlan to use for the network. Defaults to `0` meaning no VLAN.",
},
"pool_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The pool id that this network should belong to.",
},
"mtu": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Default: 1500,
ForceNew: true,
Description: "The MTU of the network. Defaults to `1500` if unspecified.",
},
"nbd": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Whether the network should use a network block device. Defaults to `false` if unspecified.",
},
},
}
}
func resourceNetworkCreate(d *schema.ResourceData, m interface{}) error {
c := m.(client.XOClient)
var pifId string
if sourcePIFDevice := d.Get("source_pif_device").(string); sourcePIFDevice != "" {
pif, err := getNetworkCreationSourcePIF(c, sourcePIFDevice, d.Get("pool_id").(string))
if err != nil {
return err
}
pifId = pif.Id
}
network, err := c.CreateNetwork(client.CreateNetworkRequest{
Automatic: d.Get("automatic").(bool),
DefaultIsLocked: d.Get("default_is_locked").(bool),
Name: d.Get("name_label").(string),
Description: d.Get("name_description").(string),
Pool: d.Get("pool_id").(string),
Mtu: d.Get("mtu").(int),
Nbd: d.Get("nbd").(bool),
Vlan: d.Get("vlan").(int),
PIF: pifId,
})
if err != nil {
return err
}
vlan, pifDevice, err := getVlanForNetwork(c, network)
if err != nil {
return err
}
return networkToData(network, vlan, pifDevice, d)
}
// This function returns the PIF specified the given device name on the pool's primary host. In order to create
// networks with a VLAN, a PIF for the given device must be provided. Xen Orchestra uses the primary host's PIF
// for this and so we emulate that behavior.
func getNetworkCreationSourcePIF(c client.XOClient, pifDevice, poolId string) (*client.PIF, error) {
pools, err := c.GetPools(client.Pool{Id: poolId})
if err != nil {
return nil, err
}
if len(pools) != 1 {
return nil, errors.New(fmt.Sprintf("expected to find a single pool, instead found %d", len(pools)))
}
pool := pools[0]
pifs, err := c.GetPIF(client.PIF{
Host: pool.Master,
Vlan: -1,
Device: pifDevice,
})
if err != nil {
return nil, err
}
if len(pifs) != 1 {
return nil, errors.New(fmt.Sprintf("expected to find a single PIF, instead found %d. %+v", len(pifs), pifs))
}
return &pifs[0], nil
}
// Returns the VLAN and device name for the given network.
func getVlanForNetwork(c client.XOClient, net *client.Network) (int, string, error) {
if len(net.PIFs) > 0 {
pifs, err := c.GetPIF(client.PIF{Id: net.PIFs[0]})
if err != nil {
return -1, "", err
}
if len(pifs) != 1 {
return -1, "", errors.New("expected to find single PIF")
}
return pifs[0].Vlan, pifs[0].Device, nil
}
return 0, "", nil
}
func resourceNetworkRead(d *schema.ResourceData, m interface{}) error {
c := m.(client.XOClient)
net, err := c.GetNetwork(
client.Network{Id: d.Id()})
if _, ok := err.(client.NotFound); ok {
d.SetId("")
return nil
}
if err != nil {
return err
}
vlan, pifDevice, err := getVlanForNetwork(c, net)
if err != nil {
return err
}
return networkToData(net, vlan, pifDevice, d)
}
func resourceNetworkUpdate(d *schema.ResourceData, m interface{}) error {
c := m.(client.XOClient)
netUpdateReq := client.UpdateNetworkRequest{
Id: d.Id(),
Automatic: d.Get("automatic").(bool),
DefaultIsLocked: d.Get("default_is_locked").(bool),
Nbd: d.Get("nbd").(bool),
}
if d.HasChange("name_label") {
nameLabel := d.Get("name_label").(string)
netUpdateReq.NameLabel = &nameLabel
}
if d.HasChange("name_description") {
nameDescription := d.Get("name_description").(string)
netUpdateReq.NameDescription = &nameDescription
}
_, err := c.UpdateNetwork(netUpdateReq)
if err != nil {
return err
}
return resourceNetworkRead(d, m)
}
func resourceNetworkDelete(d *schema.ResourceData, m interface{}) error {
c := m.(client.XOClient)
err := c.DeleteNetwork(d.Id())
if err != nil {
return err
}
d.SetId("")
return nil
}
func networkToData(network *client.Network, vlan int, pifDevice string, d *schema.ResourceData) error {
d.SetId(network.Id)
if err := d.Set("name_label", network.NameLabel); err != nil {
return err
}
log.Printf("This is being called\n")
if err := d.Set("name_description", network.NameDescription); err != nil {
return err
}
if err := d.Set("pool_id", network.PoolId); err != nil {
return err
}
if err := d.Set("mtu", network.MTU); err != nil {
return err
}
if err := d.Set("nbd", network.Nbd); err != nil {
return err
}
if err := d.Set("automatic", network.Automatic); err != nil {
return err
}
if err := d.Set("default_is_locked", network.DefaultIsLocked); err != nil {
return err
}
if err := d.Set("vlan", vlan); err != nil {
return err
}
if err := d.Set("source_pif_device", pifDevice); err != nil {
return err
}
return nil
}