diff --git a/app/models/manageiq/providers/openstack/cloud_manager/cloud_volume.rb b/app/models/manageiq/providers/openstack/cloud_manager/cloud_volume.rb index c34e5fbcf..d159ec31a 100644 --- a/app/models/manageiq/providers/openstack/cloud_manager/cloud_volume.rb +++ b/app/models/manageiq/providers/openstack/cloud_manager/cloud_volume.rb @@ -2,6 +2,7 @@ class ManageIQ::Providers::Openstack::CloudManager::CloudVolume < ::CloudVolume include_concern 'Operations' include SupportsFeatureMixin + include ManageIQ::Providers::Openstack::HelperMethods supports :create supports :backup_create @@ -18,9 +19,14 @@ def self.raw_create_volume(ext_management_system, options) # provide display_name for Cinder V1 options[:display_name] |= options[:name] - ext_management_system.with_provider_connection(cinder_connection_options(cloud_tenant)) do |service| - volume = service.volumes.new(options) - volume.save + with_notification(:cloud_volume_create, + :options => { + :volume_name => options[:name], + }) do + ext_management_system.with_provider_connection(cinder_connection_options(cloud_tenant)) do |service| + volume = service.volumes.new(options) + volume.save + end end {:ems_ref => volume.id, :status => volume.status, :name => options[:name]} rescue => e @@ -33,9 +39,14 @@ def validate_update_volume end def raw_update_volume(options) - with_provider_object do |volume| - volume.attributes.merge!(options) - volume.save + with_notification(:cloud_volume_update, + :options => { + :subject => self, + }) do + with_provider_object do |volume| + volume.attributes.merge!(options) + volume.save + end end rescue => e _log.error "volume=[#{name}], error: #{e}" @@ -52,7 +63,12 @@ def validate_delete_volume end def raw_delete_volume - with_provider_object(&:destroy) + with_notification(:cloud_volume_delete, + :options => { + :subject => self, + }) do + with_provider_object(&:destroy) + end rescue => e _log.error "volume=[#{name}], error: #{e}" raise MiqException::MiqVolumeDeleteError, e.to_s, e.backtrace diff --git a/app/models/manageiq/providers/openstack/cloud_manager/cloud_volume/operations.rb b/app/models/manageiq/providers/openstack/cloud_manager/cloud_volume/operations.rb index 098324451..a1af6b6bd 100644 --- a/app/models/manageiq/providers/openstack/cloud_manager/cloud_volume/operations.rb +++ b/app/models/manageiq/providers/openstack/cloud_manager/cloud_volume/operations.rb @@ -9,14 +9,26 @@ def validate_detach_volume def raw_attach_volume(server_ems_ref, device = nil) device = nil if device.try(:empty?) - ext_management_system.with_provider_connection(connection_options) do |service| - service.servers.get(server_ems_ref).attach_volume(ems_ref, device) + with_notification(:cloud_volume_attach, + :options => { + :subject => self, + :instance_name => server_ems_ref, + }) do + ext_management_system.with_provider_connection(connection_options) do |service| + service.servers.get(server_ems_ref).attach_volume(ems_ref, device) + end end end def raw_detach_volume(server_ems_ref) - ext_management_system.with_provider_connection(connection_options) do |service| - service.servers.get(server_ems_ref).detach_volume(ems_ref) + with_notification(:cloud_volume_detach, + :options => { + :subject => self, + :instance_name => server_ems_ref, + }) do + ext_management_system.with_provider_connection(connection_options) do |service| + service.servers.get(server_ems_ref).detach_volume(ems_ref) + end end end end diff --git a/app/models/manageiq/providers/openstack/cloud_manager/cloud_volume_snapshot.rb b/app/models/manageiq/providers/openstack/cloud_manager/cloud_volume_snapshot.rb index 05b6922ba..a8730a519 100644 --- a/app/models/manageiq/providers/openstack/cloud_manager/cloud_volume_snapshot.rb +++ b/app/models/manageiq/providers/openstack/cloud_manager/cloud_volume_snapshot.rb @@ -1,5 +1,6 @@ class ManageIQ::Providers::Openstack::CloudManager::CloudVolumeSnapshot < ::CloudVolumeSnapshot include SupportsFeatureMixin + include ManageIQ::Providers::Openstack::HelperMethods supports :create supports :update @@ -41,8 +42,14 @@ def self.create_snapshot(cloud_volume, options = {}) cloud_tenant = cloud_volume.cloud_tenant snapshot = nil options[:volume_id] = cloud_volume.ems_ref - ext_management_system.with_provider_connection(connection_options(cloud_tenant)) do |service| - snapshot = service.snapshots.create(options) + with_notification(:cloud_volume_snapshot_create, + :options => { + :snapshot_name => options[:name], + :volume_name => cloud_volume.name, + }) do + ext_management_system.with_provider_connection(connection_options(cloud_tenant)) do |service| + snapshot = service.snapshots.create(options) + end end create( @@ -111,11 +118,17 @@ def delete_snapshot_queue(userid = "system", _options = {}) end def delete_snapshot(_options = {}) - with_provider_object do |snapshot| - if snapshot - snapshot.destroy - else - _log.warn "snapshot=[#{name}] already deleted" + with_notification(:cloud_volume_snapshot_delete, + :options => { + :subject => self, + :volume_name => cloud_volume.name, + }) do + with_provider_object do |snapshot| + if snapshot + snapshot.destroy + else + _log.warn("snapshot=[#{name}] already deleted") + end end end rescue => e @@ -123,7 +136,6 @@ def delete_snapshot(_options = {}) raise MiqException::MiqVolumeSnapshotDeleteError, e.to_s, e.backtrace end - def self.connection_options(cloud_tenant = nil) connection_options = { :service => 'Volume' } connection_options[:tenant_name] = cloud_tenant.name if cloud_tenant diff --git a/app/models/manageiq/providers/openstack/cloud_manager/vm.rb b/app/models/manageiq/providers/openstack/cloud_manager/vm.rb index 11cc4f2cc..eaa1ec2bf 100644 --- a/app/models/manageiq/providers/openstack/cloud_manager/vm.rb +++ b/app/models/manageiq/providers/openstack/cloud_manager/vm.rb @@ -5,6 +5,8 @@ class ManageIQ::Providers::Openstack::CloudManager::Vm < ManageIQ::Providers::Cl include_concern 'AssociateIp' include_concern 'ManageSecurityGroups' + include ManageIQ::Providers::Openstack::HelperMethods + supports :smartstate_analysis do feature_supported, reason = check_feature_support('smartstate_analysis') unless feature_supported diff --git a/app/models/manageiq/providers/openstack/cloud_manager/vm/operations.rb b/app/models/manageiq/providers/openstack/cloud_manager/vm/operations.rb index 0653c296a..ea2035619 100644 --- a/app/models/manageiq/providers/openstack/cloud_manager/vm/operations.rb +++ b/app/models/manageiq/providers/openstack/cloud_manager/vm/operations.rb @@ -8,7 +8,12 @@ module ManageIQ::Providers::Openstack::CloudManager::Vm::Operations def raw_destroy raise "VM has no #{ui_lookup(:table => "ext_management_systems")}, unable to destroy VM" unless ext_management_system - with_provider_object(&:destroy) + with_notification(:vm_destroy, + :options => { + :subject => self, + }) do + with_provider_object(&:destroy) + end self.update_attributes!(:raw_power_state => "DELETED") end end diff --git a/app/models/manageiq/providers/openstack/cloud_manager/vm/operations/relocation.rb b/app/models/manageiq/providers/openstack/cloud_manager/vm/operations/relocation.rb index 218cb65b2..9dd38c316 100644 --- a/app/models/manageiq/providers/openstack/cloud_manager/vm/operations/relocation.rb +++ b/app/models/manageiq/providers/openstack/cloud_manager/vm/operations/relocation.rb @@ -1,8 +1,6 @@ module ManageIQ::Providers::Openstack::CloudManager::Vm::Operations::Relocation extend ActiveSupport::Concern - include ManageIQ::Providers::Openstack::HelperMethods - included do supports :live_migrate do unsupported_reason_add(:live_migrate, unsupported_reason(:control)) unless supports_control? diff --git a/app/models/manageiq/providers/openstack/helper_methods.rb b/app/models/manageiq/providers/openstack/helper_methods.rb index f77ffd2a9..4384afa3d 100644 --- a/app/models/manageiq/providers/openstack/helper_methods.rb +++ b/app/models/manageiq/providers/openstack/helper_methods.rb @@ -9,6 +9,10 @@ def parse_error_message_from_neutron_response(exception) self.class.parse_error_message_from_neutron_response(exception) end + def with_notification(type, options: {}, &block) + self.class.with_notification(type, :options => options, &block) + end + module ClassMethods def parse_error_message_from_fog_response(exception) exception_string = exception.to_s @@ -19,5 +23,25 @@ def parse_error_message_from_fog_response(exception) def parse_error_message_from_neutron_response(exception) JSON.parse(exception.response.body)["NeutronError"]["message"] end + + def with_notification(type, options: {}) + # extract success and error options from options + # :success and :error keys respectively + # with all other keys common for both cases + success_options = options.delete(:success) || {} + error_options = options.delete(:error) || {} + success_options.merge!(options) + error_options.merge!(options) + begin + yield + rescue => ex + # Fog specific + error_message = parse_error_message_from_fog_response(ex.to_s) + Notification.create(:type => "#{type}_error".to_sym, :options => error_options.merge(:error_message => error_message)) + raise + else + Notification.create(:type => "#{type}_success".to_sym, :options => success_options) + end + end end end diff --git a/spec/models/manageiq/providers/openstack/cloud_manager/vm_spec.rb b/spec/models/manageiq/providers/openstack/cloud_manager/vm_spec.rb index ee1099aff..38716d667 100644 --- a/spec/models/manageiq/providers/openstack/cloud_manager/vm_spec.rb +++ b/spec/models/manageiq/providers/openstack/cloud_manager/vm_spec.rb @@ -50,10 +50,6 @@ describe "vm actions" do context "#live_migrate" do - before do - NotificationType.seed - end - it "live migrates with default options" do expect(handle).to receive(:live_migrate_server).with(vm.ems_ref, nil, false, false) vm.live_migrate @@ -72,10 +68,6 @@ end context "evacuate" do - before do - NotificationType.seed - end - it "evacuates with default options" do expect(handle).to receive(:evacuate_server).with(vm.ems_ref, nil, true, nil) vm.evacuate diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index dabab088c..76e69ddec 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -8,6 +8,8 @@ config.cassette_library_dir = File.join(ManageIQ::Providers::Openstack::Engine.root, 'spec/vcr_cassettes') end +NotificationType.seed + Dir[Rails.root.join("spec/shared/**/*.rb")].each { |f| require f } Dir[ManageIQ::Providers::Openstack::Engine.root.join("spec/support/**/*.rb")].each { |f| require f }