From 290e9c1f9ce66c47cbfd3489cd0e69ed6a78e587 Mon Sep 17 00:00:00 2001 From: Rupak Ganguly Date: Thu, 10 May 2012 13:49:50 -0400 Subject: [PATCH] Update mocking support for metadata to match real responses. --- lib/fog/hp/requests/compute/delete_meta.rb | 27 +++++++++++++++++ lib/fog/hp/requests/compute/get_meta.rb | 12 ++++---- lib/fog/hp/requests/compute/list_metadata.rb | 30 ++++++++++++++++++- lib/fog/hp/requests/compute/set_metadata.rb | 18 +++++++---- lib/fog/hp/requests/compute/update_meta.rb | 12 ++++---- .../hp/requests/compute/update_metadata.rb | 27 +++++++++++++---- 6 files changed, 101 insertions(+), 25 deletions(-) diff --git a/lib/fog/hp/requests/compute/delete_meta.rb b/lib/fog/hp/requests/compute/delete_meta.rb index 7bceff687..7c71f249b 100644 --- a/lib/fog/hp/requests/compute/delete_meta.rb +++ b/lib/fog/hp/requests/compute/delete_meta.rb @@ -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 diff --git a/lib/fog/hp/requests/compute/get_meta.rb b/lib/fog/hp/requests/compute/get_meta.rb index a96acf345..17ea3234d 100644 --- a/lib/fog/hp/requests/compute/get_meta.rb +++ b/lib/fog/hp/requests/compute/get_meta.rb @@ -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 diff --git a/lib/fog/hp/requests/compute/list_metadata.rb b/lib/fog/hp/requests/compute/list_metadata.rb index 3b8edfd41..cd27836ea 100644 --- a/lib/fog/hp/requests/compute/list_metadata.rb +++ b/lib/fog/hp/requests/compute/list_metadata.rb @@ -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 diff --git a/lib/fog/hp/requests/compute/set_metadata.rb b/lib/fog/hp/requests/compute/set_metadata.rb index 3a5da1c8d..4eb691682 100644 --- a/lib/fog/hp/requests/compute/set_metadata.rb +++ b/lib/fog/hp/requests/compute/set_metadata.rb @@ -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 diff --git a/lib/fog/hp/requests/compute/update_meta.rb b/lib/fog/hp/requests/compute/update_meta.rb index a3748e904..f72996d4a 100644 --- a/lib/fog/hp/requests/compute/update_meta.rb +++ b/lib/fog/hp/requests/compute/update_meta.rb @@ -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 diff --git a/lib/fog/hp/requests/compute/update_metadata.rb b/lib/fog/hp/requests/compute/update_metadata.rb index 69b68f114..504722dc5 100644 --- a/lib/fog/hp/requests/compute/update_metadata.rb +++ b/lib/fog/hp/requests/compute/update_metadata.rb @@ -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