Class: Backends::Dummy::Network

Inherits:
Base
  • Object
show all
Defined in:
lib/backends/dummy/network.rb

Constant Summary

Constants inherited from Base

Base::API_VERSION, Base::FIXTURES, Base::FIXTURES_TPL

Instance Method Summary collapse

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::Dummy::Base

Instance Method Details

#create(network) ⇒ String

Instantiates a new network instance from ::Occi::Infrastructure::Network. 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:

network = ::Occi::Infrastructure::Network.new
network_id = create(network)
    #=> "65d4f65adfadf-ad2f4ad-daf5ad-f5ad4fad4ffdf"

Parameters:

  • network (::Occi::Infrastructure::Network)

    network instance containing necessary attributes

Returns:

  • (String)

    final identifier of the new network instance


73
74
75
76
77
78
79
80
# File 'lib/backends/dummy/network.rb', line 73

def create(network)
  fail Backends::Errors::IdentifierConflictError, "Instance with ID #{network.id} already exists!" if list_ids.include?(network.id)

  updated = read_network_fixtures << network
  save_network_fixtures(updated)

  network.id
end

#delete(network_id) ⇒ true, false

Deletes a specific network 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:

  • network_id (String)

    an identifier of a network instance to be deleted

Returns:

  • (true, false)

    result of the operation


118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/backends/dummy/network.rb', line 118

def delete(network_id)
  fail Backends::Errors::ResourceNotFoundError, "Instance with ID #{network_id} does not exist!" unless list_ids.include?(network_id)

  updated = read_network_fixtures.delete_if { |n| n.id == network_id }
  save_network_fixtures(updated)

  begin
    get(network_id)
    false
  rescue Backends::Errors::ResourceNotFoundError
    true
  end
end

#delete_all(mixins = nil) ⇒ true, false

Deletes all network 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


95
96
97
98
99
100
101
102
103
104
105
# File 'lib/backends/dummy/network.rb', line 95

def delete_all(mixins = nil)
  if mixins.blank?
    drop_network_fixtures
    read_network_fixtures.empty?
  else
    old_count = read_network_fixtures.count
    updated = read_network_fixtures.delete_if { |n| (n.mixins & mixins).any? }
    save_network_fixtures(updated)
    old_count != read_network_fixtures.count
  end
end

#get(network_id) ⇒ ::Occi::Infrastructure::Network?

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

Examples:

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

Parameters:

  • network_id (String)

    OCCI identifier of the requested network instance

Returns:

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

    a network instance or `nil`


53
54
55
56
57
58
# File 'lib/backends/dummy/network.rb', line 53

def get(network_id)
  found = read_network_fixtures.to_a.select { |n| n.id == network_id }.first
  fail Backends::Errors::ResourceNotFoundError, "Instance with ID #{network_id} does not exist!" unless found

  found
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)


233
234
235
236
# File 'lib/backends/dummy/network.rb', line 233

def get_extensions
  # no extensions to include
  ::Occi::Collection.new
end

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

Gets all network 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::Network instances wrapped in ::Occi::Core::Resources.

Examples:

networks = list #=> #<::Occi::Core::Resources>
networks.first #=> #<::Occi::Infrastructure::Network>

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

Parameters:

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

    a filter containing mixins

Returns:

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

    a collection of network instances


33
34
35
36
37
38
39
40
# File 'lib/backends/dummy/network.rb', line 33

def list(mixins = nil)
  if mixins.blank?
    read_network_fixtures
  else
    filtered_networks = read_network_fixtures.to_a.select { |n| (n.mixins & mixins).any? }
    ::Occi::Core::Resources.new filtered_networks
  end
end

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

Gets all network instance IDs, no details, no duplicates. Returned identifiers must correspond to those found in the occi.core.id attribute of ::Occi::Infrastructure::Network 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 network instances


15
16
17
# File 'lib/backends/dummy/network.rb', line 15

def list_ids(mixins = nil)
  list(mixins).to_a.map { |n| n.id }
end

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

Partially updates an existing network instance, instance to be updated is specified by network_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(network_id, attributes, mixins, links) #=> true

Parameters:

  • network_id (String)

    unique identifier of a network 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


148
149
150
151
# File 'lib/backends/dummy/network.rb', line 148

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

#trigger_action(network_id, action_instance) ⇒ true, false

Triggers an action on an existing network instance, the network instance in question is identified by a network 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:

  • network_id (String)

    network instance identifier

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

    action to be triggered

Returns:

  • (true, false)

    result of the operation


206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/backends/dummy/network.rb', line 206

def trigger_action(network_id, action_instance)
  case action_instance.action.type_identifier
  when 'http://schemas.ogf.org/occi/infrastructure/network/action#down'
    state = 'inactive'
  when 'http://schemas.ogf.org/occi/infrastructure/network/action#up'
    state = 'active'
  else
    fail Backends::Errors::ActionNotImplementedError,
         "Action #{action_instance.action.type_identifier.inspect} is not implemented!"
  end

  # get existing network instance and set a new state
  network = get(network_id)
  network.state = state

  # clean-up and save the new collection
  delete(network.id)
  updated = read_network_fixtures << network
  save_network_fixtures(updated)

  true
end

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

Triggers an action on all existing network 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


187
188
189
190
# File 'lib/backends/dummy/network.rb', line 187

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

#update(network) ⇒ true, false

Updates an existing network 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:

network = ::Occi::Infrastructure::Network.new
update(network) #=> true

Parameters:

  • network (::Occi::Infrastructure::Network)

    instance containing updated information

Returns:

  • (true, false)

    result of the operation


164
165
166
167
168
169
170
171
# File 'lib/backends/dummy/network.rb', line 164

def update(network)
  fail Backends::Errors::ResourceNotFoundError, "Instance with ID #{network.id} does not exist!" unless list_ids.include?(network.id)

  delete(network.id)
  updated = read_network_fixtures << network
  save_network_fixtures(updated)
  get(network.id) == network
end