Skip to content

Commit

Permalink
Merge pull request ManageIQ#65 from alexander-demichev/flavor
Browse files Browse the repository at this point in the history
creating, deleting flavors
  • Loading branch information
tzumainn authored Jul 25, 2017
2 parents f3ba0ff + d9f0941 commit bed81b7
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
31 changes: 31 additions & 0 deletions app/models/manageiq/providers/openstack/cloud_manager/flavor.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,33 @@
class ManageIQ::Providers::Openstack::CloudManager::Flavor < ::Flavor
def self.raw_create_flavor(ext_management_system, create_options)
ext_management_system.with_provider_connection({:service => 'Compute'}) do |service|
service.flavors.create(create_options)
end
rescue => err
_log.error "flavor=[#{name}], error=[#{err}]"
raise MiqException::MiqOpenstackApiRequestError, err.to_s, err.backtrace
end

def self.validate_create_flavor(ext_management_system, _options = {})
if ext_management_system
{:available => true, :message => nil}
else
{:available => false,
:message => _("The Flavor is not connected to an active %{table}") %
{:table => ui_lookup(:table => "ext_management_system")}}
end
end

def raw_delete_flavor
ext_management_system.with_provider_connection({:service => 'Compute'}) do |service|
service.delete_flavor(ems_ref)
end
rescue => err
_log.error "flavor=[#{name}], error: #{err}"
raise MiqException::MiqOpenstackApiRequestError, err.to_s, err.backtrace
end

def validate_delete_flavor
{:available => true, :message => nil}
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
describe ManageIQ::Providers::Openstack::CloudManager::Flavor do
let(:ems) { FactoryGirl.create(:ems_openstack_with_authentication) }
let(:flavor_attributes) { {:name => "flavor", :ram => "1"} }
let(:flavor_openstack) { FactoryGirl.create :flavor_openstack, :ext_management_system => ems }
let(:service) { double }

context 'when raw_create_flavor' do
before do
allow(ExtManagementSystem).to receive(:find).with(ems.id).and_return(ems)
allow(ems).to receive(:with_provider_connection).with(:service => 'Compute').and_yield(service)
allow(service).to receive(:flavors).and_return(flavors)
end

let(:flavors) { double }

context 'with correct data' do
it 'should create flavor' do
expect(flavors).to receive(:create).with(flavor_attributes).and_return(flavor_openstack).once
subject.class.raw_create_flavor(ems, flavor_attributes)
end

it 'should not raise error' do
allow(flavors).to receive(:create).with(flavor_attributes).and_return(flavor_openstack).once
expect do
subject.class.raw_create_flavor(ems, flavor_attributes)
end.not_to raise_error
end
end

context 'with incorrect data' do
let(:flavor_attributes) { { :ram => "1"} } # missing :name
[Excon::Error::BadRequest, ArgumentError].map do |error|
it "should raise error when #{error.to_s}" do
allow(flavors).to receive(:create).with(flavor_attributes).and_raise(error)
expect do
subject.class.raw_create_flavor(ems, flavor_attributes)
end.to raise_error(MiqException::MiqOpenstackApiRequestError)
end
end
end
end

context 'when raw_delete_flavor' do
before do
allow(ExtManagementSystem).to receive(:find).with(ems.id).and_return(ems)
allow(ems).to receive(:with_provider_connection).with(:service => 'Compute').and_yield(service)
end

subject { flavor_openstack }

it 'should delete flavor' do
expect(service).to receive(:delete_flavor).once
subject.raw_delete_flavor
end

it 'should raise error' do
allow(service).to receive(:delete_flavor).and_raise(Excon::Error::BadRequest)
expect do
subject.raw_delete_flavor
end.to raise_error(MiqException::MiqOpenstackApiRequestError)
end
end

context 'when validations' do
it 'fails with invalid parameters' do
expect(subject.class.validate_create_flavor(nil)).to eq(
:available => false,
:message => 'The Flavor is not connected to an active Provider')
end

it 'doesn`t fail with valid parameters' do
expect(subject.class.validate_create_flavor(ems)).to eq(
:available => true,
:message => nil)
end
end
end

0 comments on commit bed81b7

Please sign in to comment.