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
|
2017-04-26 15:12:57 -04:00
|
|
|
|
|
|
|
import (
|
2017-10-31 11:02:15 -04:00
|
|
|
"context"
|
2017-04-26 15:12:57 -04:00
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
2018-01-16 12:48:25 -05:00
|
|
|
"path"
|
2017-04-26 15:12:57 -04:00
|
|
|
|
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-04-26 15:12:57 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Network struct {
|
|
|
|
Id string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Public bool `json:"public"`
|
|
|
|
Fabric bool `json:"fabric"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
Subnet string `json:"subnet"`
|
|
|
|
ProvisioningStartIP string `json:"provision_start_ip"`
|
|
|
|
ProvisioningEndIP string `json:"provision_end_ip"`
|
|
|
|
Gateway string `json:"gateway"`
|
|
|
|
Resolvers []string `json:"resolvers"`
|
|
|
|
Routes map[string]string `json:"routes"`
|
|
|
|
InternetNAT bool `json:"internet_nat"`
|
|
|
|
}
|
|
|
|
|
2017-10-31 11:02:15 -04:00
|
|
|
type ListInput struct{}
|
2017-04-26 15:12:57 -04:00
|
|
|
|
2017-10-31 11:02:15 -04:00
|
|
|
func (c *NetworkClient) List(ctx context.Context, _ *ListInput) ([]*Network, error) {
|
2018-01-16 12:48:25 -05:00
|
|
|
fullPath := path.Join("/", c.Client.AccountName, "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)
|
2017-04-26 15:12:57 -04:00
|
|
|
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 networks")
|
2017-04-26 15:12:57 -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 networks response")
|
2017-04-26 15:12:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2017-10-31 11:02:15 -04:00
|
|
|
type GetInput struct {
|
2017-04-26 15:12:57 -04:00
|
|
|
ID string
|
|
|
|
}
|
|
|
|
|
2017-10-31 11:02:15 -04:00
|
|
|
func (c *NetworkClient) Get(ctx context.Context, input *GetInput) (*Network, error) {
|
2018-01-16 12:48:25 -05:00
|
|
|
fullPath := path.Join("/", c.Client.AccountName, "networks", 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)
|
2017-04-26 15:12:57 -04:00
|
|
|
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 network")
|
2017-04-26 15:12:57 -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 network response")
|
2017-04-26 15:12:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|