Module: Occi::Api::Client::Base::Helpers

Included in:
ClientBase
Defined in:
lib/occi/api/client/base/helpers.rb

Instance Method Summary collapse

Instance Method Details

#path_for_instance(instance) ⇒ String

Returns the path for a given instance, instances not providing path information will raise an exception.

Examples:

path_for_instance Occi::Infrastructure::Network.new
 # => "/network/35ad4f45gsf-gsfg524s6gsfg-sfgsf4gsfg"
path_for_instance Occi::Infrastructure::Compute.new
 # => "/compute/35ad4f45gsf-gsfg524s6gsfg-sfgsf4gsfg"
path_for_instance Occi::Core::Mixin.new
 # => "/mixin/my_mixin/"
path_for_instance Occi::Infrastructure::Storagelink.new
 # => "/link/storagelink/35ad4f45gsf-gsfg524s6gsfg-sfgsf4gsfg"

Parameters:

  • instance (Object)

    instance

Returns:

  • (String)

    path for the given instance


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/occi/api/client/base/helpers.rb', line 50

def path_for_instance(instance)
  unless instance.respond_to?(:location)
    raise Occi::Api::Client::Errors::TypeMismatchError,
          "Expected an instance responding to #location, " \
          "got #{instance.class.name.inspect}"
  end

  if instance.location.blank?
    raise Occi::Api::Client::Errors::LocationError,
          "Instance of #{instance.class.name.inspect} has " \
          "an empty location"
  end

  instance.location
end

#path_for_kind_type_identifier(kind_type_identifier) ⇒ String

Returns the path for a given kind type identifier

Examples:

path_for_kind_type_identifier "http://schemas.ogf.org/occi/infrastructure#compute"
 # => "/compute/"
path_for_kind_type_identifier "http://localhost:3300/compute/35ad4f45gsf-gsfg524s6gsfg-sfgsf4gsfg"
 # => "/compute/35ad4f45gsf-gsfg524s6gsfg-sfgsf4gsfg"

Parameters:

  • kind_type_identifier (String)

    kind type identifier

Returns:

  • (String)

Raises:

  • (ArgumentError)

16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/occi/api/client/base/helpers.rb', line 16

def path_for_kind_type_identifier(kind_type_identifier)
  raise ArgumentError,
        "Kind type identifier is a required argument!" if kind_type_identifier.blank?

  if kind_type_identifier.start_with?(@endpoint.to_s) || kind_type_identifier.start_with?('/')
    #we got an instance link
    return sanitize_instance_link(kind_type_identifier)
  end

  kind_type_id = get_kind_type_identifier(kind_type_identifier)
  unless kind_type_id
    raise ArgumentError,
          "There is no such kind type registered in the model! #{kind_type_identifier.inspect}"
  end

  kinds = @model.kinds.select { |kind| kind.type_identifier == kind_type_id }
  path_for_instance(kinds.first)
end

Extracts path from an instance link. It will remove the leading @endpoint and replace it with a slash.

Examples:

sanitize_instance_link "http://localhost:3300/compute/35ad4f45gsf-gsfg524s6gsfg-sfgsf4gsfg"
 # => "/compute/35ad4f45gsf-gsfg524s6gsfg-sfgsf4gsfg"
sanitize_instance_link "/compute/35ad4f45gsf-gsfg524s6gsfg-sfgsf4gsfg"
 # => "/compute/35ad4f45gsf-gsfg524s6gsfg-sfgsf4gsfg"

Parameters:

  • instance_link (String)

    string containing the full instance link

Returns:

  • (String)

    extracted path, with a leading slash


77
78
79
80
81
82
83
84
85
86
# File 'lib/occi/api/client/base/helpers.rb', line 77

def sanitize_instance_link(instance_link)
  # everything starting with '/' is considered to be a resource path
  return instance_link if instance_link.start_with? '/'

  unless instance_link.start_with?(@endpoint.to_s)
    raise ArgumentError, "Resource link #{instance_link.inspect} is not valid!"
  end

  URI(instance_link).request_uri
end