Class: Cumulus::VPC::RouteTableDiff

Inherits:
Common::Diff show all
Includes:
Common::TagsDiff, RouteTableChange
Defined in:
lib/vpc/models/RouteTableDiff.rb

Overview

Public: Represents a single difference between local configuration and AWS configuration

Constant Summary

Constants included from RouteTableChange

Cumulus::VPC::RouteTableChange::ROUTES, Cumulus::VPC::RouteTableChange::TAGS, Cumulus::VPC::RouteTableChange::VGWS

Constants included from Common::DiffChange

Common::DiffChange::ADD, Common::DiffChange::MODIFIED, Common::DiffChange::UNMANAGED

Instance Attribute Summary

Attributes inherited from Common::Diff

#aws, #changes, #info_only, #local, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Common::TagsDiff

#tags_diff_string, #tags_to_add, #tags_to_remove

Methods included from Common::DiffChange

next_change_id

Methods inherited from Common::Diff

#add_string, added, #initialize, #local_name, modified, #to_s, unmanaged, #unmanaged_string

Constructor Details

This class inherits a constructor from Cumulus::Common::Diff

Class Method Details

.propagate_vgws(aws, local) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/vpc/models/RouteTableDiff.rb', line 48

def self.propagate_vgws(aws, local)
  changes = Common::ListChange.simple_list_diff(aws, local)
  if changes
    diff = RouteTableDiff.new(VGWS, aws, local)
    diff.changes = changes
    diff
  end
end

.routes(aws, local) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/vpc/models/RouteTableDiff.rb', line 23

def self.routes(aws, local)
  aws_cidr_routes = Hash[aws.map { |route| [route.destination_cidr_block, route] }]
  local_cidr_routes = Hash[local.map { |route| [route.dest_cidr, route] }]

  added = local_cidr_routes.reject { |k, v| aws_cidr_routes.has_key? k }
  removed = aws_cidr_routes.reject { |k, v| local_cidr_routes.has_key? k }
  modified = local_cidr_routes.select { |k, v| aws_cidr_routes.has_key? k }

  added_diffs = Hash[added.map { |cidr, route| [cidr, RouteDiff.added(route)] }]
  removed_diffs = Hash[removed.map { |cidr, route| [cidr, RouteDiff.unmanaged(route)] }]
  modified_diffs = Hash[modified.map do |cidr, route|
    aws_route = aws_cidr_routes[cidr]
    route_diffs = route.diff(aws_route)
    if !route_diffs.empty?
      [cidr, RouteDiff.modified(aws_route, route, route_diffs)]
    end
  end.reject { |v| v.nil? }]

  if !added_diffs.empty? or !removed_diffs.empty? or !modified_diffs.empty?
    diff = RouteTableDiff.new(ROUTES, aws, local)
    diff.changes = Common::ListChange.new(added_diffs, removed_diffs, modified_diffs)
    diff
  end
end

Instance Method Details

#asset_typeObject



65
66
67
# File 'lib/vpc/models/RouteTableDiff.rb', line 65

def asset_type
  "Route Table"
end

#aws_nameObject



69
70
71
# File 'lib/vpc/models/RouteTableDiff.rb', line 69

def aws_name
  @aws.name
end

#aws_tagsObject



61
62
63
# File 'lib/vpc/models/RouteTableDiff.rb', line 61

def aws_tags
  @aws
end

#diff_stringObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/vpc/models/RouteTableDiff.rb', line 73

def diff_string
  case @type
  when ROUTES
    [
      "Routes:",
      @changes.removed.map { |s, _| Colors.unmanaged("\t#{s} will be deleted") },
      @changes.added.map { |s, _| Colors.added("\t#{s} will be created") },
      @changes.modified.map do |cidr, diff|
        [
          "\t#{cidr}:",
          diff.changes.map do |diff|
            diff.to_s.lines.map { |l| "\t\t#{l}".chomp("\n") }
          end
        ]
      end
    ].flatten.join("\n")
  when VGWS
    [
      "Propagate VGWs:",
      @changes.removed.map { |s, _| Colors.unmanaged("\t#{s}") },
      @changes.added.map { |s, _| Colors.added("\t#{s}") },
    ].flatten.join("\n")
  when TAGS
    tags_diff_string
  end
end

#local_tagsObject



57
58
59
# File 'lib/vpc/models/RouteTableDiff.rb', line 57

def local_tags
  @local
end