spec/support/chef_api.rb in berkshelf-0.3.7 vs spec/support/chef_api.rb in berkshelf-0.4.0.rc1
- old
+ new
@@ -3,10 +3,37 @@
require 'chef/cookbook_version'
module Berkshelf
module RSpec
module ChefAPI
+ # Return an array of Hashes containing cookbooks and their information
+ #
+ # @return [Array]
+ def get_cookbooks
+ rest.get_rest("cookbooks")
+ end
+
+ def upload_cookbook(path)
+ cached = CachedCookbook.from_store_path(path)
+ uploader.upload!(cached)
+ end
+
+ # Remove all versions of all cookbooks from the Chef Server defined in your
+ # Knife config.
+ def purge_cookbooks
+ get_cookbooks.each do |name, info|
+ info["versions"].each do |version_info|
+ rest.delete_rest("cookbooks/#{name}/#{version_info["version"]}?purge=true")
+ end
+ end
+ end
+
+ # Remove the version of the given cookbook from the Chef Server defined
+ # in your Knife config.
+ #
+ # @param [#to_s] name
+ # @param [#to_s] version
def purge_cookbook(name, version)
rest.delete_rest("cookbooks/#{name}/#{version}?purge=true")
rescue Net::HTTPServerException => e
raise unless e.to_s =~ /^404/
end
@@ -55,15 +82,20 @@
metadata << "depends '#{name}', '#{constraint}'\n"
end
end
File.write(cookbook_path.join("metadata.rb"), metadata)
+ cookbook_path
end
private
def rest
quietly { Chef::REST.new(Chef::Config[:chef_server_url]) }
+ end
+
+ def uploader
+ @uploader ||= Berkshelf::Uploader.new(Chef::Config[:chef_server_url])
end
end
end
end