2018-01-16 12:48:25 -05:00
|
|
|
//
|
|
|
|
// Copyright (c) 2018, Joyent, Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
//
|
|
|
|
|
2017-10-31 11:02:15 -04:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
2018-01-16 12:48:25 -05:00
|
|
|
"context"
|
2017-10-31 11:02:15 -04:00
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
2018-01-16 12:48:25 -05:00
|
|
|
"path"
|
|
|
|
"strconv"
|
2017-10-31 11:02:15 -04:00
|
|
|
|
|
|
|
"github.com/joyent/triton-go/client"
|
2018-01-16 12:48:25 -05:00
|
|
|
"github.com/pkg/errors"
|
2017-10-31 11:02:15 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type FabricsClient struct {
|
|
|
|
client *client.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
type FabricVLAN struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
ID int `json:"vlan_id"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ListVLANsInput struct{}
|
|
|
|
|
|
|
|
func (c *FabricsClient) ListVLANs(ctx context.Context, _ *ListVLANsInput) ([]*FabricVLAN, error) {
|
2018-01-16 12:48:25 -05:00
|
|
|
fullPath := path.Join("/", c.client.AccountName, "fabrics", "default", "vlans")
|
2017-10-31 11:02:15 -04:00
|
|
|
reqInputs := client.RequestInput{
|
|
|
|
Method: http.MethodGet,
|
2018-01-16 12:48:25 -05:00
|
|
|
Path: fullPath,
|
2017-10-31 11:02:15 -04:00
|
|
|
}
|
|
|
|
respReader, err := c.client.ExecuteRequest(ctx, reqInputs)
|
|
|
|
if respReader != nil {
|
|
|
|
defer respReader.Close()
|
|
|
|
}
|
|
|
|
if err != nil {
|
2018-01-16 12:48:25 -05:00
|
|
|
return nil, errors.Wrap(err, "unable to list VLANs")
|
2017-10-31 11:02:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var result []*FabricVLAN
|
|
|
|
decoder := json.NewDecoder(respReader)
|
|
|
|
if err = decoder.Decode(&result); err != nil {
|
2018-01-16 12:48:25 -05:00
|
|
|
return nil, errors.Wrap(err, "unable to decode list VLANs response")
|
2017-10-31 11:02:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type CreateVLANInput struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
ID int `json:"vlan_id"`
|
|
|
|
Description string `json:"description,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *FabricsClient) CreateVLAN(ctx context.Context, input *CreateVLANInput) (*FabricVLAN, error) {
|
2018-01-16 12:48:25 -05:00
|
|
|
fullPath := path.Join("/", c.client.AccountName, "fabrics", "default", "vlans")
|
2017-10-31 11:02:15 -04:00
|
|
|
reqInputs := client.RequestInput{
|
|
|
|
Method: http.MethodPost,
|
2018-01-16 12:48:25 -05:00
|
|
|
Path: fullPath,
|
2017-10-31 11:02:15 -04:00
|
|
|
Body: input,
|
|
|
|
}
|
|
|
|
respReader, err := c.client.ExecuteRequest(ctx, reqInputs)
|
|
|
|
if respReader != nil {
|
|
|
|
defer respReader.Close()
|
|
|
|
}
|
|
|
|
if err != nil {
|
2018-01-16 12:48:25 -05:00
|
|
|
return nil, errors.Wrap(err, "unable to create VLAN")
|
2017-10-31 11:02:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var result *FabricVLAN
|
|
|
|
decoder := json.NewDecoder(respReader)
|
|
|
|
if err = decoder.Decode(&result); err != nil {
|
2018-01-16 12:48:25 -05:00
|
|
|
return nil, errors.Wrap(err, "unable to decode create VLAN response")
|
2017-10-31 11:02:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type UpdateVLANInput struct {
|
|
|
|
ID int `json:"-"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *FabricsClient) UpdateVLAN(ctx context.Context, input *UpdateVLANInput) (*FabricVLAN, error) {
|
2018-01-16 12:48:25 -05:00
|
|
|
fullPath := path.Join("/", c.client.AccountName, "fabrics", "default", "vlans", strconv.Itoa(input.ID))
|
2017-10-31 11:02:15 -04:00
|
|
|
reqInputs := client.RequestInput{
|
|
|
|
Method: http.MethodPut,
|
2018-01-16 12:48:25 -05:00
|
|
|
Path: fullPath,
|
2017-10-31 11:02:15 -04:00
|
|
|
Body: input,
|
|
|
|
}
|
|
|
|
respReader, err := c.client.ExecuteRequest(ctx, reqInputs)
|
|
|
|
if respReader != nil {
|
|
|
|
defer respReader.Close()
|
|
|
|
}
|
|
|
|
if err != nil {
|
2018-01-16 12:48:25 -05:00
|
|
|
return nil, errors.Wrap(err, "unable to update VLAN")
|
2017-10-31 11:02:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var result *FabricVLAN
|
|
|
|
decoder := json.NewDecoder(respReader)
|
|
|
|
if err = decoder.Decode(&result); err != nil {
|
2018-01-16 12:48:25 -05:00
|
|
|
return nil, errors.Wrap(err, "unable to decode update VLAN response")
|
2017-10-31 11:02:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type GetVLANInput struct {
|
|
|
|
ID int `json:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *FabricsClient) GetVLAN(ctx context.Context, input *GetVLANInput) (*FabricVLAN, error) {
|
2018-01-16 12:48:25 -05:00
|
|
|
fullPath := path.Join("/", c.client.AccountName, "fabrics", "default", "vlans", strconv.Itoa(input.ID))
|
2017-10-31 11:02:15 -04:00
|
|
|
reqInputs := client.RequestInput{
|
|
|
|
Method: http.MethodGet,
|
2018-01-16 12:48:25 -05:00
|
|
|
Path: fullPath,
|
2017-10-31 11:02:15 -04:00
|
|
|
}
|
|
|
|
respReader, err := c.client.ExecuteRequest(ctx, reqInputs)
|
|
|
|
if respReader != nil {
|
|
|
|
defer respReader.Close()
|
|
|
|
}
|
|
|
|
if err != nil {
|
2018-01-16 12:48:25 -05:00
|
|
|
return nil, errors.Wrap(err, "unable to get VLAN")
|
2017-10-31 11:02:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var result *FabricVLAN
|
|
|
|
decoder := json.NewDecoder(respReader)
|
|
|
|
if err = decoder.Decode(&result); err != nil {
|
2018-01-16 12:48:25 -05:00
|
|
|
return nil, errors.Wrap(err, "unable to decode get VLAN response")
|
2017-10-31 11:02:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type DeleteVLANInput struct {
|
|
|
|
ID int `json:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *FabricsClient) DeleteVLAN(ctx context.Context, input *DeleteVLANInput) error {
|
2018-01-16 12:48:25 -05:00
|
|
|
fullPath := path.Join("/", c.client.AccountName, "fabrics", "default", "vlans", strconv.Itoa(input.ID))
|
2017-10-31 11:02:15 -04:00
|
|
|
reqInputs := client.RequestInput{
|
|
|
|
Method: http.MethodDelete,
|
2018-01-16 12:48:25 -05:00
|
|
|
Path: fullPath,
|
2017-10-31 11:02:15 -04:00
|
|
|
}
|
|
|
|
respReader, err := c.client.ExecuteRequest(ctx, reqInputs)
|
|
|
|
if respReader != nil {
|
|
|
|
defer respReader.Close()
|
|
|
|
}
|
|
|
|
if err != nil {
|
2018-01-16 12:48:25 -05:00
|
|
|
return errors.Wrap(err, "unable to delete VLAN")
|
2017-10-31 11:02:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type ListFabricsInput struct {
|
|
|
|
FabricVLANID int `json:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *FabricsClient) List(ctx context.Context, input *ListFabricsInput) ([]*Network, error) {
|
2018-01-16 12:48:25 -05:00
|
|
|
fullPath := path.Join("/", c.client.AccountName, "fabrics", "default", "vlans", strconv.Itoa(input.FabricVLANID), "networks")
|
2017-10-31 11:02:15 -04:00
|
|
|
reqInputs := client.RequestInput{
|
|
|
|
Method: http.MethodGet,
|
2018-01-16 12:48:25 -05:00
|
|
|
Path: fullPath,
|
2017-10-31 11:02:15 -04:00
|
|
|
}
|
|
|
|
respReader, err := c.client.ExecuteRequest(ctx, reqInputs)
|
|
|
|
if respReader != nil {
|
|
|
|
defer respReader.Close()
|
|
|
|
}
|
|
|
|
if err != nil {
|
2018-01-16 12:48:25 -05:00
|
|
|
return nil, errors.Wrap(err, "unable to list fabrics")
|
2017-10-31 11:02:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var result []*Network
|
|
|
|
decoder := json.NewDecoder(respReader)
|
|
|
|
if err = decoder.Decode(&result); err != nil {
|
2018-01-16 12:48:25 -05:00
|
|
|
return nil, errors.Wrap(err, "unable to decode list fabrics response")
|
2017-10-31 11:02:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type CreateFabricInput struct {
|
|
|
|
FabricVLANID int `json:"-"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Description string `json:"description,omitempty"`
|
|
|
|
Subnet string `json:"subnet"`
|
|
|
|
ProvisionStartIP string `json:"provision_start_ip"`
|
|
|
|
ProvisionEndIP string `json:"provision_end_ip"`
|
|
|
|
Gateway string `json:"gateway,omitempty"`
|
|
|
|
Resolvers []string `json:"resolvers,omitempty"`
|
|
|
|
Routes map[string]string `json:"routes,omitempty"`
|
|
|
|
InternetNAT bool `json:"internet_nat"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *FabricsClient) Create(ctx context.Context, input *CreateFabricInput) (*Network, error) {
|
2018-01-16 12:48:25 -05:00
|
|
|
fullPath := path.Join("/", c.client.AccountName, "fabrics", "default", "vlans", strconv.Itoa(input.FabricVLANID), "networks")
|
2017-10-31 11:02:15 -04:00
|
|
|
reqInputs := client.RequestInput{
|
|
|
|
Method: http.MethodPost,
|
2018-01-16 12:48:25 -05:00
|
|
|
Path: fullPath,
|
2017-10-31 11:02:15 -04:00
|
|
|
Body: input,
|
|
|
|
}
|
|
|
|
respReader, err := c.client.ExecuteRequest(ctx, reqInputs)
|
|
|
|
if respReader != nil {
|
|
|
|
defer respReader.Close()
|
|
|
|
}
|
|
|
|
if err != nil {
|
2018-01-16 12:48:25 -05:00
|
|
|
return nil, errors.Wrap(err, "unable to create fabric")
|
2017-10-31 11:02:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var result *Network
|
|
|
|
decoder := json.NewDecoder(respReader)
|
|
|
|
if err = decoder.Decode(&result); err != nil {
|
2018-01-16 12:48:25 -05:00
|
|
|
return nil, errors.Wrap(err, "unable to decode create fabric response")
|
2017-10-31 11:02:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type GetFabricInput struct {
|
|
|
|
FabricVLANID int `json:"-"`
|
|
|
|
NetworkID string `json:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *FabricsClient) Get(ctx context.Context, input *GetFabricInput) (*Network, error) {
|
2018-01-16 12:48:25 -05:00
|
|
|
fullPath := path.Join("/", c.client.AccountName, "fabrics", "default", "vlans", strconv.Itoa(input.FabricVLANID), "networks", input.NetworkID)
|
2017-10-31 11:02:15 -04:00
|
|
|
reqInputs := client.RequestInput{
|
|
|
|
Method: http.MethodGet,
|
2018-01-16 12:48:25 -05:00
|
|
|
Path: fullPath,
|
2017-10-31 11:02:15 -04:00
|
|
|
}
|
|
|
|
respReader, err := c.client.ExecuteRequest(ctx, reqInputs)
|
|
|
|
if respReader != nil {
|
|
|
|
defer respReader.Close()
|
|
|
|
}
|
|
|
|
if err != nil {
|
2018-01-16 12:48:25 -05:00
|
|
|
return nil, errors.Wrap(err, "unable to get fabric")
|
2017-10-31 11:02:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var result *Network
|
|
|
|
decoder := json.NewDecoder(respReader)
|
|
|
|
if err = decoder.Decode(&result); err != nil {
|
2018-01-16 12:48:25 -05:00
|
|
|
return nil, errors.Wrap(err, "unable to decode get fabric response")
|
2017-10-31 11:02:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type DeleteFabricInput struct {
|
|
|
|
FabricVLANID int `json:"-"`
|
|
|
|
NetworkID string `json:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *FabricsClient) Delete(ctx context.Context, input *DeleteFabricInput) error {
|
2018-01-16 12:48:25 -05:00
|
|
|
fullPath := path.Join("/", c.client.AccountName, "fabrics", "default", "vlans", strconv.Itoa(input.FabricVLANID), "networks", input.NetworkID)
|
2017-10-31 11:02:15 -04:00
|
|
|
reqInputs := client.RequestInput{
|
|
|
|
Method: http.MethodDelete,
|
2018-01-16 12:48:25 -05:00
|
|
|
Path: fullPath,
|
2017-10-31 11:02:15 -04:00
|
|
|
}
|
|
|
|
respReader, err := c.client.ExecuteRequest(ctx, reqInputs)
|
|
|
|
if respReader != nil {
|
|
|
|
defer respReader.Close()
|
|
|
|
}
|
|
|
|
if err != nil {
|
2018-01-16 12:48:25 -05:00
|
|
|
return errors.Wrap(err, "unable to delete fabric")
|
2017-10-31 11:02:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|