Class: Mappru::Driver

Inherits:
Object
  • Object
show all
Includes:
Logger::Helper, Utils::Helper
Defined in:
lib/mappru/driver.rb

Instance Method Summary collapse

Methods included from Utils::Helper

#diff, #matched?

Methods included from Logger::Helper

#log

Constructor Details

#initialize(client, options = {}) ⇒ Driver

Returns a new instance of Driver.



5
6
7
8
9
# File 'lib/mappru/driver.rb', line 5

def initialize(client, options = {})
  @client = client
  @resource = Aws::EC2::Resource.new(client: @client)
  @options = options
end

Instance Method Details

#associate_subnets(vpc_id, rt_name, subnet_ids) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mappru/driver.rb', line 25

def associate_subnets(vpc_id, rt_name, subnet_ids)
  log(:info, "Associate Subnets `#{vpc_id}` > `#{rt_name}`: #{subnet_ids.join(', ')}", color: :green)

  unless @options[:dry_run]
    subnet_ids.each do |sbnt_id|
      rt_id = rt_id_by_vpc_rt_name.fetch(vpc_id).fetch(rt_name)
      params = {route_table_id: rt_id, subnet_id: sbnt_id}
      @client.associate_route_table(params)
    end
  end
end

#create_route(vpc_id, rt_name, dest_cidr, attrs) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/mappru/driver.rb', line 49

def create_route(vpc_id, rt_name, dest_cidr, attrs)
  log(:info, "Create Route `#{vpc_id}` > `#{rt_name}` > `#{dest_cidr}`", color: :cyan)

  unless @options[:dry_run]
    rt_id = rt_id_by_vpc_rt_name.fetch(vpc_id).fetch(rt_name)
    params = attrs.merge(route_table_id: rt_id, destination_cidr_block: dest_cidr)
    @client.create_route(params)
  end
end

#create_route_table(vpc_id, name, attrs) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/mappru/driver.rb', line 11

def create_route_table(vpc_id, name, attrs)
  log(:info, "Create Route Table `#{vpc_id}` > `#{name}`", color: :cyan)

  unless @options[:dry_run]
    vpc = @resource.vpc(vpc_id)
    rt = vpc.create_route_table
    name_tag = {key: 'Name', value: name}
    rt.create_tags(tags: [name_tag])
    rt_id_by_vpc_rt_name[vpc_id][name] = rt.id
  end

  {routes: [], subnets: []}
end

#delete_route(vpc_id, rt_name, dest_cidr) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/mappru/driver.rb', line 59

def delete_route(vpc_id, rt_name, dest_cidr)
  log(:info, "Delete Route `#{vpc_id}` > `#{rt_name}` > `#{dest_cidr}`", color: :red)

  unless @options[:dry_run]
    rt_id = rt_id_by_vpc_rt_name.fetch(vpc_id).fetch(rt_name)
    params = {route_table_id: rt_id, destination_cidr_block: dest_cidr}
    @client.delete_route(params)
  end
end

#disassociate_subnets(vpc_id, rt_name, subnet_ids) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mappru/driver.rb', line 37

def disassociate_subnets(vpc_id, rt_name, subnet_ids)
  log(:info, "Disassociate Subnets `#{vpc_id}` > `#{rt_name}`: #{subnet_ids.join(', ')}", color: :red)

  unless @options[:dry_run]
    subnet_ids.each do |sbnt_id|
      assoc_id = assoc_id_by_subnet.fetch(sbnt_id)
      params = {association_id: assoc_id}
      @client.disassociate_route_table(params)
    end
  end
end

#update_route(vpc_id, rt_name, dest_cidr, route, old_route) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/mappru/driver.rb', line 69

def update_route(vpc_id, rt_name, dest_cidr, route, old_route)
  log(:info, "Update Route `#{vpc_id}` > `#{rt_name}` > `#{dest_cidr}`", color: :green)
  log(:info, diff(old_route, route, color: @options[:color]), color: false)

  unless @options[:dry_run]
    rt_id = rt_id_by_vpc_rt_name.fetch(vpc_id).fetch(rt_name)
    params = route.merge(route_table_id: rt_id, destination_cidr_block: dest_cidr)
    @client.replace_route(params)
  end
end