This repository has been archived by the owner on Jul 25, 2023. It is now read-only.
forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_cloudstack_secondary_ipaddress.go
160 lines (129 loc) · 3.68 KB
/
resource_cloudstack_secondary_ipaddress.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
package cloudstack
import (
"fmt"
"log"
"strings"
"github.com/hashicorp/terraform/helper/schema"
"github.com/xanzy/go-cloudstack/cloudstack"
)
func resourceCloudStackSecondaryIPAddress() *schema.Resource {
return &schema.Resource{
Create: resourceCloudStackSecondaryIPAddressCreate,
Read: resourceCloudStackSecondaryIPAddressRead,
Delete: resourceCloudStackSecondaryIPAddressDelete,
Schema: map[string]*schema.Schema{
"ipaddress": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"nicid": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"virtual_machine": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func resourceCloudStackSecondaryIPAddressCreate(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
nicid := d.Get("nicid").(string)
if nicid == "" {
// Retrieve the virtual_machine ID
virtualmachineid, e := retrieveID(cs, "virtual_machine", d.Get("virtual_machine").(string))
if e != nil {
return e.Error()
}
// Get the virtual machine details
vm, count, err := cs.VirtualMachine.GetVirtualMachineByID(virtualmachineid)
if err != nil {
if count == 0 {
log.Printf("[DEBUG] Instance %s does no longer exist", d.Get("virtual_machine").(string))
d.SetId("")
return nil
}
return err
}
nicid = vm.Nic[0].Id
}
// Create a new parameter struct
p := cs.Nic.NewAddIpToNicParams(nicid)
if addr := d.Get("ipaddress").(string); addr != "" {
p.SetIpaddress(addr)
}
ip, err := cs.Nic.AddIpToNic(p)
if err != nil {
return err
}
d.SetId(ip.Id)
return nil
}
func resourceCloudStackSecondaryIPAddressRead(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
// Retrieve the virtual_machine ID
virtualmachineid, e := retrieveID(cs, "virtual_machine", d.Get("virtual_machine").(string))
if e != nil {
return e.Error()
}
// Get the virtual machine details
vm, count, err := cs.VirtualMachine.GetVirtualMachineByID(virtualmachineid)
if err != nil {
if count == 0 {
log.Printf("[DEBUG] Instance %s does no longer exist", d.Get("virtual_machine").(string))
d.SetId("")
return nil
}
return err
}
nicid := d.Get("nicid").(string)
if nicid == "" {
nicid = vm.Nic[0].Id
}
p := cs.Nic.NewListNicsParams(virtualmachineid)
p.SetNicid(nicid)
l, err := cs.Nic.ListNics(p)
if err != nil {
return err
}
if l.Count == 0 {
log.Printf("[DEBUG] NIC %s does no longer exist", d.Get("nicid").(string))
d.SetId("")
return nil
}
if l.Count > 1 {
return fmt.Errorf("Found more then one possible result: %v", l.Nics)
}
for _, ip := range l.Nics[0].Secondaryip {
if ip.Id == d.Id() {
d.Set("ipaddress", ip.Ipaddress)
d.Set("nicid", l.Nics[0].Id)
return nil
}
}
log.Printf("[DEBUG] IP %s no longer exist", d.Get("ipaddress").(string))
d.SetId("")
return nil
}
func resourceCloudStackSecondaryIPAddressDelete(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
// Create a new parameter struct
p := cs.Nic.NewRemoveIpFromNicParams(d.Id())
log.Printf("[INFO] Removing secondary IP address: %s", d.Get("ipaddress").(string))
if _, err := cs.Nic.RemoveIpFromNic(p); err != nil {
// This is a very poor way to be told the ID does no longer exist :(
if strings.Contains(err.Error(), fmt.Sprintf(
"Invalid parameter id value=%s due to incorrect long value format, "+
"or entity does not exist", d.Id())) {
return nil
}
return fmt.Errorf("Error removing secondary IP address: %s", err)
}
return nil
}