From f6280910f8401580afc8601dca764ea9e23ef8f3 Mon Sep 17 00:00:00 2001 From: Paul Thornthwaite Date: Fri, 14 Jun 2013 11:02:30 +0100 Subject: [PATCH] [core] Move XML/JSON code up out of core Rather than embedding XML and JSON support within fog-core, this is the start of extracting them to exist alongside core. That will move the responsibility to each provider to declare if they want XML or JSON services. Goal being as we move towards a modular fog this allows providers to require core and either JSON or XML gems and not pick up dependencies only required by the other. --- lib/fog/aws/credential_fetcher.rb | 12 ++++++------ lib/fog/core.rb | 7 +++++-- lib/fog/core/parser.rb | 2 +- lib/fog/{core => }/json.rb | 15 +++++++++++++-- lib/fog/xml.rb | 19 +++++++++++++++++++ 5 files changed, 44 insertions(+), 11 deletions(-) rename lib/fog/{core => }/json.rb (59%) create mode 100644 lib/fog/xml.rb diff --git a/lib/fog/aws/credential_fetcher.rb b/lib/fog/aws/credential_fetcher.rb index fb13891db..1fb6dda86 100644 --- a/lib/fog/aws/credential_fetcher.rb +++ b/lib/fog/aws/credential_fetcher.rb @@ -1,4 +1,5 @@ -require 'fog/core/json' +require "fog/json" + module Fog module AWS module CredentialFetcher @@ -14,7 +15,7 @@ module Fog session = Fog::JSON.decode(role_data) credentials = {} - credentials[:aws_access_key_id] = session['AccessKeyId'] + credentials[:aws_access_key_id] = session['AccessKeyId'] credentials[:aws_secret_access_key] = session['SecretAccessKey'] credentials[:aws_session_token] = session['Token'] credentials[:aws_credentials_expire_at] = Time.xmlschema session['Expiration'] @@ -31,7 +32,7 @@ module Fog end module ConnectionMethods - + def refresh_credentials_if_expired refresh_credentials if credentials_expired? end @@ -39,8 +40,8 @@ module Fog private def credentials_expired? - @use_iam_profile && - (!@aws_credentials_expire_at || + @use_iam_profile && + (!@aws_credentials_expire_at || (@aws_credentials_expire_at && Fog::Time.now > @aws_credentials_expire_at - 15)) #new credentials become available from around 5 minutes before expiration time end @@ -61,4 +62,3 @@ module Fog end end end - diff --git a/lib/fog/core.rb b/lib/fog/core.rb index b5a3e1b1b..965311416 100644 --- a/lib/fog/core.rb +++ b/lib/fog/core.rb @@ -19,11 +19,9 @@ require 'fog/core/current_machine' require 'fog/core/deprecation' require 'fog/core/errors' require 'fog/core/hmac' -require 'fog/core/json' require 'fog/core/logger' require 'fog/core/model' require 'fog/core/mock' -require 'fog/core/parser' # FIXME: would be better to only load when nokogiri is required require 'fog/core/provider' require 'fog/core/service' require 'fog/core/ssh' @@ -32,6 +30,11 @@ require 'fog/core/time' require 'fog/core/timeout' require 'fog/core/wait_for' +# data exchange specific (to be extracted and used on a per provider basis) +require 'fog/xml' +require 'fog/json' + + # service wrappers require 'fog/compute' require 'fog/identity' diff --git a/lib/fog/core/parser.rb b/lib/fog/core/parser.rb index db3c77074..e3fa57dfe 100644 --- a/lib/fog/core/parser.rb +++ b/lib/fog/core/parser.rb @@ -1,4 +1,4 @@ -require 'nokogiri' +require "nokogiri" module Fog module Parsers diff --git a/lib/fog/core/json.rb b/lib/fog/json.rb similarity index 59% rename from lib/fog/core/json.rb rename to lib/fog/json.rb index 7048854d8..6c25588fd 100644 --- a/lib/fog/core/json.rb +++ b/lib/fog/json.rb @@ -1,6 +1,18 @@ -require 'multi_json' +require "multi_json" module Fog + + # @note Extracting JSON components out of core is a work in progress. + # + # The {JSON} module includes functionality that is common between APIs using + # JSON to send and receive data. + # + # The intent is to provide common code for provider APIs using JSON but not + # require it for those using XML. + # + # @todo Add +require "fog/json" and/or +include Fog::JSON+ to providers using + # its services + # module JSON def self.sanitize(data) @@ -28,6 +40,5 @@ module Fog def self.decode(obj) MultiJson.decode(obj) end - end end diff --git a/lib/fog/xml.rb b/lib/fog/xml.rb new file mode 100644 index 000000000..651cf2bb0 --- /dev/null +++ b/lib/fog/xml.rb @@ -0,0 +1,19 @@ +require "nokogiri" +require "fog/core/parser" + +module Fog + + # @note Extracting XML components out of core is a work in progress. + # + # The {XML} module includes functionality that is common between APIs using + # XML to send and receive data. + # + # The intent is to provide common code for provider APIs using XML but not + # require it for those using JSON. + # + # @todo Add +require "fog/xml"+ and/or +include Fog::XML+ to providers using + # its services + # + module XML + end +end