From 7ba6df8f1494c98849dbdc25bdc34a1f4605ee47 Mon Sep 17 00:00:00 2001 From: Dan Peterson Date: Fri, 4 Nov 2011 15:00:22 -0300 Subject: [PATCH] [dynect|dns] Automatically poll jobs if we get them. Closes #575 --- lib/fog/dynect/dns.rb | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/lib/fog/dynect/dns.rb b/lib/fog/dynect/dns.rb index 80d758e82..14152d37e 100644 --- a/lib/fog/dynect/dns.rb +++ b/lib/fog/dynect/dns.rb @@ -26,6 +26,8 @@ module Fog request :post_zone request :put_zone + class JobIncomplete < Error; end + class Mock def initialize(options={}) @dynect_customer = options[:dynect_customer] @@ -80,18 +82,23 @@ module Fog def request(params) begin + # any request could redirect to a job + params[:expects] = Array(params[:expects]) | [307] + params[:headers] ||= {} params[:headers]['Content-Type'] = 'application/json' params[:headers]['API-Version'] = @version params[:headers]['Auth-Token'] = auth_token unless params[:path] == "Session" - params[:path] = "#{@path}/#{params[:path]}" + params[:path] = "#{@path}/#{params[:path]}" unless params[:path] =~ %r{^#{Regexp.escape(@path)}/} response = @connection.request(params.merge!({:host => @host})) - unless response.body.empty? + if response.status == 307 + response = poll_job(response) + elsif !response.body.empty? response.body = MultiJson.decode(response.body) end - response + response rescue Excon::Errors::HTTPStatusError => error if @auth_token && error.message =~ /login: (Bad or expired credentials|inactivity logout)/ @auth_token = nil @@ -103,6 +110,21 @@ module Fog response end + + def poll_job(response, time_to_wait = 10) + job_location = response.headers['Location'] + + Fog.wait_for(time_to_wait) do + response = request(:expects => 200, :method => :get, :path => job_location) + response.body['status'] != 'incomplete' + end + + if response.body['status'] == 'incomplete' + raise JobIncomplete.new("Job #{response.body['job_id']} is still incomplete") + end + + response + end end end