Debian Codegraph @

cloudapi/fabrics.go

raw symbols SCIP
1 package cloudapi
2
3 import (
4 "net/http"
5 "strconv"
6
7 "github.com/joyent/gocommon/client"
8 "github.com/joyent/gocommon/errors"
9 )
10
11 type FabricVLAN struct {
12 Id int16 `json:"vlan_id"` // Number between 0-4095 indicating VLAN Id
13 Name string `json:"name"` // Unique name to identify VLAN
14 Description string `json:"description,omitempty"` // Optional description of the VLAN
15 }
16
17 type FabricNetwork struct {
18 Id string `json:"id"` // Unique identifier for network
19 Name string `json:"name"` // Network name
20 Public bool `json:"public"` // Whether or not this is an RFC1918 network
21 Fabric bool `json:"fabric"` // Whether this network is on a fabric
22 Description string `json:"description"` // Optional description of network
23 Subnet string `json:"subnet"` // CIDR formatted string describing network
24 ProvisionStartIp string `json:"provision_start_ip"` // First IP on the network that can be assigned
25 ProvisionEndIp string `json:"provision_end_ip"` // Last assignable IP on the network
26 Gateway string `json:"gateway"` // Optional Gateway IP
27 Resolvers []string `json:"resolvers,omitempty"` // Array of IP addresses for resolvers
28 Routes map[string]string `json:"routes,omitempty"` // Map of CIDR block to Gateway IP Address
29 InternetNAT bool `json:"internet_nat"` // If a NAT zone is provisioned at Gateway IP Address
30 VLANId int16 `json:"vlan_id"` // VLAN network is on
31 }
32
33 type CreateFabricNetworkOpts struct {
34 Name string `json:"name"` // Network name
35 Description string `json:"description,omitempty"` // Optional description of network
36 Subnet string `json:"subnet"` // CIDR formatted string describing network
37 ProvisionStartIp string `json:"provision_start_ip"` // First IP on the network that can be assigned
38 ProvisionEndIp string `json:"provision_end_ip"` // Last assignable IP on the network
39 Gateway string `json:"gateway,omitempty"` // Optional Gateway IP
40 Resolvers []string `json:"resolvers,omitempty"` // Array of IP addresses for resolvers
41 Routes map[string]string `json:"routes,omitempty"` // Map of CIDR block to Gateway IP Address
42 InternetNAT bool `json:"internet_nat"` // If a NAT zone is provisioned at Gateway IP Address
43 }
44
45 // ListFabricVLANs lists VLANs
46 // See API docs: https://apidocs.joyent.com/cloudapi/#ListFabricVLANs
47 func (c *Client) ListFabricVLANs() ([]FabricVLAN, error) {
48 var resp []FabricVLAN
49 req := request{
50 method: client.GET,
51 url: apiFabricVLANs,
52 resp: &resp,
53 }
54 if _, err := c.sendRequest(req); err != nil {
55 return nil, errors.Newf(err, "failed to get list of fabric VLANs")
56 }
57 return resp, nil
58 }
59
60 // GetFabricLAN retrieves a single VLAN by ID
61 // See API docs: https://apidocs.joyent.com/cloudapi/#GetFabricVLAN
62 func (c *Client) GetFabricVLAN(vlanID int16) (*FabricVLAN, error) {
63 var resp FabricVLAN
64 req := request{
65 method: client.GET,
66 url: makeURL(apiFabricVLANs, strconv.Itoa(int(vlanID))),
67 resp: &resp,
68 }
69 if _, err := c.sendRequest(req); err != nil {
70 return nil, errors.Newf(err, "failed to get fabric VLAN with id %d", vlanID)
71 }
72 return &resp, nil
73 }
74
75 // CreateFabricVLAN creates a new VLAN with the specified options
76 // See API docs: https://apidocs.joyent.com/cloudapi/#CreateFabricVLAN
77 func (c *Client) CreateFabricVLAN(vlan FabricVLAN) (*FabricVLAN, error) {
78 var resp FabricVLAN
79 req := request{
80 method: client.POST,
81 url: apiFabricVLANs,
82 reqValue: vlan,
83 resp: &resp,
84 expectedStatus: http.StatusCreated,
85 }
86 if _, err := c.sendRequest(req); err != nil {
87 return nil, errors.Newf(err, "failed to create fabric VLAN: %d - %s", vlan.Id, vlan.Name)
88 }
89 return &resp, nil
90 }
91
92 // UpdateFabricVLAN updates a given VLAN with new fields
93 // See API docs: https://apidocs.joyent.com/cloudapi/#UpdateFabricVLAN
94 func (c *Client) UpdateFabricVLAN(vlan FabricVLAN) (*FabricVLAN, error) {
95 var resp FabricVLAN
96 req := request{
97 method: client.PUT,
98 url: makeURL(apiFabricVLANs, strconv.Itoa(int(vlan.Id))),
99 reqValue: vlan,
100 resp: &resp,
101 expectedStatus: http.StatusAccepted,
102 }
103 if _, err := c.sendRequest(req); err != nil {
104 return nil, errors.Newf(err, "failed to update fabric VLAN with id %d to %s - %s", vlan.Id, vlan.Name, vlan.Description)
105 }
106 return &resp, nil
107 }
108
109 // DeleteFabricVLAN delets a given VLAN as specified by ID
110 // See API docs: https://apidocs.joyent.com/cloudapi/#DeleteFabricVLAN
111 func (c *Client) DeleteFabricVLAN(vlanID int16) error {
112 req := request{
113 method: client.DELETE,
114 url: makeURL(apiFabricVLANs, strconv.Itoa(int(vlanID))),
115 expectedStatus: http.StatusNoContent,
116 }
117 if _, err := c.sendRequest(req); err != nil {
118 return errors.Newf(err, "failed to delete fabric VLAN with id %d", vlanID)
119 }
120 return nil
121 }
122
123 // ListFabricNetworks lists the networks inside the given VLAN
124 // See API docs: https://apidocs.joyent.com/cloudapi/#ListFabricNetworks
125 func (c *Client) ListFabricNetworks(vlanID int16) ([]FabricNetwork, error) {
126 var resp []FabricNetwork
127 req := request{
128 method: client.GET,
129 url: makeURL(apiFabricVLANs, strconv.Itoa(int(vlanID)), apiFabricNetworks),
130 resp: &resp,
131 }
132 if _, err := c.sendRequest(req); err != nil {
133 return nil, errors.Newf(err, "failed to get list of networks on fabric %d", vlanID)
134 }
135 return resp, nil
136 }
137
138 // GetFabricNetwork gets a single network by VLAN and Network IDs
139 // See API docs: https://apidocs.joyent.com/cloudapi/#GetFabricNetwork
140 func (c *Client) GetFabricNetwork(vlanID int16, networkID string) (*FabricNetwork, error) {
141 var resp FabricNetwork
142 req := request{
143 method: client.GET,
144 url: makeURL(apiFabricVLANs, strconv.Itoa(int(vlanID)), apiFabricNetworks, networkID),
145 resp: &resp,
146 }
147 if _, err := c.sendRequest(req); err != nil {
148 return nil, errors.Newf(err, "failed to get fabric network %s on vlan %d", networkID, vlanID)
149 }
150 return &resp, nil
151 }
152
153 // CreateFabricNetwork creates a new fabric network
154 // See API docs: https://apidocs.joyent.com/cloudapi/#CreateFabricNetwork
155 func (c *Client) CreateFabricNetwork(vlanID int16, opts CreateFabricNetworkOpts) (*FabricNetwork, error) {
156 var resp FabricNetwork
157 req := request{
158 method: client.POST,
159 url: makeURL(apiFabricVLANs, strconv.Itoa(int(vlanID)), apiFabricNetworks),
160 reqValue: opts,
161 resp: &resp,
162 expectedStatus: http.StatusCreated,
163 }
164 if _, err := c.sendRequest(req); err != nil {
165 return nil, errors.Newf(err, "failed to create fabric network %s on vlan %d", opts.Name, vlanID)
166 }
167 return &resp, nil
168 }
169
170 // DeleteFabricNetwork deletes an existing fabric network
171 // See API docs: https://apidocs.joyent.com/cloudapi/#DeleteFabricNetwork
172 func (c *Client) DeleteFabricNetwork(vlanID int16, networkID string) error {
173 req := request{
174 method: client.DELETE,
175 url: makeURL(apiFabricVLANs, strconv.Itoa(int(vlanID)), apiFabricNetworks, networkID),
176 expectedStatus: http.StatusNoContent,
177 }
178 if _, err := c.sendRequest(req); err != nil {
179 return errors.Newf(err, "failed to delete fabric network %s on vlan %d", networkID, vlanID)
180 }
181 return nil
182 }