~nskaggs/+junk/juju-packaging-test

« back to all changes in this revision

Viewing changes to src/gopkg.in/amz.v3/ec2/ec2test/private_ips.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-27 20:23:11 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161027202311-sux4jk2o73p1d6rg
Re-add src

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// goamz - Go packages to interact with the Amazon Web Services.
 
3
//
 
4
//   https://wiki.ubuntu.com/goamz
 
5
//
 
6
// Copyright (c) 2011 Canonical Ltd.
 
7
//
 
8
// This file contains code handling AWS API around private IP
 
9
// addresses for Elastic Network Interfaces.
 
10
 
 
11
package ec2test
 
12
 
 
13
import (
 
14
        "encoding/xml"
 
15
        "fmt"
 
16
        "net"
 
17
        "net/http"
 
18
 
 
19
        "gopkg.in/amz.v3/ec2"
 
20
)
 
21
 
 
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)
 
26
        }
 
27
        if numToAdd == 0 {
 
28
                // Nothing more to do.
 
29
                return nil
 
30
        }
 
31
        firstIP := ""
 
32
        if len(nic.PrivateIPs) > 0 {
 
33
                firstIP = nic.PrivateIPs[len(nic.PrivateIPs)-1].Address
 
34
        } else {
 
35
                // Find the primary IP, if available, otherwise use
 
36
                // the subnet CIDR to generate a valid IP to use.
 
37
                firstIP = nic.PrivateIPAddress
 
38
                if firstIP == "" {
 
39
                        sub := srv.subnets[nic.SubnetId]
 
40
                        if sub == nil {
 
41
                                return fmt.Errorf("NIC %q uses invalid subnet id: %v", nic.Id, nic.SubnetId)
 
42
                        }
 
43
                        netIP, _, err := net.ParseCIDR(sub.CIDRBlock)
 
44
                        if err != nil {
 
45
                                return fmt.Errorf("subnet %q has bad CIDR: %v", sub.Id, err)
 
46
                        }
 
47
                        firstIP = netIP.String()
 
48
                }
 
49
        }
 
50
        ip := net.ParseIP(firstIP)
 
51
        for i := 0; i < numToAdd; i++ {
 
52
                ip[len(ip)-1] += 1
 
53
                newIP := ec2.PrivateIP{Address: ip.String(), IsPrimary: false}
 
54
                nic.PrivateIPs = append(nic.PrivateIPs, newIP)
 
55
        }
 
56
        return nil
 
57
}
 
58
 
 
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:]...)
 
64
                        return nil
 
65
                }
 
66
        }
 
67
        return fmt.Errorf("NIC %q does not have IP %q to remove", nic.Id, addr)
 
68
}
 
69
 
 
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")
 
74
        secondaryIPs := 0
 
75
        if count != "" {
 
76
                secondaryIPs = atoi(count)
 
77
        }
 
78
 
 
79
        srv.mu.Lock()
 
80
        defer srv.mu.Unlock()
 
81
 
 
82
        err := srv.addPrivateIPs(nic, secondaryIPs, extraIPs)
 
83
        if err != nil {
 
84
                fatalf(400, "InvalidParameterValue", err.Error())
 
85
        }
 
86
        return &ec2.SimpleResp{
 
87
                XMLName:   xml.Name{defaultXMLName, "AssignPrivateIpAddresses"},
 
88
                RequestId: reqId,
 
89
                Return:    true,
 
90
        }
 
91
}
 
92
 
 
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.")
 
96
 
 
97
        srv.mu.Lock()
 
98
        defer srv.mu.Unlock()
 
99
 
 
100
        for _, ip := range ips {
 
101
                if err := srv.removePrivateIP(nic, ip); err != nil {
 
102
                        fatalf(400, "InvalidParameterValue", err.Error())
 
103
                }
 
104
        }
 
105
        return &ec2.SimpleResp{
 
106
                XMLName:   xml.Name{defaultXMLName, "UnassignPrivateIpAddresses"},
 
107
                RequestId: reqId,
 
108
                Return:    true,
 
109
        }
 
110
}