Class: Kennel::Syncer

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

Defined Under Namespace

Modules: MatchedExpected, Types Classes: Change, InternalPlan, Plan, PlanDisplayer, 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 Method Summary collapse

Constructor Details

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

Returns a new instance of Syncer.



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/kennel/syncer.rb', line 23

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

  @resolver = Resolver.new(expected: expected, filter: filter)

  internal_plan = calculate_changes(expected: expected, actual: actual)
  validate_changes(internal_plan)
  @internal_plan = internal_plan

  @warnings.each { |message| Kennel.out.puts Console.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 internal_plan.empty?
  return true if ENV["CI"] || !Kennel.in.tty? || !Kennel.err.tty?
  Console.ask?("Execute Plan ?")
end

#planObject



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

def plan
  ip = @internal_plan
  Plan.new(
    changes: (ip.creates + ip.updates + ip.deletes).map(&:change)
  )
end


44
45
46
# File 'lib/kennel/syncer.rb', line 44

def print_plan
  PlanDisplayer.new.display(internal_plan)
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
# File 'lib/kennel/syncer.rb', line 54

def update
  changes = []

  internal_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

  resolver.each_resolved internal_plan.creates do |item|
    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)}"
  end

  resolver.each_resolved internal_plan.updates do |item|
    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

  Plan.new(changes: changes)
end