module Ravello

Constants

DEFAULT_URL

The default service endpoint used by the Client.

Public Class Methods

deep_copy(o) click to toggle source

Return a deep copy of an object. @param o [Hash] The object to copy. @return [Hash] A deep copy of the object.

# File lib/ravello-sdk.rb, line 32
def deep_copy(o)
  Marshal.load(Marshal.dump(o))
end
new_name(existing, prefix) click to toggle source

Return a new unique name for an object. @param prefix [String] The name prefix @params existing [Array<String>, Array<Hash>] An list of existing names @return [String] A new name that is different from all existing names

# File lib/ravello-sdk.rb, line 84
def new_name(existing, prefix)
  names = Set.new
  existing.each do |name|
    if name.is_a? Hash
      names << name['name']
    else
      names << name
    end
  end
  for i in 1..names.length+1 do
    name = "#{prefix}#{i}"
    return name if !names.include? name
  end
end
random_luid() click to toggle source

Generate a new random local ID.

# File lib/ravello-sdk.rb, line 37
def random_luid
  @@prng.rand 1<<63
end
update_luids(o) click to toggle source

Recursively replace all attributes called “id” in the supplied hash with a new random ID. This is useful when adding a VM or other object to an application’s design. All objects in a design require a unique ID. The term “luid” means “local id”.

@param o [Hash] The object to return [Hash] A copy of the object with new id attributes.

# File lib/ravello-sdk.rb, line 60
def update_luids(o)
  update_luids!(deep_copy o)
end
update_luids!(o) click to toggle source

Like update_luids, but replaces ID attributes in place.

# File lib/ravello-sdk.rb, line 42
def update_luids!(o)
  if o.is_a? Hash
    o.each_pair do |key,value|
      o[key] = if key == 'id' then random_luid else update_luids! value end
    end
  elsif o.is_a? Array
    o.map! do |x| update_luids! x end
  end
  o
end

Public Instance Methods

application_state(app) click to toggle source

Return the consolidated state of an application. @return [String, Array<String>, nil] A single string if all VMs in the

deployment are in the same state, or a list of strings if there are
multiple states. In case there are no VMs in the deployment, return nil.
# File lib/ravello-sdk.rb, line 68
def application_state(app)
  if !app.is_a? Hash
    raise ArgumentError, "expecting a Hash, got #{app}"
  elsif !app['deployment'].is_a? Hash
    return  nil
  elsif !app['deployment']['vms'].is_a? Array
    return nil
  end
  states = app['deployment']['vms'].map { |vm| vm['state'] } .uniq
  if states.length == 1 then states[0] else states end
end

Private Instance Methods

deep_copy(o) click to toggle source

Return a deep copy of an object. @param o [Hash] The object to copy. @return [Hash] A deep copy of the object.

# File lib/ravello-sdk.rb, line 32
def deep_copy(o)
  Marshal.load(Marshal.dump(o))
end
new_name(existing, prefix) click to toggle source

Return a new unique name for an object. @param prefix [String] The name prefix @params existing [Array<String>, Array<Hash>] An list of existing names @return [String] A new name that is different from all existing names

# File lib/ravello-sdk.rb, line 84
def new_name(existing, prefix)
  names = Set.new
  existing.each do |name|
    if name.is_a? Hash
      names << name['name']
    else
      names << name
    end
  end
  for i in 1..names.length+1 do
    name = "#{prefix}#{i}"
    return name if !names.include? name
  end
end
random_luid() click to toggle source

Generate a new random local ID.

# File lib/ravello-sdk.rb, line 37
def random_luid
  @@prng.rand 1<<63
end
update_luids(o) click to toggle source

Recursively replace all attributes called “id” in the supplied hash with a new random ID. This is useful when adding a VM or other object to an application’s design. All objects in a design require a unique ID. The term “luid” means “local id”.

@param o [Hash] The object to return [Hash] A copy of the object with new id attributes.

# File lib/ravello-sdk.rb, line 60
def update_luids(o)
  update_luids!(deep_copy o)
end
update_luids!(o) click to toggle source

Like update_luids, but replaces ID attributes in place.

# File lib/ravello-sdk.rb, line 42
def update_luids!(o)
  if o.is_a? Hash
    o.each_pair do |key,value|
      o[key] = if key == 'id' then random_luid else update_luids! value end
    end
  elsif o.is_a? Array
    o.map! do |x| update_luids! x end
  end
  o
end