forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_fabric.go
178 lines (158 loc) · 4.55 KB
/
resource_fabric.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
package triton
import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"github.com/joyent/gosdc/cloudapi"
)
func resourceFabric() *schema.Resource {
return &schema.Resource{
Create: resourceFabricCreate,
Exists: resourceFabricExists,
Read: resourceFabricRead,
Delete: resourceFabricDelete,
Schema: map[string]*schema.Schema{
"name": {
Description: "network name",
Required: true,
ForceNew: true,
Type: schema.TypeString,
},
"public": {
Description: "whether or not this is an RFC1918 network",
Computed: true,
Type: schema.TypeBool,
},
"fabric": {
Description: "whether or not this network is on a fabric",
Computed: true,
Type: schema.TypeBool,
},
"description": {
Description: "optional description of network",
Optional: true,
ForceNew: true,
Type: schema.TypeString,
},
"subnet": {
Description: "CIDR formatted string describing network",
Required: true,
ForceNew: true,
Type: schema.TypeString,
},
"provision_start_ip": {
Description: "first IP on the network that can be assigned",
Required: true,
ForceNew: true,
Type: schema.TypeString,
},
"provision_end_ip": {
Description: "last assignable IP on the network",
Required: true,
ForceNew: true,
Type: schema.TypeString,
},
"gateway": {
Description: "optional gateway IP",
Optional: true,
ForceNew: true,
Type: schema.TypeString,
},
"resolvers": {
Description: "array of IP addresses for resolvers",
Optional: true,
Computed: true,
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
},
"routes": {
Description: "map of CIDR block to Gateway IP address",
Computed: true,
Optional: true,
ForceNew: true,
Type: schema.TypeMap,
},
"internet_nat": {
Description: "if a NAT zone is provisioned at Gateway IP address",
Computed: true,
Optional: true,
ForceNew: true,
Type: schema.TypeBool,
},
"vlan_id": {
Description: "VLAN network is on",
Required: true,
ForceNew: true,
Type: schema.TypeInt,
},
},
}
}
func resourceFabricCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudapi.Client)
var resolvers []string
for _, resolver := range d.Get("resolvers").([]interface{}) {
resolvers = append(resolvers, resolver.(string))
}
routes := map[string]string{}
for cidr, v := range d.Get("routes").(map[string]interface{}) {
ip, ok := v.(string)
if !ok {
return fmt.Errorf(`cannot use "%v" as an IP address`, v)
}
routes[cidr] = ip
}
fabric, err := client.CreateFabricNetwork(
int16(d.Get("vlan_id").(int)),
cloudapi.CreateFabricNetworkOpts{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
Subnet: d.Get("subnet").(string),
ProvisionStartIp: d.Get("provision_start_ip").(string),
ProvisionEndIp: d.Get("provision_end_ip").(string),
Gateway: d.Get("gateway").(string),
Resolvers: resolvers,
Routes: routes,
InternetNAT: d.Get("internet_nat").(bool),
},
)
if err != nil {
return err
}
d.SetId(fabric.Id)
err = resourceFabricRead(d, meta)
if err != nil {
return err
}
return nil
}
func resourceFabricExists(d *schema.ResourceData, meta interface{}) (bool, error) {
client := meta.(*cloudapi.Client)
fabric, err := client.GetFabricNetwork(int16(d.Get("vlan_id").(int)), d.Id())
return fabric != nil && err == nil, err
}
func resourceFabricRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudapi.Client)
fabric, err := client.GetFabricNetwork(int16(d.Get("vlan_id").(int)), d.Id())
if err != nil {
return err
}
d.SetId(fabric.Id)
d.Set("name", fabric.Name)
d.Set("public", fabric.Public)
d.Set("public", fabric.Public)
d.Set("fabric", fabric.Fabric)
d.Set("description", fabric.Description)
d.Set("subnet", fabric.Subnet)
d.Set("provision_start_ip", fabric.ProvisionStartIp)
d.Set("provision_end_ip", fabric.ProvisionEndIp)
d.Set("gateway", fabric.Gateway)
d.Set("resolvers", fabric.Resolvers)
d.Set("routes", fabric.Routes)
d.Set("internet_nat", fabric.InternetNAT)
d.Set("vlan_id", fabric.VLANId)
return nil
}
func resourceFabricDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudapi.Client)
return client.DeleteFabricNetwork(int16(d.Get("vlan_id").(int)), d.Id())
}