Class: Haipa::Client::PollingState

Inherits:
Object
  • Object
show all
Defined in:
lib/haipa_rest/polling_state.rb

Overview

Class which represents a state of Haipa long running operation.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(haipa_response, retry_timeout) ⇒ PollingState

Returns a new instance of PollingState.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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

Instance Attribute Details

#error_dataHaipaOperationError

Returns the Haipa error data.

Returns:



21
22
23
# File 'lib/haipa_rest/polling_state.rb', line 21

def error_data
  @error_data
end

Returns the latest value captured from Haipa-AsyncOperation header.

Returns:

  • (String)

    the latest value captured from Haipa-AsyncOperation header.



24
25
26
# File 'lib/haipa_rest/polling_state.rb', line 24

def Haipa_async_operation_header_link
  @Haipa_async_operation_header_link
end

Returns the latest value captured from Location header.

Returns:

  • (String)

    the latest value captured from Location header.



27
28
29
# File 'lib/haipa_rest/polling_state.rb', line 27

def location_header_link
  @location_header_link
end

#requestNet::HTTPRequest

Returns the HTTP request.

Returns:

  • (Net::HTTPRequest)

    the HTTP request.



12
13
14
# File 'lib/haipa_rest/polling_state.rb', line 12

def request
  @request
end

#resourceObject

Returns the resource.

Returns:

  • the resource



15
16
17
# File 'lib/haipa_rest/polling_state.rb', line 15

def resource
  @resource
end

#responseNet::HTTPResponse

Returns the HTTP response.

Returns:

  • (Net::HTTPResponse)

    the HTTP response.



18
19
20
# File 'lib/haipa_rest/polling_state.rb', line 18

def response
  @response
end

#statusString

Returns status of the long running operation.

Returns:

  • (String)

    status of the long running operation.



30
31
32
# File 'lib/haipa_rest/polling_state.rb', line 30

def status
  @status
end

Instance Method Details

#get_delayInteger

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

Returns:

  • (Integer)

    Amount of time in seconds for long running operation polling delay.



74
75
76
77
78
79
80
81
82
# 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_errorHaipaOperationError

Composes and returns cloud error.

Returns:



109
110
111
# 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_responseHaipa::Client::HaipaOperationResponse

returns the Haipa’s response.

Returns:



100
101
102
103
# 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_stateString

Returns the provisioning status of the resource

Returns:

  • (String)

    provisioning status of the resource



58
59
60
61
62
63
64
65
66
67
68
# 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 = {}) ⇒ Object



113
114
115
116
117
# 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) ⇒ Object

Updates the polling state from the fields of given response object.

Parameters:

  • response (Net::HTTPResponse)

    the HTTP response.



87
88
89
90
91
92
93
94
# 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