Class: OpenStack::Compute::Metadata

Inherits:
Object
  • Object
show all
Defined in:
lib/openstack/compute/metadata.rb

Instance Method Summary collapse

Constructor Details

#initialize(compute, parent_url, metadata = nil) ⇒ Metadata

Returns a new instance of Metadata.



6
7
8
9
10
# File 'lib/openstack/compute/metadata.rb', line 6

def initialize(compute, parent_url, =nil)
  @compute = compute
  @base_url = "#{parent_url}/metadata"
  @metadata = 
end

Instance Method Details

#[](key) ⇒ Object



12
13
14
15
# File 'lib/openstack/compute/metadata.rb', line 12

def [](key)
  refresh if @metadata.nil?
  @metadata[key]
end

#[]=(key, value) ⇒ Object



17
18
19
20
# File 'lib/openstack/compute/metadata.rb', line 17

def []=(key, value)
  @metadata = {} if @metadata.nil?
  @metadata[key] = value
end

#clearObject



95
96
97
98
99
100
101
# File 'lib/openstack/compute/metadata.rb', line 95

def clear
  if @metadata.nil?
    @metadata = {}
  else
    @metadata.clear
  end
end

#clear!Object



103
104
105
106
# File 'lib/openstack/compute/metadata.rb', line 103

def clear!
  clear
  save
end

#delete(keys) ⇒ Object



81
82
83
84
85
86
# File 'lib/openstack/compute/metadata.rb', line 81

def delete(keys)
  return if @metadata.nil?
  keys.each { |key|
    @metadata.delete(key)
  }
end

#delete!(keys) ⇒ Object



88
89
90
91
92
93
# 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

#eachObject



39
40
41
42
# File 'lib/openstack/compute/metadata.rb', line 39

def each
  refresh if @metadata.nil?
  @metadata.each
end

#each_pairObject



27
28
29
30
31
32
# 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) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
111
# 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) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# 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

#saveObject



44
45
46
47
48
49
# 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

#sizeObject



34
35
36
37
# File 'lib/openstack/compute/metadata.rb', line 34

def size
  @metadata = {} if @metadata.nil?
  @metadata.size
end

#store(key, value) ⇒ Object



22
23
24
25
# File 'lib/openstack/compute/metadata.rb', line 22

def store(key, value)
  @metadata = {} if @metadata.nil?
  @metadata[key] = value
end

#update(keys = nil) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 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