Class: Cheffish::ChefProviderBase

Inherits:
Chef::Provider::LWRPBase
  • Object
show all
Defined in:
lib/cheffish/chef_provider_base.rb

Defined Under Namespace

Classes: FakeEntry

Instance Method Summary collapse

Instance Method Details

#apply_modifiers(modifiers, json) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/cheffish/chef_provider_base.rb', line 122

def apply_modifiers(modifiers, json)
  return json if !modifiers || modifiers.size == 0

  # If the attributes have nothing, set them to {} so we have something to add to
  if json
    json = Marshal.load(Marshal.dump(json)) # Deep copy
  else
    json = {}
  end

  modifiers.each do |path, value|
    path = [path] if !path.kind_of?(Array)
    path = path.map { |path_part| path_part.to_s }
    parent = path[0..-2].inject(json) { |hash, path_part| hash ? hash[path_part] : nil }
    existing_value = parent ? parent[path[-1]] : nil

    if value.is_a?(Proc)
      value = value.call(existing_value)
    end
    if value == :delete
      parent.delete(path[-1]) if parent
      # TODO clean up parent chain if hash is completely emptied
    else
      if !parent
        # Create parent if necessary
        parent = path[0..-2].inject(json) do |hash, path_part|
          hash[path_part] = {} if !hash[path_part]
          hash[path_part]
        end
      end
      parent[path[-1]] = value
    end
  end
  json
end

#apply_run_list_modifiers(add_to_run_list, delete_from_run_list, run_list) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/cheffish/chef_provider_base.rb', line 158

def apply_run_list_modifiers(add_to_run_list, delete_from_run_list, run_list)
  return run_list if (!add_to_run_list || add_to_run_list.size == 0) && (!delete_from_run_list || !delete_from_run_list.size)
  delete_from_run_list ||= []
  add_to_run_list ||= []

  run_list = Chef::RunList.new(*run_list)

  result = []
  add_to_run_list_index = 0
  run_list_index = 0
  while run_list_index < run_list.run_list_items.size do
    # See if the desired run list has this item
    found_desired = add_to_run_list.index { |item| same_run_list_item(item, run_list[run_list_index]) }
    if found_desired
      # If so, copy all items up to that desired run list (to preserve order).
      # If a run list item is out of order (run_list = X, B, Y, A, Z and desired = A, B)
      # then this will give us X, A, B.  When A is found later, nothing will be copied
      # because found_desired will be less than add_to_run_list_index.  The result will
      # be X, A, B, Y, Z.
      if found_desired >= add_to_run_list_index
        result += add_to_run_list[add_to_run_list_index..found_desired].map { |item| item.to_s }
        add_to_run_list_index = found_desired+1
      end
    else
      # If not, just copy it in
      unless delete_from_run_list.index { |item| same_run_list_item(item, run_list[run_list_index]) }
        result << run_list[run_list_index].to_s
      end
    end
    run_list_index += 1
  end

  # Copy any remaining desired items at the end
  result += add_to_run_list[add_to_run_list_index..-1].map { |item| item.to_s }
  result
end

#augment_current_json(json) ⇒ Object

Meant to be overridden



56
57
58
# File 'lib/cheffish/chef_provider_base.rb', line 56

def augment_current_json(json)
  json
end

#augment_new_json(json) ⇒ Object

Meant to be overridden



43
44
45
# File 'lib/cheffish/chef_provider_base.rb', line 43

def augment_new_json(json)
  json
end

#current_jsonObject



47
48
49
50
51
52
53
# File 'lib/cheffish/chef_provider_base.rb', line 47

def current_json
  @current_json ||= begin
    result = normalize(resource_to_json(current_resource))
    result = augment_current_json(result)
    result
  end
end

#current_resource_exists?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/cheffish/chef_provider_base.rb', line 12

def current_resource_exists?
  Array(current_resource.action) != [ :delete ]
end

#json_differences(old_json, new_json, print_values = true, name = '', result = nil) ⇒ Object



83
84
85
86
87
# File 'lib/cheffish/chef_provider_base.rb', line 83

def json_differences(old_json, new_json, print_values=true, name = '', result = nil)
  result ||= []
  json_differences_internal(old_json, new_json, print_values, name, result)
  result
end

#json_differences_internal(old_json, new_json, print_values, name, result) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/cheffish/chef_provider_base.rb', line 89

def json_differences_internal(old_json, new_json, print_values, name, result)
  if old_json.kind_of?(Hash) && new_json.kind_of?(Hash)
    removed_keys = old_json.keys.inject({}) { |hash, key| hash[key] = true; hash }
    new_json.each_pair do |new_key, new_value|
      if old_json.has_key?(new_key)
        removed_keys.delete(new_key)
        if new_value != old_json[new_key]
          json_differences_internal(old_json[new_key], new_value, print_values, name == '' ? new_key : "#{name}.#{new_key}", result)
        end
      else
        if print_values
          result << "  add #{name == '' ? new_key : "#{name}.#{new_key}"} = #{new_value.inspect}"
        else
          result << "  add #{name == '' ? new_key : "#{name}.#{new_key}"}"
        end
      end
    end
    removed_keys.keys.each do |removed_key|
      result << "  remove #{name == '' ? removed_key : "#{name}.#{removed_key}"}"
    end
  else
    old_json = old_json.to_s if old_json.kind_of?(Symbol)
    new_json = new_json.to_s if new_json.kind_of?(Symbol)
    if old_json != new_json
      if print_values
        result << "  update #{name} from #{old_json.inspect} to #{new_json.inspect}"
      else
        result << "  update #{name}"
      end
    end
  end
end

#json_to_resource(json) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/cheffish/chef_provider_base.rb', line 69

def json_to_resource(json)
  resource = resource_class.new(new_resource.name, run_context)
  keys.each do |json_key, resource_key|
    resource.send(resource_key, json.delete(json_key))
  end
  # Set the leftover to raw_json
  resource.raw_json json
  resource
end

#new_jsonObject



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cheffish/chef_provider_base.rb', line 30

def new_json
  @new_json ||= begin
    if new_resource.complete
      result = normalize(resource_to_json(new_resource))
    else
      # If resource is incomplete, use current json to fill any holes
      result = current_json.merge(resource_to_json(new_resource))
    end
    augment_new_json(result)
  end
end

#normalize(json) ⇒ Object



79
80
81
# File 'lib/cheffish/chef_provider_base.rb', line 79

def normalize(json)
  data_handler.normalize(json, fake_entry)
end

#normalize_for_post(json) ⇒ Object



26
27
28
# File 'lib/cheffish/chef_provider_base.rb', line 26

def normalize_for_post(json)
  data_handler.normalize_for_post(json, fake_entry)
end

#normalize_for_put(json) ⇒ Object



22
23
24
# File 'lib/cheffish/chef_provider_base.rb', line 22

def normalize_for_put(json)
  data_handler.normalize_for_put(json, fake_entry)
end

#not_found_resourceObject



16
17
18
19
20
# File 'lib/cheffish/chef_provider_base.rb', line 16

def not_found_resource
  resource = resource_class.new(new_resource.name, run_context)
  resource.action :delete
  resource
end

#resource_to_json(resource) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/cheffish/chef_provider_base.rb', line 60

def resource_to_json(resource)
  json = resource.raw_json || {}
  keys.each do |json_key, resource_key|
    value = resource.send(resource_key)
    json[json_key] = value if value
  end
  json
end

#restObject



8
9
10
# File 'lib/cheffish/chef_provider_base.rb', line 8

def rest
  @rest ||= CheffishServerAPI.new(new_resource.chef_server)
end

#same_run_list_item(a, b) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/cheffish/chef_provider_base.rb', line 195

def same_run_list_item(a, b)
  a_name = a.name
  b_name = b.name
  # Handle "a::default" being the same as "a"
  if a.type == :recipe && a_name =~ /(.+)::default$/
    a_name = $1
  elsif b.type == :recipe && b_name =~ /(.+)::default$/
    b_name = $1
  end

  a_name == b_name && a.type == b.type # We want to replace things with same name and different version
end