hpe3par_sdk 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,503 @@
1
+ # (c) Copyright 2016-2017 Hewlett Packard Enterprise Development LP
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software distributed
8
+ # under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
9
+ # CONDITIONS OF ANY KIND, either express or implied. See the License for the
10
+ # specific language governing permissions and limitations under the License.
11
+
12
+ module Hpe3parSdk
13
+ class HPE3PARException < StandardError
14
+ attr_reader :message, :code, :ref, :http_status
15
+
16
+ def initialize(code = nil, message =nil, ref =nil, http_status=nil)
17
+ @code = code
18
+ @message = message
19
+ @ref = ref
20
+ @http_status = http_status
21
+ formatted_string = 'Error: '
22
+ if @http_status
23
+ formatted_string += ' (HTTP %s)' % @http_status
24
+ end
25
+ if @code
26
+ formatted_string += ' API code: %s' % @code
27
+ end
28
+ if @message
29
+ formatted_string += ' - %s' % @message
30
+ end
31
+ if @ref
32
+ formatted_string += ' - %s' % @ref
33
+ end
34
+
35
+ super(formatted_string)
36
+ end
37
+ end
38
+
39
+ class UnsupportedVersion < HPE3PARException
40
+ end
41
+
42
+ class SSLCertFailed < HPE3PARException
43
+ @http_status = ''
44
+ @message = 'SSL Certificate Verification Failed'
45
+ end
46
+
47
+ class RequestException < HPE3PARException
48
+ #There was an ambiguous exception that occurred in Requests
49
+ end
50
+
51
+
52
+ class ConnectionError < HPE3PARException
53
+ #There was an error connecting to the server
54
+ end
55
+
56
+
57
+ class HTTPError < HPE3PARException
58
+ #An HTTP error occurred
59
+ end
60
+
61
+
62
+ class URLRequired < HPE3PARException
63
+ #A valid URL is required to make a request
64
+ end
65
+
66
+
67
+ class TooManyRedirects < HPE3PARException
68
+ #Too many redirects
69
+ end
70
+
71
+
72
+ class Timeout < HPE3PARException
73
+ #The request timed out
74
+ end
75
+
76
+
77
+ # 400 Errors
78
+
79
+
80
+ class HTTPBadRequest < HPE3PARException
81
+
82
+ #HTTP 400 - Bad request: you sent some malformed data.
83
+ attr_reader :message, :http_status
84
+
85
+
86
+ def initialize(code = nil, message =nil, ref =nil, http_status=nil)
87
+ @http_status = 400
88
+ @message= 'Bad request'
89
+ super(code, message.nil? ? @message : message,
90
+ ref,
91
+ http_status.nil? ? @http_status : http_status)
92
+ end
93
+
94
+ end
95
+
96
+ class HTTPUnauthorized < HPE3PARException
97
+ attr_reader :message, :http_status
98
+ #HTTP 401 - Unauthorized: bad credentials.
99
+
100
+ def initialize(code = nil, message =nil, ref =nil, http_status=nil)
101
+ @http_status = 401
102
+ @message ='Unauthorized'
103
+ super(code, message.nil? ? @message : message,
104
+ ref,
105
+ http_status.nil? ? @http_status : http_status)
106
+ end
107
+
108
+ end
109
+
110
+ class HTTPForbidden < HPE3PARException
111
+
112
+ #HTTP 403 - Forbidden: your credentials don't give you access to this
113
+ #resource.
114
+ attr_reader :message, :http_status
115
+
116
+
117
+ def initialize(code = nil, message =nil, ref =nil, http_status=nil)
118
+ @http_status = 403
119
+ @message = 'Forbidden'
120
+ super(code, message.nil? ? @message : message,
121
+ ref,
122
+ http_status.nil? ? @http_status : http_status)
123
+ end
124
+ end
125
+
126
+ class HTTPNotFound < HPE3PARException
127
+
128
+ #HTTP 404 - Not found
129
+ attr_reader :message, :http_status
130
+
131
+
132
+ def initialize(code = nil, message =nil, ref =nil, http_status=nil)
133
+ @http_status = 404
134
+ @message = 'Not found'
135
+ super(code, message.nil? ? @message : message,
136
+ ref,
137
+ http_status.nil? ? @http_status : http_status)
138
+ end
139
+ end
140
+
141
+ class HTTPMethodNotAllowed < HPE3PARException
142
+
143
+ #HTTP 405 - Method not Allowed
144
+ attr_reader :message, :http_status
145
+
146
+
147
+ def initialize(code = nil, message =nil, ref =nil, http_status=nil)
148
+ @http_status = 405
149
+ @message = 'Method Not Allowed'
150
+ super(code, message.nil? ? @message : message,
151
+ ref,
152
+ http_status.nil? ? @http_status : http_status)
153
+ end
154
+ end
155
+
156
+ class HTTPNotAcceptable < HPE3PARException
157
+
158
+ #HTTP 406 - Method not Acceptable
159
+ attr_reader :message, :http_status
160
+
161
+
162
+ def initialize(code = nil, message =nil, ref =nil, http_status=nil)
163
+ @http_status = 406
164
+ @message = 'Method Not Acceptable'
165
+ super(code, message.nil? ? @message : message,
166
+ ref,
167
+ http_status.nil? ? @http_status : http_status)
168
+ end
169
+ end
170
+
171
+ class HTTPProxyAuthRequired < HPE3PARException
172
+
173
+ #HTTP 407 - The client must first authenticate itself with the proxy.
174
+ attr_reader :message, :http_status
175
+
176
+ def initialize(code = nil, message =nil, ref =nil, http_status=nil)
177
+ @http_status = 407
178
+ @message = 'Proxy Authentication Required'
179
+
180
+ super(code, message.nil? ? @message : message,
181
+ ref,
182
+ http_status.nil? ? @http_status : http_status)
183
+ end
184
+ end
185
+
186
+ class HTTPRequestTimeout < HPE3PARException
187
+
188
+ #HTTP 408 - The server timed out waiting for the request.
189
+ attr_reader :message, :http_status
190
+
191
+
192
+ def initialize(code = nil, message =nil, ref =nil, http_status=nil)
193
+ @http_status = 408
194
+ @message = 'Request Timeout'
195
+ super(code, message.nil? ? @message : message,
196
+ ref,
197
+ http_status.nil? ? @http_status : http_status)
198
+ end
199
+ end
200
+
201
+ class HTTPConflict < HPE3PARException
202
+
203
+ #HTTP 409 - Conflict: A Conflict happened on the server
204
+ attr_reader :message, :http_status
205
+
206
+
207
+ def initialize(code = nil, message =nil, ref =nil, http_status=nil)
208
+ @http_status = 409
209
+ @message = 'Conflict'
210
+ super(code, message.nil? ? @message : message,
211
+ ref,
212
+ http_status.nil? ? @http_status : http_status)
213
+ end
214
+ end
215
+
216
+ class HTTPGone < HPE3PARException
217
+
218
+ #HTTP 410 - Indicates that the resource requested is no longer available and
219
+ # will not be available again.
220
+ attr_reader :message, :http_status
221
+
222
+
223
+ def initialize(code = nil, message =nil, ref =nil, http_status=nil)
224
+ @http_status = 410
225
+ @message = 'Gone'
226
+ super(code, message.nil? ? @message : message,
227
+ ref,
228
+ http_status.nil? ? @http_status : http_status)
229
+ end
230
+ end
231
+
232
+ class HTTPLengthRequired < HPE3PARException
233
+
234
+ #HTTP 411 - The request did not specify the length of its content, which is
235
+ # required by the requested resource.
236
+ attr_reader :message, :http_status
237
+
238
+
239
+ def initialize(code = nil, message =nil, ref =nil, http_status=nil)
240
+ @http_status = 411
241
+ @message = 'Length Required'
242
+ super(code, message.nil? ? @message : message,
243
+ ref,
244
+ http_status.nil? ? @http_status : http_status)
245
+ end
246
+ end
247
+
248
+ class HTTPPreconditionFailed < HPE3PARException
249
+
250
+ #HTTP 412 - The server does not meet one of the preconditions that the
251
+ # requester put on the request.
252
+ attr_reader :message, :http_status
253
+
254
+
255
+ def initialize(code = nil, message =nil, ref =nil, http_status=nil)
256
+ @http_status = 412
257
+ @message = 'Over limit'
258
+ super(code, message.nil? ? @message : message,
259
+ ref,
260
+ http_status.nil? ? @http_status : http_status)
261
+ end
262
+ end
263
+
264
+ class HTTPRequestEntityTooLarge < HPE3PARException
265
+
266
+ #HTTP 413 - The request is larger than the server is willing or able to
267
+ # process
268
+ attr_reader :message, :http_status
269
+
270
+
271
+ def initialize(code = nil, message =nil, ref =nil, http_status=nil)
272
+ @http_status = 413
273
+ @message = 'Request Entity Too Large'
274
+ super(code, message.nil? ? @message : message,
275
+ ref,
276
+ http_status.nil? ? @http_status : http_status)
277
+ end
278
+ end
279
+
280
+ class HTTPRequestURITooLong < HPE3PARException
281
+
282
+ #HTTP 414 - The URI provided was too long for the server to process.
283
+ attr_reader :message, :http_status
284
+
285
+
286
+ def initialize(code = nil, message =nil, ref =nil, http_status=nil)
287
+ @http_status = 414
288
+ @message = 'Request URI Too Large'
289
+ super(code, message.nil? ? @message : message,
290
+ ref,
291
+ http_status.nil? ? @http_status : http_status)
292
+ end
293
+ end
294
+
295
+ class HTTPUnsupportedMediaType < HPE3PARException
296
+
297
+ #HTTP 415 - The request entity has a media type which the server or resource
298
+ # does not support.
299
+ attr_reader :message, :http_status
300
+
301
+
302
+ def initialize(code = nil, message =nil, ref =nil, http_status=nil)
303
+ @http_status = 415
304
+ @message = 'Unsupported Media Type'
305
+ super(code, message.nil? ? @message : message,
306
+ ref,
307
+ http_status.nil? ? @http_status : http_status)
308
+ end
309
+ end
310
+
311
+ class HTTPRequestedRangeNotSatisfiable < HPE3PARException
312
+
313
+ #HTTP 416 - The client has asked for a portion of the file, but the server
314
+ # cannot supply that portion.
315
+ attr_reader :message, :http_status
316
+
317
+
318
+ def initialize(code = nil, message =nil, ref =nil, http_status=nil)
319
+ @http_status = 416
320
+ @message = 'Requested Range Not Satisfiable'
321
+ super(code, message.nil? ? @message : message,
322
+ ref,
323
+ http_status.nil? ? @http_status : http_status)
324
+ end
325
+ end
326
+
327
+ class HTTPExpectationFailed < HPE3PARException
328
+
329
+ #HTTP 417 - The server cannot meet the requirements of the Expect
330
+ # request-header field.
331
+ attr_reader :message, :http_status
332
+
333
+
334
+ def initialize(code = nil, message =nil, ref =nil, http_status=nil)
335
+ @http_status = 417
336
+ @message = 'Expectation Failed'
337
+ super(code, message.nil? ? @message : message,
338
+ ref,
339
+ http_status.nil? ? @http_status : http_status)
340
+ end
341
+ end
342
+
343
+ class HTTPTeaPot < HPE3PARException
344
+
345
+ #HTTP 418 - I'm a Tea Pot
346
+ attr_reader :message, :http_status
347
+
348
+
349
+ def initialize(code = nil, message =nil, ref =nil, http_status=nil)
350
+ @http_status = 418
351
+ @message = 'I' 'm A Teapot. (RFC 2324)'
352
+ super(code, message.nil? ? @message : message,
353
+ ref,
354
+ http_status.nil? ? @http_status : http_status)
355
+ end
356
+ end
357
+
358
+ # 500 Errors
359
+
360
+
361
+ class HTTPInternalServerError < HPE3PARException
362
+
363
+ #HTTP 500 - Internal Server Error: an internal error occured.
364
+ attr_reader :message, :http_status
365
+
366
+
367
+ def initialize(code = nil, message =nil, ref =nil, http_status=nil)
368
+ @http_status = 500
369
+ @message = 'Internal Server Error'
370
+ super(code, message.nil? ? @message : message,
371
+ ref,
372
+ http_status.nil? ? @http_status : http_status)
373
+ end
374
+ end
375
+
376
+ class HTTPNotImplemented < HPE3PARException
377
+
378
+ #HTTP 501 - Not Implemented: the server does not support this operation.
379
+ attr_reader :message, :http_status
380
+
381
+
382
+ def initialize(code = nil, message =nil, ref =nil, http_status=nil)
383
+ @http_status = 501
384
+ @message = 'Not Implemented'
385
+ super(code, message.nil? ? @message : message,
386
+ ref,
387
+ http_status.nil? ? @http_status : http_status)
388
+ end
389
+ end
390
+
391
+ class HTTPBadGateway < HPE3PARException
392
+
393
+ #HTTP 502 - The server was acting as a gateway or proxy and received an
394
+ # invalid response from the upstream server.
395
+ attr_reader :message, :http_status
396
+
397
+ def initialize(code = nil, message =nil, ref =nil, http_status=nil)
398
+ @http_status = 502
399
+ @message = 'Bad Gateway'
400
+ super(code, message.nil? ? @message : message,
401
+ ref,
402
+ http_status.nil? ? @http_status : http_status)
403
+ end
404
+ end
405
+
406
+ class HTTPServiceUnavailable < HPE3PARException
407
+
408
+ #HTTP 503 - The server is currently unavailable
409
+ attr_reader :message, :http_status
410
+
411
+
412
+ def initialize(code = nil, message =nil, ref =nil, http_status=nil)
413
+ @http_status = 503
414
+ @message = 'Service Unavailable'
415
+ super(code, message.nil? ? @message : message,
416
+ ref,
417
+ http_status.nil? ? @http_status : http_status)
418
+ end
419
+ end
420
+
421
+ class HTTPGatewayTimeout < HPE3PARException
422
+
423
+ #HTTP 504 - The server was acting as a gateway or proxy and did
424
+ # not receive a timely response from the upstream server.
425
+ attr_reader :message, :http_status
426
+
427
+
428
+ def initialize(code = nil, message =nil, ref =nil, http_status=nil)
429
+ @http_status = 504
430
+ @message = 'Gateway Timeout'
431
+ super(code, message.nil? ? @message : message,
432
+ ref,
433
+ http_status.nil? ? @http_status : http_status)
434
+ end
435
+ end
436
+
437
+ class HTTPVersionNotSupported < HPE3PARException
438
+
439
+ #HTTP 505 - The server does not support the HTTP protocol version used
440
+ # in the request.
441
+ attr_reader :message, :http_status
442
+
443
+
444
+ def initialize(code = nil, message =nil, ref =nil, http_status=nil)
445
+ @http_status = 505
446
+ @message = 'Version Not Supported'
447
+ super(code, message.nil? ? @message : message,
448
+ ref,
449
+ http_status.nil? ? @http_status : http_status)
450
+ end
451
+ end
452
+
453
+
454
+ attr_accessor :code_map
455
+
456
+ @@code_map = Hash.new('HPE3PARException')
457
+ exp = ["HTTPBadRequest", "HTTPUnauthorized",
458
+ "HTTPForbidden", "HTTPNotFound", "HTTPMethodNotAllowed",
459
+ "HTTPNotAcceptable", "HTTPProxyAuthRequired",
460
+ "HTTPRequestTimeout", "HTTPConflict", "HTTPGone",
461
+ "HTTPLengthRequired", "HTTPPreconditionFailed",
462
+ "HTTPRequestEntityTooLarge", "HTTPRequestURITooLong",
463
+ "HTTPUnsupportedMediaType", "HTTPRequestedRangeNotSatisfiable",
464
+ "HTTPExpectationFailed", "HTTPTeaPot",
465
+ "HTTPNotImplemented", "HTTPBadGateway",
466
+ "HTTPServiceUnavailable", "HTTPGatewayTimeout",
467
+ "HTTPVersionNotSupported", "HTTPInternalServerError"]
468
+ exp.each do |c|
469
+ inst = Hpe3parSdk.const_get(c).new
470
+ @@code_map[inst.http_status] = c
471
+ end
472
+
473
+ def self.exception_from_response(response, body)
474
+ # Return an instance of an ClientException or subclass
475
+ # based on a Python Requests response.
476
+ #
477
+ # Usage::
478
+ #
479
+ # resp, body = http.request(...)
480
+ # if resp.status != 200:
481
+ # raise exception_from_response(resp, body)
482
+
483
+
484
+ cls = @@code_map[response.code]
485
+ code = nil
486
+ msg = nil
487
+ ref = nil
488
+
489
+ if response.code >= 400
490
+ if !body.nil? and body.key?('message')
491
+ body['desc'] = body['message']
492
+ end
493
+ end
494
+
495
+ code = body['code']
496
+ msg = body['desc']
497
+ if body.key?('ref')
498
+ ref = body['ref']
499
+ end
500
+ return Hpe3parSdk.const_get(cls).new(code, msg, ref, response.code)
501
+ end
502
+ end
503
+