Module: Backends::Helpers::JsonCollectionHelper

Included in:
Dummy::Base, Ec2::Base, Opennebula::Base
Defined in:
lib/backends/helpers/json_collection_helper.rb

Overview

Helps with loading OCCI collections from JSON files.

Instance Method Summary collapse

Instance Method Details

#read_from_json(path) ⇒ ::Occi::Collection

Reads an OCCI-compliant collection from a JSON file.

Parameters:

  • path (String)

    readable JSON file

Returns:

  • (::Occi::Collection)

    parsed collection instance


9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/backends/helpers/json_collection_helper.rb', line 9

def read_from_json(path)
  fail "Couldn't read a collection from #{path.inspect}! " \
       "File not readable!" unless File.readable?(path)
  collection = ::Occi::Collection.new
  collection.model = nil

  # Load all JSON files in the given directory, these contain
  # JSON rendering of OCCI kind/mixin/action definitions
  @logger.debug "[#{self.class}] Getting fixtures from #{path}"
  parsed = JSON.parse(File.read(path))
  collection.merge! ::Occi::Collection.new(parsed)

  collection
end