Class: Kennel::Syncer
- Inherits:
-
Object
- Object
- Kennel::Syncer
- 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
-
#plan ⇒ Object
readonly
Returns the value of attribute plan.
Instance Method Summary collapse
- #confirm ⇒ Object
-
#initialize(api, expected, actual, filter:, strict_imports: true) ⇒ Syncer
constructor
A new instance of Syncer.
- #print_plan ⇒ Object
- #update ⇒ Object
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
#plan ⇒ Object (readonly)
Returns the value of attribute plan.
14 15 16 |
# File 'lib/kennel/syncer.rb', line 14 def plan @plan end |
Instance Method Details
#confirm ⇒ Object
30 31 32 33 34 |
# File 'lib/kennel/syncer.rb', line 30 def confirm return false if plan.empty? return true unless Console.tty? Console.ask?("Execute Plan ?") end |
#print_plan ⇒ Object
26 27 28 |
# File 'lib/kennel/syncer.rb', line 26 def print_plan PlanPrinter.new.print(plan) end |
#update ⇒ Object
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 74 75 76 77 78 |
# File 'lib/kennel/syncer.rb', line 36 def update changes = [] plan.deletes.each do |item| = "#{item.api_resource} #{item.tracking_id} #{item.id}" Kennel.out.puts "Deleting #{}" @api.delete item.api_resource, item.id changes << item.change Kennel.out.puts "#{LINE_UP}Deleted #{}" 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) = "#{item.api_resource} #{item.tracking_id}" Kennel.out.puts "Creating #{}" 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 #{} #{item.url(id)}" else = "#{item.api_resource} #{item.tracking_id} #{item.url}" Kennel.out.puts "Updating #{}" @api.update item.api_resource, item.id, item.expected.as_json changes << item.change Kennel.out.puts "#{LINE_UP}Updated #{}" end rescue StandardError raise unless Console.tty? Kennel.err.puts $!. Kennel.err.puts $!.backtrace raise unless Console.ask?("Continue with error ?") end plan.changes = changes plan end |