oneview-sdk 1.0.0 → 2.0.0

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.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +4 -0
  3. data/CHANGELOG.md +81 -2
  4. data/README.md +118 -113
  5. data/Rakefile +11 -0
  6. data/lib/oneview-sdk/cli.rb +21 -15
  7. data/lib/oneview-sdk/client.rb +34 -21
  8. data/lib/oneview-sdk/config_loader.rb +12 -1
  9. data/lib/oneview-sdk/exceptions.rb +55 -0
  10. data/lib/oneview-sdk/resource/connection_template.rb +48 -0
  11. data/lib/oneview-sdk/resource/datacenter.rb +77 -0
  12. data/lib/oneview-sdk/resource/enclosure.rb +46 -24
  13. data/lib/oneview-sdk/resource/enclosure_group.rb +20 -36
  14. data/lib/oneview-sdk/resource/ethernet_network.rb +19 -23
  15. data/lib/oneview-sdk/resource/fabric.rb +41 -0
  16. data/lib/oneview-sdk/resource/fc_network.rb +15 -16
  17. data/lib/oneview-sdk/resource/fcoe_network.rb +15 -12
  18. data/lib/oneview-sdk/resource/firmware_bundle.rb +23 -24
  19. data/lib/oneview-sdk/resource/firmware_driver.rb +21 -9
  20. data/lib/oneview-sdk/resource/interconnect.rb +29 -9
  21. data/lib/oneview-sdk/resource/lig_uplink_set.rb +22 -28
  22. data/lib/oneview-sdk/resource/logical_downlink.rb +53 -0
  23. data/lib/oneview-sdk/resource/logical_enclosure.rb +35 -33
  24. data/lib/oneview-sdk/resource/logical_interconnect.rb +29 -65
  25. data/lib/oneview-sdk/resource/logical_interconnect_group.rb +26 -6
  26. data/lib/oneview-sdk/resource/logical_switch.rb +184 -0
  27. data/lib/oneview-sdk/resource/logical_switch_group.rb +66 -0
  28. data/lib/oneview-sdk/resource/managed_san.rb +79 -0
  29. data/lib/oneview-sdk/resource/network_set.rb +64 -0
  30. data/lib/oneview-sdk/resource/power_device.rb +174 -0
  31. data/lib/oneview-sdk/resource/rack.rb +83 -0
  32. data/lib/oneview-sdk/resource/san_manager.rb +88 -0
  33. data/lib/oneview-sdk/resource/server_hardware.rb +139 -18
  34. data/lib/oneview-sdk/resource/server_hardware_type.rb +32 -1
  35. data/lib/oneview-sdk/resource/server_profile.rb +352 -9
  36. data/lib/oneview-sdk/resource/server_profile_template.rb +193 -4
  37. data/lib/oneview-sdk/resource/storage_pool.rb +42 -20
  38. data/lib/oneview-sdk/resource/storage_system.rb +64 -14
  39. data/lib/oneview-sdk/resource/switch.rb +86 -0
  40. data/lib/oneview-sdk/resource/unmanaged_device.rb +55 -0
  41. data/lib/oneview-sdk/resource/uplink_set.rb +24 -64
  42. data/lib/oneview-sdk/resource/volume.rb +29 -29
  43. data/lib/oneview-sdk/resource/volume_attachment.rb +79 -0
  44. data/lib/oneview-sdk/resource/volume_snapshot.rb +20 -1
  45. data/lib/oneview-sdk/resource/volume_template.rb +22 -33
  46. data/lib/oneview-sdk/resource.rb +66 -28
  47. data/lib/oneview-sdk/rest.rb +69 -24
  48. data/lib/oneview-sdk/ssl_helper.rb +20 -9
  49. data/lib/oneview-sdk/version.rb +12 -1
  50. data/lib/oneview-sdk.rb +12 -0
  51. data/oneview-sdk.gemspec +11 -0
  52. metadata +17 -2
@@ -0,0 +1,79 @@
1
+ # (C) Copyright 2016 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 specific
10
+ # language governing permissions and limitations under the License.
11
+
12
+ module OneviewSDK
13
+ # Storage volume attachment resource implementation
14
+ class VolumeAttachment < Resource
15
+ BASE_URI = '/rest/storage-volume-attachments'.freeze
16
+
17
+ # Create a resource object, associate it with a client, and set its properties.
18
+ # @param [OneviewSDK::Client] client The client object for the OneView appliance
19
+ # @param [Hash] params The options for this resource (key-value pairs)
20
+ # @param [Integer] api_ver The api version to use when interracting with this resource.
21
+ def initialize(client, params = {}, api_ver = nil)
22
+ super
23
+ # Default values:
24
+ @data['type'] ||= 'StorageVolumeAttachment'
25
+ end
26
+
27
+ # Method is not available
28
+ # @raise [OneviewSDK::MethodUnavailable] method is not available
29
+ def create
30
+ unavailable_method
31
+ end
32
+
33
+ # Method is not available
34
+ # @raise [OneviewSDK::MethodUnavailable] method is not available
35
+ def update
36
+ unavailable_method
37
+ end
38
+
39
+ # Method is not available
40
+ # @raise [OneviewSDK::MethodUnavailable] method is not available
41
+ def delete
42
+ unavailable_method
43
+ end
44
+
45
+ # Gets the list of extra unmanaged storage volumes
46
+ # @param [OneviewSDK::Client] client The client object for the OneView appliance
47
+ def self.get_extra_unmanaged_volumes(client)
48
+ response = client.rest_get(BASE_URI + '/repair?alertFixType=ExtraUnmanagedStorageVolumes')
49
+ client.response_handler(response)
50
+ end
51
+
52
+ # Removes extra presentations from a specific server profile
53
+ # @param [OneviewSDK::Client] client The client object for the OneView appliance
54
+ # @param [OneviewSDK::Resource] resource Oneview resource
55
+ def self.remove_extra_unmanaged_volume(client, resource)
56
+ requestBody = {
57
+ type: 'ExtraUnmanagedStorageVolumes',
58
+ resourceUri: resource['uri']
59
+ }
60
+ response = client.rest_post(BASE_URI + '/repair', 'body' => requestBody)
61
+ client.response_handler(response)
62
+ end
63
+
64
+ # Gets all volume attachment paths
65
+ # @return [Array] List of the storage volume attachments paths
66
+ def get_paths
67
+ response = @client.rest_get(@data['uri'] + '/paths')
68
+ @client.response_handler(response)
69
+ end
70
+
71
+ # Gets a volume attachment path by id
72
+ # @param [String] id Volume attachament path id
73
+ # @return [OneviewSDK::VolumeAttachmentPath]
74
+ def get_path(id)
75
+ response = @client.rest_get("#{@data['uri']}/paths/#{id}")
76
+ @client.response_handler(response)
77
+ end
78
+ end
79
+ end
@@ -1,18 +1,37 @@
1
+ # (C) Copyright 2016 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 specific
10
+ # language governing permissions and limitations under the License.
11
+
1
12
  module OneviewSDK
2
13
  # Volume snapshot resource implementation
3
14
  class VolumeSnapshot < Resource
4
15
  BASE_URI = nil
5
16
 
17
+ # Create a resource object, associate it with a client, and set its properties.
18
+ # @param [OneviewSDK::Client] client The client object for the OneView appliance
19
+ # @param [Hash] params The options for this resource (key-value pairs)
20
+ # @param [Integer] api_ver The api version to use when interracting with this resource.
6
21
  def initialize(client, params = {}, api_ver = nil)
7
22
  super
8
23
  # Default values
9
24
  @data['type'] ||= 'Snapshot'
10
25
  end
11
26
 
27
+ # Method is not available
28
+ # @raise [OneviewSDK::MethodUnavailable] method is not available
12
29
  def create
13
30
  unavailable_method
14
31
  end
15
32
 
33
+ # Method is not available
34
+ # @raise [OneviewSDK::MethodUnavailable] method is not available
16
35
  def update
17
36
  unavailable_method
18
37
  end
@@ -20,7 +39,7 @@ module OneviewSDK
20
39
  # Sets the volume
21
40
  # @param [OneviewSDK::Volume] volume Volume
22
41
  def set_volume(volume)
23
- fail 'Please set the volume\'s uri attribute!' unless volume['uri']
42
+ volume.retrieve! unless volume['uri']
24
43
  @data['storageVolumeUri'] = volume['uri']
25
44
  end
26
45
  end
@@ -1,10 +1,21 @@
1
+ # (C) Copyright 2016 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 specific
10
+ # language governing permissions and limitations under the License.
11
+
1
12
  module OneviewSDK
2
13
  # Volume template resource implementation
3
14
  class VolumeTemplate < Resource
4
15
  BASE_URI = '/rest/storage-volume-templates'.freeze
5
16
 
6
- # Create client object, establish connection, and set up logging and api version.
7
- # @param [Client] client The Client object with a connection to the OneView appliance
17
+ # Create the client object, establishes connection, and set up the logging and api version.
18
+ # @param [OneviewSDK::Client] client The client object for the OneView appliance
8
19
  # @param [Hash] params The options for this resource (key-value pairs)
9
20
  # @param [Integer] api_ver The api version to use when interracting with this resource.
10
21
  # Defaults to client.api_version if exists, or OneviewSDK::Client::DEFAULT_API_VERSION.
@@ -17,29 +28,10 @@ module OneviewSDK
17
28
  @data['type'] ||= 'StorageVolumeTemplateV3'
18
29
  end
19
30
 
20
- # @!group Validates
21
-
22
- VALID_REFRESH_STATES = %w(NotRefreshing RefreshFailed RefreshPending Refreshing).freeze
23
- # Validate refreshState
24
- # @param [String] value NotRefreshing, RefreshFailed, RefreshPending, Refreshing
25
- def validate_refreshState(value)
26
- fail 'Invalid refresh state' unless VALID_REFRESH_STATES.include?(value)
27
- end
28
-
29
- VALID_STATUSES = %w(OK Disabled Warning Critical Unknown).freeze
30
- # Validate status
31
- # @param [String] value OK, Disabled, Warning, Critical, Unknown
32
- def validate_status(value)
33
- fail 'Invalid status' unless VALID_STATUSES.include?(value)
34
- end
35
-
36
- # @!endgroup
37
-
38
31
  # Create the resource on OneView using the current data
39
- # Adds Accept-Language attribute in the Header equal to "en_US"
40
32
  # @note Calls refresh method to set additional data
41
- # @raise [RuntimeError] if the client is not set
42
- # @raise [RuntimeError] if the resource creation fails
33
+ # @raise [OneviewSDK::IncompleteResource] if the client is not set
34
+ # @raise [StandardError] if the resource creation fails
43
35
  # @return [Resource] self
44
36
  def create
45
37
  ensure_client
@@ -48,9 +40,8 @@ module OneviewSDK
48
40
  set_all(body)
49
41
  end
50
42
 
51
- # Delete volume template from OneView
52
- # Adds Accept-Language attribute in the Header equal to "en_US"
53
- # @return [TrueClass] if volume template was deleted successfully
43
+ # Deletes the volume template from OneView
44
+ # @return [TrueClass] if the volume template was deleted successfully
54
45
  def delete
55
46
  ensure_client && ensure_uri
56
47
  response = @client.rest_delete(@data['uri'], { 'Accept-Language' => 'en_US' }, @api_version)
@@ -58,8 +49,7 @@ module OneviewSDK
58
49
  true
59
50
  end
60
51
 
61
- # Update volume template from OneView
62
- # Adds Accept-Language attribute in the Header equal to "en_US"
52
+ # Updates the volume template from OneView
63
53
  # @return [Resource] self
64
54
  def update(attributes = {})
65
55
  set_all(attributes)
@@ -69,7 +59,7 @@ module OneviewSDK
69
59
  self
70
60
  end
71
61
 
72
- # Set storage pool
62
+ # Sets the storage pool
73
63
  # @param [Boolean] shareable
74
64
  # @param [String] provisionType 'Thin' or 'Full'
75
65
  # @param [String] capacity (in bytes)
@@ -82,25 +72,24 @@ module OneviewSDK
82
72
  @data['provisioning']['storagePoolUri'] = storage_pool['uri']
83
73
  end
84
74
 
85
- # Set storage system
75
+ # Sets the storage system
86
76
  # @param [OneviewSDK::StorageSystem] storage_system Storage System to be used to create the template
87
77
  def set_storage_system(storage_system)
88
78
  storage_system.retrieve! unless storage_system['uri']
89
79
  @data['storageSystemUri'] = storage_system['uri']
90
80
  end
91
81
 
92
- # Set snapshot pool
82
+ # Sets the snapshot pool
93
83
  # @param [OneviewSDK::StoragePool] storage_pool Storage Pool to generate the template
94
84
  def set_snapshot_pool(storage_pool)
95
85
  storage_pool.retrieve! unless storage_pool['uri']
96
86
  @data['snapshotPoolUri'] = storage_pool['uri']
97
87
  end
98
88
 
99
- # Get connectable volume templates by its attributes
89
+ # Gets the connectable volume templates by its attributes
100
90
  # @param [Hash] attributes Hash containing the attributes name and value
101
91
  def get_connectable_volume_templates(attributes = {})
102
92
  OneviewSDK::Resource.find_by(@client, attributes, BASE_URI + '/connectable-volume-templates')
103
93
  end
104
-
105
94
  end
106
95
  end
@@ -1,3 +1,14 @@
1
+ # (C) Copyright 2016 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 specific
10
+ # language governing permissions and limitations under the License.
11
+
1
12
  require_relative 'client'
2
13
 
3
14
  # OneviewSDK Resources
@@ -13,27 +24,28 @@ module OneviewSDK
13
24
  :logger
14
25
 
15
26
  # Create a resource object, associate it with a client, and set its properties.
16
- # @param [Client] client The Client object with a connection to the OneView appliance
27
+ # @param [OneviewSDK::Client] client The client object for the OneView appliance
17
28
  # @param [Hash] params The options for this resource (key-value pairs)
18
29
  # @param [Integer] api_ver The api version to use when interracting with this resource.
19
- # Defaults to client.api_version if exists, or OneviewSDK::Client::DEFAULT_API_VERSION.
30
+ # Defaults to the client.api_version if it exists, or the OneviewSDK::Client::DEFAULT_API_VERSION.
20
31
  def initialize(client, params = {}, api_ver = nil)
21
- fail 'Must specify a valid client' unless client.is_a?(OneviewSDK::Client)
32
+ fail InvalidClient, 'Must specify a valid client' unless client.is_a?(OneviewSDK::Client)
22
33
  @client = client
23
34
  @logger = @client.logger
24
35
  @api_version = api_ver || @client.api_version
25
36
  if @api_version > @client.max_api_version
26
- fail "#{self.class.name} api_version '#{@api_version}' is greater than the client's max_api_version '#{@client.max_api_version}'"
37
+ fail UnsupportedVersion,
38
+ "#{self.class.name} api_version '#{@api_version}' is greater than the client's max_api_version '#{@client.max_api_version}'"
27
39
  end
28
40
  @data ||= {}
29
41
  set_all(params)
30
42
  end
31
43
 
32
44
  # Retrieve resource details based on this resource's name or URI.
33
- # @note Name or URI must be specified inside resource
45
+ # @note Name or URI must be specified inside the resource
34
46
  # @return [Boolean] Whether or not retrieve was successful
35
47
  def retrieve!
36
- fail 'Must set resource name or uri before trying to retrieve!' unless @data['name'] || @data['uri']
48
+ fail IncompleteResource, 'Must set resource name or uri before trying to retrieve!' unless @data['name'] || @data['uri']
37
49
  results = self.class.find_by(@client, name: @data['name']) if @data['name']
38
50
  results = self.class.find_by(@client, uri: @data['uri']) if @data['uri'] && (!results || results.empty?)
39
51
  return false unless results.size == 1
@@ -45,14 +57,14 @@ module OneviewSDK
45
57
  # @note name or uri must be specified inside resource
46
58
  # @return [Boolean] Whether or not resource exists
47
59
  def exists?
48
- fail 'Must set resource name or uri before trying to retrieve!' unless @data['name'] || @data['uri']
60
+ fail IncompleteResource, 'Must set resource name or uri before trying to retrieve!' unless @data['name'] || @data['uri']
49
61
  return true if @data['name'] && self.class.find_by(@client, name: @data['name']).size == 1
50
62
  return true if @data['uri'] && self.class.find_by(@client, uri: @data['uri']).size == 1
51
63
  false
52
64
  end
53
65
 
54
66
  # Set the given hash of key-value pairs as resource data attributes
55
- # @param [Hash, Resource] params The options for this resource (key-value pairs or Resource object)
67
+ # @param [Hash, Resource] params The options for this resource (key-value pairs or resource object)
56
68
  # @note All top-level keys will be converted to strings
57
69
  # @return [Resource] self
58
70
  def set_all(params = {})
@@ -105,15 +117,15 @@ module OneviewSDK
105
117
  end
106
118
 
107
119
  # Check equality of 2 resources. Same as ==(other)
108
- # @param [Resource] other The other resource to check equality for
120
+ # @param [Resource] other The other resource to check for equality
109
121
  # @return [Boolean] Whether or not the two objects are equal
110
122
  def eql?(other)
111
123
  self == other
112
124
  end
113
125
 
114
- # Check equality of data on other resource with that of this resource.
115
- # @note Doesn't check the client, logger, or api_version if another Resource is passed in
116
- # @param [Hash, Resource] other Resource or hash to compare key-value pairs with
126
+ # Check the equality of the data for the other resource with this resource.
127
+ # @note Does not check the client, logger, or api_version if another resource is passed in
128
+ # @param [Hash, Resource] other resource or hash to compare the key-value pairs with
117
129
  # @example Compare to hash
118
130
  # myResource = OneviewSDK::Resource.new(client, { name: 'res1', description: 'example'}, 200)
119
131
  # myResource.like?(description: '') # returns false
@@ -125,8 +137,8 @@ module OneviewSDK
125
137
 
126
138
  # Create the resource on OneView using the current data
127
139
  # @note Calls the refresh method to set additional data
128
- # @raise [RuntimeError] if the client is not set
129
- # @raise [RuntimeError] if the resource creation fails
140
+ # @raise [OneviewSDK::IncompleteResource] if the client is not set
141
+ # @raise [StandardError] if the resource creation fails
130
142
  # @return [Resource] self
131
143
  def create
132
144
  ensure_client
@@ -138,8 +150,8 @@ module OneviewSDK
138
150
 
139
151
  # Delete the resource from OneView if it exists, then create it using the current data
140
152
  # @note Calls refresh method to set additional data
141
- # @raise [RuntimeError] if the client is not set
142
- # @raise [RuntimeError] if the resource creation fails
153
+ # @raise [OneviewSDK::IncompleteResource] if the client is not set
154
+ # @raise [StandardError] if the resource creation fails
143
155
  # @return [Resource] self
144
156
  def create!
145
157
  temp = self.class.new(@client, @data)
@@ -160,8 +172,8 @@ module OneviewSDK
160
172
 
161
173
  # Set data and save to OneView
162
174
  # @param [Hash] attributes The attributes to add/change for this resource (key-value pairs)
163
- # @raise [RuntimeError] if the client or uri is not set
164
- # @raise [RuntimeError] if the resource save fails
175
+ # @raise [OneviewSDK::IncompleteResource] if the client or uri is not set
176
+ # @raise [StandardError] if the resource save fails
165
177
  # @return [Resource] self
166
178
  def update(attributes = {})
167
179
  set_all(attributes)
@@ -194,7 +206,7 @@ module OneviewSDK
194
206
  when :yml, :yaml
195
207
  File.open(file_path, 'w') { |f| f.write(temp_data.to_yaml) }
196
208
  else
197
- fail "Invalid format: #{format}"
209
+ fail InvalidFormat, "Invalid format: #{format}"
198
210
  end
199
211
  true
200
212
  end
@@ -207,7 +219,7 @@ module OneviewSDK
207
219
  end
208
220
 
209
221
  # Get resource schema
210
- # @param [Client] client
222
+ # @param [OneviewSDK::Client] client The client object for the OneView appliance
211
223
  # @return [Hash] Schema
212
224
  def self.schema(client)
213
225
  response = client.rest_get("#{self::BASE_URI}/schema", client.api_version)
@@ -218,7 +230,7 @@ module OneviewSDK
218
230
  end
219
231
 
220
232
  # Load resource from a .json or .yaml file
221
- # @param [Client] client The client object to associate this resource with
233
+ # @param [OneviewSDK::Client] client The client object for the OneView appliance
222
234
  # @param [String] file_path The full path to the file
223
235
  # @return [Resource] New resource created from the file contents
224
236
  def self.from_file(client, file_path)
@@ -226,8 +238,8 @@ module OneviewSDK
226
238
  new(client, resource['data'], resource['api_version'])
227
239
  end
228
240
 
229
- # Make a GET request to the resource uri and return an array with results matching the search
230
- # @param [Client] client
241
+ # Make a GET request to the resource uri, and returns an array with results matching the search
242
+ # @param [OneviewSDK::Client] client The client object for the OneView appliance
231
243
  # @param [Hash] attributes Hash containing the attributes name and value
232
244
  # @param [String] uri URI of the endpoint
233
245
  # @return [Array<Resource>] Results matching the search
@@ -242,35 +254,61 @@ module OneviewSDK
242
254
  temp = new(client, member)
243
255
  results.push(temp) if temp.like?(attributes)
244
256
  end
245
- break unless body['nextPageUri']
257
+ break unless body['nextPageUri'] && (body['nextPageUri'] != body['uri'])
246
258
  uri = body['nextPageUri']
247
259
  end
248
260
  results
249
261
  end
250
262
 
251
- # Make a GET request to the resource base uri and return an array with all objects of this type
263
+ # Make a GET request to the resource base uri, and returns an array with all objects of this type
252
264
  # @return [Array<Resource>] Results
253
265
  def self.get_all(client)
254
266
  find_by(client, {})
255
267
  end
256
268
 
269
+ # Builds a Query string corresponding to the parameters passed
270
+ # @param [Hash{String=>String,OneviewSDK::Resource}] query_options Query parameters and values
271
+ # to be applied to the query url.
272
+ # All key values should be Strings in snake case, the values could be Strings or Resources.
273
+ # @option query_options [String] String Values that are Strings can be associated as usual
274
+ # @option query_options [String] Resources Values that are Resources can be associated as usual,
275
+ # with keys representing only the resource names (like 'ethernet_network'). This method
276
+ # translates the SDK and Ruby standards to OneView request standard.
277
+ def self.build_query(query_options)
278
+ return '' if !query_options || query_options.empty?
279
+ query_path = '?'
280
+ query_options.each do |k, v|
281
+ words = k.to_s.split('_')
282
+ words.map!(&:capitalize!)
283
+ words[0] = words.first.downcase
284
+ new_key = words.join
285
+ v.retrieve! if v.respond_to?(:retrieve!) && !v['uri']
286
+ if v.class <= OneviewSDK::Resource
287
+ new_key = new_key.concat('Uri')
288
+ v = v['uri']
289
+ end
290
+ query_path.concat("&#{new_key}=#{v}")
291
+ end
292
+ query_path.sub('?&', '?')
293
+ end
294
+
257
295
  protected
258
296
 
259
297
  # Fail unless @client is set for this resource.
260
298
  def ensure_client
261
- fail 'Please set client attribute before interacting with this resource' unless @client
299
+ fail IncompleteResource, 'Please set client attribute before interacting with this resource' unless @client
262
300
  true
263
301
  end
264
302
 
265
303
  # Fail unless @data['uri'] is set for this resource.
266
304
  def ensure_uri
267
- fail 'Please set uri attribute before interacting with this resource' unless @data['uri']
305
+ fail IncompleteResource, 'Please set uri attribute before interacting with this resource' unless @data['uri']
268
306
  true
269
307
  end
270
308
 
271
309
  # Fail for methods that are not available for one resource
272
310
  def unavailable_method
273
- fail "The method ##{caller[0][/`.*'/][1..-2]} is unavailable for this resource"
311
+ fail MethodUnavailable, "The method ##{caller[0][/`.*'/][1..-2]} is unavailable for this resource"
274
312
  end
275
313
 
276
314
  private
@@ -1,12 +1,23 @@
1
+ # (C) Copyright 2016 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 specific
10
+ # language governing permissions and limitations under the License.
11
+
1
12
  require 'uri'
2
13
  require 'net/http'
3
14
  require 'openssl'
4
15
  require 'json'
5
16
 
6
17
  module OneviewSDK
7
- # Contains all the methods for making API REST calls
18
+ # Contains all of the methods for making API REST calls
8
19
  module Rest
9
- # Make a restful API request to OneView
20
+ # Makes a restful API request to OneView
10
21
  # @param [Symbol] type The rest method/type Options: [:get, :post, :delete, :patch, :put]
11
22
  # @param [String] path The path for the request. Usually starts with "/rest/"
12
23
  # @param [Hash] options The options for the request
@@ -14,11 +25,12 @@ module OneviewSDK
14
25
  # @option options [String] :Content-Type ('application/json') Set to nil or :none to have this option removed
15
26
  # @option options [Integer] :X-API-Version (client.api_version) API version to use for this request
16
27
  # @option options [Integer] :auth (client.token) Authentication token to use for this request
17
- # @raise [RuntimeError] if SSL validation of OneView instance's certificate failed
28
+ # @param [Integer] api_ver The api version to use when interracting with this resource
29
+ # @raise [OpenSSL::SSL::SSLError] if SSL validation of OneView instance's certificate failed
18
30
  # @return [NetHTTPResponse] Response object
19
31
  def rest_api(type, path, options = {}, api_ver = @api_version)
20
32
  @logger.debug "Making :#{type} rest call to #{@url}#{path}"
21
- fail 'Must specify path' unless path
33
+ fail InvalidRequest, 'Must specify path' unless path
22
34
 
23
35
  uri = URI.parse(URI.escape(@url + path))
24
36
  http = Net::HTTP.new(uri.host, uri.port)
@@ -27,6 +39,8 @@ module OneviewSDK
27
39
  http.cert_store = @cert_store if @cert_store
28
40
  else http.verify_mode = OpenSSL::SSL::VERIFY_NONE
29
41
  end
42
+ http.read_timeout = @timeout if @timeout # Timeout for a request
43
+ http.open_timeout = @timeout if @timeout # Timeout for a connection
30
44
 
31
45
  request = build_request(type, uri, options, api_ver)
32
46
  response = http.request(request)
@@ -42,32 +56,62 @@ module OneviewSDK
42
56
  raise e
43
57
  end
44
58
 
45
- # Make a restful GET request to OneView
46
- # Parameters & return value align with those of the {OneviewSDK::Rest::rest_api} method above
59
+ # Makes a restful GET request to OneView
60
+ # @param [String] path The path for the request. Usually starts with "/rest/"
61
+ # @param [Integer] api_ver The api version to use when interracting with this resource
62
+ # @return [NetHTTPResponse] Response object
47
63
  def rest_get(path, api_ver = @api_version)
48
64
  rest_api(:get, path, {}, api_ver)
49
65
  end
50
66
 
51
- # Make a restful POST request to OneView
52
- # Parameters & return value align with those of the {OneviewSDK::Rest::rest_api} method above
67
+ # Makes a restful POST request to OneView
68
+ # @param [String] path The path for the request. Usually starts with "/rest/"
69
+ # @param [Hash] options The options for the request
70
+ # @option options [String] :body Hash to be converted into json and set as the request body
71
+ # @option options [String] :Content-Type ('application/json') Set to nil or :none to have this option removed
72
+ # @option options [Integer] :X-API-Version (client.api_version) API version to use for this request
73
+ # @option options [Integer] :auth (client.token) Authentication token to use for this request
74
+ # @param [Integer] api_ver The api version to use when interracting with this resource
75
+ # @return [NetHTTPResponse] Response object
53
76
  def rest_post(path, options = {}, api_ver = @api_version)
54
77
  rest_api(:post, path, options, api_ver)
55
78
  end
56
79
 
57
- # Make a restful PUT request to OneView
58
- # Parameters & return value align with those of the {OneviewSDK::Rest::rest_api} method above
80
+ # Makes a restful PUT request to OneView
81
+ # @param [String] path The path for the request. Usually starts with "/rest/"
82
+ # @param [Hash] options The options for the request
83
+ # @option options [String] :body Hash to be converted into json and set as the request body
84
+ # @option options [String] :Content-Type ('application/json') Set to nil or :none to have this option removed
85
+ # @option options [Integer] :X-API-Version (client.api_version) API version to use for this request
86
+ # @option options [Integer] :auth (client.token) Authentication token to use for this request
87
+ # @param [Integer] api_ver The api version to use when interracting with this resource
88
+ # @return [NetHTTPResponse] Response object
59
89
  def rest_put(path, options = {}, api_ver = @api_version)
60
90
  rest_api(:put, path, options, api_ver)
61
91
  end
62
92
 
63
- # Make a restful PATCH request to OneView
64
- # Parameters & return value align with those of the {OneviewSDK::Rest::rest_api} method above
93
+ # Makes a restful PATCH request to OneView
94
+ # @param [String] path The path for the request. Usually starts with "/rest/"
95
+ # @param [Hash] options The options for the request
96
+ # @option options [String] :body Hash to be converted into json and set as the request body
97
+ # @option options [String] :Content-Type ('application/json') Set to nil or :none to have this option removed
98
+ # @option options [Integer] :X-API-Version (client.api_version) API version to use for this request
99
+ # @option options [Integer] :auth (client.token) Authentication token to use for this request
100
+ # @param [Integer] api_ver The api version to use when interracting with this resource
101
+ # @return [NetHTTPResponse] Response object
65
102
  def rest_patch(path, options = {}, api_ver = @api_version)
66
103
  rest_api(:patch, path, options, api_ver)
67
104
  end
68
105
 
69
- # Make a restful DELETE request to OneView
70
- # Parameters & return value align with those of the {OneviewSDK::Rest::rest_api} method above
106
+ # Makes a restful DELETE request to OneView
107
+ # @param [String] path The path for the request. Usually starts with "/rest/"
108
+ # @param [Hash] options The options for the request
109
+ # @option options [String] :body Hash to be converted into json and set as the request body
110
+ # @option options [String] :Content-Type ('application/json') Set to nil or :none to have this option removed
111
+ # @option options [Integer] :X-API-Version (client.api_version) API version to use for this request
112
+ # @option options [Integer] :auth (client.token) Authentication token to use for this request
113
+ # @param [Integer] api_ver The api version to use when interracting with this resource
114
+ # @return [NetHTTPResponse] Response object
71
115
  def rest_delete(path, options = {}, api_ver = @api_version)
72
116
  rest_api(:delete, path, options, api_ver)
73
117
  end
@@ -80,12 +124,12 @@ module OneviewSDK
80
124
  RESPONSE_CODE_UNAUTHORIZED = 401
81
125
  RESPONSE_CODE_NOT_FOUND = 404
82
126
 
83
- # Handle the response from a rest call.
127
+ # Handles the response from a rest call.
84
128
  # If an asynchronous task was started, this waits for it to complete.
85
129
  # @param [HTTPResponse] response HTTP response
86
130
  # @param [Boolean] wait_on_task Wait on task (or just return task details)
87
- # @raise [RuntimeError] if the request failed
88
- # @raise [RuntimeError] if a task was returned that did not complete successfully
131
+ # @raise [StandardError] if the request failed
132
+ # @raise [StandardError] if a task was returned that did not complete successfully
89
133
  # @return [Hash] The parsed JSON body
90
134
  def response_handler(response, wait_on_task = true)
91
135
  case response.code.to_i
@@ -101,27 +145,28 @@ module OneviewSDK
101
145
  when RESPONSE_CODE_ACCEPTED # Asynchronous add, update or delete
102
146
  return JSON.parse(response.body) unless wait_on_task
103
147
  @logger.debug "Waiting for task: response.header['location']"
104
- task = wait_for(response.header['location'])
148
+ uri = response.header['location'] || JSON.parse(response.body)['uri'] # If task uri is not returned in header
149
+ task = wait_for(uri)
105
150
  return true unless task['associatedResource'] && task['associatedResource']['resourceUri']
106
151
  resource_data = rest_get(task['associatedResource']['resourceUri'])
107
152
  return JSON.parse(resource_data.body)
108
153
  when RESPONSE_CODE_NO_CONTENT # Synchronous delete
109
154
  return {}
110
155
  when RESPONSE_CODE_BAD_REQUEST
111
- fail "400 BAD REQUEST #{response.body}"
156
+ fail BadRequest, "400 BAD REQUEST #{response.body}"
112
157
  when RESPONSE_CODE_UNAUTHORIZED
113
- fail "401 UNAUTHORIZED #{response.body}"
158
+ fail Unauthorized, "401 UNAUTHORIZED #{response.body}"
114
159
  when RESPONSE_CODE_NOT_FOUND
115
- fail "404 NOT FOUND #{response.body}"
160
+ fail NotFound, "404 NOT FOUND #{response.body}"
116
161
  else
117
- fail "#{response.code} #{response.body}"
162
+ fail RequestError, "#{response.code} #{response.body}"
118
163
  end
119
164
  end
120
165
 
121
166
 
122
167
  private
123
168
 
124
- # Build a request object using the data given
169
+ # Builds a request object using the data given
125
170
  def build_request(type, uri, options, api_ver)
126
171
  case type.downcase.to_sym
127
172
  when :get
@@ -135,7 +180,7 @@ module OneviewSDK
135
180
  when :delete
136
181
  request = Net::HTTP::Delete.new(uri.request_uri)
137
182
  else
138
- fail "Invalid rest call: #{type}"
183
+ fail InvalidRequest, "Invalid rest call: #{type}"
139
184
  end
140
185
 
141
186
  options['X-API-Version'] ||= api_ver