ManageIQ/manageiq-providers-openstack

View on GitHub
app/models/manageiq/providers/openstack/cloud_manager/vm/associate_ip.rb

Summary

Maintainability
A
2 hrs
Test Coverage
C
76%
module ManageIQ::Providers::Openstack::CloudManager::Vm::AssociateIp
  extend ActiveSupport::Concern

  included do
    supports :associate_floating_ip do
      if cloud_tenant.nil? || cloud_tenant.floating_ips.empty?
        _("There are no %{floating_ips} available to this %{instance}.") % {
          :floating_ips => ui_lookup(:tables => "floating_ips"),
          :instance     => ui_lookup(:table => "vm_cloud")
        }
      end
    end
    supports :disassociate_floating_ip do
      if floating_ips.empty?
        _("This %{instance} does not have any associated %{floating_ips}") % {
          :instance     => ui_lookup(:table => 'vm_cloud'),
          :floating_ips => ui_lookup(:tables => 'floating_ip')
        }
      end
    end
  end

  def raw_associate_floating_ip(floating_ip)
    ext_management_system.with_provider_connection(compute_connection_options) do |connection|
      connection.associate_address(ems_ref, floating_ip)
    end
  rescue => err
    _log.error "vm=[#{name}], floating_ip=[#{floating_ip}], error: #{err}"
    raise MiqException::MiqOpenstackApiRequestError, parse_error_message_from_fog_response(err), err.backtrace
  end

  def raw_disassociate_floating_ip(floating_ip)
    ext_management_system.with_provider_connection(compute_connection_options) do |connection|
      connection.disassociate_address(ems_ref, floating_ip)
    end
  rescue => err
    _log.error "vm=[#{name}], floating_ip=[#{floating_ip}], error: #{err}"
    raise MiqException::MiqOpenstackApiRequestError, parse_error_message_from_fog_response(err), err.backtrace
  end

  def compute_connection_options
    {:service => 'Compute', :tenant_name => cloud_tenant.name}
  end
end