Class: Foreman::Model::EC2

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

Constant Summary

Constants inherited from ComputeResource

ComputeResource::PROVIDERS, ComputeResource::STI_PREFIX

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ComputeResource

#as_json, #available_images, #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_param, #vms

Methods included from Authorization

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

Methods included from Taxonomix

included

Class Method Details

.model_nameObject


18
19
20
# File 'lib/foreman/model/ec2.rb', line 18

def self.model_name
  ComputeResource.model_name
end

Instance Method Details

#capabilitiesObject


22
23
24
# File 'lib/foreman/model/ec2.rb', line 22

def capabilities
  [:image]
end

#console(uuid) ⇒ Object


78
79
80
81
# File 'lib/foreman/model/ec2.rb', line 78

def console(uuid)
  vm = find_vm_by_uuid(uuid)
  vm.console_output.body.merge(:type=>'log', :name=>vm.name)
end

#create_vm(args = { }) ⇒ Object


32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/foreman/model/ec2.rb', line 32

def create_vm args = { }
  args = vm_instance_defaults.merge(args.to_hash.symbolize_keys)
  if (name = args[:name])
    args.merge!(:tags => {:Name => name})
  end
  if (image_id = args[:image_id])
    image = images.find_by_uuid(image_id)
    iam_hash = image.iam_role.present? ? {:iam_instance_profile_name => image.iam_role} : {}
    args.merge!(iam_hash)
  end
  args[:groups].reject!(&:empty?) if args.has_key?(:groups)
  super(args)
end

#destroy_vm(uuid) ⇒ Object


83
84
85
86
87
# File 'lib/foreman/model/ec2.rb', line 83

def destroy_vm(uuid)
  vm = find_vm_by_uuid(uuid)
  vm.destroy if vm
  true
end

#find_vm_by_uuid(uuid) ⇒ Object


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

def find_vm_by_uuid uuid
  client.servers.get(uuid)
rescue Fog::Compute::AWS::Error
  raise(ActiveRecord::RecordNotFound)
end

#flavorsObject


59
60
61
# File 'lib/foreman/model/ec2.rb', line 59

def flavors
  client.flavors
end

#provided_attributesObject


14
15
16
# File 'lib/foreman/model/ec2.rb', line 14

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

#regionObject


74
75
76
# File 'lib/foreman/model/ec2.rb', line 74

def region
  @region ||= url.present? ? url : nil
end

#region=(value) ⇒ Object


70
71
72
# File 'lib/foreman/model/ec2.rb', line 70

def region= value
  self.url = value
end

#regionsObject


50
51
52
53
# File 'lib/foreman/model/ec2.rb', line 50

def regions
  return [] if user.blank? or password.blank?
  @regions ||= client.describe_regions.body["regionInfo"].map { |r| r["regionName"] }
end

#security_groupsObject


46
47
48
# File 'lib/foreman/model/ec2.rb', line 46

def security_groups
  client.security_groups.map(&:name)
end

#test_connection(options = {}) ⇒ Object


63
64
65
66
67
68
# File 'lib/foreman/model/ec2.rb', line 63

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

#to_labelObject


10
11
12
# File 'lib/foreman/model/ec2.rb', line 10

def to_label
  "#{name} (#{region}-#{provider_friendly_name})"
end

#update_required?(old_attrs, new_attrs) ⇒ Boolean

not supporting update at the moment


90
91
92
# File 'lib/foreman/model/ec2.rb', line 90

def update_required?(old_attrs, new_attrs)
  false
end

#zonesObject


55
56
57
# File 'lib/foreman/model/ec2.rb', line 55

def zones
  @zones ||= client.describe_availability_zones.body["availabilityZoneInfo"].map { |r| r["zoneName"] if r["regionName"] == region }.compact
end