Skip to content

Commit

Permalink
Merge pull request #15552 from alexander-demichev/flavors
Browse files Browse the repository at this point in the history
Flavors create and add methods
  • Loading branch information
blomquisg authored Sep 6, 2017
2 parents 9d413e2 + 7521c70 commit d65d0a0
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
74 changes: 74 additions & 0 deletions app/models/flavor.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
class Flavor < ApplicationRecord
include NewWithTypeStiMixin
include CloudTenancyMixin
include SupportsFeatureMixin

supports :create
supports :delete

acts_as_miq_taggable

Expand Down Expand Up @@ -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
8 changes: 8 additions & 0 deletions db/fixtures/miq_product_features.yml
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,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
Expand Down
35 changes: 35 additions & 0 deletions spec/models/flavor_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d65d0a0

Please sign in to comment.