| 1 |
package cloudapi |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"time" |
| 6 |
|
| 7 |
"github.com/joyent/gosdc/cloudapi" |
| 8 |
"github.com/joyent/gosdc/localservices" |
| 9 |
) |
| 10 |
|
| 11 |
func (c *CloudAPI) ListNICs(machineID string) ([]cloudapi.NIC, error) { |
| 12 |
machine, err := c.getMachineWrapper(machineID) |
| 13 |
if err != nil { |
| 14 |
return nil, err |
| 15 |
} |
| 16 |
|
| 17 |
out := []cloudapi.NIC{} |
| 18 |
for _, nic := range machine.NICs { |
| 19 |
out = append(out, *nic) |
| 20 |
} |
| 21 |
|
| 22 |
return out, nil |
| 23 |
} |
| 24 |
|
| 25 |
func (c *CloudAPI) GetNIC(machineID, MAC string) (*cloudapi.NIC, error) { |
| 26 |
machine, err := c.getMachineWrapper(machineID) |
| 27 |
if err != nil { |
| 28 |
return nil, err |
| 29 |
} |
| 30 |
|
| 31 |
nic, present := machine.NICs[MAC] |
| 32 |
if !present { |
| 33 |
return nil, fmt.Errorf("NIC with MAC %s not found", MAC) |
| 34 |
} |
| 35 |
|
| 36 |
return nic, nil |
| 37 |
} |
| 38 |
|
| 39 |
func (c *CloudAPI) AddNIC(machineID, networkID string) (*cloudapi.NIC, error) { |
| 40 |
machine, err := c.getMachineWrapper(machineID) |
| 41 |
if err != nil { |
| 42 |
return nil, err |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
_, err = c.GetNetwork(networkID) |
| 47 |
if err != nil { |
| 48 |
return nil, err |
| 49 |
} |
| 50 |
|
| 51 |
found := false |
| 52 |
for _, network := range machine.Networks { |
| 53 |
if network == networkID { |
| 54 |
found = true |
| 55 |
} |
| 56 |
} |
| 57 |
if found { |
| 58 |
return nil, fmt.Errorf("Machine %s is already in network %s", machineID, networkID) |
| 59 |
} |
| 60 |
|
| 61 |
mac, err := localservices.NewMAC() |
| 62 |
if err != nil { |
| 63 |
return nil, err |
| 64 |
} |
| 65 |
|
| 66 |
machine.NICs[mac] = &cloudapi.NIC{ |
| 67 |
IP: "10.88.88.100", |
| 68 |
MAC: mac, |
| 69 |
Primary: false, |
| 70 |
Netmask: "255.255.255.0", |
| 71 |
Gateway: "10.88.88.2", |
| 72 |
State: cloudapi.NICStateRunning, |
| 73 |
Network: networkID, |
| 74 |
} |
| 75 |
machine.Updated = time.Now().Format("2013-11-26T19:47:13.448Z") |
| 76 |
machine.Networks = append(machine.Networks, networkID) |
| 77 |
machine.NetworkNICs[mac] = networkID |
| 78 |
|
| 79 |
return machine.NICs[mac], nil |
| 80 |
} |
| 81 |
|
| 82 |
func (c *CloudAPI) RemoveNIC(machineID, MAC string) error { |
| 83 |
machine, err := c.getMachineWrapper(machineID) |
| 84 |
if err != nil { |
| 85 |
return err |
| 86 |
} |
| 87 |
|
| 88 |
_, present := machine.NICs[MAC] |
| 89 |
if !present { |
| 90 |
return fmt.Errorf("NIC with MAC %s not found", MAC) |
| 91 |
} |
| 92 |
|
| 93 |
machine.Updated = time.Now().Format("2013-11-26T19:47:13.448Z") |
| 94 |
destNetwork := machine.NetworkNICs[MAC] |
| 95 |
|
| 96 |
for i, network := range machine.Networks { |
| 97 |
if network == destNetwork { |
| 98 |
machine.Networks = append(machine.Networks[:i], machine.Networks[i+1:]...) |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
delete(machine.NICs, MAC) |
| 103 |
delete(machine.NetworkNICs, MAC) |
| 104 |
|
| 105 |
return nil |
| 106 |
} |