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



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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/cheffish/chef_provider_base.rb', line 124

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 = 0.upto(path.size-2).inject(json) do |hash, index|
      if hash.nil?
        nil
      elsif !hash.is_a?(Hash)
        raise "Attempt to set #{path} to #{value} when #{path[0..index-1]} is not a hash"
      else
        hash[path[index]]
      end
    end
    if !parent.nil? && !parent.is_a?(Hash)
      raise "Attempt to set #{path} to #{value} when #{path[0..-2]} is not a hash"
    end
    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
    else
      # Create parent if necessary, overwriting values
      parent = path[0..-2].inject(json) do |hash, path_part|
        hash[path_part] = {} if !hash[path_part]
        hash[path_part]
      end
      if path.size > 0
        parent[path[-1]] = value
      else
        json = value
      end
    end
  end
  json
end

#apply_run_list_modifiers(add_to_run_list, delete_from_run_list, run_list) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/cheffish/chef_provider_base.rb', line 172

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



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

def augment_current_json(json)
  json
end

#augment_new_json(json) ⇒ Object

Meant to be overridden



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

def augment_new_json(json)
  json
end

#current_jsonObject



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

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)


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

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

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



85
86
87
88
89
# File 'lib/cheffish/chef_provider_base.rb', line 85

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



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
121
122
# File 'lib/cheffish/chef_provider_base.rb', line 91

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



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

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



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

def new_json
  @new_json ||= begin
    if new_resource.complete
      result = normalize(resource_to_json(new_resource))
    else
      # If the resource is incomplete, we use the 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



81
82
83
# File 'lib/cheffish/chef_provider_base.rb', line 81

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

#normalize_for_post(json) ⇒ Object



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

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

#normalize_for_put(json) ⇒ Object



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

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

#not_found_resourceObject



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

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

#resource_to_json(resource) ⇒ Object



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

def resource_to_json(resource)
  json = resource.raw_json || {}
  keys.each do |json_key, resource_key|
    value = resource.send(resource_key)
    # This takes care of Chef ImmutableMash and ImmutableArray
    value = value.to_hash if value.is_a?(Hash)
    value = value.to_a if value.is_a?(Array)
    json[json_key] = value if value
  end
  json
end

#restObject



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

def rest
  @rest ||= Cheffish.chef_server_api(new_resource.chef_server)
end

#same_run_list_item(a, b) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/cheffish/chef_provider_base.rb', line 209

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