diff options
author | John Keiser <john@johnkeiser.com> | 2015-12-03 17:07:35 -0800 |
---|---|---|
committer | Chris Doherty <cdoherty@chef.io> | 2015-12-07 14:33:18 -0800 |
commit | 82407750aa4b872c73baeae8afce42465b870a48 (patch) | |
tree | ec0fb8e4c504353272160c663c0e2ba9a7cb5887 /lib/chef_zero | |
parent | 4eaad3f905d963121d8bce2dd27f60ade40707c0 (diff) | |
download | chef-zero-82407750aa4b872c73baeae8afce42465b870a48.tar.gz |
Make policy groups and policies betterer
Diffstat (limited to 'lib/chef_zero')
-rw-r--r-- | lib/chef_zero/chef_data/data_normalizer.rb | 14 | ||||
-rw-r--r-- | lib/chef_zero/chef_data/default_creator.rb | 2 | ||||
-rw-r--r-- | lib/chef_zero/endpoints/organization_policies_endpoint.rb | 11 | ||||
-rw-r--r-- | lib/chef_zero/endpoints/organization_policy_groups_endpoint.rb | 10 | ||||
-rw-r--r-- | lib/chef_zero/endpoints/policies_endpoint.rb | 151 | ||||
-rw-r--r-- | lib/chef_zero/rspec.rb | 13 | ||||
-rw-r--r-- | lib/chef_zero/server.rb | 8 |
7 files changed, 50 insertions, 159 deletions
diff --git a/lib/chef_zero/chef_data/data_normalizer.rb b/lib/chef_zero/chef_data/data_normalizer.rb index 2dff91e..87e7c8a 100644 --- a/lib/chef_zero/chef_data/data_normalizer.rb +++ b/lib/chef_zero/chef_data/data_normalizer.rb @@ -166,6 +166,20 @@ module ChefZero node end + def self.normalize_policy(policy, name, revision) + policy['name'] ||= name + policy['revision_id'] ||= revision + policy['run_list'] ||= [] + policy['cookbook_locks'] ||= {} + policy + end + + def self.normalize_policy_group(policy_group, name) + policy_group[name] ||= 'name' + policy_group['policies'] ||= {} + policy_group + end + def self.normalize_organization(org, name) org['name'] ||= name org['full_name'] ||= name diff --git a/lib/chef_zero/chef_data/default_creator.rb b/lib/chef_zero/chef_data/default_creator.rb index 8a5b7fb..7b18d51 100644 --- a/lib/chef_zero/chef_data/default_creator.rb +++ b/lib/chef_zero/chef_data/default_creator.rb @@ -155,6 +155,8 @@ module ChefZero 'checksums' => {} }, 'nodes' => {}, + 'policies' => {}, + 'policy_groups' => {}, 'roles' => {}, 'sandboxes' => {}, 'users' => {}, diff --git a/lib/chef_zero/endpoints/organization_policies_endpoint.rb b/lib/chef_zero/endpoints/organization_policies_endpoint.rb index a495d9b..e03ba61 100644 --- a/lib/chef_zero/endpoints/organization_policies_endpoint.rb +++ b/lib/chef_zero/endpoints/organization_policies_endpoint.rb @@ -1,3 +1,4 @@ +require 'chef_zero/chef_data/data_normalizer' module ChefZero module Endpoints @@ -43,8 +44,9 @@ module ChefZero if !exists_data?(request, nil) return error(404, "Revision ID #{request.rest_path.last} not found" ) else - data = get_data(request) - return already_json_response(200, data) + data = parse_json(get_data(request)) + data = ChefData::DataNormalizer.normalize_policy(data, request.rest_path[3], request.rest_path[5]) + return json_response(200, data) end end end @@ -82,9 +84,10 @@ module ChefZero if request.rest_path[-2] == "revisions" if exists_data?(request) - policyfile_data = get_data(request) + policyfile_data = parse_json(get_data(request)) + policyfile_data = ChefData::DataNormalizer.normalize_policy(policyfile_data) delete_data(request) - return already_json_response(200, policyfile_data) + return json_response(200, policyfile_data) else return error(404, "Revision ID #{request.rest_path.last} not found") end diff --git a/lib/chef_zero/endpoints/organization_policy_groups_endpoint.rb b/lib/chef_zero/endpoints/organization_policy_groups_endpoint.rb index 51e0812..38e1481 100644 --- a/lib/chef_zero/endpoints/organization_policy_groups_endpoint.rb +++ b/lib/chef_zero/endpoints/organization_policy_groups_endpoint.rb @@ -1,5 +1,6 @@ require 'ffi_yajl' require 'chef_zero/rest_base' +require 'chef_zero/chef_data/data_normalizer' module ChefZero module Endpoints @@ -25,6 +26,7 @@ module ChefZero # vanilla /policy_groups. if request.rest_path.last == "policy_groups" policy_group_names = list_data_or_else(request, nil, []) + puts "List #{request.rest_path}: #{policy_group_names}" # no policy groups, so sad. if policy_group_names.size == 0 return already_json_response(200, '{}') @@ -77,7 +79,8 @@ module ChefZero revision_id = parse_json(get_data(request)) result = get_data(request, ["organizations", uri_params[:org_name], "policies", uri_params[:policy_name], "revisions", revision_id], :nil) - return already_json_response(200, result) + result = ChefData::DataNormalizer.normalize_policy(parse_json(result), uri_params[:policy_name], revision_id) + return json_response(200, result) end end # end get() @@ -246,8 +249,9 @@ module ChefZero policy_path = request.rest_path.first(2) + ["policies", request.rest_path.last, "revisions", current_revision_id] - full_policy_doc = get_data(request, policy_path) - return already_json_response(200, full_policy_doc) + full_policy_doc = parse_json(get_data(request, policy_path)) + full_policy_doc = ChefData::DataNormalizer.normalize_policy(full_policy_doc, request.rest_path.last, current_revision_id) + return json_response(200, full_policy_doc) end return error(404, "Don't know what to do with path #{request.rest_path}") diff --git a/lib/chef_zero/endpoints/policies_endpoint.rb b/lib/chef_zero/endpoints/policies_endpoint.rb deleted file mode 100644 index ddb2e9b..0000000 --- a/lib/chef_zero/endpoints/policies_endpoint.rb +++ /dev/null @@ -1,151 +0,0 @@ -require 'ffi_yajl' - -require 'chef_zero/endpoints/rest_object_endpoint' -require 'chef_zero/chef_data/data_normalizer' - -module ChefZero - module Endpoints - # /policies/:group/:name - class PoliciesEndpoint < RestObjectEndpoint - def initialize(server) - super(server, 'id') - end - - def get(request) - already_json_response(200, get_data(request)) - end - - # Right now we're allowing PUT to create. - def put(request) - error = validate(request) - return error if error - - code = - if data_store.exists?(request.rest_path) - set_data(request, request.rest_path, request.body, :data_store_exceptions) - 200 - else - name = request.rest_path[4] - data_store.create(request.rest_path[0..3], name, request.body, :create_dir) - 201 - end - already_json_response(code, request.body) - end - - def delete(request) - result = get_data(request, request.rest_path) - delete_data(request, request.rest_path, :data_store_exceptions) - already_json_response(200, result) - end - - private - - def validate(request) - req_object = validate_json(request.body) - validate_revision_id(request, req_object) || - validate_name(request, req_object) || - validate_run_list(req_object) || - validate_each_run_list_item(req_object) || - validate_cookbook_locks_collection(req_object) || - validate_each_cookbook_locks_item(req_object) - end - - def validate_json(request_body) - FFI_Yajl::Parser.parse(request_body) - # TODO: rescue parse error, return 400 - # error(400, "Must specify #{identity_keys.map { |k| k.inspect }.join(' or ')} in JSON") - end - - def validate_revision_id(request, req_object) - if !req_object.key?("revision_id") - error(400, "Field 'revision_id' missing") - elsif req_object["revision_id"].empty? - error(400, "Field 'revision_id' invalid") - elsif req_object["revision_id"].size > 255 - error(400, "Field 'revision_id' invalid") - elsif req_object["revision_id"] !~ /^[\-[:alnum:]_\.\:]+$/ - error(400, "Field 'revision_id' invalid") - end - end - - def validate_name(request, req_object) - if !req_object.key?("name") - error(400, "Field 'name' missing") - elsif req_object["name"] != (uri_policy_name = URI.decode(request.rest_path[4])) - error(400, "Field 'name' invalid : #{uri_policy_name} does not match #{req_object["name"]}") - elsif req_object["name"].size > 255 - error(400, "Field 'name' invalid") - elsif req_object["name"] !~ /^[\-[:alnum:]_\.\:]+$/ - error(400, "Field 'name' invalid") - end - end - - def validate_run_list(req_object) - if !req_object.key?("run_list") - error(400, "Field 'run_list' missing") - elsif !req_object["run_list"].kind_of?(Array) - error(400, "Field 'run_list' is not a valid run list") - end - end - - def validate_each_run_list_item(req_object) - req_object["run_list"].each do |run_list_item| - if res_400 = validate_run_list_item(run_list_item) - return res_400 - end - end - nil - end - - def validate_run_list_item(run_list_item) - if !run_list_item.kind_of?(String) - error(400, "Field 'run_list' is not a valid run list") - elsif run_list_item !~ /\Arecipe\[[^\s]+::[^\s]+\]\Z/ - error(400, "Field 'run_list' is not a valid run list") - end - end - - def validate_cookbook_locks_collection(req_object) - if !req_object.key?("cookbook_locks") - error(400, "Field 'cookbook_locks' missing") - elsif !req_object["cookbook_locks"].kind_of?(Hash) - error(400, "Field 'cookbook_locks' invalid") - end - end - - def validate_each_cookbook_locks_item(req_object) - req_object["cookbook_locks"].each do |cookbook_name, lock| - if res_400 = validate_cookbook_locks_item(cookbook_name, lock) - return res_400 - end - end - nil - end - - def validate_cookbook_locks_item(cookbook_name, lock) - if !lock.kind_of?(Hash) - error(400, "cookbook_lock entries must be a JSON object") - elsif !lock.key?("identifier") - error(400, "Field 'identifier' missing") - elsif lock["identifier"].size > 255 - error(400, "Field 'identifier' invalid") - elsif !lock.key?("version") - error(400, "Field 'version' missing") - elsif lock.key?("dotted_decimal_identifier") - unless valid_version?(lock["dotted_decimal_identifier"]) - error(400, "Field 'dotted_decimal_identifier' is not a valid version") - end - end - end - - def valid_version?(version_string) - Gem::Version.new(version_string) - true - rescue ArgumentError - false - end - - end - end -end - diff --git a/lib/chef_zero/rspec.rb b/lib/chef_zero/rspec.rb index 29192b3..12d5ebf 100644 --- a/lib/chef_zero/rspec.rb +++ b/lib/chef_zero/rspec.rb @@ -153,6 +153,10 @@ module ChefZero before(chef_server_options[:server_scope]) { node(name, data, &block) } end + def policy_group(name, data, &block) + before(chef_server_options[:server_scope]) { policy_group(name, data, &block) } + end + def role(name, data, &block) before(chef_server_options[:server_scope]) { role(name, data, &block) } end @@ -255,13 +259,20 @@ module ChefZero ChefZero::RSpec.server.load_data({ 'members' => usernames }, current_org) end - def policy(name, version, data) + def policy(name, version, data, &block) with_object_path("policies/#{name}") do ChefZero::RSpec.server.load_data({ 'policies' => { name => { version => data } } }, current_org) instance_eval(&block) if block_given? end end + def policy_group(name, data, &block) + with_object_path("policy_groups/#{name}") do + ChefZero::RSpec.server.load_data({ 'policy_groups' => { name => data } }, current_org) + instance_eval(&block) if block_given? + end + end + def role(name, data, &block) with_object_path("roles/#{name}") do ChefZero::RSpec.server.load_data({ 'roles' => { name => data } }, current_org) diff --git a/lib/chef_zero/server.rb b/lib/chef_zero/server.rb index 301ca5d..e882369 100644 --- a/lib/chef_zero/server.rb +++ b/lib/chef_zero/server.rb @@ -457,6 +457,14 @@ module ChefZero end end + if contents['policy_groups'] + contents['policy_groups'].each_pair do |group_name, group| + group['policies'].each do |policy_name, policy_revision| + data_store.set(['organizations', org_name, 'policy_groups', group_name, 'policies', policy_name], FFI_Yajl::Encoder.encode(policy_revision['revision_id'], :pretty => true), :create, :create_dir) + end + end + end + if contents['cookbooks'] contents['cookbooks'].each_pair do |name_version, cookbook| if name_version =~ /(.+)-(\d+\.\d+\.\d+)$/ |