Debian Codegraph @

cloudapi/machine_nics.go

raw symbols SCIP
1 package cloudapi
2
3 import (
4 "net/http"
5
6 "github.com/joyent/gocommon/client"
7 "github.com/joyent/gocommon/errors"
8 )
9
10 // NICState represents the state of a NIC
11 type NICState string
12
13 var (
14 NICStateProvisioning NICState = "provisioning"
15 NICStateRunning NICState = "running"
16 NICStateStopped NICState = "stopped"
17 )
18
19 // NIC represents a NIC on a machine
20 type NIC struct {
21 IP string `json:"ip"` // NIC's IPv4 Address
22 MAC string `json:"mac"` // NIC's MAC address
23 Primary bool `json:"primary"` // Whether this is the machine's primary NIC
24 Netmask string `json:"netmask"` // IPv4 netmask
25 Gateway string `json:"gateway"` // IPv4 gateway
26 State NICState `json:"state"` // Describes the state of the NIC (e.g. provisioning, running, or stopped)
27 Network string `json:"network"` // Network ID this NIC is attached to
28 }
29
30 type addNICOptions struct {
31 Network string `json:"network"` // UUID of network this NIC should attach to
32 }
33
34 // ListNICs lists all the NICs on a machine belonging to a given account
35 // See API docs: https://apidocs.joyent.com/cloudapi/#ListNics
36 func (c *Client) ListNICs(machineID string) ([]NIC, error) {
37 var resp []NIC
38 req := request{
39 method: client.GET,
40 url: makeURL(apiMachines, machineID, apiNICs),
41 resp: &resp,
42 }
43 if _, err := c.sendRequest(req); err != nil {
44 return nil, errors.Newf(err, "failed to list NICs")
45 }
46 return resp, nil
47 }
48
49 // GetNIC gets a specific NIC on a machine belonging to a given account
50 // See API docs: https://apidocs.joyent.com/cloudapi/#GetNic
51 func (c *Client) GetNIC(machineID, MAC string) (*NIC, error) {
52 resp := new(NIC)
53 req := request{
54 method: client.GET,
55 url: makeURL(apiMachines, machineID, apiNICs, MAC),
56 resp: resp,
57 }
58 if _, err := c.sendRequest(req); err != nil {
59 return nil, errors.Newf(err, "failed to get NIC with MAC: %s", MAC)
60 }
61 return resp, nil
62 }
63
64 // AddNIC creates a new NIC on a machine belonging to a given account.
65 // *WARNING*: this causes the machine to reboot while adding the NIC.
66 // See API docs: https://apidocs.joyent.com/cloudapi/#AddNic
67 func (c *Client) AddNIC(machineID, networkID string) (*NIC, error) {
68 resp := new(NIC)
69 req := request{
70 method: client.POST,
71 url: makeURL(apiMachines, machineID, apiNICs),
72 reqValue: addNICOptions{networkID},
73 resp: resp,
74 expectedStatus: http.StatusCreated,
75 }
76 if _, err := c.sendRequest(req); err != nil {
77 return nil, errors.Newf(err, "failed to add NIC to machine %s on network: %s", machineID, networkID)
78 }
79 return resp, nil
80 }
81
82 // RemoveNIC removes a NIC on a machine belonging to a given account.
83 // *WARNING*: this causes the machine to reboot while removing the NIC.
84 // See API docs: https://apidocs.joyent.com/cloudapi/#RemoveNic
85 func (c *Client) RemoveNIC(machineID, MAC string) error {
86 req := request{
87 method: client.DELETE,
88 url: makeURL(apiMachines, machineID, apiNICs, MAC),
89 expectedStatus: http.StatusNoContent,
90 }
91 if _, err := c.sendRequest(req); err != nil {
92 return errors.Newf(err, "failed to remove NIC: %s", MAC)
93 }
94 return nil
95 }