class Awspec::Type::NetworkAcl

Constants

PROTOCOLS

rubocop:disable Metrics/LineLength

Public Instance Methods

allowed?(port = nil, protocol = nil, cidr = nil, rule_number = nil) click to toggle source
# File lib/awspec/type/network_acl.rb, line 23
def allowed?(port = nil, protocol = nil, cidr = nil, rule_number = nil)
  rule_action = 'allow'
  entry?(rule_action, port, protocol, cidr, rule_number)
end
denied?(port = nil, protocol = nil, cidr = nil, rule_number = nil) click to toggle source
# File lib/awspec/type/network_acl.rb, line 28
def denied?(port = nil, protocol = nil, cidr = nil, rule_number = nil)
  rule_action = 'deny'
  entry?(rule_action, port, protocol, cidr, rule_number)
end
has_subnet?(subnet_id) click to toggle source
# File lib/awspec/type/network_acl.rb, line 14
def has_subnet?(subnet_id)
  resource_via_client.associations.find do |a|
    next true if a.subnet_id == subnet_id
    subnet = find_subnet(subnet_id)
    next false unless subnet
    next a.subnet_id == subnet.subnet_id
  end
end
id() click to toggle source
# File lib/awspec/type/network_acl.rb, line 10
def id
  @id ||= resource_via_client.network_acl_id if resource_via_client
end
inbound() click to toggle source
# File lib/awspec/type/network_acl.rb, line 33
def inbound
  @egress = false
  self
end
inbound_entries_count() click to toggle source
# File lib/awspec/type/network_acl.rb, line 43
def inbound_entries_count
  resource_via_client.entries.count do |entry|
    entry.egress == false
  end
end
outbound() click to toggle source
# File lib/awspec/type/network_acl.rb, line 38
def outbound
  @egress = true
  self
end
outbound_entries_count() click to toggle source
# File lib/awspec/type/network_acl.rb, line 49
def outbound_entries_count
  resource_via_client.entries.count do |entry|
    entry.egress == true
  end
end
resource_via_client() click to toggle source
# File lib/awspec/type/network_acl.rb, line 6
def resource_via_client
  @resource_via_client ||= find_network_acl(@display_name)
end

Private Instance Methods

entry?(rule_action, port = nil, protocol = nil, cidr = nil, rule_number = nil) click to toggle source

rubocop:enable Metrics/LineLength

# File lib/awspec/type/network_acl.rb, line 75
def entry?(rule_action, port = nil, protocol = nil, cidr = nil, rule_number = nil)
  resource_via_client.entries.find do |entry|
    # egress rule_action
    next false if entry.egress != @egress
    next false if entry.rule_action != rule_action
    # protocol
    unless protocol.nil?
      next false unless protocol_match?(protocol, entry.protocol)
    end
    # cidr
    next false if !cidr.nil? && entry.cidr_block != cidr
    # rule_number
    rule_number = 32_767 if rule_number == '*'
    next false if !rule_number.nil? && entry.rule_number != rule_number
    # port
    unless entry.port_range.nil?
      next false unless port_between?(port, entry.port_range.from, entry.port_range.to)
    end
    next true
  end
end
port_between?(port, from_port, to_port) click to toggle source
# File lib/awspec/type/network_acl.rb, line 108
def port_between?(port, from_port, to_port)
  if port.is_a?(String) && port.include?('-')
    f, t = port.split('-')
    from_port == f.to_i && to_port == t.to_i
  else
    port.between?(from_port, to_port)
  end
end
protocol_match?(a, b) click to toggle source
# File lib/awspec/type/network_acl.rb, line 97
def protocol_match?(a, b)
  if a.match(/\A\d+\z/) && a.to_i >= 0
    return false unless b.to_i == a.to_i
  else
    lower_h = PROTOCOLS.map { |k, v| [k.downcase, v] }.to_h
    return false unless lower_h.key?(a.downcase)
    return false unless b.to_i == lower_h[a.downcase]
  end
  true
end