From e3502878f663c927b847a83cd7391db6695ad3e8 Mon Sep 17 00:00:00 2001 From: John Keiser Date: Thu, 24 Jul 2014 19:23:45 -0600 Subject: Allow groups and containers to be indexed by either of 2 keys --- lib/chef_zero/data_normalizer.rb | 4 ++-- lib/chef_zero/endpoints/actor_endpoint.rb | 2 +- lib/chef_zero/endpoints/container_endpoint.rb | 2 +- lib/chef_zero/endpoints/containers_endpoint.rb | 2 +- lib/chef_zero/endpoints/data_bags_endpoint.rb | 5 +++-- lib/chef_zero/endpoints/group_endpoint.rb | 2 +- lib/chef_zero/endpoints/groups_endpoint.rb | 2 +- lib/chef_zero/endpoints/rest_list_endpoint.rb | 12 +++++++----- lib/chef_zero/endpoints/rest_object_endpoint.rb | 10 ++++++---- lib/chef_zero/rest_router.rb | 1 + 10 files changed, 24 insertions(+), 18 deletions(-) diff --git a/lib/chef_zero/data_normalizer.rb b/lib/chef_zero/data_normalizer.rb index 8f592d4..cd4f40a 100644 --- a/lib/chef_zero/data_normalizer.rb +++ b/lib/chef_zero/data_normalizer.rb @@ -39,8 +39,8 @@ module ChefZero container end - def self.normalize_user(user, name, identity_key) - user[identity_key] ||= name + def self.normalize_user(user, name, identity_keys) + user[identity_keys.first] ||= name user['admin'] ||= false user['admin'] = !!user['admin'] user['openid'] ||= nil diff --git a/lib/chef_zero/endpoints/actor_endpoint.rb b/lib/chef_zero/endpoints/actor_endpoint.rb index aa07373..6bf151d 100644 --- a/lib/chef_zero/endpoints/actor_endpoint.rb +++ b/lib/chef_zero/endpoints/actor_endpoint.rb @@ -55,7 +55,7 @@ module ChefZero if request.rest_path[2] == 'clients' response = DataNormalizer.normalize_client(response, request.rest_path[3]) else - response = DataNormalizer.normalize_user(response, request.rest_path[3], identity_key) + response = DataNormalizer.normalize_user(response, request.rest_path[3], identity_keys) end JSON.pretty_generate(response) end diff --git a/lib/chef_zero/endpoints/container_endpoint.rb b/lib/chef_zero/endpoints/container_endpoint.rb index 696697c..685ddc6 100644 --- a/lib/chef_zero/endpoints/container_endpoint.rb +++ b/lib/chef_zero/endpoints/container_endpoint.rb @@ -7,7 +7,7 @@ module ChefZero # /organizations/ORG/containers/NAME class ContainerEndpoint < RestObjectEndpoint def initialize(server) - super(server, 'containername') + super(server, %w(id containername)) end def populate_defaults(request, response_json) diff --git a/lib/chef_zero/endpoints/containers_endpoint.rb b/lib/chef_zero/endpoints/containers_endpoint.rb index 433193d..82ffbfc 100644 --- a/lib/chef_zero/endpoints/containers_endpoint.rb +++ b/lib/chef_zero/endpoints/containers_endpoint.rb @@ -6,7 +6,7 @@ module ChefZero # /organizations/ORG/containers class ContainersEndpoint < RestListEndpoint def initialize(server) - super(server, 'containername') + super(server, %w(id containername)) end end end diff --git a/lib/chef_zero/endpoints/data_bags_endpoint.rb b/lib/chef_zero/endpoints/data_bags_endpoint.rb index a27b35d..c3e5970 100644 --- a/lib/chef_zero/endpoints/data_bags_endpoint.rb +++ b/lib/chef_zero/endpoints/data_bags_endpoint.rb @@ -7,9 +7,10 @@ module ChefZero class DataBagsEndpoint < RestListEndpoint def post(request) contents = request.body - name = JSON.parse(contents, :create_additions => false)[identity_key] + json = JSON.parse(contents, :create_additions => false) + name = identity_keys.map { |k| json[k] }.select { |v| v }.first if name.nil? - error(400, "Must specify '#{identity_key}' in JSON") + error(400, "Must specify #{identity_keys.map { |k| k.inspect }.join(' or ')} in JSON") elsif exists_data_dir?(request, request.rest_path[0..1] + ['data', name]) error(409, "Object already exists") else diff --git a/lib/chef_zero/endpoints/group_endpoint.rb b/lib/chef_zero/endpoints/group_endpoint.rb index fbd9e08..74d55f6 100644 --- a/lib/chef_zero/endpoints/group_endpoint.rb +++ b/lib/chef_zero/endpoints/group_endpoint.rb @@ -7,7 +7,7 @@ module ChefZero # /organizations/ORG/groups/NAME class GroupEndpoint < RestObjectEndpoint def initialize(server) - super(server, 'groupname') + super(server, %w(id groupname)) end def populate_defaults(request, response_json) diff --git a/lib/chef_zero/endpoints/groups_endpoint.rb b/lib/chef_zero/endpoints/groups_endpoint.rb index b35701f..9683824 100644 --- a/lib/chef_zero/endpoints/groups_endpoint.rb +++ b/lib/chef_zero/endpoints/groups_endpoint.rb @@ -6,7 +6,7 @@ module ChefZero # /organizations/ORG/groups/NAME class GroupsEndpoint < RestListEndpoint def initialize(server) - super(server, 'groupname') + super(server, %w(id groupname)) end end end diff --git a/lib/chef_zero/endpoints/rest_list_endpoint.rb b/lib/chef_zero/endpoints/rest_list_endpoint.rb index 4fa277b..9a0ab7d 100644 --- a/lib/chef_zero/endpoints/rest_list_endpoint.rb +++ b/lib/chef_zero/endpoints/rest_list_endpoint.rb @@ -5,12 +5,13 @@ module ChefZero module Endpoints # Typical REST list endpoint (/roles or /data/BAG) class RestListEndpoint < RestBase - def initialize(server, identity_key = 'name') + def initialize(server, identity_keys = [ 'name' ]) super(server) - @identity_key = identity_key + identity_keys = [ identity_keys ] if identity_keys.is_a?(String) + @identity_keys = identity_keys end - attr_reader :identity_key + attr_reader :identity_keys def get(request) # Get the result @@ -25,7 +26,7 @@ module ChefZero contents = request.body key = get_key(contents) if key.nil? - error(400, "Must specify '#{identity_key}' in JSON") + error(400, "Must specify #{identity_keys.map { |k| k.inspect }.join(' or ')} in JSON") else create_data(request, request.rest_path, key, contents) json_response(201, {'uri' => "#{build_uri(request.base_uri, request.rest_path + [key])}"}) @@ -33,7 +34,8 @@ module ChefZero end def get_key(contents) - JSON.parse(contents, :create_additions => false)[identity_key] + json = JSON.parse(contents, :create_additions => false) + identity_keys.map { |k| json[k] }.select { |v| v }.first end end end diff --git a/lib/chef_zero/endpoints/rest_object_endpoint.rb b/lib/chef_zero/endpoints/rest_object_endpoint.rb index 116755d..42d7ff3 100644 --- a/lib/chef_zero/endpoints/rest_object_endpoint.rb +++ b/lib/chef_zero/endpoints/rest_object_endpoint.rb @@ -6,12 +6,13 @@ module ChefZero module Endpoints # Typical REST leaf endpoint (/roles/NAME or /data/BAG/NAME) class RestObjectEndpoint < RestBase - def initialize(server, identity_key = 'name') + def initialize(server, identity_keys = [ 'name' ]) super(server) - @identity_key = identity_key + identity_keys = [ identity_keys ] if identity_keys.is_a?(String) + @identity_keys = identity_keys end - attr_reader :identity_key + attr_reader :identity_keys def get(request) already_json_response(200, populate_defaults(request, get_data(request))) @@ -21,7 +22,8 @@ module ChefZero # We grab the old body to trigger a 404 if it doesn't exist old_body = get_data(request) request_json = JSON.parse(request.body, :create_additions => false) - key = request_json[identity_key] || request.rest_path[-1] + key = identity_keys.map { |k| request_json[k] }.select { |v| v }.first + key ||= request.rest_path[-1] # If it's a rename, check for conflict and delete the old value rename = key != request.rest_path[-1] if rename diff --git a/lib/chef_zero/rest_router.rb b/lib/chef_zero/rest_router.rb index 58f8e58..f2770d3 100644 --- a/lib/chef_zero/rest_router.rb +++ b/lib/chef_zero/rest_router.rb @@ -17,6 +17,7 @@ module ChefZero def call(request) begin ChefZero::Log.debug(request) + ChefZero::Log.debug(request.body) if request.body clean_path = "/" + request.rest_path.join("/") -- cgit v1.2.1