forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_vlan.go
132 lines (107 loc) · 2.97 KB
/
resource_vlan.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
package triton
import (
"errors"
"strconv"
"github.com/hashicorp/terraform/helper/schema"
"github.com/joyent/gosdc/cloudapi"
)
func resourceVLAN() *schema.Resource {
return &schema.Resource{
Create: resourceVLANCreate,
Exists: resourceVLANExists,
Read: resourceVLANRead,
Update: resourceVLANUpdate,
Delete: resourceVLANDelete,
Schema: map[string]*schema.Schema{
"vlan_id": {
Description: "number between 0-4095 indicating VLAN ID",
Required: true,
ForceNew: true,
Type: schema.TypeInt,
ValidateFunc: func(val interface{}, field string) (warn []string, err []error) {
value := val.(int)
if value < 0 || value > 4095 {
err = append(err, errors.New("vlan_id must be between 0 and 4095"))
}
return
},
},
"name": {
Description: "Unique name to identify VLAN",
Required: true,
Type: schema.TypeString,
},
"description": {
Description: "Optional description of the VLAN",
Optional: true,
Type: schema.TypeString,
},
},
}
}
func resourceVLANCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudapi.Client)
vlan, err := client.CreateFabricVLAN(cloudapi.FabricVLAN{
Id: int16(d.Get("vlan_id").(int)),
Name: d.Get("name").(string),
Description: d.Get("description").(string),
})
if err != nil {
return err
}
d.SetId(resourceVLANIDString(vlan.Id))
return resourceVLANRead(d, meta)
}
func resourceVLANExists(d *schema.ResourceData, meta interface{}) (bool, error) {
client := meta.(*cloudapi.Client)
id, err := resourceVLANIDInt16(d.Id())
if err != nil {
return false, err
}
vlan, err := client.GetFabricVLAN(id)
return vlan != nil && err == nil, err
}
func resourceVLANRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudapi.Client)
vlan, err := client.GetFabricVLAN(int16(d.Get("vlan_id").(int)))
if err != nil {
return err
}
d.SetId(resourceVLANIDString(vlan.Id))
d.Set("vlan_id", vlan.Id)
d.Set("name", vlan.Name)
d.Set("description", vlan.Description)
return nil
}
func resourceVLANUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudapi.Client)
vlan, err := client.UpdateFabricVLAN(cloudapi.FabricVLAN{
Id: int16(d.Get("vlan_id").(int)),
Name: d.Get("name").(string),
Description: d.Get("description").(string),
})
if err != nil {
return err
}
d.SetId(resourceVLANIDString(vlan.Id))
return resourceVLANRead(d, meta)
}
func resourceVLANDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudapi.Client)
id, err := resourceVLANIDInt16(d.Id())
if err != nil {
return err
}
return client.DeleteFabricVLAN(id)
}
// convenience conversion functions
func resourceVLANIDString(id int16) string {
return strconv.Itoa(int(id))
}
func resourceVLANIDInt16(id string) (int16, error) {
result, err := strconv.ParseInt(id, 10, 16)
if err != nil {
return 0, err
}
return int16(result), nil
}