Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flavors create and add methods #15552

Merged
merged 4 commits into from
Sep 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexander-demichev Thanks for the PR!

Just curious, don't we need also supports :delete?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aufi yeah, it`s there now

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 @@ -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
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