Class: Kennel::Syncer

Inherits:
Object
  • Object
show all
Defined in:
lib/kennel/syncer.rb,
lib/kennel/syncer/plan.rb,
lib/kennel/syncer/types.rb,
lib/kennel/syncer/resolver.rb,
lib/kennel/syncer/plan_printer.rb,
lib/kennel/syncer/matched_expected.rb

Defined Under Namespace

Modules: MatchedExpected, Types Classes: Change, Plan, PlanPrinter, Resolver

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 Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api, expected, actual, filter:, strict_imports: true) ⇒ Syncer

Returns a new instance of Syncer.



16
17
18
19
20
21
22
23
24
# File 'lib/kennel/syncer.rb', line 16

def initialize(api, expected, actual, filter:, strict_imports: true)
  @api = api
  @strict_imports = strict_imports
  @filter = filter

  @resolver = Resolver.new(expected: expected, filter: filter)
  @plan = Plan.new(*calculate_changes(expected: expected, actual: actual))
  validate_changes
end

Instance Attribute Details

#planObject (readonly)

Returns the value of attribute plan.



14
15
16
# File 'lib/kennel/syncer.rb', line 14

def plan
  @plan
end

Instance Method Details

#confirmObject



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

def confirm
  return false if plan.empty?
  return true if ENV["CI"] || !Kennel.in.tty? || !Kennel.err.tty?
  Console.ask?("Execute Plan ?")
end


26
27
28
# File 'lib/kennel/syncer.rb', line 26

def print_plan
  PlanPrinter.new.print(plan)
end

#updateObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/kennel/syncer.rb', line 36

def update
  changes = []

  plan.deletes.each do |item|
    message = "#{item.api_resource} #{item.tracking_id} #{item.id}"
    Kennel.out.puts "Deleting #{message}"
    @api.delete item.api_resource, item.id
    changes << item.change
    Kennel.out.puts "#{LINE_UP}Deleted #{message}"
  end

  planned_actions = plan.creates + plan.updates

  # slos need to be updated first in case their timeframes changed
  # because datadog validates that update+create of slo alerts match an existing timeframe
  planned_actions.sort_by! { |item| item.expected.is_a?(Models::Slo) ? 0 : 1 }

  resolver.each_resolved(planned_actions) do |item|
    if item.is_a?(Types::PlannedCreate)
      message = "#{item.api_resource} #{item.tracking_id}"
      Kennel.out.puts "Creating #{message}"
      reply = @api.create item.api_resource, item.expected.as_json
      id = reply.fetch(:id)
      changes << item.change(id)
      resolver.add_actual [reply] # allow resolving ids we could previously not resolve
      Kennel.out.puts "#{LINE_UP}Created #{message} #{item.url(id)}"
    else
      message = "#{item.api_resource} #{item.tracking_id} #{item.url}"
      Kennel.out.puts "Updating #{message}"
      @api.update item.api_resource, item.id, item.expected.as_json
      changes << item.change
      Kennel.out.puts "#{LINE_UP}Updated #{message}"
    end
  end

  plan.changes = changes
  plan
end