File: describe_network_acls.rb

package info (click to toggle)
ruby-fog-aws 3.18.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,140 kB
  • sloc: ruby: 73,328; javascript: 14; makefile: 9; sh: 4
file content (108 lines) | stat: -rw-r--r-- 5,668 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
module Fog
  module AWS
    class Compute
      class Real
        require 'fog/aws/parsers/compute/describe_network_acls'

        # Describe all or specified network ACLs
        #
        # ==== Parameters
        # * filters<~Hash> - List of filters to limit results with
        #
        # === Returns
        # * response<~Excon::Response>:
        # * body<~Hash>:
        # * 'requestId'<~String>                   - Id of request
        # * 'networkAclSet'<~Array>:               - A list of network ACLs
        # *   'networkAclId'<~String>              - The ID of the network ACL
        # *   'vpcId'<~String>                     - The ID of the VPC for the network ACL
        # *   'default'<~Boolean>                  - Indicates whether this is the default network ACL for the VPC
        # *   'entrySet'<~Array>:                  - A list of entries (rules) in the network ACL
        # *     'ruleNumber'<~Integer>             - The rule number for the entry. ACL entries are processed in ascending order by rule number
        # *     'protocol'<~Integer>               - The protocol. A value of -1 means all protocols
        # *     'ruleAction'<~String>              - Indicates whether to allow or deny the traffic that matches the rule
        # *     'egress'<~Boolean>                 - Indicates whether the rule is an egress rule (applied to traffic leaving the subnet)
        # *     'cidrBlock'<~String>               - The network range to allow or deny, in CIDR notation
        # *     'icmpTypeCode'<~Hash>              - ICMP protocol: The ICMP type and code
        # *       'code'<~Integer>                 - The ICMP code. A value of -1 means all codes for the specified ICMP type
        # *       'type'<~Integer>                 - The ICMP type. A value of -1 means all types
        # *     'portRange'<~Hash>                 - TCP or UDP protocols: The range of ports the rule applies to
        # *       'from'<~Integer>                 - The first port in the range
        # *       'to'<~Integer>                   - The last port in the range
        # *   'associationSet'<~Array>:            - A list of associations between the network ACL and subnets
        # *     'networkAclAssociationId'<~String> - The ID of the association
        # *     'networkAclId'<~String>            - The ID of the network ACL
        # *     'subnetId'<~String>                - The ID of the subnet
        # *   'tagSet'<~Array>:                    - Tags assigned to the resource.
        # *     'key'<~String>                     - Tag's key
        # *     'value'<~String>                   - Tag's value
        #
        # {Amazon API Reference}[http://docs.aws.amazon.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeNetworkAcls.html]
        def describe_network_acls(filters = {})
          params = Fog::AWS.indexed_filters(filters)
          request({
            'Action' => 'DescribeNetworkAcls',
            :idempotent => true,
            :parser => Fog::Parsers::AWS::Compute::DescribeNetworkAcls.new
          }.merge!(params))
        end
      end

      class Mock
        def describe_network_acls(filters = {})
          response = Excon::Response.new

          network_acls = self.data[:network_acls].values
          network_acls = apply_tag_filters(network_acls, filters, 'networkAclId')

          aliases = {
            'vpc-id'         => 'vpcId',
            'network-acl-id' => 'networkAclId',
            'default'        => 'default',
          }
          association_aliases = {
            'association-id' => 'networkAclAssociationId',
            'network-acl-id' => 'networkAclId',
            'subnet-id'      => 'subnetId',
          }
          entry_aliases = {
            'cidr'        => 'cidrBlock',
            'egress'      => 'egress',
            'rule-action' => 'ruleAction',
            'rule-number' => 'ruleNumber',
            'protocol'    => 'protocol'
          }
          for filter_key, filter_value in filters
            filter_key = filter_key.to_s
            if association_key = filter_key.split('association.')[1]
              aliased_key = association_aliases[association_key]
              network_acls = network_acls.reject{|nacl| !nacl['associationSet'].find {|association| [*filter_value].include?(association[aliased_key])}}
            elsif entry_key = filter_key.split('entry.icmp.')[1]
              network_acls = network_acls.reject{|nacl| !nacl['entrySet'].find {|association| [*filter_value].include?(association['icmpTypeCode'][entry_key])}}
            elsif entry_key = filter_key.split('entry.port-range.')[1]
              network_acls = network_acls.reject{|nacl| !nacl['entrySet'].find {|association| [*filter_value].include?(association['portRange'][entry_key])}}
            elsif entry_key = filter_key.split('entry.')[1]
              aliased_key = entry_aliases[entry_key]
              network_acls = network_acls.reject{|nacl| !nacl['entrySet'].find {|association| [*filter_value].include?(association[aliased_key])}}
            else
              aliased_key = aliases[filter_key]
              network_acls = network_acls.reject{|nacl| ![*filter_value].include?(nacl[aliased_key])}
            end
          end

          network_acls.each do |acl|
            tags = self.data[:tag_sets][acl['networkAclId']]
            acl.merge!('tagSet' => tags) if tags
          end

          response.status = 200
          response.body = {
            'requestId'     => Fog::AWS::Mock.request_id,
            'networkAclSet' => network_acls
          }
          response
        end
      end
    end
  end
end