Class: Banacle::SlashCommand::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/banacle/slash_command/builder.rb

Defined Under Namespace

Classes: InvalidActionError, InvalidCidrBlockError, InvalidRegionError, InvalidVpcError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(action:, region:, vpc_id_or_name:, cidr_blocks:) ⇒ Builder

Returns a new instance of Builder.



19
20
21
22
23
24
# File 'lib/banacle/slash_command/builder.rb', line 19

def initialize(action:, region:, vpc_id_or_name:, cidr_blocks:)
  @action = action
  @region = region
  @vpc_id_or_name = vpc_id_or_name
  @cidr_blocks = cidr_blocks
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



26
27
28
# File 'lib/banacle/slash_command/builder.rb', line 26

def action
  @action
end

#cidr_blocksObject (readonly)

Returns the value of attribute cidr_blocks.



26
27
28
# File 'lib/banacle/slash_command/builder.rb', line 26

def cidr_blocks
  @cidr_blocks
end

#regionObject (readonly)

Returns the value of attribute region.



26
27
28
# File 'lib/banacle/slash_command/builder.rb', line 26

def region
  @region
end

#vpc_idObject

Returns the value of attribute vpc_id.



27
28
29
# File 'lib/banacle/slash_command/builder.rb', line 27

def vpc_id
  @vpc_id
end

#vpc_id_or_nameObject (readonly)

Returns the value of attribute vpc_id_or_name.



26
27
28
# File 'lib/banacle/slash_command/builder.rb', line 26

def vpc_id_or_name
  @vpc_id_or_name
end

Class Method Details

.build(action:, region:, vpc_id_or_name:, cidr_blocks:) ⇒ Object



15
16
17
# File 'lib/banacle/slash_command/builder.rb', line 15

def self.build(action:, region:, vpc_id_or_name:, cidr_blocks:)
  new(action: action, region: region, vpc_id_or_name: vpc_id_or_name, cidr_blocks: cidr_blocks).build
end

Instance Method Details

#buildObject



29
30
31
32
33
34
35
# File 'lib/banacle/slash_command/builder.rb', line 29

def build
  validate!
  set_vpc_id!
  normalize_cidr_blocks!

  Command.new(action: action, region: region, vpc_id: vpc_id, cidr_blocks: cidr_blocks)
end

#normalize_cidr_blocks!Object



92
93
94
95
96
97
# File 'lib/banacle/slash_command/builder.rb', line 92

def normalize_cidr_blocks!
  cidr_blocks.map! do |c|
    ip = IPAddr.new(c)
    "#{ip}/#{ip.prefix}"
  end
end

#set_vpc_id!Object



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/banacle/slash_command/builder.rb', line 80

def set_vpc_id!
  begin
    self.vpc_id = AwsWrapper::Vpc.resolve_vpc_id(region, vpc_id_or_name)
  rescue AwsWrapper::Vpc::InvalidRegionError
    raise InvalidRegionError.new("specified region: #{region} is invalid")
  end

  unless vpc_id
    raise InvalidVpcError.new("specified vpc: #{vpc_id_or_name} in #{region} not found")
  end
end

#validate!Object



37
38
39
40
41
42
# File 'lib/banacle/slash_command/builder.rb', line 37

def validate!
  validate_action!
  validate_region!
  validate_vpc_id_or_name!
  validate_cidr_blocks!
end

#validate_action!Object



44
45
46
47
48
49
50
51
52
# File 'lib/banacle/slash_command/builder.rb', line 44

def validate_action!
  if !action || action.empty?
    raise InvalidActionError.new("action is required")
  end

  unless Command::PERMITTED_ACTIONS.include?(action)
    raise InvalidActionError.new("permitted actions are: (#{Command::PERMITTED_ACTIONS.join("|")})")
  end
end

#validate_cidr_blocks!Object



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/banacle/slash_command/builder.rb', line 66

def validate_cidr_blocks!
  if !cidr_blocks || cidr_blocks.empty?
    raise InvalidVpcError.new("at least one cidr_block is required with #{action} action")
  end

  cidr_blocks.each do |cidr_block|
    begin
      IPAddr.new(cidr_block)
    rescue IPAddr::InvalidAddressError
      raise InvalidCidrBlockError.new("#{cidr_block} is invalid address")
    end
  end
end

#validate_region!Object



54
55
56
57
58
# File 'lib/banacle/slash_command/builder.rb', line 54

def validate_region!
  if !region || region.empty?
    raise InvalidRegionError.new("region is required")
  end
end

#validate_vpc_id_or_name!Object



60
61
62
63
64
# File 'lib/banacle/slash_command/builder.rb', line 60

def validate_vpc_id_or_name!
  unless vpc_id_or_name
    raise InvalidVpcError.new("vpc_id or vpc_name is required with #{action} action")
  end
end