Class: Foreman::Model::Openstack

Inherits:
ComputeResource show all
Defined in:
lib/foreman/model/openstack.rb

Constant Summary

Constants inherited from ComputeResource

ComputeResource::PROVIDERS, ComputeResource::STI_PREFIX

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ComputeResource

#as_json, #find_vm_by_uuid, #hardware_profile, #hardware_profiles, #new_interface, new_provider, #new_vm, #ping, #provider, #provider=, #provider_friendly_name, #save_vm, #start_vm, #stop_vm, #supports_update?, #to_label, #to_param, #update_required?, #vms

Methods included from Authorization

#enforce_create_permissions, #enforce_destroy_permissions, #enforce_edit_permissions, #enforce_permissions, included, #permission_failed?

Methods included from Taxonomix

included

Instance Attribute Details

#tenantObject

Returns the value of attribute tenant


3
4
5
# File 'lib/foreman/model/openstack.rb', line 3

def tenant
  @tenant
end

Class Method Details

.model_nameObject


17
18
19
# File 'lib/foreman/model/openstack.rb', line 17

def self.model_name
  ComputeResource.model_name
end

Instance Method Details

#address_poolsObject


36
37
38
# File 'lib/foreman/model/openstack.rb', line 36

def address_pools
  client.addresses.get_address_pools.map { |p| p["name"] }
end

#available_imagesObject


32
33
34
# File 'lib/foreman/model/openstack.rb', line 32

def available_images
  client.images
end

#capabilitiesObject


21
22
23
# File 'lib/foreman/model/openstack.rb', line 21

def capabilities
  [:image]
end

#console(uuid) ⇒ Object


68
69
70
71
# File 'lib/foreman/model/openstack.rb', line 68

def console(uuid)
  vm = find_vm_by_uuid(uuid)
  vm.console.body.merge({'timestamp' => Time.now.utc})
end

#create_vm(args = {}) ⇒ Object


40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/foreman/model/openstack.rb', line 40

def create_vm(args = {})
  network = args.delete(:network)
  vm      = super(args)
  if network.present?
    address = allocate_address(network)
    assign_floating_ip(address, vm)
  end
  vm
rescue => e
  message = JSON.parse(e.response.body)['badRequest']['message'] rescue (e.to_s)
  logger.warn "failed to create vm: #{message}"
  destroy_vm vm.id if vm
  raise message
end

#destroy_vm(uuid) ⇒ Object


55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/foreman/model/openstack.rb', line 55

def destroy_vm uuid
  vm           = find_vm_by_uuid(uuid)
  floating_ips = vm.all_addresses
  floating_ips.each do |address|
    client.disassociate_address(uuid, address['ip'])
    client.release_address(address['id'])
  end
  super(uuid)
rescue ActiveRecord::RecordNotFound
  # if the VM does not exists, we don't really care.
  true
end

#provided_attributesObject


13
14
15
# File 'lib/foreman/model/openstack.rb', line 13

def provided_attributes
  super.merge({ :ip => :floating_ip_address })
end

#test_connection(options = {}) ⇒ Object


25
26
27
28
29
30
# File 'lib/foreman/model/openstack.rb', line 25

def test_connection options = {}
  super
  errors[:user].empty? and errors[:password] and tenants
rescue => e
  errors[:base] << e.message
end