Class: StackMaster::SecurityGroupFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/stack_master/security_group_finder.rb

Constant Summary collapse

SecurityGroupNotFound =
Class.new(StandardError)
MultipleSecurityGroupsFound =
Class.new(StandardError)

Instance Method Summary collapse

Constructor Details

#initialize(region) ⇒ SecurityGroupFinder

Returns a new instance of SecurityGroupFinder.



6
7
8
# File 'lib/stack_master/security_group_finder.rb', line 6

def initialize(region)
  @resource = Aws::EC2::Resource.new(region: region)
end

Instance Method Details

#find(reference) ⇒ Object

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/stack_master/security_group_finder.rb', line 10

def find(reference)
  raise ArgumentError, 'Security group references must be non-empty strings' unless reference.is_a?(String) && !reference.empty?

  groups = @resource.security_groups({
                                       filters: [
                                         {
                                           name: "group-name",
                                           values: [reference],
                                         },
                                       ],
                                     })

  raise SecurityGroupNotFound, "No security group with name #{reference} found" unless groups.any?
  raise MultipleSecurityGroupsFound, "More than one security group with name #{reference} found" if groups.count > 1

  groups.first.id
end