1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

Update mocking support for metadata to match real responses.

This commit is contained in:
Rupak Ganguly 2012-05-10 13:49:50 -04:00
parent 926e9d67e1
commit 290e9c1f9c
6 changed files with 101 additions and 25 deletions

View file

@ -3,6 +3,17 @@ module Fog
class HP
class Real
# Delete metadata item for specific collections
#
# ==== Parameters
# * 'collection_name'<~String> - name of the collection i.e. images, servers for which the metadata is intented.
# * 'parent_id'<~Integer> - id of the collection i.e. image_id or the server_id
# * 'key'<~String> - key for the metadata item
#
# ==== Returns
# * response<~Excon::Response>:
# * body: Empty response body
#
def delete_meta(collection_name, parent_id, key)
request(
:expects => 204,
@ -16,6 +27,22 @@ module Fog
class Mock
def delete_meta(collection_name, parent_id, key)
if collection_name == "images" then
if get_image_details(parent_id)
self.data[:images][parent_id]['metadata'].delete(key)
else
raise Fog::Compute::HP::NotFound
end
end
if collection_name == "servers" then
if get_server_details(parent_id)
self.data[:servers][parent_id]['metadata'].delete(key)
else
raise Fog::Compute::HP::NotFound
end
end
response = Excon::Response.new
response.status = 204
response

View file

@ -6,7 +6,7 @@ module Fog
# Get metadata item for specific collections
#
# ==== Parameters
# * 'collection_name'<~String> - name of the collection i.e. images, servers for which the metadata is intented.
# * 'collection_name'<~String> - name of the collection i.e. images, servers for which the metadata is intended.
# * 'parent_id'<~Integer> - id of the collection i.e. image_id or the server_id
# * 'key'<~String> - key for the metadata item
#
@ -29,16 +29,16 @@ module Fog
def get_meta(collection_name, parent_id, key)
if collection_name == "images" then
if parent = get_image_details(parent_id)
self.data[:images][parent_id]['image']['metadata'][key]
if get_image_details(parent_id)
raise Fog::Compute::HP::NotFound unless midata = self.data[:images][parent_id]['metadata'].fetch(key, nil)
else
raise Fog::Compute::HP::NotFound
end
end
if collection_name == "servers" then
if parent = get_server_details(parent_id)
self.data[:servers][parent_id]['server']['metadata'][key] = value
if get_server_details(parent_id)
raise Fog::Compute::HP::NotFound unless midata = self.data[:servers][parent_id]['metadata'].fetch(key, nil)
else
raise Fog::Compute::HP::NotFound
end
@ -46,7 +46,7 @@ module Fog
response = Excon::Response.new
response.status = 200
response.body = { 'meta' => {} }
response.body = { 'meta' => { key => midata } }
response
end

View file

@ -3,6 +3,17 @@ module Fog
class HP
class Real
# List metadata for specific collections
#
# ==== Parameters
# * 'collection_name'<~String> - name of the collection i.e. images, servers for which the metadata is intended.
# * 'parent_id'<~Integer> - id of the collection i.e. image_id or the server_id
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Hash>:
# * metadata<~Hash>: hash of key/value pair for the metadata items found
#
def list_metadata(collection_name, parent_id)
request(
:expects => [200, 203],
@ -16,9 +27,26 @@ module Fog
class Mock
def list_metadata(collection_name, parent_id)
mdata = {}
if collection_name == "images" then
if get_image_details(parent_id)
mdata = self.data[:images][parent_id]['metadata']
else
raise Fog::Compute::HP::NotFound
end
end
if collection_name == "servers" then
if get_server_details(parent_id)
mdata = self.data[:servers][parent_id]['metadata']
else
raise Fog::Compute::HP::NotFound
end
end
response = Excon::Response.new
response.status = 200
response.body = {}
response.body = {'metadata' => mdata}
response
end

View file

@ -8,11 +8,13 @@ module Fog
# ==== Parameters
# * 'collection_name'<~String> - name of the collection i.e. images, servers for which the metadata is intented.
# * 'parent_id'<~Integer> - id of the collection i.e. image_id or the server_id
#
# * 'metadata'<~Hash> - A hash of key/value pairs containing the metadata
# ==== Returns
# * response<~Excon::Response>:
# * body<~Hash>:
# * metadata<~Hash> - key/value pairs of metadata items
#
def set_metadata(collection_name, parent_id, metadata = {})
request(
:body => MultiJson.encode({ 'metadata' => metadata }),
@ -29,15 +31,19 @@ module Fog
def set_metadata(collection_name, parent_id, metadata = {})
if collection_name == "images" then
if not list_images_detail.body['images'].detect {|_| _['id'] == parent_id}
if get_image_details(parent_id)
self.data[:images][parent_id]['metadata'] = metadata
else
raise Fog::Compute::HP::NotFound
end
end
end
if collection_name == "servers" then
if not list_servers_detail.body['servers'].detect {|_| _['id'] == parent_id}
if get_server_details(parent_id)
self.data[:servers][parent_id]['metadata'] = metadata
else
raise Fog::Compute::HP::NotFound
end
end
end
response = Excon::Response.new

View file

@ -32,19 +32,19 @@ module Fog
def update_meta(collection_name, parent_id, key, value)
if collection_name == "images" then
if parent = get_image_details(parent_id)
self.data[:images][parent_id]['image']['metadata'][key] = value
if get_image_details(parent_id)
self.data[:images][parent_id]['metadata'][key] = value
else
raise Fog::Compute::HP::NotFound
end
end
end
if collection_name == "servers" then
if parent = get_server_details(parent_id)
self.data[:servers][parent_id]['server']['metadata'][key] = value
if get_server_details(parent_id)
self.data[:servers][parent_id]['metadata'][key] = value
else
raise Fog::Compute::HP::NotFound
end
end
end
response = Excon::Response.new

View file

@ -3,6 +3,18 @@ module Fog
class HP
class Real
# Update metadata for specific collections
#
# ==== Parameters
# * 'collection_name'<~String> - name of the collection i.e. images, servers for which the metadata is intented.
# * 'parent_id'<~Integer> - id of the collection i.e. image_id or the server_id
# * 'metadata'<~Hash> - A hash of key/value pairs containing the metadata
# ==== Returns
# * response<~Excon::Response>:
# * body<~Hash>:
# * metadata<~Hash> - all key/value pairs of metadata items merged with existing metadata
#
def update_metadata(collection_name, parent_id, metadata = {})
request(
:body => MultiJson.encode({ 'metadata' => metadata }),
@ -19,20 +31,23 @@ module Fog
def update_metadata(collection_name, parent_id, metadata = {})
if collection_name == "images" then
if not list_images_detail.body['images'].detect {|_| _['id'] == parent_id}
if get_image_details(parent_id)
newmetadata = self.data[:images][parent_id]['metadata'].merge!(metadata)
else
raise Fog::Compute::HP::NotFound
end
end
end
if collection_name == "servers" then
if not list_servers_detail.body['servers'].detect {|_| _['id'] == parent_id}
if get_server_details(parent_id)
newmetadata = self.data[:servers][parent_id]['metadata'].merge!(metadata)
else
raise Fog::Compute::HP::NotFound
end
end
end
#FIXME join w/ existing metadata here
response = Excon::Response.new
response.body = { "metadata" => metadata }
response.body = { "metadata" => newmetadata }
response.status = 200
response