ms_rest_azure 0.2.2 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d7203d54f4f4ae83bb46c11e9cc10a2add963878
4
- data.tar.gz: 722bf9172b7dc18abb6c7df979abfa07e11fefb2
3
+ metadata.gz: 3bc35b29a1cb68a457f14527a362b628b2974677
4
+ data.tar.gz: 2a05929f18773754540929f509aab0d18d80d650
5
5
  SHA512:
6
- metadata.gz: ed36ae9615edf1964910abffae804573486d033f4e691a1cb5b72201e2aa37e9c243eb6df7e0c1f28c9ffe23991d440a40d890bc67c6e8875b4ce8ce6c35b432
7
- data.tar.gz: 631883016525fbd8de0a4c78f2db3d4849e948fddf2a2258249b79ed833ed03729191e76f502d8708a8801f429dedd1666293050ff4607d9ba8a4ef4b0b17805
6
+ metadata.gz: 57237e0f415c933903b374234a61549443fc74f56c459fa7735686ccbec25ce9cf7e7c9a79c679e27301c9b128e3db1cf597883a827abd277c55bfd871f58418
7
+ data.tar.gz: 4ba53a1a08501303f8b6dfe35c447ee750910690d3b8d9a4ee85a05418eb1346b679b25dbc26f74eddb006087954e0ebd46619d10f373ade3d10bf523a2cd3df
@@ -15,36 +15,35 @@ module MsRestAzure
15
15
  attr_accessor :api_version
16
16
 
17
17
  #
18
- # Retrieves the result of 'PUT' operation. Perfroms polling of required.
18
+ # Retrieves the result of 'POST','DELETE','PUT' or 'PATCH' operation. Performs polling of required.
19
19
  # @param azure_response [MsRestAzure::AzureOperationResponse] response from Azure service.
20
20
  # @param custom_deserialization_block [Proc] custom logic for response deserialization.
21
21
  #
22
22
  # @return [MsRest::HttpOperationResponse] the response.
23
- def get_put_operation_result(azure_response, custom_deserialization_block)
24
- fail MsRest::ValidationError, 'Azure response cannot be nil' if azure_response.nil?
23
+ def get_long_running_operation_result(azure_response, custom_deserialization_block)
24
+ check_for_status_code_failure(azure_response)
25
25
 
26
- status_code = azure_response.response.status
27
-
28
- if (status_code != 200 && status_code != 201 && status_code != 202)
29
- fail AzureOperationError, "Unexpected polling status code from long running operation #{status_code}"
30
- end
26
+ http_method = azure_response.request.method
31
27
 
32
28
  polling_state = PollingState.new(azure_response, @long_running_operation_retry_timeout)
33
29
  request = azure_response.request
34
30
 
35
- if (!AsyncOperationStatus.is_terminal_status(polling_state.status))
31
+ if !AsyncOperationStatus.is_terminal_status(polling_state.status)
36
32
  task = Concurrent::TimerTask.new do
37
33
  begin
38
34
  if !polling_state.azure_async_operation_header_link.nil?
39
35
  update_state_from_azure_async_operation_header(polling_state.get_request(headers: request.headers, base_uri: request.base_uri), polling_state)
40
36
  elsif !polling_state.location_header_link.nil?
41
- update_state_from_location_header_on_put(polling_state.get_request(headers: request.headers, base_uri: request.base_uri), polling_state, custom_deserialization_block)
42
- else
43
- get_request = MsRest::HttpOperationRequest.new(request.base_uri, request.build_path.to_s, 'get', query_params: request.query_params)
37
+ update_state_from_location_header(polling_state.get_request(headers: request.headers, base_uri: request.base_uri), polling_state, custom_deserialization_block)
38
+ elsif http_method === :put
39
+ get_request = MsRest::HttpOperationRequest.new(request.base_uri, request.build_path.to_s, :get, query_params: request.query_params)
44
40
  update_state_from_get_resource_operation(get_request, polling_state, custom_deserialization_block)
41
+ else
42
+ task.shutdown
43
+ fail AzureOperationError, 'Location header is missing from long running operation'
45
44
  end
46
45
 
47
- if (AsyncOperationStatus.is_terminal_status(polling_state.status))
46
+ if AsyncOperationStatus.is_terminal_status(polling_state.status)
48
47
  task.shutdown
49
48
  end
50
49
  rescue Exception => e
@@ -64,74 +63,52 @@ module MsRestAzure
64
63
  fail polling_error if polling_error.is_a?(Exception)
65
64
  end
66
65
 
67
- if (AsyncOperationStatus.is_successful_status(polling_state.status) && polling_state.resource.nil?)
68
- get_request = MsRest::HttpOperationRequest.new(request.base_uri, request.build_path.to_s, 'get', query_params: request.query_params)
66
+ if (http_method === :put || http_method === :patch) && AsyncOperationStatus.is_successful_status(polling_state.status) && polling_state.resource.nil?
67
+ get_request = MsRest::HttpOperationRequest.new(request.base_uri, request.build_path.to_s, :get, query_params: request.query_params)
69
68
  update_state_from_get_resource_operation(get_request, polling_state, custom_deserialization_block)
70
69
  end
71
70
 
72
- if (AsyncOperationStatus.is_failed_status(polling_state.status))
71
+ if AsyncOperationStatus.is_failed_status(polling_state.status)
73
72
  fail polling_state.get_operation_error
74
73
  end
75
74
 
76
- return polling_state.get_operation_response
75
+ polling_state.get_operation_response
76
+ end
77
+
78
+ # @TODO Update name of the method to get_put_or_patch_operation_result when introducing breaking change / updating major version of gem
79
+ # Retrieves the result of 'PUT' or 'PATCH' operation. Performs polling of required.
80
+ # @param azure_response [MsRestAzure::AzureOperationResponse] response from Azure service.
81
+ # @param custom_deserialization_block [Proc] custom logic for response deserialization.
82
+ #
83
+ # @return [MsRest::HttpOperationResponse] the response.
84
+ def get_put_operation_result(azure_response, custom_deserialization_block)
85
+ get_long_running_operation_result(azure_response, custom_deserialization_block)
77
86
  end
78
87
 
79
88
  #
80
- # Retrieves the result of 'POST' or 'DELETE' operations. Perfroms polling of required.
89
+ # Retrieves the result of 'POST' or 'DELETE' operations. Performs polling of required.
81
90
  # @param azure_response [MsRestAzure::AzureOperationResponse] response from Azure service.
82
91
  # @param custom_deserialization_block [Proc] custom logic for response deserialization.
83
92
  #
84
93
  # @return [MsRest::HttpOperationResponse] the response.
85
94
  def get_post_or_delete_operation_result(azure_response, custom_deserialization_block)
95
+ get_long_running_operation_result(azure_response, custom_deserialization_block)
96
+ end
97
+
98
+ #
99
+ # Verifies for unexpected polling status code
100
+ # @param azure_response [MsRestAzure::AzureOperationResponse] response from Azure service.
101
+ def check_for_status_code_failure(azure_response)
86
102
  fail MsRest::ValidationError, 'Azure response cannot be nil' if azure_response.nil?
87
103
  fail MsRest::ValidationError, 'Azure response cannot have empty response object' if azure_response.response.nil?
104
+ fail MsRest::ValidationError, 'Azure response cannot have empty request object' if azure_response.request.nil?
88
105
 
89
106
  status_code = azure_response.response.status
107
+ http_method = azure_response.request.method
90
108
 
91
- if (status_code != 200 && status_code != 202 && status_code != 204)
92
- fail AzureOperationError, "Unexpected polling status code from long running operation #{status_code}"
93
- end
94
-
95
- polling_state = PollingState.new(azure_response, @long_running_operation_retry_timeout)
96
- request = azure_response.request
97
-
98
- if (!AsyncOperationStatus.is_terminal_status(polling_state.status))
99
- task = Concurrent::TimerTask.new do
100
- begin
101
- if !polling_state.azure_async_operation_header_link.nil?
102
- update_state_from_azure_async_operation_header(polling_state.get_request(headers: request.headers, base_uri: request.base_uri), polling_state)
103
- elsif !polling_state.location_header_link.nil?
104
- update_state_from_location_header_on_post_or_delete(polling_state.get_request(headers: request.headers, base_uri: request.base_uri), polling_state, custom_deserialization_block)
105
- else
106
- task.shutdown
107
- fail AzureOperationError, 'Location header is missing from long running operation'
108
- end
109
-
110
- if (AsyncOperationStatus.is_terminal_status(polling_state.status))
111
- task.shutdown
112
- end
113
- rescue Exception => e
114
- task.shutdown
115
- e
116
- end
117
- end
118
-
119
- polling_delay = polling_state.get_delay
120
- polling_delay = 0.1 if polling_delay.nil? || polling_delay == 0
121
-
122
- task.execution_interval = polling_delay
123
- task.execute
124
- task.wait_for_termination
125
-
126
- polling_error = task.value
127
- fail polling_error if polling_error.is_a?(Exception)
128
- end
129
-
130
- if (AsyncOperationStatus.is_failed_status(polling_state.status))
131
- fail polling_state.get_operation_error
132
- end
133
-
134
- return polling_state.get_operation_response
109
+ fail AzureOperationError, "Unexpected polling status code from long running operation #{status_code}" unless status_code === 200 || status_code === 202 ||
110
+ (status_code === 201 && http_method === :put) ||
111
+ (status_code === 204 && (http_method === :delete || http_method === :post))
135
112
  end
136
113
 
137
114
  #
@@ -145,7 +122,7 @@ module MsRestAzure
145
122
 
146
123
  fail AzureOperationError, 'The response from long running operation does not contain a body' if result.response.body.nil? || result.response.body.empty?
147
124
 
148
- if (result.body.respond_to?(:properties) && result.body.properties.respond_to?(:provisioning_state) && !result.body.properties.provisioning_state.nil?)
125
+ if result.body.respond_to?(:properties) && result.body.properties.respond_to?(:provisioning_state) && !result.body.properties.provisioning_state.nil?
149
126
  polling_state.status = result.body.properties.provisioning_state
150
127
  else
151
128
  polling_state.status = AsyncOperationStatus::SUCCESS_STATUS
@@ -162,30 +139,23 @@ module MsRestAzure
162
139
  end
163
140
 
164
141
  #
165
- # Updates polling state based on location header for PUT HTTP requests.
142
+ # Updates polling state based on location header for HTTP requests.
166
143
  # @param request [MsRest::HttpOperationRequest] The url retrieve data from.
167
144
  # @param polling_state [MsRestAzure::PollingState] polling state to update.
168
145
  # @param custom_deserialization_block [Proc] custom deserialization method for parsing response.
169
- def update_state_from_location_header_on_put(request, polling_state, custom_deserialization_block)
146
+ def update_state_from_location_header(request, polling_state, custom_deserialization_block)
170
147
  result = get_async_with_custom_deserialization(request, custom_deserialization_block)
171
148
 
172
149
  polling_state.update_response(result.response)
173
- polling_state.request = result.response
174
-
150
+ polling_state.request = result.request
175
151
  status_code = result.response.status
152
+ http_method = request.method
176
153
 
177
- if (status_code == 202)
154
+ if status_code === 202
178
155
  polling_state.status = AsyncOperationStatus::IN_PROGRESS_STATUS
179
- elsif (status_code == 200 || status_code == 201)
180
- fail AzureOperationError, 'The response from long running operation does not contain a body' if result.body.nil?
181
-
182
- # In 202 pattern on PUT ProvisioningState may not be present in
183
- # the response. In that case the assumption is the status is Succeeded.
184
- if (result.body.respond_to?(:properties) && result.body.properties.respond_to?(:provisioning_state) && !result.body.properties.provisioning_state.nil?)
185
- polling_state.status = result.body.properties.provisioning_state
186
- else
187
- polling_state.status = AsyncOperationStatus::SUCCESS_STATUS
188
- end
156
+ elsif status_code === 200 || (status_code === 201 && http_method === :put) ||
157
+ (status_code === 204 && (http_method === :delete || http_method === :post || http_method === :get))
158
+ polling_state.status = AsyncOperationStatus::SUCCESS_STATUS
189
159
 
190
160
  error_data = CloudErrorData.new
191
161
  error_data.code = polling_state.status
@@ -193,6 +163,8 @@ module MsRestAzure
193
163
 
194
164
  polling_state.error_data = error_data
195
165
  polling_state.resource = result.body
166
+ else
167
+ fail AzureOperationError, "The response from long running operation does not have a valid status code. Method: #{http_method}, Status Code: #{status_code}"
196
168
  end
197
169
  end
198
170
 
@@ -212,26 +184,7 @@ module MsRestAzure
212
184
  end
213
185
 
214
186
  #
215
- # Updates polling state based on location header for POST and DELETE HTTP requests.
216
- # @param polling_state [MsRest::HttpOperationRequest] [description]
217
- # @param custom_deserialization_block [Proc] custom deserialization method for parsing response.
218
- def update_state_from_location_header_on_post_or_delete(request, polling_state, custom_deserialization_block)
219
- result = get_async_with_custom_deserialization(request, custom_deserialization_block)
220
-
221
- polling_state.update_response(result.response)
222
- polling_state.request = result.request
223
- status_code = result.response.status
224
-
225
- if (status_code == 202)
226
- polling_state.status = AsyncOperationStatus::IN_PROGRESS_STATUS
227
- elsif (status_code == 200 || status_code == 201 || status_code == 204)
228
- polling_state.status = AsyncOperationStatus::SUCCESS_STATUS
229
- polling_state.resource = result.body
230
- end
231
- end
232
-
233
- #
234
- # Retrives data by given URL.
187
+ # Retrieves data by given URL.
235
188
  # @param request [MsRest::HttpOperationRequest] the URL.
236
189
  # @param custom_deserialization_block [Proc] function to perform deserialization of the HTTP response.
237
190
  #
@@ -239,7 +192,7 @@ module MsRestAzure
239
192
  def get_async_with_custom_deserialization(request, custom_deserialization_block)
240
193
  result = get_async_common(request)
241
194
 
242
- if (!result.body.nil? && !custom_deserialization_block.nil?)
195
+ if !result.body.nil? && !custom_deserialization_block.nil?
243
196
  begin
244
197
  result.body = custom_deserialization_block.call(result.body)
245
198
  rescue Exception => e
@@ -251,7 +204,7 @@ module MsRestAzure
251
204
  end
252
205
 
253
206
  #
254
- # Retrives data by given URL.
207
+ # Retrieves data by given URL.
255
208
  # @param request [MsRest::HttpOperationRequest] the URL.
256
209
  #
257
210
  # @return [MsRest::HttpOperationResponse] the response.
@@ -263,13 +216,13 @@ module MsRestAzure
263
216
  end
264
217
 
265
218
  #
266
- # Retrives data by given URL.
219
+ # Retrieves data by given URL.
267
220
  # @param request [MsRest::HttpOperationRequest] the URL.
268
221
  #
269
222
  # @return [MsRest::HttpOperationResponse] the response.
270
223
  def get_async_common(request)
271
224
  fail ValidationError, 'Request cannot be nil' if request.nil?
272
-
225
+
273
226
  request.middlewares = [[MsRest::RetryPolicyMiddleware, times: 3, retry: 0.02], [:cookie_jar]]
274
227
  request.headers.merge!({'x-ms-client-request-id' => SecureRandom.uuid, 'Content-Type' => 'application/json'})
275
228
 
@@ -277,10 +230,10 @@ module MsRestAzure
277
230
  http_response = request.run_promise do |req|
278
231
  @credentials.sign_request(req) unless @credentials.nil?
279
232
  end.execute.value!
280
-
233
+
281
234
  status_code = http_response.status
282
235
 
283
- if (status_code != 200 && status_code != 201 && status_code != 202 && status_code != 204)
236
+ if status_code != 200 && status_code != 201 && status_code != 202 && status_code != 204
284
237
  json_error_data = JSON.load(http_response.body)
285
238
  error_data = CloudErrorData.deserialize_object(json_error_data)
286
239
 
@@ -94,7 +94,7 @@ module MsRestAzure
94
94
 
95
95
  def get_request(options = {})
96
96
  link = @azure_async_operation_header_link || @location_header_link
97
- MsRest::HttpOperationRequest.new(nil, link, 'get', options)
97
+ MsRest::HttpOperationRequest.new(nil, link, :get, options)
98
98
  end
99
99
 
100
100
  private
@@ -3,5 +3,5 @@
3
3
  # Licensed under the MIT License. See License.txt in the project root for license information.
4
4
 
5
5
  module MsRestAzure
6
- VERSION = '0.2.2'
6
+ VERSION = '0.2.3'
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ms_rest_azure
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Microsoft Corporation
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-07 00:00:00.000000000 Z
11
+ date: 2016-05-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler