2
// goamz - Go packages to interact with the Amazon Web Services.
4
// https://wiki.ubuntu.com/goamz
6
// Copyright (c) 2011 Canonical Ltd.
8
// This file contains code handling AWS API around private IP
9
// addresses for Elastic Network Interfaces.
22
func (srv *Server) addPrivateIPs(nic *iface, numToAdd int, addrs []string) error {
23
for _, addr := range addrs {
24
newIP := ec2.PrivateIP{Address: addr, IsPrimary: false}
25
nic.PrivateIPs = append(nic.PrivateIPs, newIP)
28
// Nothing more to do.
32
if len(nic.PrivateIPs) > 0 {
33
firstIP = nic.PrivateIPs[len(nic.PrivateIPs)-1].Address
35
// Find the primary IP, if available, otherwise use
36
// the subnet CIDR to generate a valid IP to use.
37
firstIP = nic.PrivateIPAddress
39
sub := srv.subnets[nic.SubnetId]
41
return fmt.Errorf("NIC %q uses invalid subnet id: %v", nic.Id, nic.SubnetId)
43
netIP, _, err := net.ParseCIDR(sub.CIDRBlock)
45
return fmt.Errorf("subnet %q has bad CIDR: %v", sub.Id, err)
47
firstIP = netIP.String()
50
ip := net.ParseIP(firstIP)
51
for i := 0; i < numToAdd; i++ {
53
newIP := ec2.PrivateIP{Address: ip.String(), IsPrimary: false}
54
nic.PrivateIPs = append(nic.PrivateIPs, newIP)
59
func (srv *Server) removePrivateIP(nic *iface, addr string) error {
60
for i, privateIP := range nic.PrivateIPs {
61
if privateIP.Address == addr {
62
// Remove it, preserving order.
63
nic.PrivateIPs = append(nic.PrivateIPs[:i], nic.PrivateIPs[i+1:]...)
67
return fmt.Errorf("NIC %q does not have IP %q to remove", nic.Id, addr)
70
func (srv *Server) assignPrivateIP(w http.ResponseWriter, req *http.Request, reqId string) interface{} {
71
nic := srv.iface(req.Form.Get("NetworkInterfaceId"))
72
extraIPs := parseInOrder(req.Form, "PrivateIpAddress.")
73
count := req.Form.Get("SecondaryPrivateIpAddressCount")
76
secondaryIPs = atoi(count)
82
err := srv.addPrivateIPs(nic, secondaryIPs, extraIPs)
84
fatalf(400, "InvalidParameterValue", err.Error())
86
return &ec2.SimpleResp{
87
XMLName: xml.Name{defaultXMLName, "AssignPrivateIpAddresses"},
93
func (srv *Server) unassignPrivateIP(w http.ResponseWriter, req *http.Request, reqId string) interface{} {
94
nic := srv.iface(req.Form.Get("NetworkInterfaceId"))
95
ips := parseInOrder(req.Form, "PrivateIpAddress.")
100
for _, ip := range ips {
101
if err := srv.removePrivateIP(nic, ip); err != nil {
102
fatalf(400, "InvalidParameterValue", err.Error())
105
return &ec2.SimpleResp{
106
XMLName: xml.Name{defaultXMLName, "UnassignPrivateIpAddresses"},