Class: Banacle::AwsWrapper::Vpc

Inherits:
Object
  • Object
show all
Defined in:
lib/banacle/aws_wrapper/vpc.rb

Defined Under Namespace

Classes: InvalidRegionError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(region, vpc_id_or_name) ⇒ Vpc

Returns a new instance of Vpc.



13
14
15
16
# File 'lib/banacle/aws_wrapper/vpc.rb', line 13

def initialize(region, vpc_id_or_name)
  @region = region
  @vpc_id_or_name = vpc_id_or_name
end

Instance Attribute Details

#regionObject (readonly)

Returns the value of attribute region.



18
19
20
# File 'lib/banacle/aws_wrapper/vpc.rb', line 18

def region
  @region
end

#vpc_id_or_nameObject (readonly)

Returns the value of attribute vpc_id_or_name.



18
19
20
# File 'lib/banacle/aws_wrapper/vpc.rb', line 18

def vpc_id_or_name
  @vpc_id_or_name
end

Class Method Details

.resolve_vpc_id(region, vpc_id_or_name) ⇒ Object



9
10
11
# File 'lib/banacle/aws_wrapper/vpc.rb', line 9

def self.resolve_vpc_id(region, vpc_id_or_name)
  new(region, vpc_id_or_name).resolve_vpc_id
end

Instance Method Details

#resolve_vpc_idObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/banacle/aws_wrapper/vpc.rb', line 20

def resolve_vpc_id
  begin
    vpc_list = ec2.describe_vpcs.each.flat_map(&:vpcs).map do |vpc|
      name_tag = vpc.tags.find { |t| t.key == "Name" }
      [
        name_tag.value,
        vpc.vpc_id,
      ]
    end.sort_by { |e| e[0] }.to_h
  rescue Aws::Errors::NoSuchEndpointError
    raise InvalidRegionError.new("region: #{region} is invalid")
  end

  vpc_id = nil
  if vpc_list.values.include?(vpc_id_or_name)
    vpc_id = vpc_id_or_name
  else
    vpc_id = vpc_list[vpc_id_or_name]
  end

  vpc_id
end