class OpenStack::Compute::Metadata

Public Class Methods

new(compute, parent_url, metadata=nil) click to toggle source
# File lib/openstack/compute/metadata.rb, line 6
def initialize(compute, parent_url, metadata=nil)
  @compute = compute
  @base_url = "#{parent_url}/metadata"
  @metadata = metadata
end

Public Instance Methods

[](key) click to toggle source
# File lib/openstack/compute/metadata.rb, line 12
def [](key)
  refresh if @metadata.nil?
  @metadata[key]
end
[]=(key, value) click to toggle source
# File lib/openstack/compute/metadata.rb, line 17
def []=(key, value)
  @metadata = {} if @metadata.nil?
  @metadata[key] = value
end
clear() click to toggle source
# File lib/openstack/compute/metadata.rb, line 95
def clear
  if @metadata.nil?
    @metadata = {}
  else
    @metadata.clear
  end
end
clear!() click to toggle source
# File lib/openstack/compute/metadata.rb, line 103
def clear!
  clear
  save
end
delete(keys) click to toggle source
# File lib/openstack/compute/metadata.rb, line 81
def delete(keys)
  return if @metadata.nil?
  keys.each { |key|
    @metadata.delete(key)
  }
end
delete!(keys) click to toggle source
# File lib/openstack/compute/metadata.rb, line 88
def delete!(keys)
  keys.each { |key|
    @compute.connection.req('DELETE', "#{@base_url}/#{key}")
    @metadata.delete(key) if not @metadata.nil?
  }
end
each() click to toggle source
# File lib/openstack/compute/metadata.rb, line 39
def each
  refresh if @metadata.nil?
  @metadata.each
end
each_pair() { |k, v| ... } click to toggle source
# File lib/openstack/compute/metadata.rb, line 27
def each_pair
  @metadata = {} if @metadata.nil?
  @metadata.each_pair do |k,v|
      yield k, v
  end
end
has_key?(key) click to toggle source
# File lib/openstack/compute/metadata.rb, line 108
def has_key?(key)
  return False if @metadata.nil?
  return @metadata.has_key?(key)
end
refresh(keys=nil) click to toggle source
# File lib/openstack/compute/metadata.rb, line 66
def refresh(keys=nil)
  if keys.nil?
    response = @compute.connection.req('GET', @base_url)
    @metadata = JSON.parse(response.body)['metadata']
  else
    @metadata = {} if @metadata == nil
    keys.each { |key|
      response = @compute.connection.req('GET', "#{@base_url}/#{key}")
      next if response.code == "404"
      meta = JSON.parse(response.body)['meta']
      meta.each { |k, v| @metadata[k] = v }
    }
  end
end
save() click to toggle source
# File lib/openstack/compute/metadata.rb, line 44
def save
  return if @metadata.nil?
  json = JSON.generate(:metadata => @metadata)
  response = @compute.connection.req('PUT', @base_url, :data => json)
  @metadata = JSON.parse(response.body)['metadata']
end
size() click to toggle source
# File lib/openstack/compute/metadata.rb, line 34
def size
  @metadata = {} if @metadata.nil?
  @metadata.size
end
store(key, value) click to toggle source
# File lib/openstack/compute/metadata.rb, line 22
def store(key, value)
  @metadata = {} if @metadata.nil?
  @metadata[key] = value
end
update(keys=nil) click to toggle source
# File lib/openstack/compute/metadata.rb, line 51
def update(keys=nil)
  return if @metadata.nil?
  if keys.nil?
    json = JSON.generate(:metadata => @metadata)
    response = @compute.connection.req('POST', @base_url, :data => json)
    @metadata = JSON.parse(response.body)['metadata']
  else
    keys.each { |key|
      next if not @metadata.has_key?(key)
      json = JSON.generate(:meta => { key => @metadata[key] })
      @compute.connection.req('PUT', "#{@base_url}/#{key}", :data => json)
    }
  end
end