class StackMaster::SecurityGroupFinder

Constants

MultipleSecurityGroupsFound
SecurityGroupNotFound

Public Class Methods

new(region) click to toggle source
# File lib/stack_master/security_group_finder.rb, line 6
def initialize(region)
  @resource = Aws::EC2::Resource.new(region: region)
end

Public Instance Methods

find(reference) click to toggle source
# 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