Class: Cloudkeeper::Entities::Appliance

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudkeeper/entities/appliance.rb

Constant Summary collapse

REJECTED_ATTRIBUTES =
%i[vo image_list_identifier].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(identifier, mpuri, vo, expiration_date, image_list_identifier, title = '', description = '', group = '', ram = 1024, core = 1, version = '', architecture = '', operating_system = '', image = nil, attributes = {}) ⇒ Appliance

Returns a new instance of Appliance.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cloudkeeper/entities/appliance.rb', line 9

def initialize(identifier, mpuri, vo, expiration_date, image_list_identifier, title = '', description = '', group = '',
               ram = 1024, core = 1, version = '', architecture = '', operating_system = '', image = nil, attributes = {})
  if identifier.blank? || \
     mpuri.blank? || \
     vo.blank? || \
     expiration_date.blank? || \
     image_list_identifier.blank?
    raise Cloudkeeper::Errors::ArgumentError, 'identifier, mpuri, vo, expiration_date and image_list_identifier ' \
                                              'cannot be nil nor empty'
  end

  @identifier = identifier
  @description = description
  @mpuri = mpuri
  @title = title
  @group = group
  @ram = ram
  @core = core
  @version = version
  @architecture = architecture
  @operating_system = operating_system
  @image = image
  @attributes = attributes
  @vo = vo
  @expiration_date = expiration_date
  @image_list_identifier = image_list_identifier
end

Instance Attribute Details

#architectureObject

Returns the value of attribute architecture.



4
5
6
# File 'lib/cloudkeeper/entities/appliance.rb', line 4

def architecture
  @architecture
end

#attributesObject

Returns the value of attribute attributes.



5
6
7
# File 'lib/cloudkeeper/entities/appliance.rb', line 5

def attributes
  @attributes
end

#coreObject

Returns the value of attribute core.



4
5
6
# File 'lib/cloudkeeper/entities/appliance.rb', line 4

def core
  @core
end

#descriptionObject

Returns the value of attribute description.



4
5
6
# File 'lib/cloudkeeper/entities/appliance.rb', line 4

def description
  @description
end

#expiration_dateObject

Returns the value of attribute expiration_date.



5
6
7
# File 'lib/cloudkeeper/entities/appliance.rb', line 5

def expiration_date
  @expiration_date
end

#groupObject

Returns the value of attribute group.



4
5
6
# File 'lib/cloudkeeper/entities/appliance.rb', line 4

def group
  @group
end

#identifierObject

Returns the value of attribute identifier.



4
5
6
# File 'lib/cloudkeeper/entities/appliance.rb', line 4

def identifier
  @identifier
end

#imageObject

Returns the value of attribute image.



5
6
7
# File 'lib/cloudkeeper/entities/appliance.rb', line 5

def image
  @image
end

#image_list_identifierObject

Returns the value of attribute image_list_identifier.



5
6
7
# File 'lib/cloudkeeper/entities/appliance.rb', line 5

def image_list_identifier
  @image_list_identifier
end

#mpuriObject

Returns the value of attribute mpuri.



4
5
6
# File 'lib/cloudkeeper/entities/appliance.rb', line 4

def mpuri
  @mpuri
end

#operating_systemObject

Returns the value of attribute operating_system.



5
6
7
# File 'lib/cloudkeeper/entities/appliance.rb', line 5

def operating_system
  @operating_system
end

#ramObject

Returns the value of attribute ram.



4
5
6
# File 'lib/cloudkeeper/entities/appliance.rb', line 4

def ram
  @ram
end

#titleObject

Returns the value of attribute title.



4
5
6
# File 'lib/cloudkeeper/entities/appliance.rb', line 4

def title
  @title
end

#versionObject

Returns the value of attribute version.



4
5
6
# File 'lib/cloudkeeper/entities/appliance.rb', line 4

def version
  @version
end

#voObject

Returns the value of attribute vo.



5
6
7
# File 'lib/cloudkeeper/entities/appliance.rb', line 5

def vo
  @vo
end

Class Method Details

.construct_appliance(appliance_hash) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cloudkeeper/entities/appliance.rb', line 63

def construct_appliance(appliance_hash)
  Appliance.new appliance_hash[:'dc:identifier'],
                appliance_hash[:'ad:mpuri'],
                appliance_hash[:vo],
                Cloudkeeper::Utils::Date.parse(appliance_hash[:'dc:date:expires']),
                appliance_hash[:image_list_identifier],
                appliance_hash[:'dc:title'],
                appliance_hash[:'dc:description'],
                appliance_hash[:'ad:group'],
                appliance_hash[:'hv:ram_minimum'],
                appliance_hash[:'hv:core_minimum'],
                appliance_hash[:'hv:version'],
                appliance_hash[:'sl:arch']
end

.construct_os_name!(appliance, appliance_hash) ⇒ Object



78
79
80
81
82
# File 'lib/cloudkeeper/entities/appliance.rb', line 78

def construct_os_name!(appliance, appliance_hash)
  appliance.operating_system = appliance_hash[:'sl:os'].to_s
  appliance.operating_system = "#{appliance.operating_system} #{appliance_hash[:'sl:osname']}".strip
  appliance.operating_system = "#{appliance.operating_system} #{appliance_hash[:'sl:osversion']}".strip
end

.from_hash(appliance_hash) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/cloudkeeper/entities/appliance.rb', line 42

def from_hash(appliance_hash)
  appliance_hash.deep_symbolize_keys!
  appliance = populate_appliance appliance_hash
  appliance.image = Image.from_hash(appliance_hash)

  appliance
end

.populate_appliance(appliance_hash) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/cloudkeeper/entities/appliance.rb', line 50

def populate_appliance(appliance_hash)
  raise Cloudkeeper::Errors::Parsing::InvalidApplianceHashError, 'invalid appliance hash' if appliance_hash.blank?

  appliance = construct_appliance(appliance_hash)
  construct_os_name!(appliance, appliance_hash)
  populate_attributes!(appliance, appliance_hash)

  appliance
rescue Cloudkeeper::Errors::ArgumentError => ex
  raise Cloudkeeper::Errors::Parsing::InvalidApplianceHashError, ex, "appliance hash #{appliance_hash.inspect} " \
                                                                     "doesn't contain all the necessary data"
end

.populate_attributes!(appliance, appliance_hash) ⇒ Object



84
85
86
87
# File 'lib/cloudkeeper/entities/appliance.rb', line 84

def populate_attributes!(appliance, appliance_hash)
  appliance_hash.reject! { |k, _v| REJECTED_ATTRIBUTES.include? k }
  appliance.attributes = appliance_hash.map { |k, v| [k.to_s, v.to_s] }.to_h
end

Instance Method Details

#expired?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/cloudkeeper/entities/appliance.rb', line 37

def expired?
  expiration_date < Time.now
end