Class: Occi::Bin::ResourceOutputFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/occi/bin/resource_output_factory.rb

Constant Summary collapse

@@allowed_formats =
[:json, :plain].freeze
@@allowed_resource_types =
[:compute, :storage, :network, :os_tpl, :resource_tpl].freeze
@@allowed_data_types =
[:locations, :resources].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output_format = :plain) ⇒ ResourceOutputFactory

Returns a new instance of ResourceOutputFactory


15
16
17
18
# File 'lib/occi/bin/resource_output_factory.rb', line 15

def initialize(output_format = :plain)
  raise "Unsupported output format!" unless @@allowed_formats.include? output_format
  @output_format = output_format
end

Instance Attribute Details

#output_formatObject (readonly)

Returns the value of attribute output_format


13
14
15
# File 'lib/occi/bin/resource_output_factory.rb', line 13

def output_format
  @output_format
end

Class Method Details

.allowed_data_typesObject


46
47
48
# File 'lib/occi/bin/resource_output_factory.rb', line 46

def self.allowed_data_types
  @@allowed_data_types
end

.allowed_formatsObject


38
39
40
# File 'lib/occi/bin/resource_output_factory.rb', line 38

def self.allowed_formats
  @@allowed_formats
end

.allowed_resource_typesObject


42
43
44
# File 'lib/occi/bin/resource_output_factory.rb', line 42

def self.allowed_resource_types
  @@allowed_resource_types
end

.locations_to_json(url_locations, resource_type) ⇒ Object


70
71
72
73
74
75
76
77
78
79
80
# File 'lib/occi/bin/resource_output_factory.rb', line 70

def self.locations_to_json(url_locations, resource_type)
  # give all locatios a common key and convert to JSON
  locations = { resource_type => [] }

  url_locations.each do |location|
    location = location.split("/").last if [:os_tpl, :resource_tpl].include? resource_type
    locations[resource_type] << location
  end

  locations.to_json
end

.locations_to_plain(url_locations, resource_type) ⇒ Object


82
83
84
85
86
87
88
89
90
91
92
# File 'lib/occi/bin/resource_output_factory.rb', line 82

def self.locations_to_plain(url_locations, resource_type)
  # just an attempt to make the array more readable 
  output = "\n" + resource_type.to_s.capitalize + " locations:\n"

  url_locations.each do |location|
    location = location.split("/").last if [:os_tpl, :resource_tpl].include? resource_type
    output << "\t" << location << "\n"
  end

  output
end

.resources_to_json(occi_resources, resource_type) ⇒ Object


50
51
52
53
# File 'lib/occi/bin/resource_output_factory.rb', line 50

def self.resources_to_json(occi_resources, resource_type)
  # generate JSON document from an array of JSON objects 
  JSON.generate occi_resources
end

.resources_to_plain(occi_resources, resource_type) ⇒ Object


55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/occi/bin/resource_output_factory.rb', line 55

def self.resources_to_plain(occi_resources, resource_type)
  # using ERB templates for known resource and mixin types
  file = File.expand_path("..", __FILE__) + '/templates/' + resource_type.to_s + ".erb"
  template = ERB.new File.new(file).read

  formatted_output = ""

  occi_resources.each do |occi_resource|
    json_resource = occi_resource.as_json
    formatted_output << template.result(binding) unless json_resource.nil? || json_resource.empty?
  end

  formatted_output
end

Instance Method Details

#format(data, data_type, resource_type) ⇒ Object


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/occi/bin/resource_output_factory.rb', line 20

def format(data, data_type, resource_type)
  raise "Data has to be in an array!" unless data.is_a? Array
  raise "Unsupported resource type! Got: #{resource_type.to_s}" unless @@allowed_resource_types.include? resource_type
  raise "Unsupported data type! Got: #{data_type.to_s}" unless @@allowed_data_types.include? data_type

  # validate incoming data
  if data_type == :resources
    data.each do |occi_collection|
      raise "I need the resources to be in a Collection!" unless occi_collection.respond_to? :as_json
    end
  end

  # construct method name from data type and output format
  method = (data_type.to_s + "_to_" + output_format.to_s).to_sym

  ResourceOutputFactory.send method, data, resource_type
end