Class: Kennel::Syncer

Inherits:
Object
  • Object
show all
Defined in:
lib/kennel/syncer.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, strict_imports: true, project_filter: nil, tracking_id_filter: nil) ⇒ Syncer

Returns a new instance of Syncer.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/kennel/syncer.rb', line 18

def initialize(api, expected, actual, strict_imports: true, project_filter: nil, tracking_id_filter: nil)
  @api = api
  @strict_imports = strict_imports
  @project_filter = project_filter
  @tracking_id_filter = tracking_id_filter

  @resolver = Resolver.new(expected: expected, project_filter: @project_filter, tracking_id_filter: @tracking_id_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



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

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

#planObject



33
34
35
36
37
38
# File 'lib/kennel/syncer.rb', line 33

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


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

def print_plan
  PlanDisplayer.new.display(internal_plan)
end

#updateObject



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
79
80
# File 'lib/kennel/syncer.rb', line 50

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