Skip to content

Instantly share code, notes, and snippets.

@durandom
Last active November 29, 2016 18:39
Show Gist options
  • Save durandom/82437a51d096575e274426bcbbf18ec7 to your computer and use it in GitHub Desktop.
Save durandom/82437a51d096575e274426bcbbf18ec7 to your computer and use it in GitHub Desktop.
Service Objects MIQ
# current implementation Amazon
def raw_stop
with_provider_object(&:stop)
# Temporarily update state for quick UI response until refresh comes along
self.update_attributes!(:raw_power_state => "shutting_down")
end
# implementation with Service Object
# base class for api docs
class Vm::Operations::Stop
def call(provider_object, ems)
end
end
# amazon implementation
class Amazon::Vm::Operations::Stop < Vm::Operations::Stop
def call(provider_object, ems)
provider_object.stop
end
end
# calling it
class Platform::PowerOperations
def stop(vm)
# check if vm is connected to ems
raise "not connected" unless vm.ems.present?
# do some perm checking
check_policy_prevent(:request_vm_stop)
vm.with_provider_object do |provider_object|
vm.class::Operations::Stop.new.call(provider_object)
vm.update_attributes!(:raw_power_state => vm.class::Operations::STOP_POWERSTATE)
rescue => e
# do some logging
end
end
# Openstack
def self.create_snapshot(cloud_volume, options = {})
raise ArgumentError, _("cloud_volume cannot be nil") if cloud_volume.nil?
ext_management_system = cloud_volume.try(:ext_management_system)
raise ArgumentError, _("ext_management_system cannot be nil") if ext_management_system.nil?
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)
end
create(
:name => snapshot.name,
:description => snapshot.description,
:ems_ref => snapshot.id,
:status => snapshot.status,
:cloud_volume => cloud_volume,
:cloud_tenant => cloud_tenant,
:ext_management_system => ext_management_system,
)
rescue => e
_log.error "snapshot=[#{options[:name]}], error: #{e}"
raise MiqException::MiqVolumeSnapshotCreateError, e.to_s, e.backtrace
end
class OpenStack::CloudVolumeSnapshot::Create
def call(provider_connection, cloud_tenant, options)
snapshot = provider_connection.snapshots.create(options)
MiqEvent.put_on_queue("CLOUD_VOLUME_SNAPSHOT_CREATED", :payload => snapshot)
# will do this
# Dto::SaveInventory.save(snapshot)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment