From 023200ed63ece921a98549971dbdb34c6103e01d Mon Sep 17 00:00:00 2001 From: Tzu-Mainn Chen Date: Tue, 11 Apr 2017 18:32:40 +0200 Subject: [PATCH 1/5] Add cloud tenants to API --- .../api/cloud_tenants_controller.rb | 4 ++ app/controllers/api/providers_controller.rb | 1 + .../api/subcollections/cloud_tenants.rb | 9 ++++ config/api.yml | 28 +++++++++++ spec/requests/api/cloud_tenants_spec.rb | 50 +++++++++++++++++++ spec/requests/api/collections_spec.rb | 10 ++++ spec/requests/api/providers_spec.rb | 46 +++++++++++++++++ 7 files changed, 148 insertions(+) create mode 100644 app/controllers/api/cloud_tenants_controller.rb create mode 100644 app/controllers/api/subcollections/cloud_tenants.rb create mode 100644 spec/requests/api/cloud_tenants_spec.rb diff --git a/app/controllers/api/cloud_tenants_controller.rb b/app/controllers/api/cloud_tenants_controller.rb new file mode 100644 index 00000000000..c684e8b3412 --- /dev/null +++ b/app/controllers/api/cloud_tenants_controller.rb @@ -0,0 +1,4 @@ +module Api + class CloudTenantsController < BaseController + end +end diff --git a/app/controllers/api/providers_controller.rb b/app/controllers/api/providers_controller.rb index 0d2c86f1071..b29c4d03755 100644 --- a/app/controllers/api/providers_controller.rb +++ b/app/controllers/api/providers_controller.rb @@ -14,6 +14,7 @@ class ProvidersController < BaseController include Subcollections::PolicyProfiles include Subcollections::Tags include Subcollections::CloudNetworks + include Subcollections::CloudTenants include Subcollections::CustomAttributes include Subcollections::LoadBalancers diff --git a/app/controllers/api/subcollections/cloud_tenants.rb b/app/controllers/api/subcollections/cloud_tenants.rb new file mode 100644 index 00000000000..93bb65bed0c --- /dev/null +++ b/app/controllers/api/subcollections/cloud_tenants.rb @@ -0,0 +1,9 @@ +module Api + module Subcollections + module CloudTenants + def cloud_tenants_query_resource(object) + object.respond_to?(:cloud_tenants) ? object.cloud_tenants : [] + end + end + end +end diff --git a/config/api.yml b/config/api.yml index adfcf9e2a40..dd1ff7086a6 100644 --- a/config/api.yml +++ b/config/api.yml @@ -484,6 +484,33 @@ :get: - :name: read :identifier: miq_cloud_networks_view + :cloud_tenants: + :description: Cloud Tenants + :identifier: cloud_tenant + :options: + - :collection + - :subcollection + :verbs: *gp + :klass: CloudTenant + :collection_actions: + :get: + - :name: read + :identifier: cloud_tenant_show_list + :post: + - :name: query + :identifier: cloud_tenant_show_list + :resource_actions: + :get: + - :name: read + :identifier: cloud_tenant_show + :subcollection_actions: + :get: + - :name: read + :identifier: cloud_tenant_show + :subresource_actions: + :get: + - :name: read + :identifier: cloud_tenant_show :cloud_volumes: :description: Cloud Volumes :identifier: cloud_volume @@ -1198,6 +1225,7 @@ - :policies - :policy_profiles - :cloud_networks + - :cloud_tenants - :custom_attributes - :load_balancers :collection_actions: diff --git a/spec/requests/api/cloud_tenants_spec.rb b/spec/requests/api/cloud_tenants_spec.rb new file mode 100644 index 00000000000..d39694da464 --- /dev/null +++ b/spec/requests/api/cloud_tenants_spec.rb @@ -0,0 +1,50 @@ +RSpec.describe 'CloudTenants API' do + describe 'GET /api/cloud_tenants' do + it 'lists all cloud tenants with an appropriate role' do + cloud_tenant = FactoryGirl.create(:cloud_tenant) + api_basic_authorize collection_action_identifier(:cloud_tenants, :read, :get) + + expected = { + 'count' => 1, + 'subcount' => 1, + 'name' => 'cloud_tenants', + 'resources' => [ + hash_including('href' => a_string_matching(cloud_tenants_url(cloud_tenant.id))) + ] + } + run_get(cloud_tenants_url) + + expect(response).to have_http_status(:ok) + expect(response.parsed_body).to include(expected) + end + + it 'forbids access to cloud tenants without an appropriate role' do + api_basic_authorize + + run_get(cloud_tenants_url) + + expect(response).to have_http_status(:forbidden) + end + end + + describe 'GET /api/cloud_tenants/:id' do + it 'will show a cloud tenant with an appropriate role' do + cloud_tenant = FactoryGirl.create(:cloud_tenant) + api_basic_authorize action_identifier(:cloud_tenants, :read, :resource_actions, :get) + + run_get(cloud_tenants_url(cloud_tenant.id)) + + expect(response.parsed_body).to include('href' => a_string_matching(cloud_tenants_url(cloud_tenant.id))) + expect(response).to have_http_status(:ok) + end + + it 'forbids access to a cloud tenant without an appropriate role' do + cloud_tenant = FactoryGirl.create(:cloud_tenant) + api_basic_authorize + + run_get(cloud_tenants_url(cloud_tenant.id)) + + expect(response).to have_http_status(:forbidden) + end + end +end diff --git a/spec/requests/api/collections_spec.rb b/spec/requests/api/collections_spec.rb index 052987c7413..587b0b5e8e6 100644 --- a/spec/requests/api/collections_spec.rb +++ b/spec/requests/api/collections_spec.rb @@ -295,6 +295,11 @@ def test_collection_bulk_query(collection, collection_url, klass, id = nil) test_collection_query(:cloud_networks, cloud_networks_url, CloudNetwork) end + it 'queries CloudTenants' do + FactoryGirl.create(:cloud_tenant) + test_collection_query(:cloud_tenants, cloud_tenants_url, CloudTenant) + end + it 'queries ArbitrationSettings' do FactoryGirl.create(:arbitration_setting) test_collection_query(:arbitration_settings, arbitration_settings_url, ArbitrationSetting) @@ -576,6 +581,11 @@ def test_collection_bulk_query(collection, collection_url, klass, id = nil) test_collection_bulk_query(:load_balancers, load_balancers_url, LoadBalancer) end + it 'bulk query CloudTenants' do + FactoryGirl.create(:cloud_tenant) + test_collection_bulk_query(:cloud_tenants, cloud_tenants_url, CloudTenant) + end + it 'bulk query CloudVolumes' do FactoryGirl.create(:cloud_volume) test_collection_bulk_query(:cloud_volumes, cloud_volumes_url, CloudVolume) diff --git a/spec/requests/api/providers_spec.rb b/spec/requests/api/providers_spec.rb index ebb62cf8798..b61a075ed3a 100644 --- a/spec/requests/api/providers_spec.rb +++ b/spec/requests/api/providers_spec.rb @@ -929,6 +929,52 @@ def gen_import_request end end + context 'cloud tenants subcollection' do + before do + @provider = FactoryGirl.create(:ems_openstack) + @cloud_tenant = FactoryGirl.create(:cloud_tenant, :ext_management_system => @provider) + end + + it 'queries all cloud tenants' do + api_basic_authorize subcollection_action_identifier(:providers, :cloud_tenants, :read, :get) + expected = { + 'resources' => [ + { 'href' => a_string_matching("#{providers_url(@provider.id)}/cloud_tenants/#{@cloud_tenant.id}") } + ] + + } + run_get("#{providers_url(@provider.id)}/cloud_tenants") + + expect(response).to have_http_status(:ok) + expect(response.parsed_body).to include(expected) + end + + it "will not show a provider's cloud tenants without the appropriate role" do + api_basic_authorize + + run_get("#{providers_url(@provider.id)}/cloud_tenants") + + expect(response).to have_http_status(:forbidden) + end + + it 'queries a single cloud tenant' do + api_basic_authorize subcollection_action_identifier(:providers, :cloud_tenants, :read, :get) + + run_get("#{providers_url(@provider.id)}/cloud_tenants/#{@cloud_tenant.id}") + + expect(response).to have_http_status(:ok) + expect(response.parsed_body).to include('id' => @cloud_tenant.id) + end + + it "will not show a provider's cloud tenant without the appropriate role" do + api_basic_authorize + + run_get("#{providers_url(@provider.id)}/cloud_tenants/#{@cloud_tenant.id}") + + expect(response).to have_http_status(:forbidden) + end + end + describe 'edit custom_attributes on providers' do context 'provider_class=provider' do let(:generic_provider) { FactoryGirl.create(:provider) } From b235985c4b21db324ff776380d7ef9c4eb633f47 Mon Sep 17 00:00:00 2001 From: Tzu-Mainn Chen Date: Wed, 12 Apr 2017 20:27:24 +0200 Subject: [PATCH 2/5] remove responds_to in cloud tenant subcollection --- app/controllers/api/subcollections/cloud_tenants.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/api/subcollections/cloud_tenants.rb b/app/controllers/api/subcollections/cloud_tenants.rb index 93bb65bed0c..9d658913da4 100644 --- a/app/controllers/api/subcollections/cloud_tenants.rb +++ b/app/controllers/api/subcollections/cloud_tenants.rb @@ -2,7 +2,7 @@ module Api module Subcollections module CloudTenants def cloud_tenants_query_resource(object) - object.respond_to?(:cloud_tenants) ? object.cloud_tenants : [] + object.cloud_tenants end end end From e11e5685e2e8059fc17501abaa94d01100362e47 Mon Sep 17 00:00:00 2001 From: Tzu-Mainn Chen Date: Wed, 12 Apr 2017 21:02:53 +0200 Subject: [PATCH 3/5] fix incorrect cloud tenant entry --- config/api.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/api.yml b/config/api.yml index dd1ff7086a6..1bea282740a 100644 --- a/config/api.yml +++ b/config/api.yml @@ -506,7 +506,7 @@ :subcollection_actions: :get: - :name: read - :identifier: cloud_tenant_show + :identifier: cloud_tenant_show_list :subresource_actions: :get: - :name: read From fcece768b3783ebeed0a1e92d4245c3537f235f4 Mon Sep 17 00:00:00 2001 From: Tzu-Mainn Chen Date: Wed, 12 Apr 2017 21:03:14 +0200 Subject: [PATCH 4/5] fix provider cloud tenant subcollection test --- spec/requests/api/providers_spec.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/spec/requests/api/providers_spec.rb b/spec/requests/api/providers_spec.rb index b61a075ed3a..295d3dc657c 100644 --- a/spec/requests/api/providers_spec.rb +++ b/spec/requests/api/providers_spec.rb @@ -937,14 +937,15 @@ def gen_import_request it 'queries all cloud tenants' do api_basic_authorize subcollection_action_identifier(:providers, :cloud_tenants, :read, :get) + + run_get("#{providers_url(@provider.id)}/cloud_tenants") + expected = { 'resources' => [ { 'href' => a_string_matching("#{providers_url(@provider.id)}/cloud_tenants/#{@cloud_tenant.id}") } ] } - run_get("#{providers_url(@provider.id)}/cloud_tenants") - expect(response).to have_http_status(:ok) expect(response.parsed_body).to include(expected) end @@ -958,7 +959,7 @@ def gen_import_request end it 'queries a single cloud tenant' do - api_basic_authorize subcollection_action_identifier(:providers, :cloud_tenants, :read, :get) + api_basic_authorize action_identifier(:cloud_tenants, :read, :subresource_actions, :get) run_get("#{providers_url(@provider.id)}/cloud_tenants/#{@cloud_tenant.id}") From dece2357561219b3ca08153863930370f5652a99 Mon Sep 17 00:00:00 2001 From: Tzu-Mainn Chen Date: Wed, 12 Apr 2017 21:13:40 +0200 Subject: [PATCH 5/5] test cleanup --- spec/requests/api/cloud_tenants_spec.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/spec/requests/api/cloud_tenants_spec.rb b/spec/requests/api/cloud_tenants_spec.rb index d39694da464..557e0108ecd 100644 --- a/spec/requests/api/cloud_tenants_spec.rb +++ b/spec/requests/api/cloud_tenants_spec.rb @@ -3,6 +3,7 @@ it 'lists all cloud tenants with an appropriate role' do cloud_tenant = FactoryGirl.create(:cloud_tenant) api_basic_authorize collection_action_identifier(:cloud_tenants, :read, :get) + run_get(cloud_tenants_url) expected = { 'count' => 1, @@ -12,8 +13,6 @@ hash_including('href' => a_string_matching(cloud_tenants_url(cloud_tenant.id))) ] } - run_get(cloud_tenants_url) - expect(response).to have_http_status(:ok) expect(response.parsed_body).to include(expected) end