Skip to content

Commit

Permalink
Merge pull request #295 from HewlettPackard/bugfix/create_method
Browse files Browse the repository at this point in the history
Fixed #294
  • Loading branch information
fgbulsoni authored Jan 8, 2018
2 parents f8879cc + 5d8b21a commit 66fe037
Show file tree
Hide file tree
Showing 35 changed files with 357 additions and 108 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#### Bug fixes & Enhancements
- [#226](https://github.com/HewlettPackard/oneview-sdk-ruby/issues/226) Add_rack_resource method returns NOMATCHING_ETAG_MESSAGE.
- [#294](https://github.com/HewlettPackard/oneview-sdk-ruby/issues/294) Overridden methods without receiving header parameter.
- [#301](https://github.com/HewlettPackard/oneview-sdk-ruby/issues/301) Bug - Race condition when requiring oneview-sdk gem on ruby 2.4

## v5.2.0
Expand Down
5 changes: 3 additions & 2 deletions lib/oneview-sdk/resource/api200/logical_interconnect_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,13 @@ def get_settings

# Create the resource on OneView using the current data
# @note Calls the refresh method to set additional data
# @param [Hash] header The header options for the request (key-value pairs)
# @raise [OneviewSDK::IncompleteResource] if the client is not set
# @raise [StandardError] if the resource creation fails
# @return [Resource] self
def create
def create(header = {})
verify_interconnects_before_save!
super
super(DEFAULT_REQUEST_HEADER.merge(header))
end

# Set data and save to OneView
Expand Down
6 changes: 4 additions & 2 deletions lib/oneview-sdk/resource/api200/logical_switch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,18 @@ def initialize(client, params = {}, api_ver = nil)
end

# Create method
# @param [Hash] header The header options for the request (key-value pairs)
# @raise [OneviewSDK::IncompleteResource] if the client is not set
# @raise [StandardError] if the resource creation fails
# @return [OneviewSDK::LogicalSwitch] self
def create
def create(header = {})
ensure_client
request_body = {}
request_body['logicalSwitchCredentials'] = generate_logical_switch_credentials
request_body['logicalSwitch'] = @data.clone
request_body['logicalSwitch']['switchCredentialConfiguration'] = generate_logical_switch_credential_configuration
response = @client.rest_post(self.class::BASE_URI, { 'body' => request_body }, @api_version)
options = DEFAULT_REQUEST_HEADER.merge(header).merge('body' => request_body)
response = @client.rest_post(self.class::BASE_URI, options, @api_version)
body = @client.response_handler(response)
set_all(body)
self
Expand Down
6 changes: 6 additions & 0 deletions lib/oneview-sdk/resource/api200/server_hardware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ def initialize(client, params = {}, api_ver = nil)
@data['type'] ||= 'server-hardware-4'
end

# Method is not available
# @raise [OneviewSDK::MethodUnavailable] method is not available
def create!(*)
unavailable_method
end

# Retrieve resource details based on this resource's name or URI.
# @note one of the UNIQUE_IDENTIFIERS must be specified in the resource
# @return [Boolean] Whether or not retrieve was successful
Expand Down
6 changes: 6 additions & 0 deletions lib/oneview-sdk/resource/api200/storage_pool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ def create(*)
unavailable_method
end

# Method is not available
# @raise [OneviewSDK::MethodUnavailable] method is not available
def create!(*)
unavailable_method
end

# Method is not available
# @raise [OneviewSDK::MethodUnavailable] method is not available
def delete(*)
Expand Down
6 changes: 6 additions & 0 deletions lib/oneview-sdk/resource/api200/storage_system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ def create(*)
unavailable_method
end

# Method is not available
# @raise [OneviewSDK::MethodUnavailable] method is not available
def create!(*)
unavailable_method
end

# Method is not available
# @raise [OneviewSDK::MethodUnavailable] method is not available
def delete(*)
Expand Down
5 changes: 3 additions & 2 deletions lib/oneview-sdk/resource/api200/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ def initialize(client, params = {}, api_ver = nil)
# Create the resource on OneView using the current data
# @note Calls the refresh method to set additional data
# @note Removes the password attribute after creation
# @param [Hash] header The header options for the request (key-value pairs)
# @raise [OneviewSDK::IncompleteResource] if the client is not set
# @raise [StandardError] if the resource creation fails
# @return [Resource] self
def create
def create(header = {})
ensure_client
response = @client.rest_post(self.class::BASE_URI, { 'body' => @data }, @api_version)
response = @client.rest_post(BASE_URI, DEFAULT_REQUEST_HEADER.merge(header).merge('body' => @data), @api_version)
body = @client.response_handler(response)
@data.delete('password')
set_all(body)
Expand Down
38 changes: 21 additions & 17 deletions lib/oneview-sdk/resource/api200/volume.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,29 @@ class Volume < Resource

# Creates the volume
# @note provisioning parameters are required for creation, but not afterwards; after creation, they will be removed.
# @param [Hash] header The header options for the request (key-value pairs)
# @raise [OneviewSDK::IncompleteResource] if the client is not set
# @raise [StandardError] if the resource creation fails
# @return [Resource] self
def create
ensure_client
response = @client.rest_post(self.class::BASE_URI, { 'body' => @data }, @api_version)
body = @client.response_handler(response)
set_all(body)
def create(header = {})
super(DEFAULT_REQUEST_HEADER.merge(header))
@data.delete('provisioningParameters')
self
end

# Delete the resource from OneView if it exists, then create it using the current data
# @note Calls refresh method to set additional data
# @param [Hash] header The header options for the request (key-value pairs)
# @raise [OneviewSDK::IncompleteResource] if the client is not set
# @raise [StandardError] if the resource creation fails
# @return [Resource] self
def create!(header = {})
temp = self.class.new(@client, @data)
header = DEFAULT_REQUEST_HEADER.merge(header)
temp.delete(:all, header) if temp.retrieve!(header)
create(header)
end

# Update resource attributes
# @param [Hash] attributes attributes to be updated
# @return [OneviewSDK::Volume] self
Expand All @@ -49,20 +60,13 @@ def update(attributes = {})

# Deletes the resource from OneView or from Oneview and storage system
# @param [Symbol] flag Delete storage system from Oneview only or in storage system as well
# @param [Hash] header The header options for the request (key-value pairs)
# @return [true] if resource was deleted successfully
def delete(flag = :all)
def delete(flag = :all, header = {})
ensure_client && ensure_uri
case flag
when :oneview
response = @client.rest_delete(@data['uri'], { 'exportOnly' => true }, @api_version)
@client.response_handler(response)
when :all
response = @client.rest_delete(@data['uri'], {}, @api_version)
@client.response_handler(response)
else
raise InvalidResource, 'Invalid flag value, use :oneview or :all'
end
true
raise InvalidResource, 'Invalid flag value, use :oneview or :all' unless %i[oneview all].include?(flag)
header = DEFAULT_REQUEST_HEADER.merge(header).merge('exportOnly' => true) if flag == :oneview
super(header)
end

# Sets the storage system to the volume
Expand Down
17 changes: 7 additions & 10 deletions lib/oneview-sdk/resource/api200/volume_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module API200
# Volume template resource implementation
class VolumeTemplate < Resource
BASE_URI = '/rest/storage-volume-templates'.freeze
DEFAULT_REQUEST_HEADER = { 'Accept-Language' => 'en_US' }.freeze

# Create the client object, establishes connection, and set up the logging and api version.
# @param [OneviewSDK::Client] client The client object for the OneView appliance
Expand All @@ -33,23 +34,19 @@ def initialize(client, params = {}, api_ver = nil)

# Create the resource on OneView using the current data
# @note Calls refresh method to set additional data
# @param [Hash] header The header options for the request (key-value pairs)
# @raise [OneviewSDK::IncompleteResource] if the client is not set
# @raise [StandardError] if the resource creation fails
# @return [Resource] self
def create
ensure_client
response = @client.rest_post(self.class::BASE_URI, { 'Accept-Language' => 'en_US', 'body' => @data }, @api_version)
body = @client.response_handler(response)
set_all(body)
def create(header = {})
super(DEFAULT_REQUEST_HEADER.merge(header))
end

# Deletes the volume template from OneView
# @param [Hash] header The header options for the request (key-value pairs)
# @return [TrueClass] if the volume template was deleted successfully
def delete
ensure_client && ensure_uri
response = @client.rest_delete(@data['uri'], { 'Accept-Language' => 'en_US' }, @api_version)
@client.response_handler(response)
true
def delete(header = {})
super(DEFAULT_REQUEST_HEADER.merge(header))
end

# Updates the volume template from OneView
Expand Down
8 changes: 3 additions & 5 deletions lib/oneview-sdk/resource/api300/c7000/scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,11 @@ def update(attributes = {})
end

# Delete resource from OneView
# @param [Hash] header The header options for the request (key-value pairs)
# @raise [OneviewSDK::IncompleteResource] if the client or uri is not set
# @return [true] if resource was deleted successfully
def delete
ensure_client && ensure_uri
response = @client.rest_delete(@data['uri'], { 'If-Match' => @data['eTag'] }, @api_version)
@client.response_handler(response)
true
def delete(header = {})
super(DEFAULT_REQUEST_HEADER.merge(header).merge('If-Match' => @data['eTag']))
end

# Adds resource assignments
Expand Down
6 changes: 6 additions & 0 deletions lib/oneview-sdk/resource/api500/c7000/storage_pool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ def create(*)
unavailable_method
end

# Method is not available
# @raise [OneviewSDK::MethodUnavailable] method is not available
def create!(*)
unavailable_method
end

# Method is not available
# @raise [OneviewSDK::MethodUnavailable] method is not available
def delete(*)
Expand Down
31 changes: 19 additions & 12 deletions lib/oneview-sdk/resource/api500/c7000/volume.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ def set_storage_system(*)

# Creates the volume
# @note properties and templateUri parameters are required for creation, but not afterwards; after creation, they will be removed.
# @param [Hash] header The header options for the request (key-value pairs)
# @raise [OneviewSDK::IncompleteResource] if the client is not set.
# @raise [StandardError] if the resource creation fails.
# @return [Resource] self
def create
def create(header = {})
properties = Hash[@data['properties'].map { |k, v| [k.to_sym, v] }]
family = properties[:dataProtectionLevel].nil? ? 'StoreServ' : 'StoreVirtual'
template_data = {
Expand All @@ -37,7 +38,7 @@ def create
}
@data['templateUri'] = get_volume_template_uri(template_data) unless @data['templateUri']

OneviewSDK::Resource.instance_method(:create).bind(self).call
OneviewSDK::Resource.instance_method(:create).bind(self).call(DEFAULT_REQUEST_HEADER.merge(header))
@data.delete('properties')
@data.delete('templateUri')
self
Expand All @@ -54,14 +55,15 @@ def update(attributes = {})
# Deletes the resource from OneView or from Oneview and storage system
# @param [Symbol] flag Delete storage system from Oneview only or in storage system as well.
# Flags: :all = removes the volume from oneview and storage system. :oneview = removes from oneview only.
# @param [Hash] header The header options for the request (key-value pairs)
# @raise [InvalidResource] if an invalid flag is passed.
# @return [true] if resource was deleted successfully.
def delete(flag = :all)
def delete(flag = :all, header = {})
ensure_client && ensure_uri
raise InvalidResource, 'Invalid flag value, use :oneview or :all' unless %i[oneview all].include?(flag)
uri = @data['uri']
uri << '?suppressDeviceUpdates=true' if flag == :oneview
response = @client.rest_delete(uri, 'If-Match' => @data['eTag'])
response = @client.rest_delete(uri, DEFAULT_REQUEST_HEADER.merge(header).merge('If-Match' => @data['eTag']))
@client.response_handler(response)
true
end
Expand Down Expand Up @@ -158,21 +160,25 @@ def add

# Retrieve resource details based on this resource's name or URI.
# @note one of the UNIQUE_IDENTIFIERS, e.g. name or uri or properties['name'], must be specified in the resource
# @param [Hash] header The header options for the request (key-value pairs)
# @return [Boolean] Whether or not retrieve was successful
def retrieve!
return super unless @data['properties']
results = find_by_name_in_properties
def retrieve!(header = {})
header = DEFAULT_REQUEST_HEADER.merge(header)
return super(header) unless @data['properties']
results = find_by_name_in_properties(header)
return false unless results.size == 1
set_all(results.first.data)
true
end

# Check if a resource exists
# @note one of the UNIQUE_IDENTIFIERS, e.g. name or uri or properties['name'], must be specified in the resource
# @param [Hash] header The header options for the request (key-value pairs)
# @return [Boolean] Whether or not resource exists
def exists?
return super unless @data['properties']
find_by_name_in_properties.size == 1
def exists?(header = {})
header = DEFAULT_REQUEST_HEADER.merge(header)
return super(header) unless @data['properties']
find_by_name_in_properties(header).size == 1
end

private
Expand Down Expand Up @@ -201,12 +207,13 @@ def generate_snapshot_data(name, description = nil)
end

# Gets the volume
# @param [Hash] header The header options for the request (key-value pairs)
# @raise [OneviewSDK::IncompleteResource] if the name parameter is not set
# @return [Array] the array of volumes
def find_by_name_in_properties
def find_by_name_in_properties(header)
name = @data['properties']['name'] || @data['properties'][:name]
raise IncompleteResource, 'Must set resource name within the properties before trying to retrieve!' unless name
self.class.find_by(@client, 'name' => name)
self.class.find_by(@client, { 'name' => name }, BASE_URI, header)
end
end
end
Expand Down
8 changes: 3 additions & 5 deletions lib/oneview-sdk/resource/api500/c7000/volume_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ class VolumeTemplate < Resource
BASE_URI = '/rest/storage-volume-templates'.freeze

# Delete resource from OneView
# @param [Hash] header The header options for the request (key-value pairs)
# @return [true] if resource was deleted successfully
def delete
ensure_client && ensure_uri
response = @client.rest_delete(@data['uri'], { 'If-Match' => @data['eTag'] }, @api_version)
@client.response_handler(response)
true
def delete(header = {})
super(DEFAULT_REQUEST_HEADER.merge(header).merge('If-Match' => @data['eTag']))
end

# Sets the root template
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@
end
end

describe '#create!' do
it 'should retrieve, delete and create the resource' do
item = described_class.new(current_client, name: LOG_INT_GROUP4_NAME)
expect { item.create! }.not_to raise_error
expect(item.retrieve!).to eq(true)
list = described_class.find_by(current_client, name: LOG_INT_GROUP4_NAME)
expect(list.size).to eq(1)
end
end

describe '#retrieve!' do
it 'retrieves the objects' do
lig = described_class.new(current_client, name: LOG_INT_GROUP_NAME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,24 @@
end
end

describe '#create!' do
it 'creates an uplink set and a LIG with internal networks' do
lig_default_options = {
'name' => LOG_INT_GROUP2_NAME,
'redundancyType' => 'Redundant',
'interconnectBaySet' => 3
}
item = described_class.new(current_client, lig_default_options)
item.add_interconnect(3, interconnect_type)
item.add_interconnect(6, interconnect_type)
item.add_internal_network(ethernet_network)
expect { item.create! }.not_to raise_error
expect(item.retrieve!).to eq(true)
list = described_class.find_by(current_client, lig_default_options)
expect(list.size).to eq(1)
end
end

describe '#retrieve!' do
it 'retrieves the objects' do
lig_default_options = {
Expand Down
Loading

0 comments on commit 66fe037

Please sign in to comment.