~nskaggs/+junk/xenial-test

« back to all changes in this revision

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

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

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) 2015 Canonical Ltd.
 
7
//
 
8
// This file contains code handling AWS API around Internet Gateways.
 
9
 
 
10
package ec2test
 
11
 
 
12
import (
 
13
        "encoding/xml"
 
14
        "fmt"
 
15
        "net/http"
 
16
 
 
17
        "gopkg.in/amz.v3/ec2"
 
18
)
 
19
 
 
20
// AddInternetGateway inserts the given internet gateway in the test
 
21
// server, as if it was created using the simulated AWS API. The Id
 
22
// field of igw is ignored and replaced by the next igwId counter
 
23
// value, prefixed by "igw-". When set, the VPCId field must refer to
 
24
// a VPC the test server knows about. If VPCId is empty the IGW is
 
25
// considered not attached.
 
26
func (srv *Server) AddInternetGateway(igw ec2.InternetGateway) (ec2.InternetGateway, error) {
 
27
        zeroGateway := ec2.InternetGateway{}
 
28
        srv.mu.Lock()
 
29
        defer srv.mu.Unlock()
 
30
        if igw.VPCId != "" {
 
31
                if _, found := srv.vpcs[igw.VPCId]; !found {
 
32
                        return zeroGateway, fmt.Errorf("VPC %q not found", igw.VPCId)
 
33
                }
 
34
        }
 
35
        added := &internetGateway{igw}
 
36
        added.Id = fmt.Sprintf("igw-%d", srv.igwId.next())
 
37
        srv.internetGateways[added.Id] = added
 
38
        return added.InternetGateway, nil
 
39
}
 
40
 
 
41
// UpdateInternetGateway updates the internet gateway info stored in
 
42
// the test server, matching the Id field of igw, replacing all the
 
43
// other values with igw's field values. Both the Id and VPCId fields
 
44
// (the latter when set) must refer to entities known by the test
 
45
// server, otherwise errors are returned. If VPCId is empty, this is
 
46
// treated as if the IGW is not attached to a VPC.
 
47
func (srv *Server) UpdateInternetGateway(igw ec2.InternetGateway) error {
 
48
        if igw.Id == "" {
 
49
                return fmt.Errorf("missing internet gateway id")
 
50
        }
 
51
        srv.mu.Lock()
 
52
        defer srv.mu.Unlock()
 
53
        _, found := srv.internetGateways[igw.Id]
 
54
        if !found {
 
55
                return fmt.Errorf("internet gateway %q not found", igw.Id)
 
56
        }
 
57
        if igw.VPCId != "" {
 
58
                if _, found := srv.vpcs[igw.VPCId]; !found {
 
59
                        return fmt.Errorf("VPC %q not found", igw.VPCId)
 
60
                }
 
61
        }
 
62
        srv.internetGateways[igw.Id] = &internetGateway{igw}
 
63
        return nil
 
64
}
 
65
 
 
66
// RemoveInternetGateway removes the internet gateway with the given
 
67
// igwId, stored in the test server. It's an error to try to remove an
 
68
// unknown or empty igwId.
 
69
func (srv *Server) RemoveInternetGateway(igwId string) error {
 
70
        if igwId == "" {
 
71
                return fmt.Errorf("missing internet gateway id")
 
72
        }
 
73
        srv.mu.Lock()
 
74
        defer srv.mu.Unlock()
 
75
        if _, found := srv.internetGateways[igwId]; found {
 
76
                delete(srv.internetGateways, igwId)
 
77
                return nil
 
78
        }
 
79
        return fmt.Errorf("internet gateway %q not found", igwId)
 
80
}
 
81
 
 
82
type internetGateway struct {
 
83
        ec2.InternetGateway
 
84
}
 
85
 
 
86
func (i *internetGateway) matchAttr(attr, value string) (ok bool, err error) {
 
87
        switch attr {
 
88
        case "internet-gateway-id":
 
89
                return i.Id == value, nil
 
90
        case "attachment.state":
 
91
                return i.AttachmentState == value, nil
 
92
        case "attachment.vpc-id":
 
93
                return i.VPCId == value, nil
 
94
        case "tag", "tag-key", "tag-value":
 
95
                return false, fmt.Errorf("%q filter is not implemented", attr)
 
96
        }
 
97
        return false, fmt.Errorf("unknown attribute %q", attr)
 
98
}
 
99
 
 
100
func (srv *Server) describeInternetGateways(w http.ResponseWriter, req *http.Request, reqId string) interface{} {
 
101
        srv.mu.Lock()
 
102
        defer srv.mu.Unlock()
 
103
 
 
104
        idMap := parseIDs(req.Form, "InternetGatewayId.")
 
105
        f := newFilter(req.Form)
 
106
        var resp struct {
 
107
                XMLName xml.Name
 
108
                ec2.InternetGatewaysResp
 
109
        }
 
110
        resp.XMLName = xml.Name{defaultXMLName, "DescribeInternetGatewaysResponse"}
 
111
        resp.RequestId = reqId
 
112
        for _, i := range srv.internetGateways {
 
113
                ok, err := f.ok(i)
 
114
                _, known := idMap[i.Id]
 
115
                if ok && (len(idMap) == 0 || known) {
 
116
                        resp.InternetGateways = append(resp.InternetGateways, i.InternetGateway)
 
117
                } else if err != nil {
 
118
                        fatalf(400, "InvalidParameterValue", "describe internet gateways: %v", err)
 
119
                }
 
120
        }
 
121
        return &resp
 
122
}