Class: ChefZero::ChefData::DataNormalizer

Inherits:
Object
  • Object
show all
Defined in:
lib/chef_zero/chef_data/data_normalizer.rb

Constant Summary collapse

COOKBOOK_SEGMENTS =
%w{ resources providers recipes definitions libraries attributes files templates root_files }.freeze

Class Method Summary collapse

Class Method Details

.normalize_acls(acls) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/chef_zero/chef_data/data_normalizer.rb', line 11

def self.normalize_acls(acls)
  ChefData::DefaultCreator::PERMISSIONS.each do |perm|
    acls[perm] ||= {}
    acls[perm]["groups"] ||= []
    if acls[perm].key? "users"
      # When clients and users are split, their combined list
      # is the final list of actors that a subsequent GET will
      # provide. Each list is guaranteed to be unique, but the
      # combined list is not.
      acls[perm]["actors"] = acls[perm]["clients"].uniq +
        acls[perm]["users"].uniq
    else
      # this gets doubled sometimes, for reasons.
      (acls[perm]["actors"] ||= []).uniq!
    end
  end
  acls
end

.normalize_client(client, name, orgname = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/chef_zero/chef_data/data_normalizer.rb', line 30

def self.normalize_client(client, name, orgname = nil)
  client["name"] ||= name
  client["clientname"] ||= name
  client["admin"] = !!client["admin"] if client.key?("admin")
  client["public_key"] = PUBLIC_KEY unless client.key?("public_key")
  client["orgname"] ||= orgname
  client["validator"] ||= false
  client["validator"] = !!client["validator"]
  client["json_class"] ||= "Chef::ApiClient"
  client["chef_type"] ||= "client"
  client
end

.normalize_container(container, name) ⇒ Object



43
44
45
46
47
48
# File 'lib/chef_zero/chef_data/data_normalizer.rb', line 43

def self.normalize_container(container, name)
  container.delete("id")
  container["containername"] = name
  container["containerpath"] = name
  container
end

.normalize_cookbook(endpoint, org_prefix, cookbook, name, version, base_uri, method, is_cookbook_artifact = false, api_version: 2) ⇒ Object



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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/chef_zero/chef_data/data_normalizer.rb', line 95

def self.normalize_cookbook(endpoint, org_prefix, cookbook, name, version, base_uri, method,
  is_cookbook_artifact = false, api_version: 2)
  # TODO I feel dirty
  if method == "PUT" && api_version < 2
    cookbook["all_files"] = cookbook.delete(["root_files"]) { [] }
    COOKBOOK_SEGMENTS.each do |segment|
      next unless cookbook.key? segment

      cookbook[segment].each do |file|
        file["name"] = "#{segment}/#{file["name"]}"
        cookbook["all_files"] << file
      end
      cookbook.delete(segment)
    end
  elsif method != "PUT"
    if cookbook.key? "all_files"
      cookbook["all_files"].each do |file|
        if file.is_a?(Hash) && file.key?("checksum")
          file["url"] ||= endpoint.build_uri(base_uri, org_prefix + ["file_store", "checksums", file["checksum"]])
        end
      end

      # down convert to old style manifest, ensuring we don't send all_files on the wire and that we correctly divine segments
      # any file that's not in an old segment is just dropped on the floor.
      if api_version < 2

        # the spec appears to think we should send empty arrays for each segment, so let's do that
        COOKBOOK_SEGMENTS.each { |seg| cookbook[seg] ||= [] }

        cookbook["all_files"].each do |file|
          segment, name = file["name"].split("/")

          # root_files have no segment prepended
          if name.nil?
            name = segment
            segment = "root_files"
          end

          file.delete("full_path")
          next unless COOKBOOK_SEGMENTS.include? segment

          file["name"] = name
          cookbook[segment] << file
        end
        cookbook.delete("all_files")
      end
    end

    cookbook["name"] ||= "#{name}-#{version}"
    # TODO it feels wrong, but the real chef server doesn't expand 'version', so we don't either.

    cookbook["frozen?"] ||= false
    cookbook["metadata"] ||= {}
    cookbook["metadata"]["version"] ||= version

    # defaults set by the client and not the Server:
    # metadata[name, description, maintainer, maintainer_email, license]

    cookbook["metadata"]["long_description"] ||= ""
    cookbook["metadata"]["dependencies"] ||= {}
    cookbook["metadata"]["attributes"] ||= {}
    cookbook["metadata"]["recipes"] ||= {}
  end

  if is_cookbook_artifact
    cookbook.delete("json_class")
  else
    cookbook["cookbook_name"] ||= name
    cookbook["json_class"] ||= "Chef::CookbookVersion"
  end

  cookbook["chef_type"] ||= "cookbook_version"
  if method == "MIN"
    cookbook["metadata"].delete("attributes")
    cookbook["metadata"].delete("long_description")
  end
  cookbook
end

.normalize_data_bag_item(data_bag_item, data_bag_name, id, method) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/chef_zero/chef_data/data_normalizer.rb', line 69

def self.normalize_data_bag_item(data_bag_item, data_bag_name, id, method)
  if method == "DELETE"
    # TODO SERIOUSLY, WHO DOES THIS MANY EXCEPTIONS IN THEIR INTERFACE
    unless data_bag_item["json_class"] == "Chef::DataBagItem" && data_bag_item["raw_data"]
      data_bag_item["id"] ||= id
      data_bag_item = { "raw_data" => data_bag_item }
      data_bag_item["chef_type"] ||= "data_bag_item"
      data_bag_item["json_class"] ||= "Chef::DataBagItem"
      data_bag_item["data_bag"] ||= data_bag_name
      data_bag_item["name"] ||= "data_bag_item_#{data_bag_name}_#{id}"
    end
  else
    # If it's not already wrapped with raw_data, wrap it.
    if data_bag_item["json_class"] == "Chef::DataBagItem" && data_bag_item["raw_data"]
      data_bag_item = data_bag_item["raw_data"]
    end
    # Argh.  We don't do this on GET, but we do on PUT and POST????
    if %w{PUT POST}.include?(method)
      data_bag_item["chef_type"] ||= "data_bag_item"
      data_bag_item["data_bag"] ||= data_bag_name
    end
    data_bag_item["id"] ||= id
  end
  data_bag_item
end

.normalize_environment(environment, name) ⇒ Object



174
175
176
177
178
179
180
181
182
183
# File 'lib/chef_zero/chef_data/data_normalizer.rb', line 174

def self.normalize_environment(environment, name)
  environment["name"] ||= name
  environment["description"] ||= ""
  environment["cookbook_versions"] ||= {}
  environment["json_class"] ||= "Chef::Environment"
  environment["chef_type"] ||= "environment"
  environment["default_attributes"] ||= {}
  environment["override_attributes"] ||= {}
  environment
end

.normalize_group(group, name, orgname) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/chef_zero/chef_data/data_normalizer.rb', line 185

def self.normalize_group(group, name, orgname)
  group.delete("id")
  if group["actors"].is_a?(Hash)
    group["users"] ||= group["actors"]["users"]
    group["clients"] ||= group["actors"]["clients"]
    group["groups"] ||= group["actors"]["groups"]
    group["actors"] = nil
  end
  group["users"] ||= []
  group["clients"] ||= []
  group["actors"] ||= (group["clients"] + group["users"])
  group["groups"] ||= []
  group["orgname"] ||= orgname if orgname
  group["name"] ||= name
  group["groupname"] ||= name

  group["users"].uniq!
  group["clients"].uniq!
  group["actors"].uniq!
  group["groups"].uniq!
  group
end

.normalize_node(node, name) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/chef_zero/chef_data/data_normalizer.rb', line 208

def self.normalize_node(node, name)
  node["name"] ||= name
  node["json_class"] ||= "Chef::Node"
  node["chef_type"] ||= "node"
  node["chef_environment"] ||= "_default"
  node["override"] ||= {}
  node["normal"] ||= { "tags" => [] }
  node["default"] ||= {}
  node["automatic"] ||= {}
  node["run_list"] ||= []
  node["run_list"] = normalize_run_list(node["run_list"])
  node
end

.normalize_organization(org, name) ⇒ Object



236
237
238
239
240
241
242
243
# File 'lib/chef_zero/chef_data/data_normalizer.rb', line 236

def self.normalize_organization(org, name)
  org["name"] ||= name
  org["full_name"] ||= name
  org["org_type"] ||= "Business"
  org["clientname"] ||= "#{name}-validator"
  org["billing_plan"] ||= "platform-free"
  org
end

.normalize_policy(policy, name, revision) ⇒ Object



222
223
224
225
226
227
228
# File 'lib/chef_zero/chef_data/data_normalizer.rb', line 222

def self.normalize_policy(policy, name, revision)
  policy["name"] ||= name
  policy["revision_id"] ||= revision
  policy["run_list"] ||= []
  policy["cookbook_locks"] ||= {}
  policy
end

.normalize_policy_group(policy_group, name) ⇒ Object



230
231
232
233
234
# File 'lib/chef_zero/chef_data/data_normalizer.rb', line 230

def self.normalize_policy_group(policy_group, name)
  policy_group[name] ||= "name"
  policy_group["policies"] ||= {}
  policy_group
end

.normalize_role(role, name) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/chef_zero/chef_data/data_normalizer.rb', line 245

def self.normalize_role(role, name)
  role["name"] ||= name
  role["description"] ||= ""
  role["json_class"] ||= "Chef::Role"
  role["chef_type"] ||= "role"
  role["default_attributes"] ||= {}
  role["override_attributes"] ||= {}
  role["run_list"] ||= []
  role["run_list"] = normalize_run_list(role["run_list"])
  role["env_run_lists"] ||= {}
  role["env_run_lists"].each_pair do |env, run_list|
    role["env_run_lists"][env] = normalize_run_list(run_list)
  end
  role
end

.normalize_run_list(run_list) ⇒ Object



261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/chef_zero/chef_data/data_normalizer.rb', line 261

def self.normalize_run_list(run_list)
  run_list.map do |item|
    case item
    when /^recipe\[.*\]$/
      item # explicit recipe
    when /^role\[.*\]$/
      item # explicit role
    else
      "recipe[#{item}]"
    end
  end.uniq
end

.normalize_user(user, name, identity_keys, osc_compat, method = nil) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/chef_zero/chef_data/data_normalizer.rb', line 50

def self.normalize_user(user, name, identity_keys, osc_compat, method = nil)
  user[identity_keys.first] ||= name
  user["public_key"] = PUBLIC_KEY unless user.key?("public_key")
  user["admin"] ||= false
  user["admin"] = !!user["admin"]
  user["openid"] ||= nil
  unless osc_compat
    if method == "GET"
      user.delete("admin")
      user.delete("password")
      user.delete("openid")
    end
    user["email"] ||= nil
    user["first_name"] ||= nil
    user["last_name"] ||= nil
  end
  user
end