Class: Kennel::Syncer

Inherits:
Object
  • Object
show all
Defined in:
lib/kennel/syncer.rb

Defined Under Namespace

Classes: Change, Plan

Constant Summary collapse

DELETE_ORDER =

dashboards references monitors + slos, slos reference monitors

["dashboard", "slo", "monitor", "synthetics/tests"].freeze
LINE_UP =

go up and clear

"\e[1A\033[K"

Instance Method Summary collapse

Constructor Details

#initialize(api, expected, actual, strict_imports: true, project_filter: nil, tracking_id_filter: nil) ⇒ Syncer

Returns a new instance of Syncer.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/kennel/syncer.rb', line 11

def initialize(api, expected, actual, strict_imports: true, project_filter: nil, tracking_id_filter: nil)
  @api = api
  @expected = Set.new expected # need Set to speed up deletion
  @actual = actual
  @strict_imports = strict_imports
  @project_filter = project_filter
  @tracking_id_filter = tracking_id_filter

  @attribute_differ = AttributeDiffer.new

  calculate_changes
  validate_changes
  prevent_irreversible_partial_updates

  @warnings.each { |message| Kennel.out.puts Utils.color(:yellow, "Warning: #{message}") }
end

Instance Method Details

#confirmObject



48
49
50
51
52
# File 'lib/kennel/syncer.rb', line 48

def confirm
  return false if noop?
  return true if ENV["CI"] || !STDIN.tty? || !Kennel.err.tty?
  Utils.ask("Execute Plan ?")
end

#planObject



28
29
30
31
32
33
34
35
# File 'lib/kennel/syncer.rb', line 28

def plan
  Plan.new(
    changes:
      @create.map { |_id, e, _a| Change.new(:create, e.class.api_resource, e.tracking_id, nil) } +
        @update.map { |id, e, _a| Change.new(:update, e.class.api_resource, e.tracking_id, id) } +
        @delete.map { |id, _e, a| Change.new(:delete, a.fetch(:klass).api_resource, a.fetch(:tracking_id), id) }
  )
end


37
38
39
40
41
42
43
44
45
46
# File 'lib/kennel/syncer.rb', line 37

def print_plan
  Kennel.out.puts "Plan:"
  if noop?
    Kennel.out.puts Utils.color(:green, "Nothing to do")
  else
    print_changes "Create", @create, :green
    print_changes "Update", @update, :yellow
    print_changes "Delete", @delete, :red
  end
end

#updateObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/kennel/syncer.rb', line 54

def update
  changes = []

  each_resolved @create do |_, e|
    message = "#{e.class.api_resource} #{e.tracking_id}"
    Kennel.out.puts "Creating #{message}"
    reply = @api.create e.class.api_resource, e.as_json
    Utils. reply, e.class
    id = reply.fetch(:id)
    changes << Change.new(:create, e.class.api_resource, e.tracking_id, id)
    populate_id_map [], [reply] # allow resolving ids we could previously no resolve
    Kennel.out.puts "#{LINE_UP}Created #{message} #{e.class.url(id)}"
  end

  each_resolved @update do |id, e|
    message = "#{e.class.api_resource} #{e.tracking_id} #{e.class.url(id)}"
    Kennel.out.puts "Updating #{message}"
    @api.update e.class.api_resource, id, e.as_json
    changes << Change.new(:update, e.class.api_resource, e.tracking_id, id)
    Kennel.out.puts "#{LINE_UP}Updated #{message}"
  end

  @delete.each do |id, _, a|
    klass = a.fetch(:klass)
    message = "#{klass.api_resource} #{a.fetch(:tracking_id)} #{id}"
    Kennel.out.puts "Deleting #{message}"
    @api.delete klass.api_resource, id
    changes << Change.new(:delete, klass.api_resource, a.fetch(:tracking_id), id)
    Kennel.out.puts "#{LINE_UP}Deleted #{message}"
  end

  Plan.new(changes: changes)
end