Class: Kongfigure::Synchronizers::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/kongfigure/synchronizers/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http_client, resources, parent_resource = nil) ⇒ Base

Returns a new instance of Base.



8
9
10
11
12
13
# File 'lib/kongfigure/synchronizers/base.rb', line 8

def initialize(http_client, resources, parent_resource=nil)
  @http_client      = http_client
  @parent_resource  = parent_resource
  @resources        = resources
  @remote_resources = load_all_remote_resources
end

Instance Attribute Details

#parent_resourceObject (readonly)

Returns the value of attribute parent_resource.



6
7
8
# File 'lib/kongfigure/synchronizers/base.rb', line 6

def parent_resource
  @parent_resource
end

#remote_resourcesObject (readonly)

Returns the value of attribute remote_resources.



6
7
8
# File 'lib/kongfigure/synchronizers/base.rb', line 6

def remote_resources
  @remote_resources
end

#resourcesObject (readonly)

Returns the value of attribute resources.



6
7
8
# File 'lib/kongfigure/synchronizers/base.rb', line 6

def resources
  @resources
end

Instance Method Details

#cleanup(remote_resource) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/kongfigure/synchronizers/base.rb', line 54

def cleanup(remote_resource)
  puts "#{parent_resource.nil? ? '' : '  *'}#{'---'.colorize(:red)} #{remote_resource.display_name}"
  path = if parent_resource
    "#{parent_resource.api_name}/#{parent_resource.identifier}/#{resource_api_name}/#{remote_resource.identifier}"
  else
    "#{resource_api_name}/#{remote_resource.identifier}"
  end
  @http_client.delete(path)
end

#create(resource) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/kongfigure/synchronizers/base.rb', line 64

def create(resource)
  puts "#{parent_resource.nil? ? '' : '  *'}#{'+++'.colorize(:green)} #{resource.display_name}"
  path = if parent_resource
    "#{parent_resource.api_name}/#{parent_resource.identifier}/#{resource_api_name}"
  else
    "#{resource_api_name}"
  end
  @http_client.post(path, resource.api_attributes.to_json)
end

#find(identifier) ⇒ Object



15
16
17
18
19
20
# File 'lib/kongfigure/synchronizers/base.rb', line 15

def find(identifier)
  module_name = Kongfigure::Resources.const_get(self.class.to_s.split("::").last)
  module_name.build(@http_client.get("#{resource_api_name}/#{identifier}"))
rescue Kongfigure::Errors::ResourceNotFound
  nil
end

#synchronize(resource) ⇒ Object



40
41
42
43
# File 'lib/kongfigure/synchronizers/base.rb', line 40

def synchronize(resource)
  synchronize_resource(resource)
  synchronize_plugins(resource)
end

#synchronize_allObject



45
46
47
48
49
50
51
52
# File 'lib/kongfigure/synchronizers/base.rb', line 45

def synchronize_all
  @resources.each do |resource|
    synchronize(resource)
  end
  @remote_resources.each do |remote_resource|
    cleanup(remote_resource) if remote_resource.has_to_be_deleted?
  end
end

#synchronize_plugins(resource) ⇒ Object



35
36
37
38
# File 'lib/kongfigure/synchronizers/base.rb', line 35

def synchronize_plugins(resource)
  plugins_synchronizer = Kongfigure::Synchronizers::Plugin.new(@http_client, resource.plugins, resource)
  plugins_synchronizer.synchronize_all
end

#synchronize_resource(resource) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/kongfigure/synchronizers/base.rb', line 22

def synchronize_resource(resource)
  remote_resource = find_related_remote_resource(resource)
  if remote_resource.nil?
    create(resource)
  elsif resource == remote_resource
    unchanged(resource)
    remote_resource.mark_as_unchanged
  else
    update(resource, remote_resource)
    remote_resource.mark_as_updated
  end
end

#unchanged(resource) ⇒ Object



74
75
76
# File 'lib/kongfigure/synchronizers/base.rb', line 74

def unchanged(resource)
  puts "#{parent_resource.nil? ? '' : '  *'}#{'==='.colorize(:blue)} #{resource.display_name}"
end

#update(resource, remote_resource) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/kongfigure/synchronizers/base.rb', line 78

def update(resource, remote_resource)
  puts "#{'uuu'.colorize(:yellow)} #{resource.display_name}"
  path = if parent_resource
    "#{parent_resource.api_name}/#{parent_resource.identifier}/#{resource_api_name}/#{remote_resource.identifier}"
  else
    "#{resource_api_name}/#{remote_resource.identifier}"
  end
  @http_client.put(path, resource.api_attributes.to_json)
end