Module: Occi::Api::Client::Base::MixinMethods

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

Instance Method Summary collapse

Instance Method Details

#describe_mixin(name, type = nil) ⇒ Occi::Core::Mixin?

Looks up a mixin using its name and, optionally, a type as well. Will return mixin's full description.

Examples:

client.describe_mixin "debian6"
 # => #<Occi::Core::Mixin>
client.describe_mixin "debian6", "os_tpl"
 # => #<Occi::Core::Mixin>
client.describe_mixin "large", "resource_tpl"
 # => #<Occi::Core::Mixin>
client.describe_mixin "debian6", "resource_tpl" # => nil

Parameters:

  • name (String)

    name of the mixin

  • type (String) (defaults to: nil)

    type of the mixin

Returns:

  • (Occi::Core::Mixin, nil)

    mixin description or nothing found


47
48
49
50
51
52
# File 'lib/occi/api/client/base/mixin_methods.rb', line 47

def describe_mixin(name, type = nil)
  mixins = get_mixins(type)

  mixins = mixins.to_a.select { |m| m.term == name }
  mixins.any? ? mixins.first : nil
end

#describe_mixin_w_type(name, type) ⇒ Occi::Core::Mixin

Looks up a mixin with a specific type, will return mixin's full description.

Parameters:

  • name (String)

    name of the mixin

  • type (String)

    type of the mixin

Returns:

  • (Occi::Core::Mixin)

    mixin description


60
61
62
# File 'lib/occi/api/client/base/mixin_methods.rb', line 60

def describe_mixin_w_type(name, type)
  describe_mixin(name, type)
end

#describe_mixin_wo_type(name) ⇒ Occi::Core::Mixin

Looks up a mixin in all available mixin types, will return mixin's full description. Returns always the first match found, search will start in os_tpl.

Parameters:

  • name (String)

    name of the mixin

Returns:

  • (Occi::Core::Mixin)

    mixin description


70
71
72
# File 'lib/occi/api/client/base/mixin_methods.rb', line 70

def describe_mixin_wo_type(name)
  describe_mixin(name, nil)
end

#get_mixin(name, type = nil, describe = false) ⇒ String, ...

Looks up a mixin using its name and, optionally, a type as well. Will return mixin's full location (a link) or a description.

Examples:

client.get_mixin "debian6"
 # => "http://my.occi.service/occi/infrastructure/os_tpl#debian6"
client.get_mixin "debian6", "os_tpl", true
 # => #<Occi::Core::Mixin>
client.get_mixin "large", "resource_tpl"
 # => "http://my.occi.service/occi/infrastructure/resource_tpl#large"
client.get_mixin "debian6", "resource_tpl" # => nil

Parameters:

  • name (String)

    name of the mixin

  • type (String) (defaults to: nil)

    type of the mixin

  • describe (Boolean) (defaults to: false)

    should we describe the mixin or return its link?

Returns:

  • (String, Occi::Core::Mixin, nil)

    link, mixin description or nothing found


22
23
24
25
26
27
28
29
30
# File 'lib/occi/api/client/base/mixin_methods.rb', line 22

def get_mixin(name, type = nil, describe = false)
  # TODO: mixin fix
  Occi::Api::Log.debug "Looking for mixin #{name.inspect} #{type.inspect} " \
                  "#{describe.inspect}"

  # TODO: extend this code to support multiple matches and regex filters
  # should we look for links or descriptions?
  describe ? describe_mixin(name, type) : list_mixin(name, type)
end

#get_mixin_type_identifier(type) ⇒ String?

Retrieves available mixin type identifier for the given mixin type.

Examples:

client.get_mixin_type_identifier("os_tpl")
 # => 'http://schemas.ogf.org/occi/infrastructure#os_tpl'

Parameters:

  • type (String)

    short mixin type

Returns:

  • (String, nil)

    mixin type identifier for the given mixin type


187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/occi/api/client/base/mixin_methods.rb', line 187

def get_mixin_type_identifier(type)
  return type if (type =~ URI::ABS_URI) || (type && type.start_with?('/'))

  mixins = @model.mixins.to_a.select { |m| m.term == type }
  tis = mixins.collect { |m| m.type_identifier }
  tis.uniq!

  if tis.length > 1
    raise Occi::Api::Client::Errors::AmbiguousNameError,
          "Mixin type #{type.inspect} is ambiguous, use a type identifier!"
  end

  tis.first
end

#get_mixin_type_identifiersArray<String>

Retrieves available mixin type identifiers.

Examples:

client.get_mixin_type_identifiers
 # => ['http://schemas.ogf.org/occi/infrastructure#os_tpl',
 #     'http://schemas.ogf.org/occi/infrastructure#resource_tpl']

Returns:

  • (Array<String>)

    list of available mixin type identifiers


175
176
177
# File 'lib/occi/api/client/base/mixin_methods.rb', line 175

def get_mixin_type_identifiers
  list_mixins(nil)
end

#get_mixin_typesArray<String>

Retrieves available mixin types. Mixin types are presented in a shortened format (i.e. not as type identifiers).

Examples:

client.get_mixin_types # => [ "os_tpl", "resource_tpl" ]

Returns:

  • (Array<String>)

    list of available mixin types


163
164
165
# File 'lib/occi/api/client/base/mixin_methods.rb', line 163

def get_mixin_types
  get_mixins.to_a.collect { |m| m.term }
end

#get_mixins(type = nil, include_self = false) ⇒ Occi::Core::Mixins

Retrieves available mixins of a specified type or all available mixins if the type wasn't specified. Mixins are returned in the form of mixin instances.

Examples:

client.get_mixins
 # => #<Occi::Core::Mixins>
client.get_mixins "os_tpl"
 # => #<Occi::Core::Mixins>
client.get_mixins "resource_tpl"
 # => #<Occi::Core::Mixins>

Parameters:

  • type (String) (defaults to: nil)

    type of mixins

  • include_self (Boolean) (defaults to: false)

    include type itself as a mixin

Returns:

  • (Occi::Core::Mixins)

    collection of available mixins


109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/occi/api/client/base/mixin_methods.rb', line 109

def get_mixins(type = nil, include_self = false)
  unless type.blank?
    type_id = get_mixin_type_identifier(type)
    unless type_id
      raise ArgumentError,
            "There is no such mixin type registered in the model! #{type.inspect}"
    end

    mixins = @model.mixins.to_a.select { |m| m.related_to?(type_id) }

    # drop the type mixin itself
    mixins.delete_if { |m| m.type_identifier == type_id } unless include_self
  else
    # we did not get a type, return all mixins
    mixins = Occi::Core::Mixins.new(@model.mixins)
  end

  unless mixins.kind_of? Occi::Core::Mixins
    col = Occi::Core::Mixins.new
    mixins.each { |m| col << m }
  else
    col = mixins
  end

  col
end

#get_os_templatesOcci::Core::Mixins Also known as: get_os_tpls

Retrieves available os_tpls from the model.

Examples:

get_os_templates # => #<Occi::Core::Mixins>

Returns:

  • (Occi::Core::Mixins)

    collection containing all registered OS templates


208
209
210
# File 'lib/occi/api/client/base/mixin_methods.rb', line 208

def get_os_templates
  get_mixins Occi::Infrastructure::OsTpl.mixin.type_identifier
end

#get_resource_templatesOcci::Core::Mixins Also known as: get_resource_tpls

Retrieves available resource_tpls from the model.

Examples:

get_resource_templates # => #<Occi::Core::Mixins>

Returns:

  • (Occi::Core::Mixins)

    collection containing all registered resource templates


219
220
221
# File 'lib/occi/api/client/base/mixin_methods.rb', line 219

def get_resource_templates
  get_mixins Occi::Infrastructure::ResourceTpl.mixin.type_identifier
end

#list_mixin(name, type = nil) ⇒ String?

Looks up a mixin using its name and, optionally, a type as well. Will return mixin's full location.

Examples:

client.list_mixin "debian6"
 # => "http://my.occi.service/occi/infrastructure/os_tpl#debian6"
client.list_mixin "debian6", "os_tpl"
 # => "http://my.occi.service/occi/infrastructure/os_tpl#debian6"
client.list_mixin "large", "resource_tpl"
 # => "http://my.occi.service/occi/infrastructure/resource_tpl#large"
client.list_mixin "debian6", "resource_tpl" # => nil

Parameters:

  • name (String)

    name of the mixin

  • type (String) (defaults to: nil)

    type of the mixin

Returns:

  • (String, nil)

    link or nothing found


89
90
91
92
# File 'lib/occi/api/client/base/mixin_methods.rb', line 89

def list_mixin(name, type = nil)
  mixin = describe_mixin(name, type)
  mixin ? mixin.type_identifier : nil
end

#list_mixins(type = nil, include_self = false) ⇒ Array<String>

Retrieves available mixins of a specified type or all available mixins if the type wasn't specified. Mixins are returned in the form of mixin identifiers.

Examples:

client.list_mixins
 # => #<Array<String>>
client.list_mixins "os_tpl"
 # => #<Array<String>>
client.list_mixins "resource_tpl"
 # => #<Array<String>>

Parameters:

  • type (String) (defaults to: nil)

    type of mixins

  • include_self (Boolean) (defaults to: false)

    include type itself as a mixin

Returns:

  • (Array<String>)

    collection of available mixin identifiers


151
152
153
154
# File 'lib/occi/api/client/base/mixin_methods.rb', line 151

def list_mixins(type = nil, include_self = false)
  mixins = get_mixins(type, include_self)
  mixins.to_a.collect { |m| m.type_identifier }
end