Module: Backends::Opennebula::Helpers::StorageParseHelper

Included in:
Storage
Defined in:
lib/backends/opennebula/helpers/storage_parse_helper.rb

Instance Method Summary collapse

Instance Method Details

#parse_backend_obj(backend_storage) ⇒ Object


5
6
7
8
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
# File 'lib/backends/opennebula/helpers/storage_parse_helper.rb', line 5

def parse_backend_obj(backend_storage)
  storage = ::Occi::Infrastructure::Storage.new

  # include some basic mixins
  storage.mixins << 'http://schemas.opennebula.org/occi/infrastructure#storage'

  # include mixins stored in ON's VN template
  unless backend_storage['TEMPLATE/OCCI_STORAGE_MIXINS'].blank?
    backend_storage_mixins = backend_storage['TEMPLATE/OCCI_STORAGE_MIXINS'].split(' ')
    backend_storage_mixins.each do |mixin|
      storage.mixins << mixin unless mixin.blank?
    end
  end

  # include basic OCCI attributes
  basic_attrs = parse_basic_attrs(backend_storage)
  storage.attributes.merge! basic_attrs

  # include ONE-specific attributes
  one_attrs = parse_one_attrs(backend_storage)
  storage.attributes.merge! one_attrs

  # include state information and available actions
  result = parse_state(backend_storage)
  storage.state = result.state
  result.actions.each { |a| storage.actions << a }

  storage
end

#parse_basic_attrs(backend_storage) ⇒ Object


35
36
37
38
39
40
41
42
43
44
45
# File 'lib/backends/opennebula/helpers/storage_parse_helper.rb', line 35

def parse_basic_attrs(backend_storage)
  basic_attrs = ::Occi::Core::Attributes.new

  basic_attrs['occi.core.id']  = backend_storage['ID']
  basic_attrs['occi.core.title'] = backend_storage['NAME'] if backend_storage['NAME']
  basic_attrs['occi.core.summary'] = backend_storage['TEMPLATE/DESCRIPTION'] unless backend_storage['TEMPLATE/DESCRIPTION'].blank?

  basic_attrs['occi.storage.size'] = backend_storage['SIZE'].to_f / 1024 if backend_storage['SIZE']

  basic_attrs
end

#parse_one_attrs(backend_storage) ⇒ Object


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/backends/opennebula/helpers/storage_parse_helper.rb', line 47

def parse_one_attrs(backend_storage)
  one_attrs = ::Occi::Core::Attributes.new

  one_attrs['org.opennebula.storage.id'] = backend_storage['ID']
  one_attrs['org.opennebula.storage.type'] = backend_storage.type_str

  if backend_storage['PERSISTENT'].blank? || backend_storage['PERSISTENT'].to_i == 0
    one_attrs['org.opennebula.storage.persistent'] = 'NO'
  else
    one_attrs['org.opennebula.storage.persistent'] = 'YES'
  end

  one_attrs['org.opennebula.storage.dev_prefix'] = backend_storage['TEMPLATE/DEV_PREFIX'] if backend_storage['TEMPLATE/DEV_PREFIX']
  one_attrs['org.opennebula.storage.bus'] = backend_storage['TEMPLATE/BUS'] if backend_storage['TEMPLATE/BUS']
  one_attrs['org.opennebula.storage.driver'] = backend_storage['TEMPLATE/DRIVER'] if backend_storage['TEMPLATE/DRIVER']

  one_attrs
end

#parse_state(backend_storage) ⇒ Object


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/backends/opennebula/helpers/storage_parse_helper.rb', line 66

def parse_state(backend_storage)
  result = Hashie::Mash.new

  # In ON 4.4:
  #    IMAGE_STATES=%w{INIT READY USED DISABLED LOCKED ERROR
  #                    CLONE DELETE USED_PERS}
  #
  case backend_storage.state_str
  when 'READY'
    result.state = 'online'
    result.actions = %w|http://schemas.ogf.org/occi/infrastructure/storage/action#offline http://schemas.ogf.org/occi/infrastructure/storage/action#backup|
  when 'USED', 'CLONE', 'USED_PERS'
    result.state = 'online'
    result.actions = []
  when 'DISABLED'
    result.state = 'offline'
    result.actions = %w|http://schemas.ogf.org/occi/infrastructure/storage/action#online|
  when 'ERROR'
    result.state = 'degraded'
    result.actions = %w|http://schemas.ogf.org/occi/infrastructure/storage/action#online|
  else
    result.state = 'offline'
    result.actions = []
  end

  result
end