| 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"` |
| 13 |
Name string `json:"name"` |
| 14 |
Description string `json:"description,omitempty"` |
| 15 |
} |
| 16 |
|
| 17 |
type FabricNetwork struct { |
| 18 |
Id string `json:"id"` |
| 19 |
Name string `json:"name"` |
| 20 |
Public bool `json:"public"` |
| 21 |
Fabric bool `json:"fabric"` |
| 22 |
Description string `json:"description"` |
| 23 |
Subnet string `json:"subnet"` |
| 24 |
ProvisionStartIp string `json:"provision_start_ip"` |
| 25 |
ProvisionEndIp string `json:"provision_end_ip"` |
| 26 |
Gateway string `json:"gateway"` |
| 27 |
Resolvers []string `json:"resolvers,omitempty"` |
| 28 |
Routes map[string]string `json:"routes,omitempty"` |
| 29 |
InternetNAT bool `json:"internet_nat"` |
| 30 |
VLANId int16 `json:"vlan_id"` |
| 31 |
} |
| 32 |
|
| 33 |
type CreateFabricNetworkOpts struct { |
| 34 |
Name string `json:"name"` |
| 35 |
Description string `json:"description,omitempty"` |
| 36 |
Subnet string `json:"subnet"` |
| 37 |
ProvisionStartIp string `json:"provision_start_ip"` |
| 38 |
ProvisionEndIp string `json:"provision_end_ip"` |
| 39 |
Gateway string `json:"gateway,omitempty"` |
| 40 |
Resolvers []string `json:"resolvers,omitempty"` |
| 41 |
Routes map[string]string `json:"routes,omitempty"` |
| 42 |
InternetNAT bool `json:"internet_nat"` |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 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 |
|
| 61 |
|
| 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 |
|
| 76 |
|
| 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 |
|
| 93 |
|
| 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 |
|
| 110 |
|
| 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 |
|
| 124 |
|
| 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 |
|
| 139 |
|
| 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 |
|
| 154 |
|
| 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 |
|
| 171 |
|
| 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 |
} |