diff options
author | Andrew Brown <anbrown@blackberry.com> | 2015-07-03 21:03:04 -0400 |
---|---|---|
committer | tyler-ball <tyleraball@gmail.com> | 2015-07-10 10:25:59 -0700 |
commit | 4366f5e580337876502478500c135b6cfc62d937 (patch) | |
tree | 9c125c3955a5c11482f83b25ebee18c33b4f96ce /lib | |
parent | e38a9bf13d52274011acde16b4cca7184a47d7d5 (diff) | |
download | chef-zero-4366f5e580337876502478500c135b6cfc62d937.tar.gz |
Support POST /organizations/ORGNAME/users
This commit re-adds support for POST /organizations/ORGNAME/users.
Since the code is very similar to
/organizations/ORG/association_requests, we introduced a helper module
to avoid code duplication.
Diffstat (limited to 'lib')
3 files changed, 39 insertions, 15 deletions
diff --git a/lib/chef_zero/endpoints/organization_association_requests_endpoint.rb b/lib/chef_zero/endpoints/organization_association_requests_endpoint.rb index 608d548..e1b6a7e 100644 --- a/lib/chef_zero/endpoints/organization_association_requests_endpoint.rb +++ b/lib/chef_zero/endpoints/organization_association_requests_endpoint.rb @@ -6,23 +6,14 @@ module ChefZero # /organizations/ORG/association_requests class OrganizationAssociationRequestsEndpoint < RestBase def post(request) - json = FFI_Yajl::Parser.parse(request.body, :create_additions => false) - username = json['user'] - orgname = request.rest_path[1] - id = "#{username}-#{orgname}" - - if exists_data?(request, [ 'organizations', orgname, 'users', username ]) - raise RestErrorResponse.new(409, "User #{username} is already in organization #{orgname}") - end - - create_data(request, request.rest_path, username, '{}') - json_response(201, { "uri" => build_uri(request.base_uri, request.rest_path + [ id ]) }) + ChefZero::Endpoints::OrganizationUserBase.post(self, request, 'user') end def get(request) orgname = request.rest_path[1] - result = list_data(request).map { |username| { "id" => "#{username}-#{orgname}", 'username' => username } } - json_response(200, result) + ChefZero::Endpoints::OrganizationUserBase.get(self, request) do |username| + { "id" => "#{username}-#{orgname}", 'username' => username } + end end end end diff --git a/lib/chef_zero/endpoints/organization_user_base.rb b/lib/chef_zero/endpoints/organization_user_base.rb new file mode 100644 index 0000000..aaa2e3a --- /dev/null +++ b/lib/chef_zero/endpoints/organization_user_base.rb @@ -0,0 +1,29 @@ +require 'ffi_yajl' +require 'chef_zero/rest_base' + +module ChefZero + module Endpoints + module OrganizationUserBase + + def self.get(obj, request, &block) + result = obj.list_data(request).map(&block) + obj.json_response(200, result) + end + + def self.post(obj, request, key) + json = FFI_Yajl::Parser.parse(request.body, :create_additions => false) + username = json[key] + orgname = request.rest_path[1] + id = "#{username}-#{orgname}" + + if obj.exists_data?(request, [ 'organizations', orgname, 'users', username ]) + raise RestErrorResponse.new(409, "User #{username} is already in organization #{orgname}") + end + + obj.create_data(request, request.rest_path, username, '{}') + obj.json_response(201, { "uri" => obj.build_uri(request.base_uri, request.rest_path + [ id ]) }) + end + + end + end +end diff --git a/lib/chef_zero/endpoints/organization_users_endpoint.rb b/lib/chef_zero/endpoints/organization_users_endpoint.rb index ed70cf6..e914820 100644 --- a/lib/chef_zero/endpoints/organization_users_endpoint.rb +++ b/lib/chef_zero/endpoints/organization_users_endpoint.rb @@ -1,13 +1,17 @@ require 'ffi_yajl' require 'chef_zero/rest_base' +require 'chef_zero/endpoints/organization_user_base' module ChefZero module Endpoints # /organizations/ORG/users class OrganizationUsersEndpoint < RestBase + def post(request) + ChefZero::Endpoints::OrganizationUserBase.post(self, request, 'username') + end + def get(request) - result = list_data(request).map { |username| { "user" => { "username" => username } } } - json_response(200, result) + ChefZero::Endpoints::OrganizationUserBase.get(self, request) { |username| { "user" => { "username" => username } } } end end end |