lib/codily/engine.rb in codily-0.2.0 vs lib/codily/engine.rb in codily-0.3.0
- old
+ new
@@ -15,19 +15,20 @@
require 'codily/elements/vcl'
require 'codily/elements/settings'
module Codily
class Engine
- def initialize(fastly, present, desired, service_filter: nil)
+ def initialize(fastly, present, desired, service_filter: nil, activate: false)
@fastly = fastly
@present = present
@desired = desired
@service_filter = service_filter
+ @activate = activate
end
- attr_reader :fastly, :present, :desired, :service_filter
+ attr_reader :fastly, :present, :desired, :service_filter, :activate
ORDER = [
Elements::Service,
Elements::Settings,
Elements::Condition,
@@ -47,26 +48,23 @@
if dry_run
puts "(dry-run)"
puts
end
- act_any = false
- act = false
-
- present.service_versions.each do |id, version|
+ affected_services.each do |key|
+ version = present.service_version_get(key)
if !version[:dev]
puts "CLONE VERSION: #{version[:name]}.#{version[:active]}"
if !dry_run
- version[:dev] = fastly.get_version(id, version[:active]).clone.number
+ version[:dev] = fastly.get_version(version[:id], version[:active]).clone.number
end
end
end
puts
creations.each do |new_element|
- act_any = act = true
puts "CREATE: #{new_element.inspect}"
hash = new_element.as_hash
if new_element.parent_class == Elements::Service
@@ -82,15 +80,11 @@
present.service_version_set(new_obj.name, new_obj.id, new_obj.versions)
end
end
end
- puts if act
- act = false
-
updates.each do |present_elem, desired_elem|
- act_any = act = true
puts "UPDATE: - #{present_elem.inspect}"
puts " + #{desired_elem.inspect}"
payload = UpdatePayload.new(name: present_elem.name, id: present_elem.id, hash: desired_elem.as_hash)
if desired_elem.parent_class == Elements::Service
@@ -102,52 +96,76 @@
unless dry_run
fastly.update(desired_elem.fastly_class, payload)
end
end
- puts if act
- act = false
-
removals.each do |removed_element|
- act_any = act = true
puts "DELETE: #{removed_element.inspect}"
unless dry_run
removed_element.fastly_obj.delete!
end
end
+ act_any = !affected_services.empty?
+
unless act_any
puts "No difference."
end
+
+ puts
+
+ if activate
+ affected_services.each do |id|
+ version = present.service_version_get(id)
+ puts "ACTIVATE VERSION: #{version[:name]}.#{version[:dev]}"
+ if !dry_run
+ fastly.get_version(version[:id], version[:dev]).activate!
+ end
+ end
+ end
+
act_any
end
def creations
- new_keys = desired_element_keys - present_element_keys
- sort_elements(new_keys.map{ |_| desired.elements[_[0]][_[1]] })
+ @creations ||= begin
+ new_keys = desired_element_keys - present_element_keys
+ sort_elements(new_keys.map{ |_| desired.elements[_[0]][_[1]] })
+ end
end
def updates
- common = present_element_keys & desired_element_keys
+ @updates ||= begin
+ common = present_element_keys & desired_element_keys
- present_existing = common.map{ |_| present.elements[_[0]][_[1]] }
- desired_existing = common.map{ |_| desired.elements[_[0]][_[1]] }
+ present_existing = common.map{ |_| present.elements[_[0]][_[1]] }
+ desired_existing = common.map{ |_| desired.elements[_[0]][_[1]] }
- raise '!?' if present_existing.size != desired_existing.size
+ raise '!?' if present_existing.size != desired_existing.size
- present_existing.zip(desired_existing).map do |present_elem, desired_elem|
- if present_elem.as_hash != desired_elem.as_hash
- [present_elem, desired_elem]
- else
- nil
- end
- end.compact.sort_by { |_| ORDER.index(_[0].class) }
+ present_existing.zip(desired_existing).map do |present_elem, desired_elem|
+ if present_elem.as_hash != desired_elem.as_hash
+ [present_elem, desired_elem]
+ else
+ nil
+ end
+ end.compact.sort_by { |_| ORDER.index(_[0].class) }
+ end
end
def removals
- removed_keys = present_element_keys - desired_element_keys
- sort_elements(removed_keys.map{ |_| present.elements[_[0]][_[1]] })
+ @removals ||= begin
+ removed_keys = present_element_keys - desired_element_keys
+ sort_elements(removed_keys.map{ |_| present.elements[_[0]][_[1]] })
+ end
+ end
+
+ def affected_services
+ [*creations, *removals, *updates.map(&:first)].
+ select { |_| _.parent_class == Elements::Service }.
+ map(&:parent_key).
+ uniq
end
private
def present_elements