Class: Awspec::Helper::Finder::Subnet::SubnetCache

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/awspec/helper/finder/subnet.rb

Overview

Usage

Includes Singleton module, so use instance instead of new to get a instance.

It is intended to be used internally by the find_subnet function only.

Many of the methods expect a symbol to search through the cache to avoid having to call to_sym multiple times.

Instance Method Summary collapse

Constructor Details

#initializeSubnetCache

:nodoc:



23
24
25
26
27
28
# File 'lib/awspec/helper/finder/subnet.rb', line 23

def initialize # :nodoc:
  @by_tag_name = {}
  @by_cidr = {}
  @subnet_ids = {}
  @ip_matcher = Regexp.new('^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/\d{1,2}$')
end

Instance Method Details

#add_by_cidr(cidr, subnet_id) ⇒ Object

Add a mapping of a CIDR to the respective subnet ID



31
32
33
34
# File 'lib/awspec/helper/finder/subnet.rb', line 31

def add_by_cidr(cidr, subnet_id)
  key_sym = cidr.to_sym
  @by_cidr[key_sym] = subnet_id.to_sym unless @by_cidr.key?(key_sym)
end

#add_by_tag(tag, subnet_id) ⇒ Object

Add a mapping of a tag to the respective subnet ID



37
38
39
40
# File 'lib/awspec/helper/finder/subnet.rb', line 37

def add_by_tag(tag, subnet_id)
  key_sym = tag.to_sym
  @by_tag_name[key_sym] = subnet_id.to_sym unless @by_tag_name.key?(key_sym)
end

#add_subnet(subnet) ⇒ Object

Add a Aws::EC2::Types::Subnet instance to the cache, mapping it’s ID to the instance itself.



44
45
46
47
# File 'lib/awspec/helper/finder/subnet.rb', line 44

def add_subnet(subnet)
  key_sym = subnet.subnet_id.to_sym
  @subnet_ids[key_sym] = subnet unless @subnet_ids.key?(key_sym)
end

#empty?Boolean

Check if the cache was already initialized or not.

Returns:

  • (Boolean)


80
81
82
# File 'lib/awspec/helper/finder/subnet.rb', line 80

def empty?
  @subnet_ids.empty?
end

#has_cidr?(cidr_symbol) ⇒ Boolean

Check if a IPv4 CIDR (as a symbol) exists in the cache.

Returns:

  • (Boolean)


55
56
57
# File 'lib/awspec/helper/finder/subnet.rb', line 55

def has_cidr?(cidr_symbol)
  @by_cidr.key?(cidr_symbol)
end

#has_subnet?(subnet_id_symbol) ⇒ Boolean

Check if a subnet ID (as a symbol) exists in the cache.

Returns:

  • (Boolean)


50
51
52
# File 'lib/awspec/helper/finder/subnet.rb', line 50

def has_subnet?(subnet_id_symbol)
  @subnet_ids.key?(subnet_id_symbol)
end

#is_cidr?(subnet_id) ⇒ Boolean

Check if a given string looks like a IPv4 CIDR.

Returns:

  • (Boolean)


75
76
77
# File 'lib/awspec/helper/finder/subnet.rb', line 75

def is_cidr?(subnet_id)
  @ip_matcher.match(subnet_id)
end

#subnet_by_cidr(cidr_symbol) ⇒ Object

Return a Aws::EC2::Types::Subnet that matches the given CIDR.



60
61
62
# File 'lib/awspec/helper/finder/subnet.rb', line 60

def subnet_by_cidr(cidr_symbol)
  @subnet_ids[@by_cidr[cidr_symbol]]
end

#subnet_by_id(subnet_id_symbol) ⇒ Object

Return a Aws::EC2::Types::Subnet that matches the given subnet ID.



70
71
72
# File 'lib/awspec/helper/finder/subnet.rb', line 70

def subnet_by_id(subnet_id_symbol)
  @subnet_ids[subnet_id_symbol]
end

#subnet_by_tag(tag_symbol) ⇒ Object

Return a Aws::EC2::Types::Subnet that matches the given tag.



65
66
67
# File 'lib/awspec/helper/finder/subnet.rb', line 65

def subnet_by_tag(tag_symbol)
  @subnet_ids[@by_tag_name[tag_symbol]]
end

#to_sObject

Return the cache as a string.



85
86
87
# File 'lib/awspec/helper/finder/subnet.rb', line 85

def to_s
  "by tag name: #{@by_tag_name}, by CIDR: #{@by_cidr}"
end