2017-04-26 15:12:57 -04:00
|
|
|
package triton
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2017-05-05 10:00:09 -04:00
|
|
|
"fmt"
|
2017-04-26 15:12:57 -04:00
|
|
|
"net/http"
|
|
|
|
|
2017-05-05 10:00:09 -04:00
|
|
|
"context"
|
2017-04-26 15:12:57 -04:00
|
|
|
"github.com/hashicorp/errwrap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type NetworksClient struct {
|
|
|
|
*Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// Networks returns a c used for accessing functions pertaining to
|
|
|
|
// Network functionality in the Triton API.
|
|
|
|
func (c *Client) Networks() *NetworksClient {
|
|
|
|
return &NetworksClient{c}
|
|
|
|
}
|
|
|
|
|
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ListNetworksInput struct{}
|
|
|
|
|
2017-05-05 10:00:09 -04:00
|
|
|
func (client *NetworksClient) ListNetworks(ctx context.Context, _ *ListNetworksInput) ([]*Network, error) {
|
2017-04-26 15:12:57 -04:00
|
|
|
path := fmt.Sprintf("/%s/networks", client.accountName)
|
2017-05-05 10:00:09 -04:00
|
|
|
respReader, err := client.executeRequest(ctx, http.MethodGet, path, nil)
|
2017-04-26 15:12:57 -04:00
|
|
|
if respReader != nil {
|
|
|
|
defer respReader.Close()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, errwrap.Wrapf("Error executing ListNetworks request: {{err}}", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var result []*Network
|
|
|
|
decoder := json.NewDecoder(respReader)
|
|
|
|
if err = decoder.Decode(&result); err != nil {
|
|
|
|
return nil, errwrap.Wrapf("Error decoding ListNetworks response: {{err}}", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type GetNetworkInput struct {
|
|
|
|
ID string
|
|
|
|
}
|
|
|
|
|
2017-05-05 10:00:09 -04:00
|
|
|
func (client *NetworksClient) GetNetwork(ctx context.Context, input *GetNetworkInput) (*Network, error) {
|
2017-04-26 15:12:57 -04:00
|
|
|
path := fmt.Sprintf("/%s/networks/%s", client.accountName, input.ID)
|
2017-05-05 10:00:09 -04:00
|
|
|
respReader, err := client.executeRequest(ctx, http.MethodGet, path, nil)
|
2017-04-26 15:12:57 -04:00
|
|
|
if respReader != nil {
|
|
|
|
defer respReader.Close()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, errwrap.Wrapf("Error executing GetNetwork request: {{err}}", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var result *Network
|
|
|
|
decoder := json.NewDecoder(respReader)
|
|
|
|
if err = decoder.Decode(&result); err != nil {
|
|
|
|
return nil, errwrap.Wrapf("Error decoding GetNetwork response: {{err}}", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|