ms_rest_azure 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cc34817f4903d18cc696dfd20e3d19045f9e592b
4
+ data.tar.gz: ee3c46de975b8a35bfc125cdfd25da680f5b14f0
5
+ SHA512:
6
+ metadata.gz: c246b542c2e5bab01d8b30c571d9f302ac64f1428dbf1d108467eb776a27917976a2c9c2456b109dea600eff3ccdca6611294d623b4c93b8bb27a93a379c0732
7
+ data.tar.gz: de19f73216952f9487dc3e2762a1cb4406aa5cb54ba70b3cd29a45a0c53d599c180e95a8634b733a637b48bcd95803ff9c896c8f9584cead30a813b7361732e4
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.5
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ # encoding: utf-8
2
+ # Copyright (c) Microsoft Corporation. All rights reserved.
3
+ # Licensed under the MIT License. See License.txt in the project root for license information.
4
+
5
+ source 'https://rubygems.org'
6
+
7
+ # Specify your gem's dependencies in ms_rest_azure.gemspec
8
+ gemspec
9
+
10
+ group :development do
11
+ gem 'ms_rest', path: '../ms-rest'
12
+ end
13
+
14
+ group :test do
15
+ gem 'rspec'
16
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Microsoft Corporation
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # Intro
2
+
3
+ MsRestAzure is a library which supports the Azure clients (SDKs) generated with Autorest tool. It contains core logic and helper classes for error handling and authentication. Also it includes azure specific logic like long polling functionality and Azure application authentication. Usually it is not supposed to be used as a standalone gem but only as a dependency for generated client gems.
4
+
5
+ # Supported Ruby Versions
6
+
7
+ * Ruby 1.9.3
8
+ * Ruby 2.0
9
+ * Ruby 2.1
10
+ * Ruby 2.2
11
+
12
+ Note: x64 Ruby for Windows is known to have some compatibility issues.
13
+
14
+ # Installation
15
+
16
+ install the appropriate gem:
17
+
18
+ ```
19
+ gem install ms_rest_azure
20
+ ```
21
+
22
+ and reference it in your code:
23
+
24
+ ```Ruby
25
+ require 'ms_rest_azure'
26
+ ```
27
+
28
+ # Running tests
29
+
30
+ MsRestAzure has only unit tests which doesn't require any preparation, just run 'rspec' command from the gem directory.
31
+
32
+ # Contribution
33
+
34
+ To start working on the gem the only additional dev dependecy is required - rspec. After you've added a new feature and all specs pass - you're good to go with PR. But before starting any bug/feature - please make sure you've thoroughly discussed it with repository maintainers. This gem already powers a few SDKs and backward compatibility should taken in account.
35
+
36
+ # Adding gem to you generated SDK
37
+
38
+ Reference it in the gemfile and also add this line to your client's gemspec file:
39
+
40
+ ```ruby
41
+ spec.add_runtime_dependency 'ms_rest_azure', '~> 0.1.0'
42
+ ```
43
+
44
+ Don't forget to correct the version.
45
+
46
+ # Provide feedback
47
+
48
+ Send email to the azsdkteam@microsoft.com or file new issue in this repository.
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ # encoding: utf-8
2
+ # Copyright (c) Microsoft Corporation. All rights reserved.
3
+ # Licensed under the MIT License. See License.txt in the project root for license information.
4
+
5
+ require "bundler/gem_tasks"
@@ -0,0 +1,21 @@
1
+ # encoding: utf-8
2
+ # Copyright (c) Microsoft Corporation. All rights reserved.
3
+ # Licensed under the MIT License. See License.txt in the project root for license information.
4
+
5
+ require 'ms_rest'
6
+
7
+ require 'ms_rest_azure/version'
8
+
9
+ require 'ms_rest_azure/credentials/application_token_provider.rb'
10
+
11
+ require 'ms_rest_azure/resource.rb'
12
+ require 'ms_rest_azure/sub_resource.rb'
13
+ require 'ms_rest_azure/cloud_error_data.rb'
14
+ require 'ms_rest_azure/azure_operation_error.rb'
15
+ require 'ms_rest_azure/azure_operation_response.rb'
16
+ require 'ms_rest_azure/async_operation_status.rb'
17
+ require 'ms_rest_azure/polling_state.rb'
18
+ require 'ms_rest_azure/active_directory_service_settings.rb'
19
+ require 'ms_rest_azure/azure_service_client.rb'
20
+
21
+ module MsRestAzure; end
@@ -0,0 +1,39 @@
1
+ # encoding: utf-8
2
+ # Copyright (c) Microsoft Corporation. All rights reserved.
3
+ # Licensed under the MIT License. See License.txt in the project root for license information.
4
+
5
+ module MsRestAzure
6
+ #
7
+ # Class which represents an settings for Azure AD authentication.
8
+ #
9
+ class ActiveDirectoryServiceSettings
10
+
11
+ # @return [String] auth token.
12
+ attr_accessor :authentication_endpoint
13
+
14
+ # @return [String] auth token.
15
+ attr_accessor :token_audience
16
+
17
+ #
18
+ # Returns a set of properties required to login into regular Azure.
19
+ #
20
+ # @return [ActiveDirectoryServiceSettings] settings required for authentication.
21
+ def self.get_azure_settings
22
+ settings = ActiveDirectoryServiceSettings.new
23
+ settings.authentication_endpoint = 'https://login.windows.net/'
24
+ settings.token_audience = 'https://management.core.windows.net/'
25
+ settings
26
+ end
27
+
28
+ #
29
+ # Returns a set of properties required to login into Azure China.
30
+ #
31
+ # @return [ActiveDirectoryServiceSettings] settings required for authentication.
32
+ def self.get_azure_china_settings
33
+ settings = ActiveDirectoryServiceSettings.new
34
+ settings.authentication_endpoint = 'https://login.chinacloudapi.cn/'
35
+ settings.token_audience = 'https://management.core.chinacloudapi.cn/'
36
+ settings
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,77 @@
1
+ # encoding: utf-8
2
+ # Copyright (c) Microsoft Corporation. All rights reserved.
3
+ # Licensed under the MIT License. See License.txt in the project root for license information.
4
+
5
+ module MsRestAzure
6
+ #
7
+ # Defines values for AsyncOperationStatus enum.
8
+ #
9
+ class AsyncOperationStatus
10
+ IN_PROGRESS_STATUS = "InProgress"
11
+ SUCCESS_STATUS = "Succeeded"
12
+ FAILED_STATUS = "Failed"
13
+ CANCELED_STATUS = "Canceled"
14
+
15
+ ALL_STATUSES = [FAILED_STATUS, CANCELED_STATUS, SUCCESS_STATUS, IN_PROGRESS_STATUS]
16
+ FAILED_STATUSES = [FAILED_STATUS, CANCELED_STATUS]
17
+ TERMINAL_STATUSES = [FAILED_STATUS, CANCELED_STATUS, SUCCESS_STATUS]
18
+
19
+ DEFAULT_DELAY = 30
20
+
21
+ # @return [Integer] delay in seconds which should be used for polling for result of async operation.
22
+ attr_accessor :retry_after
23
+
24
+ # @return [MsRestAzure::CloudErrorData] error information about async operation.
25
+ attr_accessor :error
26
+
27
+ # @return [Stirng] status of polling.
28
+ attr_accessor :status
29
+
30
+ #
31
+ # Checks if given status is terminal one.
32
+ # @param status [String] status to verify
33
+ #
34
+ # @return [Boolean] True if given status is terminal one, false otherwise.
35
+ def self.is_terminal_status(status)
36
+ TERMINAL_STATUSES.any? { |st| st == status }
37
+ end
38
+
39
+ #
40
+ # Checks if given status is failed one.
41
+ # @param status [String] status to verify
42
+ #
43
+ # @return [Boolean] True if given status is failed one, false otherwise.
44
+ def self.is_failed_status(status)
45
+ FAILED_STATUSES.any? { |st| st == status }
46
+ end
47
+
48
+ #
49
+ # Checks if given status is successful one.
50
+ # @param status [String] status to verify
51
+ #
52
+ # @return [Boolean] True if given status is successful one, false otherwise.
53
+ def self.is_successful_status(status)
54
+ return status == SUCCESS_STATUS
55
+ end
56
+
57
+ #
58
+ # Deserializes given hash into AsyncOperationStatus object.
59
+ # @param object [Hash] object to deserialize.
60
+ #
61
+ # @return [AsyncOperationStatus] deserialized object.
62
+ def self.deserialize_object(object)
63
+ return if object.nil?
64
+ output_object = AsyncOperationStatus.new
65
+
66
+ fail AzureOperationError, 'Invalid status was recieved during polling' unless ALL_STATUSES.include?(object['status'])
67
+ output_object.status = object['status']
68
+
69
+ output_object.error = CloudErrorData.deserialize_object(object['error'])
70
+
71
+ output_object.retry_after = Integer(object['retryAfter']) unless object['retryAfter'].nil?
72
+
73
+ output_object
74
+ end
75
+ end
76
+
77
+ end
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+ # Copyright (c) Microsoft Corporation. All rights reserved.
3
+ # Licensed under the MIT License. See License.txt in the project root for license information.
4
+
5
+ module MsRestAzure
6
+ #
7
+ # Class which represents an Azure error.
8
+ #
9
+ class AzureOperationError < MsRest::HttpOperationError
10
+ end
11
+ end
@@ -0,0 +1,14 @@
1
+ # encoding: utf-8
2
+ # Copyright (c) Microsoft Corporation. All rights reserved.
3
+ # Licensed under the MIT License. See License.txt in the project root for license information.
4
+
5
+ module MsRestAzure
6
+ #
7
+ # Class which represents the data received and deserialized from Azure service.
8
+ #
9
+ class AzureOperationResponse < MsRest::HttpOperationResponse
10
+
11
+ # @return [String] identificator of the request.
12
+ attr_accessor :request_id
13
+ end
14
+ end
@@ -0,0 +1,326 @@
1
+ # encoding: utf-8
2
+ # Copyright (c) Microsoft Corporation. All rights reserved.
3
+ # Licensed under the MIT License. See License.txt in the project root for license information.
4
+
5
+ module MsRestAzure
6
+ #
7
+ # Class which represents a point of access to the REST API.
8
+ #
9
+ class AzureServiceClient < MsRest::ServiceClient
10
+
11
+ # @return [Integer] execution interval for long running operations.
12
+ attr_accessor :long_running_operation_retry_timeout
13
+
14
+ # @return [String] api version of the Azure in string format.
15
+ attr_accessor :api_version
16
+
17
+ #
18
+ # Retrieves the result of 'PUT' operation. Perfroms polling of required.
19
+ # @param azure_response [MsRestAzure::AzureOperationResponse] response from Azure service.
20
+ # @param custom_headers [Hash] custom HTTP headers to apply to HTTP requests.
21
+ # @param custom_deserialization_block [Proc] custom logic for response deserialization.
22
+ #
23
+ # @return [MsRest::HttpOperationResponse] the response.
24
+ def get_put_operation_result(azure_response, custom_headers, custom_deserialization_block)
25
+ fail MsRest::ValidationError, 'Azure response cannot be nil' if azure_response.nil?
26
+
27
+ status_code = azure_response.response.status
28
+
29
+ if (status_code != 200 && status_code != 201 && status_code != 202)
30
+ fail AzureOperationError, "Unexpected polling status code from long running operation #{status_code}"
31
+ end
32
+
33
+ polling_state = PollingState.new(azure_response, @long_running_operation_retry_timeout)
34
+ operation_url = azure_response.request.url_prefix.to_s
35
+
36
+ if (!AsyncOperationStatus.is_terminal_status(polling_state.status))
37
+ task = Concurrent::TimerTask.new do
38
+ begin
39
+ if !polling_state.azure_async_operation_header_link.nil?
40
+ update_state_from_azure_async_operation_header(polling_state, custom_headers)
41
+ elsif !polling_state.location_header_link.nil?
42
+ update_state_from_location_header_on_put(polling_state, custom_headers, custom_deserialization_block)
43
+ else
44
+ update_state_from_get_resource_operation(operation_url, polling_state, custom_headers, custom_deserialization_block)
45
+ end
46
+
47
+ if (AsyncOperationStatus.is_terminal_status(polling_state.status))
48
+ task.shutdown
49
+ end
50
+ rescue Exception => e
51
+ task.shutdown
52
+ e
53
+ end
54
+ end
55
+
56
+ polling_delay = polling_state.get_delay
57
+ polling_delay = 0.1 if polling_delay.nil? || polling_delay == 0
58
+
59
+ task.execution_interval = polling_delay
60
+ task.execute
61
+ task.wait_for_termination
62
+
63
+ polling_error = task.value
64
+ fail polling_error if polling_error.is_a?(Exception)
65
+ end
66
+
67
+ if (AsyncOperationStatus.is_successful_status(polling_state.status) && polling_state.resource.nil?)
68
+ update_state_from_get_resource_operation(operation_url, polling_state, custom_headers, custom_deserialization_block)
69
+ end
70
+
71
+ if (AsyncOperationStatus.is_failed_status(polling_state.status))
72
+ fail polling_state.get_operation_error
73
+ end
74
+
75
+ return polling_state.get_operation_response
76
+ end
77
+
78
+ #
79
+ # Retrieves the result of 'POST' or 'DELETE' operations. Perfroms polling of required.
80
+ # @param azure_response [MsRestAzure::AzureOperationResponse] response from Azure service.
81
+ # @param custom_headers [Proc] custom method for polling.
82
+ # @param custom_deserialization_block [Proc] custom logic for response deserialization.
83
+ #
84
+ # @return [MsRest::HttpOperationResponse] the response.
85
+ def get_post_or_delete_operation_result(azure_response, custom_headers, custom_deserialization_block)
86
+ fail MsRest::ValidationError, 'Azure response cannot be nil' if azure_response.nil?
87
+ fail MsRest::ValidationError, 'Azure response cannot have empty response object' if azure_response.response.nil?
88
+
89
+ status_code = azure_response.response.status
90
+
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
+
97
+ if (!AsyncOperationStatus.is_terminal_status(polling_state.status))
98
+ task = Concurrent::TimerTask.new do
99
+ begin
100
+ if !polling_state.azure_async_operation_header_link.nil?
101
+ update_state_from_azure_async_operation_header(polling_state, custom_headers)
102
+ elsif !polling_state.location_header_link.nil?
103
+ update_state_from_location_header_on_post_or_delete(polling_state, custom_headers, custom_deserialization_block)
104
+ else
105
+ task.shutdown
106
+ fail AzureOperationError, 'Location header is missing from long running operation'
107
+ end
108
+
109
+ if (AsyncOperationStatus.is_terminal_status(polling_state.status))
110
+ task.shutdown
111
+ end
112
+ rescue Exception => e
113
+ task.shutdown
114
+ e
115
+ end
116
+ end
117
+
118
+ polling_delay = polling_state.get_delay
119
+ polling_delay = 0.1 if polling_delay.nil? || polling_delay == 0
120
+
121
+ task.execution_interval = polling_delay
122
+ task.execute
123
+ task.wait_for_termination
124
+
125
+ polling_error = task.value
126
+ fail polling_error if polling_error.is_a?(Exception)
127
+ end
128
+
129
+ if (AsyncOperationStatus.is_failed_status(polling_state.status))
130
+ fail polling_state.get_operation_error
131
+ end
132
+
133
+ return polling_state.get_operation_response
134
+ end
135
+
136
+ #
137
+ # Updates polling state based on location header for PUT HTTP requests.
138
+ # @param operation_url [String] The url retrieve data from.
139
+ # @param polling_state [MsRestAzure::PollingState] polling state to update.
140
+ # @param custom_headers [Hash] custom headers to apply to HTTP request.
141
+ # @param custom_deserialization_block [Proc] custom deserialization method for parsing response.
142
+ #
143
+ def update_state_from_get_resource_operation(operation_url, polling_state, custom_headers, custom_deserialization_block)
144
+ result = get_async_with_custom_deserialization(operation_url, custom_headers, custom_deserialization_block)
145
+
146
+ fail AzureOperationError, 'The response from long running operation does not contain a body' if result.response.body.nil? || result.response.body.empty?
147
+
148
+ if (result.body.respond_to?(:properties) && result.body.properties.respond_to?(:provisioning_state) && !result.body.properties.provisioning_state.nil?)
149
+ polling_state.status = result.body.properties.provisioning_state
150
+ else
151
+ polling_state.status = AsyncOperationStatus::SUCCESS_STATUS
152
+ end
153
+
154
+ error_data = CloudErrorData.new
155
+ error_data.code = polling_state.status
156
+ error_data.message = "Long running operation failed with status #{polling_state.status}"
157
+
158
+ polling_state.error_data = error_data
159
+ polling_state.update_response(result.response)
160
+ polling_state.request = result.request
161
+ polling_state.resource = result.body
162
+ end
163
+
164
+ #
165
+ # Updates polling state based on location header for PUT HTTP requests.
166
+ # @param polling_state [MsRestAzure::PollingState] polling state to update.
167
+ # @param custom_headers [Hash] custom headers to apply to HTTP request.
168
+ # @param custom_deserialization_block [Proc] custom deserialization method for parsing response.
169
+ def update_state_from_location_header_on_put(polling_state, custom_headers, custom_deserialization_block)
170
+ result = get_async_with_custom_deserialization(polling_state.location_header_link, custom_headers, custom_deserialization_block)
171
+
172
+ polling_state.update_response(result.response)
173
+ polling_state.request = result.response
174
+
175
+ status_code = result.response.status
176
+
177
+ if (status_code == 202)
178
+ 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
189
+
190
+ error_data = CloudErrorData.new
191
+ error_data.code = polling_state.status
192
+ error_data.message = "Long running operation failed with status #{polling_state.status}"
193
+
194
+ polling_state.error_data = error_data
195
+ polling_state.resource = result.body
196
+ end
197
+ end
198
+
199
+ #
200
+ # Updates polling state from Azure async operation header.
201
+ # @param polling_state [MsRestAzure::PollingState] polling state.
202
+ # @param custom_headers [Hash] custom headers to apply to HTTP request.
203
+ def update_state_from_azure_async_operation_header(polling_state, custom_headers)
204
+ result = get_async_with_async_operation_deserialization(polling_state.azure_async_operation_header_link, custom_headers)
205
+
206
+ fail AzureOperationError, 'The response from long running operation does not contain a body' if result.body.nil? || result.body.status.nil?
207
+
208
+ polling_state.status = result.body.status
209
+ polling_state.error_data = result.body.error
210
+ polling_state.response = result.response
211
+ polling_state.request = result.request
212
+ polling_state.resource = nil
213
+ end
214
+
215
+ #
216
+ # Updates polling state based on location header for POST and DELETE HTTP requests.
217
+ # @param polling_state [MsRestAzure::PollingState] [description]
218
+ # @param custom_headers [Hash] custom headers to apply to HTTP requests.
219
+ # @param custom_deserialization_block [Proc] custom deserialization method for parsing response.
220
+ def update_state_from_location_header_on_post_or_delete(polling_state, custom_headers, custom_deserialization_block)
221
+ result = get_async_with_custom_deserialization(polling_state.location_header_link, custom_headers, custom_deserialization_block)
222
+
223
+ polling_state.update_response(result.response)
224
+ polling_state.request = result.request
225
+ status_code = result.response.status
226
+
227
+ if (status_code == 202)
228
+ polling_state.status = AsyncOperationStatus::IN_PROGRESS_STATUS
229
+ elsif (status_code == 200 || status_code == 201 || status_code == 204)
230
+ polling_state.status = AsyncOperationStatus::SUCCESS_STATUS
231
+ polling_state.resource = result.body
232
+ end
233
+ end
234
+
235
+ #
236
+ # Retrives data by given URL.
237
+ # @param operation_url [String] the URL.
238
+ # @param custom_headers [String] headers to apply to the HTTP request.
239
+ # @param custom_deserialization_block [Proc] function to perform deserialization of the HTTP response.
240
+ #
241
+ # @return [MsRest::HttpOperationResponse] the response.
242
+ def get_async_with_custom_deserialization(operation_url, custom_headers, custom_deserialization_block)
243
+ result = get_async_common(operation_url, custom_headers)
244
+
245
+ if (!result.body.nil? && !custom_deserialization_block.nil?)
246
+ begin
247
+ result.body = custom_deserialization_block.call(result.body)
248
+ rescue Exception => e
249
+ fail MsRest::DeserializationError.new("Error occured in deserializing the response", e.message, e.backtrace, http_response.body)
250
+ end
251
+ end
252
+
253
+ result
254
+ end
255
+
256
+ #
257
+ # Retrives data by given URL.
258
+ # @param operation_url [String] the URL.
259
+ # @param custom_headers [String] headers to apply to the HTTP request.
260
+ #
261
+ # @return [MsRest::HttpOperationResponse] the response.
262
+ def get_async_with_async_operation_deserialization(operation_url, custom_headers)
263
+ result = get_async_common(operation_url, custom_headers)
264
+
265
+ result.body = AsyncOperationStatus.deserialize_object(result.body)
266
+ result
267
+ end
268
+
269
+ #
270
+ # Retrives data by given URL.
271
+ # @param operation_url [String] the URL.
272
+ # @param custom_headers [String] headers to apply to the HTTP request.
273
+ #
274
+ # @return [MsRest::HttpOperationResponse] the response.
275
+ def get_async_common(operation_url, custom_headers)
276
+ fail ValidationError, 'Operation url cannot be nil' if operation_url.nil?
277
+
278
+ url = URI(operation_url.gsub(' ', '%20'))
279
+
280
+ fail URI::Error unless url.to_s =~ /\A#{URI::regexp}\z/
281
+
282
+ # Create HTTP transport object
283
+ connection = Faraday.new(:url => url) do |faraday|
284
+ faraday.use MsRest::RetryPolicyMiddleware, times: 3, retry: 0.02
285
+ faraday.use :cookie_jar
286
+ faraday.adapter Faraday.default_adapter
287
+ end
288
+
289
+ request_headers = Hash.new
290
+ request_headers['x-ms-client-request-id'] = SecureRandom.uuid
291
+ request_headers['Content-Type'] = 'application/json'
292
+
293
+ unless custom_headers.nil?
294
+ custom_headers.each do |key, value|
295
+ request_headers[key] = value
296
+ end
297
+ end
298
+
299
+ # Send Request
300
+ http_response = connection.get do |request|
301
+ request.headers = request_headers
302
+ @credentials.sign_request(request) unless @credentials.nil?
303
+ end
304
+
305
+ status_code = http_response.status
306
+
307
+ if (status_code != 200 && status_code != 201 && status_code != 202 && status_code != 204)
308
+ json_error_data = JSON.load(http_response.body)
309
+ error_data = CloudErrorData.deserialize_object(json_error_data)
310
+
311
+ fail AzureOperationError.new connection, http_response, error_data, "Long running operation failed with status #{status_code}"
312
+ end
313
+
314
+ result = MsRest::HttpOperationResponse.new(connection, http_response, http_response.body)
315
+
316
+ begin
317
+ result.body = JSON.load(http_response.body) unless http_response.body.to_s.empty?
318
+ rescue Exception => e
319
+ fail MsRest::DeserializationError.new("Error occured in deserializing the response", e.message, e.backtrace, http_response.body)
320
+ end
321
+
322
+ result
323
+ end
324
+ end
325
+
326
+ end
@@ -0,0 +1,33 @@
1
+ # encoding: utf-8
2
+ # Copyright (c) Microsoft Corporation. All rights reserved.
3
+ # Licensed under the MIT License. See License.txt in the project root for license information.
4
+
5
+ module MsRestAzure
6
+ #
7
+ # Class which represents keeps aux data about Azure invalid response.
8
+ #
9
+ class CloudErrorData
10
+
11
+ # @return [String] the error code parsed from the body of the http error response.
12
+ attr_accessor :code
13
+
14
+ # @return [String] the error message parsed from the body of the http error response.
15
+ attr_accessor :message
16
+
17
+ #
18
+ # Deserializes given hash into CloudErrorData object.
19
+ # @param object [Hash] object to deserialize.
20
+ #
21
+ # @return [CloudErrorData] deserialized object.
22
+ def self.deserialize_object(object)
23
+ return if object.nil?
24
+ output_object = CloudErrorData.new
25
+
26
+ output_object.code = object['code']
27
+
28
+ output_object.message = object['message']
29
+
30
+ output_object
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,118 @@
1
+ # encoding: utf-8
2
+ # Copyright (c) Microsoft Corporation. All rights reserved.
3
+ # Licensed under the MIT License. See License.txt in the project root for license information.
4
+
5
+ module MsRestAzure
6
+ #
7
+ # Class that provides access to authentication token.
8
+ #
9
+ class ApplicationTokenProvider < MsRest::TokenProvider
10
+
11
+ private
12
+
13
+ TOKEN_ACQUIRE_URL = '{authentication_endpoint}{tenant_id}/oauth2/token'
14
+ REQUEST_BODY_PATTERN = 'resource={resource_uri}&client_id={client_id}&client_secret={client_secret}&grant_type=client_credentials'
15
+ DEFAULT_SCHEME = 'Bearer'
16
+
17
+ # @return [ActiveDirectoryServiceSettings] settings.
18
+ attr_accessor :settings
19
+
20
+ # @return [String] tenant id (also known as domain).
21
+ attr_accessor :tenant_id
22
+
23
+ # @return [String] application id.
24
+ attr_accessor :client_id
25
+
26
+ # @return [String] application secret key.
27
+ attr_accessor :client_secret
28
+
29
+ # @return [String] auth token.
30
+ attr_accessor :token
31
+
32
+ # @return [Time] the date when the current token expires.
33
+ attr_accessor :token_expires_on
34
+
35
+ # @return [Integer] the amount of time we refresh token before it expires.
36
+ attr_reader :expiration_threshold
37
+
38
+ # @return [String] the type of token.
39
+ attr_reader :token_type
40
+
41
+ public
42
+
43
+ #
44
+ # Creates and initialize new instance of the ApplicationTokenProvider class.
45
+ # @param tenant_id [String] tenant id (also known as domain).
46
+ # @param client_id [String] client id.
47
+ # @param client_secret [String] client secret.
48
+ # @param settings [ActiveDirectoryServiceSettings] client secret.
49
+ def initialize(tenant_id, client_id, client_secret, settings = ActiveDirectoryServiceSettings.get_azure_settings)
50
+ fail ArgumentError, 'Tenant id cannot be nil' if tenant_id.nil?
51
+ fail ArgumentError, 'Client id cannot be nil' if client_id.nil?
52
+ fail ArgumentError, 'Client secret key cannot be nil' if client_secret.nil?
53
+ fail ArgumentError, 'Azure AD settings cannot be nil' if settings.nil?
54
+
55
+ @tenant_id = tenant_id
56
+ @client_id = client_id
57
+ @client_secret = client_secret
58
+ @settings = settings
59
+
60
+ @expiration_threshold = 5 * 60
61
+ end
62
+
63
+ #
64
+ # Returns the string value which needs to be attached
65
+ # to HTTP request header in order to be authorized.
66
+ #
67
+ # @return [String] authentication headers.
68
+ def get_authentication_header
69
+ acquire_token if token_expired
70
+ "#{token_type} #{token}"
71
+ end
72
+
73
+ private
74
+
75
+ #
76
+ # Checks whether token is about to expire.
77
+ #
78
+ # @return [Bool] True if token is about to expire, false otherwise.
79
+ def token_expired
80
+ @token.nil? || Time.now >= @token_expires_on + expiration_threshold
81
+ end
82
+
83
+ #
84
+ # Retrieves a new authenticaion token.
85
+ #
86
+ # @return [String] new authentication token.
87
+ def acquire_token
88
+ token_acquire_url = TOKEN_ACQUIRE_URL.dup
89
+ token_acquire_url['{authentication_endpoint}'] = @settings.authentication_endpoint
90
+ token_acquire_url['{tenant_id}'] = @tenant_id
91
+
92
+ url = URI.parse(token_acquire_url)
93
+
94
+ connection = Faraday.new(:url => url) do |builder|
95
+ builder.adapter Faraday.default_adapter
96
+ end
97
+
98
+ request_body = REQUEST_BODY_PATTERN.dup
99
+ request_body['{resource_uri}'] = ERB::Util.url_encode(@settings.token_audience)
100
+ request_body['{client_id}'] = ERB::Util.url_encode(@client_id)
101
+ request_body['{client_secret}'] = ERB::Util.url_encode(@client_secret)
102
+
103
+ response = connection.get do |request|
104
+ request.headers['content-type'] = 'application/x-www-form-urlencoded'
105
+ request.body = request_body
106
+ end
107
+
108
+ fail AzureOperationError,
109
+ 'Couldn\'t login to Azure, please verify your tenant id, client id and client secret' unless response.status == 200
110
+
111
+ response_body = JSON.load(response.body)
112
+ @token = response_body['access_token']
113
+ @token_expires_on = Time.at(Integer(response_body['expires_on']))
114
+ @token_type = response_body['token_type']
115
+ end
116
+ end
117
+
118
+ end
@@ -0,0 +1,101 @@
1
+ # encoding: utf-8
2
+ # Copyright (c) Microsoft Corporation. All rights reserved.
3
+ # Licensed under the MIT License. See License.txt in the project root for license information.
4
+
5
+ module MsRestAzure
6
+ #
7
+ # Class which represents a state of Azure long running operation.
8
+ #
9
+ class PollingState
10
+
11
+ # @return [Net::HTTPRequest] the HTTP request.
12
+ attr_accessor :request
13
+
14
+ # @return the resource
15
+ attr_accessor :resource
16
+
17
+ # @return [Net::HTTPResponse] the HTTP response.
18
+ attr_accessor :response
19
+
20
+ # @return [AzureOperationError] the azure error data.
21
+ attr_accessor :error_data
22
+
23
+ # @return [String] the latest value captured from Azure-AsyncOperation header.
24
+ attr_accessor :azure_async_operation_header_link
25
+
26
+ # @return [String] the latest value captured from Location header.
27
+ attr_accessor :location_header_link
28
+
29
+ # @return [String] status of the long running operation.
30
+ attr_accessor :status
31
+
32
+ def initialize(azure_response, retry_timeout)
33
+ @retry_timeout = retry_timeout
34
+ @request = azure_response.request
35
+ update_response(azure_response.response)
36
+ @resource = azure_response.body
37
+
38
+ if (!@resource.nil? && @resource.respond_to?(:properties) && @resource.properties.respond_to?(:provisioning_state) && !@resource.properties.provisioning_state.nil?)
39
+ @status = @resource.properties.provisioning_state
40
+ else
41
+ case @response.status
42
+ when 202
43
+ @status = AsyncOperationStatus::IN_PROGRESS_STATUS
44
+ when 200, 201, 204
45
+ @status = AsyncOperationStatus::SUCCESS_STATUS
46
+ else
47
+ @status = AsyncOperationStatus::FAILED_STATUS
48
+ end
49
+ end
50
+ end
51
+
52
+ #
53
+ # Returns the amount of time in seconds for long running operation polling delay.
54
+ #
55
+ # @return [Integer] Amount of time in seconds for long running operation polling delay.
56
+ def get_delay
57
+ return @retry_timeout unless @retry_timeout.nil?
58
+
59
+ if (!response.nil? && !response.headers['Retry-After'].nil?)
60
+ return response.headers['Retry-After'].to_i
61
+ end
62
+
63
+ return AsyncOperationStatus::DEFAULT_DELAY
64
+ end
65
+
66
+ #
67
+ # Updates the polling state from the fields of given response object.
68
+ # @param response [Net::HTTPResponse] the HTTP response.
69
+ def update_response(response)
70
+ @response = response
71
+
72
+ if (!response.nil?)
73
+ @azure_async_operation_header_link = response.headers['Azure-AsyncOperation'] unless response.headers['Azure-AsyncOperation'].nil?
74
+ @location_header_link = response.headers['Location'] unless response.headers['Location'].nil?
75
+ end
76
+ end
77
+
78
+ #
79
+ # returns the Azure's response.
80
+ #
81
+ # @return [MsRestAzure::AzureOperationResponse] Azure's response.
82
+ def get_operation_response
83
+ azure_response = AzureOperationResponse.new(@request, @response, @resource)
84
+ azure_response
85
+ end
86
+
87
+ #
88
+ # Composes and returns cloud error.
89
+ #
90
+ # @return [AzureOperationError] the cloud error.
91
+ def get_operation_error
92
+ AzureOperationError.new @request, @response, @error_data, "Long running operation failed with status #{@status}"
93
+ end
94
+
95
+ private
96
+
97
+ # @return [Integer] retry timeout.
98
+ attr_accessor :retry_timeout
99
+ end
100
+
101
+ end
@@ -0,0 +1,89 @@
1
+ # encoding: utf-8
2
+ # Copyright (c) Microsoft Corporation. All rights reserved.
3
+ # Licensed under the MIT License. See License.txt in the project root for license information.
4
+
5
+ module MsRestAzure
6
+ #
7
+ # Class which represents any Azure resource.
8
+ #
9
+ class Resource
10
+
11
+ # @return [String] the id of the resource.
12
+ attr_accessor :id
13
+
14
+ # @return [String] the name of the resource.
15
+ attr_accessor :name
16
+
17
+ # @return [String] the type of the resource.
18
+ attr_accessor :type
19
+
20
+ # @return [String] the location of the resource (required).
21
+ attr_accessor :location
22
+
23
+ # @return [Hash{String => String}] the tags attached to resources (optional).
24
+ attr_accessor :tags
25
+
26
+ #
27
+ # Serializes given resource object into hash.
28
+ # @param object [Resource] resource object to serialize.
29
+ #
30
+ # @return [Hash] hash representation of resource.
31
+ def self.serialize_object(object)
32
+ object.validate
33
+ output_object = {}
34
+
35
+ serialized_property = object.id
36
+ output_object['id'] = serialized_property unless serialized_property.nil?
37
+
38
+ serialized_property = object.name
39
+ output_object['name'] = serialized_property unless serialized_property.nil?
40
+
41
+ serialized_property = object.type
42
+ output_object['type'] = serialized_property unless serialized_property.nil?
43
+
44
+ serialized_property = object.location
45
+ output_object['location'] = serialized_property unless serialized_property.nil?
46
+
47
+ serialized_property = object.tags
48
+ output_object['tags'] = serialized_property unless serialized_property.nil?
49
+
50
+ output_object
51
+ end
52
+
53
+ #
54
+ # Deserializes given hash object into resource.
55
+ # @param object [Hash] resource in hash representation to deserialize.
56
+ #
57
+ # @return [Resource] deserialized resource.
58
+ def self.deserialize_object(object)
59
+ return if object.nil?
60
+ output_object = Resource.new
61
+
62
+ deserialized_property = object['id']
63
+ output_object.id = deserialized_property
64
+
65
+ deserialized_property = object['name']
66
+ output_object.name = deserialized_property
67
+
68
+ deserialized_property = object['type']
69
+ output_object.type = deserialized_property
70
+
71
+ deserialized_property = object['location']
72
+ output_object.location = deserialized_property
73
+
74
+ deserialized_property = object['tags']
75
+ output_object.tags = deserialized_property
76
+
77
+ output_object.validate
78
+ output_object
79
+ end
80
+
81
+ #
82
+ # Validates the resource. Throws error if there is any property is incorrect.
83
+ #
84
+ def validate
85
+ fail MsRest::ValidationError, 'Location cannot be nil in the Resource object' if @location.nil?
86
+ end
87
+
88
+ end
89
+ end
@@ -0,0 +1,51 @@
1
+ # encoding: utf-8
2
+ # Copyright (c) Microsoft Corporation. All rights reserved.
3
+ # Licensed under the MIT License. See License.txt in the project root for license information.
4
+
5
+ module MsRestAzure
6
+ #
7
+ # Class which represents any Azure subresource.
8
+ #
9
+ class SubResource
10
+
11
+ # @return [String] the id of the subresource.
12
+ attr_accessor :id
13
+
14
+ #
15
+ # Serializes given subresource object into hash.
16
+ # @param object [SubResource] subresource object to serialize.
17
+ #
18
+ # @return [Hash] hash representation of subresource.
19
+ def self.serialize_object(object)
20
+ object.validate
21
+ output_object = {}
22
+
23
+ serialized_property = object.id
24
+ output_object['id'] = serialized_property unless serialized_property.nil?
25
+
26
+ output_object
27
+ end
28
+
29
+ #
30
+ # Deserializes given hash object into subresource.
31
+ # @param object [Hash] subresource in hash representation to deserialize.
32
+ #
33
+ # @return [SubResource] deserialized subresource.
34
+ def self.deserialize_object(object)
35
+ return if object.nil?
36
+ output_object = SubResource.new
37
+
38
+ deserialized_property = object['id']
39
+ output_object.id = deserialized_property
40
+
41
+ output_object.validate
42
+ output_object
43
+ end
44
+
45
+ #
46
+ # Validates the subresource. Throws error if there is any property is incorrect.
47
+ #
48
+ def validate
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,7 @@
1
+ # encoding: utf-8
2
+ # Copyright (c) Microsoft Corporation. All rights reserved.
3
+ # Licensed under the MIT License. See License.txt in the project root for license information.
4
+
5
+ module MsRestAzure
6
+ VERSION = '0.1.1'
7
+ end
@@ -0,0 +1,36 @@
1
+ # Copyright (c) Microsoft Corporation. All rights reserved.
2
+ # Licensed under the MIT License. See License.txt in the project root for license information.
3
+
4
+ # coding: utf-8
5
+ lib = File.expand_path('../lib', __FILE__)
6
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
7
+ require 'ms_rest_azure/version'
8
+
9
+ Gem::Specification.new do |spec|
10
+ spec.name = 'ms_rest_azure'
11
+ spec.version = MsRestAzure::VERSION
12
+ spec.authors = 'Microsoft Corporation'
13
+ spec.email = 'azsdkteam@microsoft.com'
14
+
15
+ spec.summary = %q{Azure Client Library for Ruby.}
16
+ spec.description = %q{Azure Client Library for Ruby.}
17
+ spec.homepage = 'https://rubygems.org/gems/azure'
18
+ spec.license = 'MIT'
19
+
20
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ spec.bindir = 'bin'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.required_ruby_version = '>= 1.9.3'
26
+
27
+ spec.add_development_dependency 'bundler', '~> 1.9'
28
+ spec.add_development_dependency 'rake', '~> 10.0'
29
+ spec.add_development_dependency 'rspec', '~> 3.3.0'
30
+
31
+ spec.add_runtime_dependency 'json', '~> 1.8.3'
32
+ spec.add_runtime_dependency 'concurrent-ruby', ['>= 1.0.0.pre1', '<2']
33
+ spec.add_runtime_dependency 'faraday', '~> 0.9.1'
34
+ spec.add_runtime_dependency 'faraday-cookie_jar', '~> 0.0.6'
35
+ spec.add_runtime_dependency 'ms_rest', '~> 0.1.0'
36
+ end
metadata ADDED
@@ -0,0 +1,180 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ms_rest_azure
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Microsoft Corporation
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 3.3.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.3.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: json
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 1.8.3
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 1.8.3
69
+ - !ruby/object:Gem::Dependency
70
+ name: concurrent-ruby
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 1.0.0.pre1
76
+ - - "<"
77
+ - !ruby/object:Gem::Version
78
+ version: '2'
79
+ type: :runtime
80
+ prerelease: false
81
+ version_requirements: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: 1.0.0.pre1
86
+ - - "<"
87
+ - !ruby/object:Gem::Version
88
+ version: '2'
89
+ - !ruby/object:Gem::Dependency
90
+ name: faraday
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: 0.9.1
96
+ type: :runtime
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: 0.9.1
103
+ - !ruby/object:Gem::Dependency
104
+ name: faraday-cookie_jar
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: 0.0.6
110
+ type: :runtime
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: 0.0.6
117
+ - !ruby/object:Gem::Dependency
118
+ name: ms_rest
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - "~>"
122
+ - !ruby/object:Gem::Version
123
+ version: 0.1.0
124
+ type: :runtime
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - "~>"
129
+ - !ruby/object:Gem::Version
130
+ version: 0.1.0
131
+ description: Azure Client Library for Ruby.
132
+ email: azsdkteam@microsoft.com
133
+ executables: []
134
+ extensions: []
135
+ extra_rdoc_files: []
136
+ files:
137
+ - ".gitignore"
138
+ - ".travis.yml"
139
+ - Gemfile
140
+ - LICENSE.txt
141
+ - README.md
142
+ - Rakefile
143
+ - lib/ms_rest_azure.rb
144
+ - lib/ms_rest_azure/active_directory_service_settings.rb
145
+ - lib/ms_rest_azure/async_operation_status.rb
146
+ - lib/ms_rest_azure/azure_operation_error.rb
147
+ - lib/ms_rest_azure/azure_operation_response.rb
148
+ - lib/ms_rest_azure/azure_service_client.rb
149
+ - lib/ms_rest_azure/cloud_error_data.rb
150
+ - lib/ms_rest_azure/credentials/application_token_provider.rb
151
+ - lib/ms_rest_azure/polling_state.rb
152
+ - lib/ms_rest_azure/resource.rb
153
+ - lib/ms_rest_azure/sub_resource.rb
154
+ - lib/ms_rest_azure/version.rb
155
+ - ms_rest_azure.gemspec
156
+ homepage: https://rubygems.org/gems/azure
157
+ licenses:
158
+ - MIT
159
+ metadata: {}
160
+ post_install_message:
161
+ rdoc_options: []
162
+ require_paths:
163
+ - lib
164
+ required_ruby_version: !ruby/object:Gem::Requirement
165
+ requirements:
166
+ - - ">="
167
+ - !ruby/object:Gem::Version
168
+ version: 1.9.3
169
+ required_rubygems_version: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ requirements: []
175
+ rubyforge_project:
176
+ rubygems_version: 2.4.6
177
+ signing_key:
178
+ specification_version: 4
179
+ summary: Azure Client Library for Ruby.
180
+ test_files: []