lib/ridley/resources/cookbook_resource.rb in ridley-0.8.6 vs lib/ridley/resources/cookbook_resource.rb in ridley-0.9.0

- old
+ new

@@ -27,14 +27,10 @@ cookbooks[name] = details["versions"].collect { |version| version["version"] } end end end - def create(*args) - raise NotImplementedError - end - # Delete a cookbook of the given name and version on the remote Chef server # # @param [Ridley::Client] client # @param [String] name # @param [String] version @@ -138,41 +134,44 @@ find(client, name, version) rescue Solve::Errors::NoSolutionError nil end - # Save a new Cookbook Version of the given name, version with the + # Update or create a new Cookbook Version of the given name, version with the # given manifest of files and checksums. # # @param [Ridley::Client] client - # @param [String] name - # @param [String] version - # @param [String] manifest - # a JSON blob containing file names, file paths, and checksums for each - # that describe the cookbook version being uploaded. + # @param [Ridley::Chef::Cookbook] cookbook + # the cookbook to save # # @option options [Boolean] :force # Upload the Cookbook even if the version already exists and is frozen on # the target Chef Server # @option options [Boolean] :freeze # Freeze the uploaded Cookbook on the Chef Server so that it cannot be # overwritten # + # @raise [Ridley::Errors::FrozenCookbook] + # if a cookbook of the same name and version already exists on the remote Chef server + # and is frozen. If the :force option is provided the given cookbook will be saved + # regardless. + # # @return [Hash] - def save(client, name, version, manifest, options = {}) + def update(client, cookbook, options = {}) options.reverse_merge(force: false, freeze: false) - url = "cookbooks/#{name}/#{version}" + cookbook.frozen = options[:freeze] + + url = "cookbooks/#{cookbook.cookbook_name}/#{cookbook.version}" url << "?force=true" if options[:force] - client.connection.put(url, manifest) + client.connection.put(url, cookbook.to_json) + rescue Ridley::Errors::HTTPConflict => ex + raise Ridley::Errors::FrozenCookbook, ex end + alias_method :create, :update - def update(*args) - raise NotImplementedError - end - # Uploads a cookbook to the remote Chef server from the contents of a filepath # # @param [Ridley::Client] client # @param [String] path # path to a cookbook on local disk @@ -193,21 +192,28 @@ # @return [Hash] def upload(client, path, options = {}) options = options.reverse_merge(validate: true, force: false, freeze: false) cookbook = Ridley::Chef::Cookbook.from_path(path, options.slice(:name)) + unless (existing = find(client, cookbook.cookbook_name, cookbook.version)).nil? + if existing.frozen? && options[:force] == false + msg = "The cookbook #{cookbook.cookbook_name} (#{cookbook.version}) already exists and is" + msg << " frozen on the Chef server. Use the 'force' option to override." + raise Ridley::Errors::FrozenCookbook, msg + end + end + if options[:validate] cookbook.validate end - name = options[:name] || cookbook.name checksums = cookbook.checksums.dup sandbox = client.sandbox.create(checksums.keys) sandbox.upload(checksums) sandbox.commit - save(client, name, cookbook.version, cookbook.to_json, options.slice(:force, :freeze)) + update(client, cookbook, options.slice(:force, :freeze)) end # Return a list of versions for the given cookbook present on the remote Chef server # # @param [Ridley::Client] client @@ -290,9 +296,12 @@ type: Array, default: Array.new attribute :version, type: String + + attribute :frozen?, + type: Boolean # Download the entire cookbook # # @param [String] destination (Dir.mktmpdir) # the place to download the cookbook too. If no value is provided the cookbook