diff --git a/app/models/flavor.rb b/app/models/flavor.rb index bf37910f2fd..5b7764e0d74 100644 --- a/app/models/flavor.rb +++ b/app/models/flavor.rb @@ -1,6 +1,10 @@ class Flavor < ApplicationRecord include NewWithTypeStiMixin include CloudTenancyMixin + include SupportsFeatureMixin + + supports :create + supports :delete acts_as_miq_taggable @@ -35,7 +39,77 @@ def name_with_details } end + def self.class_by_ems(ext_management_system) + ext_management_system.class::Flavor + end + def self.tenant_joins_clause(scope) scope.includes(:cloud_tenants => "source_tenant").includes(:ext_management_system) end + + def self.create_flavor_queue(userid, ext_management_system, options = {}) + task_opts = { + :action => "Creating flavor for user #{userid}", + :userid => userid + } + + queue_opts = { + :class_name => 'Flavor', + :method_name => 'create_flavor', + :role => 'ems_operations', + :zone => ext_management_system.my_zone, + :args => [ext_management_system.id, options] + } + MiqTask.generic_action_with_callback(task_opts, queue_opts) + end + + def self.raw_create_flavor(_ext_management_system, _options = {}) + raise NotImplementedError, "raw_create_flavor must be implemented in a subclass" + end + + def validate_create_flavor(_ext_management_system, _options = {}) + validate_unsupported(_("Create Flavor Operation")) + end + + def self.create_flavor(ems_id, options) + raise ArgumentError, _("ems cannot be nil") if ems_id.nil? + ext_management_system = ExtManagementSystem.find(ems_id) + raise ArgumentError, _("ems cannot be found") if ext_management_system.nil? + + klass = class_by_ems(ext_management_system) + klass.raw_create_flavor(ext_management_system, options) + end + + def delete_flavor_queue(userid) + task_opts = { + :action => "Deleting flavor for user #{userid}", + :userid => userid + } + queue_opts = { + :class_name => "Flavor", + :method_name => 'delete_flavor', + :instance_id => id, + :role => 'ems_operations', + :zone => ext_management_system.my_zone, + :args => [] + } + MiqTask.generic_action_with_callback(task_opts, queue_opts) + end + + def raw_delete_flavor + raise NotImplementedError, _("raw_delete_flavor must be implemented in a subclass") + end + + def validate_delete_flavor + validate_unsupported(_("Delete Flavor Operation")) + end + + def delete_flavor + raw_delete_flavor + end + + def validate_unsupported(message_prefix) + {:available => false, + :message => _("%{message} is not available for %{name}.") % {:message => message_prefix, :name => name}} + end end diff --git a/db/fixtures/miq_product_features.yml b/db/fixtures/miq_product_features.yml index 0fa81685dd9..f167f4ba9cd 100644 --- a/db/fixtures/miq_product_features.yml +++ b/db/fixtures/miq_product_features.yml @@ -893,6 +893,14 @@ :description: Edit Tags of Flavor :feature_type: control :identifier: flavor_tag + - :name: Add Flavor + :description: Add Flavor + :feature_type: control + :identifier: flavor_create + - :name: Delete Flavor + :description: Delete Flavor + :feature_type: control + :identifier: flavor_delete # EmsInfra - :name: Infrastructure Providers diff --git a/spec/models/flavor_spec.rb b/spec/models/flavor_spec.rb new file mode 100644 index 00000000000..3dedfbe2b98 --- /dev/null +++ b/spec/models/flavor_spec.rb @@ -0,0 +1,35 @@ +describe Flavor do + let(:ems) { FactoryGirl.create(:ems_openstack) } + + context 'when calling raw_create_flavor methods' do + it 'raises NotImplementedError' do + expect do + subject.class.raw_create_flavor(1, {}) + end.to raise_error(NotImplementedError, "raw_create_flavor must be implemented in a subclass") + end + end + + context 'when calling raw_create_flavor methods' do + it 'raises NotImplementedError' do + expect do + subject.raw_delete_flavor + end.to raise_error(NotImplementedError, "raw_delete_flavor must be implemented in a subclass") + end + end + + context 'when calling create_flavor method' do + it 'should call raw_create_flavor' do + flavor_double = class_double('ManageIQ::Providers::Openstack::CloudManager::Flavor') + allow(subject.class).to receive(:class_by_ems).and_return(flavor_double) + expect(flavor_double).to receive(:raw_create_flavor) + subject.class.create_flavor(ems.id, {}) + end + end + + context 'when calling delete_flavor method' do + it 'should call raw_delete_flavor' do + expect(subject).to receive(:raw_delete_flavor) + subject.delete_flavor + end + end +end