Class: Cloud::Appliance::Descriptor::Appliance

Inherits:
Object
  • Object
show all
Includes:
Tools::GeneralInit
Defined in:
lib/cloud/appliance/descriptor/appliance.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Tools::GeneralInit

#initialize

Instance Attribute Details

#actionObject

Returns the value of attribute action.



9
10
11
# File 'lib/cloud/appliance/descriptor/appliance.rb', line 9

def action
  @action
end

#attributesObject

Returns the value of attribute attributes.



9
10
11
# File 'lib/cloud/appliance/descriptor/appliance.rb', line 9

def attributes
  @attributes
end

#cpuObject

Returns the value of attribute cpu.



9
10
11
# File 'lib/cloud/appliance/descriptor/appliance.rb', line 9

def cpu
  @cpu
end

#descriptionObject

Returns the value of attribute description.



9
10
11
# File 'lib/cloud/appliance/descriptor/appliance.rb', line 9

def description
  @description
end

#disksObject

Returns the value of attribute disks.



9
10
11
# File 'lib/cloud/appliance/descriptor/appliance.rb', line 9

def disks
  @disks
end

#groupsObject

Returns the value of attribute groups.



9
10
11
# File 'lib/cloud/appliance/descriptor/appliance.rb', line 9

def groups
  @groups
end

#identifierObject

Returns the value of attribute identifier.



9
10
11
# File 'lib/cloud/appliance/descriptor/appliance.rb', line 9

def identifier
  @identifier
end

#memoryObject

Returns the value of attribute memory.



9
10
11
# File 'lib/cloud/appliance/descriptor/appliance.rb', line 9

def memory
  @memory
end

#osObject

Returns the value of attribute os.



9
10
11
# File 'lib/cloud/appliance/descriptor/appliance.rb', line 9

def os
  @os
end

#titleObject

Returns the value of attribute title.



9
10
11
# File 'lib/cloud/appliance/descriptor/appliance.rb', line 9

def title
  @title
end

#versionObject

Returns the value of attribute version.



9
10
11
# File 'lib/cloud/appliance/descriptor/appliance.rb', line 9

def version
  @version
end

Class Method Details

.from_hash(hash) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/cloud/appliance/descriptor/appliance.rb', line 40

def self.from_hash(hash)
  hash.each do |key, value|
    action = key
    parameters = value
    os = parameters.delete 'os'
    disks = parameters.delete 'disks'
    parameters[:action] = action

    appliance = Appliance.new parameters

    if os
      os = Os.new os
      appliance.os = os
    end

    if disks
      disks.each do |disk|
        disk = Disk.new disk
        appliance.add_disk disk
      end
    end

    return appliance
  end

  nil
end

.from_json(string) ⇒ Object



35
36
37
38
# File 'lib/cloud/appliance/descriptor/appliance.rb', line 35

def self.from_json(string)
  raw = JSON.parse(string)
  from_hash(raw)
end

Instance Method Details

#add_attribute(attribute) ⇒ Object



76
77
78
# File 'lib/cloud/appliance/descriptor/appliance.rb', line 76

def add_attribute(attribute)
  attributes.merge! attribute
end

#add_disk(disk) ⇒ Object



72
73
74
# File 'lib/cloud/appliance/descriptor/appliance.rb', line 72

def add_disk(disk)
  disks << disk
end

#add_group(group) ⇒ Object



68
69
70
# File 'lib/cloud/appliance/descriptor/appliance.rb', line 68

def add_group(group)
  groups << group
end

#remove_attribute(key) ⇒ Object



88
89
90
# File 'lib/cloud/appliance/descriptor/appliance.rb', line 88

def remove_attribute(key)
  attributes.delete(key)
end

#remove_disk(disk) ⇒ Object



84
85
86
# File 'lib/cloud/appliance/descriptor/appliance.rb', line 84

def remove_disk(disk)
  disks.delete(disk)
end

#remove_group(group) ⇒ Object



80
81
82
# File 'lib/cloud/appliance/descriptor/appliance.rb', line 80

def remove_group(group)
  groups.delete(group)
end

#set_defaultsObject



11
12
13
14
15
16
17
18
19
# File 'lib/cloud/appliance/descriptor/appliance.rb', line 11

def set_defaults
  @description ||= ''
  @memory ||= 1024
  @cpu ||= 1
  @groups ||= []
  @os ||= Os.new
  @disks ||= []
  @attributes ||= {}
end

#to_jsonObject



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/cloud/appliance/descriptor/appliance.rb', line 21

def to_json
  variables = {}
  except = [:@action, :@os, :@disks]
  instance_variables.select { |element| !except.include?(element) }.each do |variable|
    value = instance_variable_get variable
    variables[variable.to_s[1..-1]] = value
  end

  variables['os'] = os.to_hash
  variables['disks'] = disks.map(&:to_hash)

  JSON.pretty_generate(action => variables)
end