Class: Backends::Opennebula::Storage

Inherits:
Base
  • Object
show all
Includes:
Helpers::StorageActionHelper, Helpers::StorageParseHelper
Defined in:
lib/backends/opennebula/storage.rb

Constant Summary

Constants inherited from Base

Base::API_VERSION

Instance Method Summary collapse

Methods included from Helpers::StorageActionHelper

#trigger_action_backup, #trigger_action_offline, #trigger_action_online, #trigger_action_prolog, #trigger_action_state_check

Methods included from Helpers::StorageParseHelper

#parse_backend_obj, #parse_basic_attrs, #parse_one_attrs, #parse_state

Methods inherited from Base

#add_other_backend, #initialize

Methods included from Helpers::ExtensionsHelper

#read_extensions

Methods included from Helpers::JsonCollectionHelper

#read_from_json

Constructor Details

This class inherits a constructor from Backends::Opennebula::Base

Instance Method Details

#create(storage) ⇒ String

Instantiates a new storage instance from ::Occi::Infrastructure::Storage. ID given in the occi.core.id attribute is optional and can be changed inside this method. Final occi.core.id must be returned as a String. If the requested instance cannot be created, an error describing the problem must be raised, @see Backends::Errors.

Examples:

storage = ::Occi::Infrastructure::Storage.new
storage_id = create(storage)
    #=> "65d4f65adfadf-ad2f4ad-daf5ad-f5ad4fad4ffdf"

Parameters:

  • storage (::Occi::Infrastructure::Storage)

    storage instance containing necessary attributes

Returns:

  • (String)

    final identifier of the new storage instance


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/backends/opennebula/storage.rb', line 89

def create(storage)
  @logger.debug "[Backends] [Opennebula] Creating storage #{storage.inspect} "\
                "in DS[#{@options.storage_datastore_id}]"

  # include some basic mixins
  # WARNING: adding mix-ins will re-set their attributes
  attr_backup = ::Occi::Core::Attributes.new(storage.attributes)
  storage.mixins << 'http://schemas.opennebula.org/occi/infrastructure#storage'
  storage.attributes = attr_backup

  template_location = File.join(@options.templates_dir, 'storage.erb')
  template = Erubis::Eruby.new(File.read(template_location)).evaluate(storage: storage)

  @logger.debug "[Backends] [Opennebula] Template #{template.inspect}"

  image_alloc = ::OpenNebula::Image.build_xml
  backend_object = ::OpenNebula::Image.new(image_alloc, @client)

  rc = backend_object.allocate(template, @options.storage_datastore_id.to_i)
  check_retval(rc, Backends::Errors::ResourceCreationError)

  rc = backend_object.info
  check_retval(rc, Backends::Errors::ResourceRetrievalError)

  rc = backend_object.persistent
  check_retval(rc, Backends::Errors::ResourceActionError)

  backend_object['ID']
end

#delete(storage_id) ⇒ true, false

Deletes a specific storage instance, instance to be deleted is specified by an ID, this ID must match the occi.core.id attribute of the deleted instance. If the requested instance cannot be deleted, an error describing the problem must be raised, @see Backends::Errors.

Examples:

delete("65d4f65adfadf-ad2f4ad-daf5ad-f5ad4fad4ffdf") #=> true

Parameters:

  • storage_id (String)

    an identifier of a storage instance to be deleted

Returns:

  • (true, false)

    result of the operation


157
158
159
160
161
162
163
164
165
166
# File 'lib/backends/opennebula/storage.rb', line 157

def delete(storage_id)
  storage = ::OpenNebula::Image.new(::OpenNebula::Image.build_xml(storage_id), @client)
  rc = storage.info
  check_retval(rc, Backends::Errors::ResourceRetrievalError)

  rc = storage.delete
  check_retval(rc, Backends::Errors::ResourceActionError)

  true
end

#delete_all(mixins = nil) ⇒ true, false

Deletes all storage instances, instances to be deleted must be filtered by the specified filter, filter (if set) must contain an ::Occi::Core::Mixins instance. If the requested instances cannot be deleted, an error describing the problem must be raised, @see Backends::Errors.

Examples:

delete_all #=> true

mixins = ::Occi::Core::Mixins.new << ::Occi::Core::Mixin.new
delete_all(mixins)  #=> true

Parameters:

  • mixins (::Occi::Core::Mixins) (defaults to: nil)

    a filter containing mixins

Returns:

  • (true, false)

    result of the operation


132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/backends/opennebula/storage.rb', line 132

def delete_all(mixins = nil)
  # TODO: impl filtering with mixins
  backend_storage_pool = ::OpenNebula::ImagePool.new(@client)
  rc = backend_storage_pool.info_all
  check_retval(rc, Backends::Errors::ResourceRetrievalError)

  backend_storage_pool.each do |backend_storage|
    rc = backend_storage.delete
    check_retval(rc, Backends::Errors::ResourceActionError)
  end

  true
end

#get(storage_id) ⇒ ::Occi::Infrastructure::Storage?

Gets a specific storage instance as ::Occi::Infrastructure::Storage. ID given as an argument must match the occi.core.id attribute inside the returned ::Occi::Infrastructure::Storage instance, however it is possible to implement internal mapping to a platform-specific identifier.

Examples:

storage = get('65d4f65adfadf-ad2f4ad-daf5ad-f5ad4fad4ffdf')
    #=> #<::Occi::Infrastructure::Storage>

Parameters:

  • storage_id (String)

    OCCI identifier of the requested storage instance

Returns:

  • (::Occi::Infrastructure::Storage, nil)

    a storage instance or `nil`


68
69
70
71
72
73
74
# File 'lib/backends/opennebula/storage.rb', line 68

def get(storage_id)
  image = ::OpenNebula::Image.new(::OpenNebula::Image.build_xml(storage_id), @client)
  rc = image.info
  check_retval(rc, Backends::Errors::ResourceRetrievalError)

  parse_backend_obj(image)
end

#get_extensions::Occi::Collection

Returns a collection of custom mixins introduced (and specific for) the enabled backend. Only mixins and actions are allowed.

Returns:

  • (::Occi::Collection)

    collection of extensions (custom mixins and/or actions)


258
259
260
# File 'lib/backends/opennebula/storage.rb', line 258

def get_extensions
  read_extensions 'storage', @options.model_extensions_dir
end

#list(mixins = nil) ⇒ ::Occi::Core::Resources

Gets all storage instances, instances must be filtered by the specified filter, filter (if set) must contain an ::Occi::Core::Mixins instance. Returned collection must contain ::Occi::Infrastructure::Storage instances wrapped in ::Occi::Core::Resources.

Examples:

storages = list #=> #<::Occi::Core::Resources>
storages.first #=> #<::Occi::Infrastructure::Storage>

mixins = ::Occi::Core::Mixins.new << ::Occi::Core::Mixin.new
storages = list(mixins) #=> #<::Occi::Core::Resources>

Parameters:

  • mixins (::Occi::Core::Mixins) (defaults to: nil)

    a filter containing mixins

Returns:

  • (::Occi::Core::Resources)

    a collection of storage instances


43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/backends/opennebula/storage.rb', line 43

def list(mixins = nil)
  # TODO: impl filtering with mixins
  storage = ::Occi::Core::Resources.new
  backend_storage_pool = ::OpenNebula::ImagePool.new(@client)
  rc = backend_storage_pool.info_all
  check_retval(rc, Backends::Errors::ResourceRetrievalError)

  backend_storage_pool.each do |backend_storage|
    storage << parse_backend_obj(backend_storage)
  end

  storage
end

#list_ids(mixins = nil) ⇒ Array<String>

Gets all storage instance IDs, no details, no duplicates. Returned identifiers must correspond to those found in the occi.core.id attribute of ::Occi::Infrastructure::Storage instances.

Examples:

list_ids #=> []
list_ids #=> ["65d4f65adfadf-ad2f4ad-daf5ad-f5ad4fad4ffdf",
                         "ggf4f65adfadf-adgg4ad-daggad-fydd4fadyfdfd"]

Parameters:

  • mixins (::Occi::Core::Mixins) (defaults to: nil)

    a filter containing mixins

Returns:

  • (Array<String>)

    IDs for all available storage instances


15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/backends/opennebula/storage.rb', line 15

def list_ids(mixins = nil)
  # TODO: impl filtering with mixins
  backend_image_pool = ::OpenNebula::ImagePool.new(@client)
  rc = backend_image_pool.info_all
  check_retval(rc, Backends::Errors::ResourceRetrievalError)

  storage = []
  backend_image_pool.each do |backend_image|
    storage << backend_image['ID']
  end

  storage
end

#partial_update(storage_id, attributes = nil, mixins = nil, links = nil) ⇒ true, false

Partially updates an existing storage instance, instance to be updated is specified by storage_id. If the requested instance cannot be updated, an error describing the problem must be raised, @see Backends::Errors.

Examples:

attributes = ::Occi::Core::Attributes.new
mixins = ::Occi::Core::Mixins.new
links = ::Occi::Core::Links.new
partial_update(storage_id, attributes, mixins, links) #=> true

Parameters:

  • storage_id (String)

    unique identifier of a storage instance to be updated

  • attributes (::Occi::Core::Attributes) (defaults to: nil)

    a collection of attributes to be updated

  • mixins (::Occi::Core::Mixins) (defaults to: nil)

    a collection of mixins to be added

  • links (::Occi::Core::Links) (defaults to: nil)

    a collection of links to be added

Returns:

  • (true, false)

    result of the operation


184
185
186
187
# File 'lib/backends/opennebula/storage.rb', line 184

def partial_update(storage_id, attributes = nil, mixins = nil, links = nil)
  # TODO: impl
  fail Backends::Errors::MethodNotImplementedError, 'Partial updates are currently not supported!'
end

#trigger_action(storage_id, action_instance) ⇒ true, false

Triggers an action on an existing storage instance, the storage instance in question is identified by a storage instance ID, action is identified by the action.term attribute of the action instance passed as an argument. If the requested action cannot be triggered, an error describing the problem must be raised, @see Backends::Errors.

Examples:

action_instance = ::Occi::Core::ActionInstance.new
trigger_action("65d4f65adfadf-ad2f4ad-daf5ad-f5ad4fad4ffdf", action_instance)
  #=> true

Parameters:

  • storage_id (String)

    storage instance identifier

  • action_instance (::Occi::Core::ActionInstance)

    action to be triggered

Returns:

  • (true, false)

    result of the operation


238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/backends/opennebula/storage.rb', line 238

def trigger_action(storage_id, action_instance)
  case action_instance.action.type_identifier
  when 'http://schemas.ogf.org/occi/infrastructure/storage/action#online'
    trigger_action_online(storage_id, action_instance.attributes)
  when 'http://schemas.ogf.org/occi/infrastructure/storage/action#offline'
    trigger_action_offline(storage_id, action_instance.attributes)
  when 'http://schemas.ogf.org/occi/infrastructure/storage/action#backup'
    trigger_action_backup(storage_id, action_instance.attributes)
  else
    fail Backends::Errors::ActionNotImplementedError,
         "Action #{action_instance.action.type_identifier.inspect} is not implemented!"
  end

  true
end

#trigger_action_on_all(action_instance, mixins = nil) ⇒ true, false

Triggers an action on all existing storage instance, instances must be filtered by the specified filter, filter (if set) must contain an ::Occi::Core::Mixins instance, action is identified by the action.term attribute of the action instance passed as an argument. If the requested action cannot be triggered, an error describing the problem must be raised, @see Backends::Errors.

Examples:

action_instance = ::Occi::Core::ActionInstance.new
mixins = ::Occi::Core::Mixins.new << ::Occi::Core::Mixin.new
trigger_action_on_all(action_instance, mixin) #=> true

Parameters:

  • action_instance (::Occi::Core::ActionInstance)

    action to be triggered

  • mixins (::Occi::Core::Mixins) (defaults to: nil)

    a filter containing mixins

Returns:

  • (true, false)

    result of the operation


219
220
221
222
# File 'lib/backends/opennebula/storage.rb', line 219

def trigger_action_on_all(action_instance, mixins = nil)
  list_ids(mixins).each { |strg| trigger_action(strg, action_instance) }
  true
end

#update(storage) ⇒ true, false

Updates an existing storage instance, instance to be updated is specified using the occi.core.id attribute of the instance passed as an argument. If the requested instance cannot be updated, an error describing the problem must be raised, @see Backends::Errors.

Examples:

storage = ::Occi::Infrastructure::Storage.new
update(storage) #=> true

Parameters:

  • storage (::Occi::Infrastructure::Storage)

    instance containing updated information

Returns:

  • (true, false)

    result of the operation


200
201
202
203
# File 'lib/backends/opennebula/storage.rb', line 200

def update(storage)
  # TODO: impl
  fail Backends::Errors::MethodNotImplementedError, 'Updates are currently not supported!'
end