forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 2
/
resource_cloudstack_private_gateway.go
173 lines (140 loc) · 4.06 KB
/
resource_cloudstack_private_gateway.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
package cloudstack
import (
"fmt"
"log"
"strings"
"github.com/hashicorp/terraform/helper/schema"
"github.com/xanzy/go-cloudstack/cloudstack"
)
func resourceCloudStackPrivateGateway() *schema.Resource {
return &schema.Resource{
Create: resourceCloudStackPrivateGatewayCreate,
Read: resourceCloudStackPrivateGatewayRead,
Update: resourceCloudStackPrivateGatewayUpdate,
Delete: resourceCloudStackPrivateGatewayDelete,
Schema: map[string]*schema.Schema{
"gateway": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"ip_address": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"netmask": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"vlan": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"physical_network_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"network_offering": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"acl_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"vpc_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func resourceCloudStackPrivateGatewayCreate(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
ipaddress := d.Get("ip_address").(string)
networkofferingid := d.Get("network_offering").(string)
// Create a new parameter struct
p := cs.VPC.NewCreatePrivateGatewayParams(
d.Get("gateway").(string),
ipaddress,
d.Get("netmask").(string),
d.Get("vlan").(string),
d.Get("vpc_id").(string),
)
// Retrieve the network_offering ID
if networkofferingid != "" {
networkofferingid, e := retrieveID(cs, "network_offering", networkofferingid)
if e != nil {
return e.Error()
}
p.SetNetworkofferingid(networkofferingid)
}
// Check if we want to associate an ACL
if aclid, ok := d.GetOk("acl_id"); ok {
// Set the acl ID
p.SetAclid(aclid.(string))
}
// Create the new private gateway
r, err := cs.VPC.CreatePrivateGateway(p)
if err != nil {
return fmt.Errorf("Error creating private gateway for %s: %s", ipaddress, err)
}
d.SetId(r.Id)
return resourceCloudStackPrivateGatewayRead(d, meta)
}
func resourceCloudStackPrivateGatewayRead(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
// Get the private gateway details
gw, count, err := cs.VPC.GetPrivateGatewayByID(d.Id())
if err != nil {
if count == 0 {
log.Printf("[DEBUG] Private gateway %s does no longer exist", d.Id())
d.SetId("")
return nil
}
return err
}
d.Set("gateway", gw.Gateway)
d.Set("ip_address", gw.Ipaddress)
d.Set("netmask", gw.Netmask)
d.Set("vlan", gw.Vlan)
d.Set("acl_id", gw.Aclid)
d.Set("vpc_id", gw.Vpcid)
return nil
}
func resourceCloudStackPrivateGatewayUpdate(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
// Replace the ACL if the ID has changed
if d.HasChange("acl_id") {
p := cs.NetworkACL.NewReplaceNetworkACLListParams(d.Get("acl_id").(string))
p.SetNetworkid(d.Id())
_, err := cs.NetworkACL.ReplaceNetworkACLList(p)
if err != nil {
return fmt.Errorf("Error replacing ACL: %s", err)
}
}
return resourceCloudStackNetworkRead(d, meta)
}
func resourceCloudStackPrivateGatewayDelete(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cloudstack.CloudStackClient)
// Create a new parameter struct
p := cs.VPC.NewDeletePrivateGatewayParams(d.Id())
// Delete the private gateway
_, err := cs.VPC.DeletePrivateGateway(p)
if 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 deleting private gateway %s: %s", d.Id(), err)
}
return nil
}