class Haipa::Client::PollingState

Class which represents a state of Haipa long running operation.

Attributes

connection[RW]
error_data[RW]

@return [HaipaOperationError] the Haipa error data.

request[RW]

@return [Net::HTTPRequest] the HTTP request.

resource[RW]

@return the resource

response[RW]

@return [Net::HTTPResponse] the HTTP response.

retry_timeout[RW]

@return [Integer] retry timeout.

status[RW]

@return [String] status of the long running operation.

Public Class Methods

new(haipa_response, retry_timeout) click to toggle source
# File lib/haipa_rest/polling_state.rb, line 32
def initialize(haipa_response, retry_timeout)
  @retry_timeout = retry_timeout
  @request = haipa_response.request
  update_response(haipa_response.response)
  @resource = haipa_response.body

  case @response.status
    when 200
      provisioning_state = get_provisioning_state
      @status = provisioning_state.nil?? (AsyncOperationStatus::SUCCESS_STATUS):provisioning_state
    when 201
      provisioning_state = get_provisioning_state
      @status = provisioning_state.nil?? (AsyncOperationStatus::IN_PROGRESS_STATUS):provisioning_state
    when 202
      @status = AsyncOperationStatus::IN_PROGRESS_STATUS
    when 204
      @status = AsyncOperationStatus::SUCCESS_STATUS
    else
      @status = AsyncOperationStatus::FAILED_STATUS
  end
end

Public Instance Methods

get_delay() click to toggle source

Returns the amount of time in seconds for long running operation polling delay.

@return [Integer] Amount of time in seconds for long running operation polling delay.

# File lib/haipa_rest/polling_state.rb, line 74
def get_delay
  return @retry_timeout unless @retry_timeout.nil?

  if !response.nil? && !response.headers['Retry-After'].nil?
    return response.headers['Retry-After'].to_i
  end

  return AsyncOperationStatus::DEFAULT_DELAY
end
get_operation_error() click to toggle source

Composes and returns cloud error.

@return [HaipaOperationError] the cloud error.

# File lib/haipa_rest/polling_state.rb, line 109
def get_operation_error
  HaipaOperationError.new @request, @response, @error_data, "Long running operation failed with status #{@status}"
end
get_operation_response() click to toggle source

returns the Haipa's response.

@return [Haipa::Client::HaipaOperationResponse] Haipa's response.

# File lib/haipa_rest/polling_state.rb, line 100
def get_operation_response
  haipa_response = HaipaOperationResponse.new(@request, @response, @resource)
  haipa_response
end
get_provisioning_state() click to toggle source

Returns the provisioning status of the resource

@return [String] provisioning status of the resource

# File lib/haipa_rest/polling_state.rb, line 58
def get_provisioning_state
  # On non flattened resource, we should find provisioning_state inside 'properties'
  if (!@resource.nil? && @resource.respond_to?(:properties) && @resource.properties.respond_to?(:provisioning_state) && !@resource.properties.provisioning_state.nil?)
    @resource.properties.provisioning_state
    # On flattened resource, we should find provisioning_state at the top level
  elsif !@resource.nil? && @resource.respond_to?(:provisioning_state) && !@resource.provisioning_state.nil?
    @resource.provisioning_state
  else
    nil
  end
end
get_request(options = {}) click to toggle source
# File lib/haipa_rest/polling_state.rb, line 113
def get_request(options = {})
  link = @Haipa_async_operation_header_link || @location_header_link
  options[:connection] = create_connection(options[:base_uri])
  MsRest::HttpOperationRequest.new(nil, link, :get, options)
end
update_response(response) click to toggle source

Updates the polling state from the fields of given response object. @param response [Net::HTTPResponse] the HTTP response.

# File lib/haipa_rest/polling_state.rb, line 87
def update_response(response)
  @response = response

  unless response.nil?
    @Haipa_async_operation_header_link = response.headers['Haipa-AsyncOperation'] unless response.headers['Haipa-AsyncOperation'].nil?
    @location_header_link = response.headers['Location'] unless response.headers['Location'].nil?
  end
end

Private Instance Methods

create_connection(base_url) click to toggle source
# File lib/haipa_rest/polling_state.rb, line 126
def create_connection(base_url)
  @connection ||= Faraday.new(:url => base_url, :ssl => MsRest.ssl_options) do |faraday|
    [[MsRest::RetryPolicyMiddleware, times: 3, retry: 0.02], [:cookie_jar]].each{ |args| faraday.use(*args) }
    faraday.adapter Faraday.default_adapter
    faraday.headers = request.headers
    logging = ENV['HAIPA_HTTP_LOGGING'] || request.log
    if logging
      faraday.response :logger, nil, { :bodies => logging == 'full' }
    end
  end
end