2
// goamz - Go packages to interact with the Amazon Web Services.
4
// https://wiki.ubuntu.com/goamz
6
// Copyright (c) 2015 Canonical Ltd.
8
// This file contains code handling AWS API around Internet Gateways.
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{}
31
if _, found := srv.vpcs[igw.VPCId]; !found {
32
return zeroGateway, fmt.Errorf("VPC %q not found", igw.VPCId)
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
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 {
49
return fmt.Errorf("missing internet gateway id")
53
_, found := srv.internetGateways[igw.Id]
55
return fmt.Errorf("internet gateway %q not found", igw.Id)
58
if _, found := srv.vpcs[igw.VPCId]; !found {
59
return fmt.Errorf("VPC %q not found", igw.VPCId)
62
srv.internetGateways[igw.Id] = &internetGateway{igw}
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 {
71
return fmt.Errorf("missing internet gateway id")
75
if _, found := srv.internetGateways[igwId]; found {
76
delete(srv.internetGateways, igwId)
79
return fmt.Errorf("internet gateway %q not found", igwId)
82
type internetGateway struct {
86
func (i *internetGateway) matchAttr(attr, value string) (ok bool, err error) {
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)
97
return false, fmt.Errorf("unknown attribute %q", attr)
100
func (srv *Server) describeInternetGateways(w http.ResponseWriter, req *http.Request, reqId string) interface{} {
102
defer srv.mu.Unlock()
104
idMap := parseIDs(req.Form, "InternetGatewayId.")
105
f := newFilter(req.Form)
108
ec2.InternetGatewaysResp
110
resp.XMLName = xml.Name{defaultXMLName, "DescribeInternetGatewaysResponse"}
111
resp.RequestId = reqId
112
for _, i := range srv.internetGateways {
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)