Method: Chef::ChefFS::ChefFSDataStore#create

Defined in:
lib/chef/chef_fs/chef_fs_data_store.rb

#create(path, name, data, *options) ⇒ Object

If you want to get the contents of /data/x/y from the server, you say chef_fs.child('data').child('x').child('y').read. It will make exactly one network request: GET /data/x/y And that will return 404 if it doesn't exist.

ChefFS objects do not go to the network until you ask them for data. This means you can construct a /data/x/y ChefFS entry early.

Alternative: chef_fs.child('data') could have done a GET /data preemptively, allowing it to know whether child('x') was valid (GET /data gives you a list of data bags). Then child('x') could have done a GET /data/x, allowing it to know whether child('y') (the item) existed. Finally, we would do the GET /data/x/y to read the contents. Three network requests instead of 1.



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/chef/chef_fs/chef_fs_data_store.rb', line 202

def create(path, name, data, *options)
  if use_memory_store?(path)
    @memory_store.create(path, name, data, *options)

  elsif path[0] == "cookbooks" && path.length == 2
  # Do nothing.  The entry gets created when the cookbook is created.

  # /policy_groups/GROUP/policies/NAME
  elsif path[0] == "policy_groups" && path[2] == "policies"
    # Just set or create the proper entry in the hash
    update_json(to_chef_fs_path(path[0..1]), {}, *options) do |group|
      if policies.key?(path[3])
        raise ChefZero::DataStore::DataAlreadyExistsError.new(path, group)
      end

      group["policies"] ||= {}
      group["policies"][path[3]] = { "revision_id" => Chef::JSONCompat.parse(data) }
      group
    end

  # create [/organizations/ORG]/users/NAME (with content '{}')
  # Manipulate the `members.json` file that contains a list of all users
  elsif is_org? && path == [ "users" ]
    update_json("members.json", [], *options) do |members|
      # Format of each entry: { "user": { "username": "jkeiser" } }
      if members.any? { |member| member["user"]["username"] == name }
        raise ChefZero::DataStore::DataAlreadyExistsError.new(path, entry)
      end

      # Actually add the user
      members << { "user" => { "username" => name } }
    end

  # create [/organizations/ORG]/association_requests/NAME (with content '{}')
  # Manipulate the `invitations.json` file that contains a list of all users
  elsif is_org? && path == [ "association_requests" ]
    update_json("invitations.json", [], *options) do |invitations|
      # Format of each entry: { "id" => "jkeiser-chef", 'username' => 'jkeiser' }
      if invitations.any? { |member| member["username"] == name }
        raise ChefZero::DataStore::DataAlreadyExistsError.new(path)
      end

      # Actually add the user (TODO insert org name??)
      invitations << { "username" => name }
    end

  else
    unless data.is_a?(String)
      raise "set only works with strings"
    end

    with_parent_dir(path + [name], *options) do |parent, name|

      parent.create_child(name, data)
    rescue Chef::ChefFS::FileSystem::AlreadyExistsError => e
      raise ChefZero::DataStore::DataAlreadyExistsError.new(to_zero_path(e.entry), e)

    end
  end
end