Skip to content

Commit

Permalink
Merge pull request #274 from HewlettPackard/enhancement/find_volume_i…
Browse files Browse the repository at this point in the history
…n_api500

Overwriting the methods retrieve! and exists? in the volume
  • Loading branch information
aalexmonteiro committed Sep 28, 2017
2 parents 402fca6 + d2ae558 commit ceb3514
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,8 @@
## v5.0.5

#### Bug fixes & Enhancements
- [#273](https://github.com/HewlettPackard/oneview-sdk-ruby/issues/273) 'retrieve!' and 'exists?' methods return false for existing Volumes on API500

## v5.0.4

#### Bug fixes & Enhancements
Expand Down
28 changes: 28 additions & 0 deletions lib/oneview-sdk/resource/api500/c7000/volume.rb
Expand Up @@ -127,6 +127,25 @@ def self.add(client, storage_system, volume_name, is_shareable = false, options
new(client, client.response_handler(response))
end

# 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
# @return [Boolean] Whether or not retrieve was successful
def retrieve!
return super unless @data['properties']
results = find_by_name_in_properties
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
# @return [Boolean] Whether or not resource exists
def exists?
return super unless @data['properties']
find_by_name_in_properties.size == 1
end

private

# Gets the storage volume template URI
Expand All @@ -151,6 +170,15 @@ def get_volume_template_uri(template_data)
def generate_snapshot_data(name, description = nil)
{ description: description, name: name }
end

# Gets the volume
# @raise [OneviewSDK::IncompleteResource] if the name parameter is not set
# @return [Array] the array of volumes
def find_by_name_in_properties
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)
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/oneview-sdk/version.rb
Expand Up @@ -11,5 +11,5 @@

# Gem version defined here
module OneviewSDK
VERSION = '5.0.4'.freeze
VERSION = '5.0.5'.freeze
end
48 changes: 48 additions & 0 deletions spec/integration/shared_examples/volume/api500/create.rb
Expand Up @@ -206,4 +206,52 @@
expect(item['properties']['snapshotPool']).to eq(storage_pool['uri'])
end
end

describe '#retrieve!' do
it 'should call super method when properties is not set' do
item = described_class.new(current_client, name: VOLUME_NAME)
expect(item.retrieve!).to eq(true)
end

it 'should find by name when properties is set' do
item = described_class.new(current_client, properties: { name: 'volume 1' })
expect(item.retrieve!).to eq(false)
item = described_class.new(current_client, properties: { name: VOLUME_NAME })
expect(item.retrieve!).to eq(true)
end

it 'raises an exception when uri or name and property is not set' do
item = described_class.new(current_client, {})
expect { item.retrieve! }.to raise_error(OneviewSDK::IncompleteResource, /Must set resource name or uri before/)
end

it 'raises an exception name is not set and property is set' do
item = described_class.new(current_client, properties: {})
expect { item.retrieve! }.to raise_error(OneviewSDK::IncompleteResource, /Must set resource name within the properties before/)
end
end

describe '#exists?' do
it 'should call super method when properties is not set' do
item = described_class.new(current_client, name: VOLUME_NAME)
expect(item.exists?).to eq(true)
end

it 'should find by name when properties is set' do
item = described_class.new(current_client, properties: { name: 'volume 1' })
expect(item.exists?).to eq(false)
item = described_class.new(current_client, properties: { name: VOLUME_NAME })
expect(item.exists?).to eq(true)
end

it 'raises an exception when uri or name and property is not set' do
item = described_class.new(current_client, {})
expect { item.exists? } .to raise_error(OneviewSDK::IncompleteResource, /Must set resource name or uri before/)
end

it 'raises an exception name is not set and property is set' do
item = described_class.new(current_client, properties: {})
expect { item.exists? } .to raise_error(OneviewSDK::IncompleteResource, /Must set resource name within the properties before/)
end
end
end
58 changes: 58 additions & 0 deletions spec/unit/resource/api500/c7000/volume_spec.rb
Expand Up @@ -265,4 +265,62 @@
expect(instance_item.send(:get_volume_template_uri, parameters)).to eq('/rest/template/2')
end
end

describe '#retrieve!' do
let(:item_found) { [described_class.new(@client_500, name: 'volume 1', uri: '/rest/fake')] }

it 'should call super method when properties is not set' do
item = described_class.new(@client_500, name: 'volume 1')
expect(described_class).to receive(:find_by).with(@client_500, { 'name' => 'volume 1' }, anything, anything).and_return([])
expect(item.retrieve!).to eq(false)
end

it 'should find by name when properties is set' do
item = described_class.new(@client_500, properties: { name: 'volume 1' })
expect(described_class).to receive(:find_by).with(@client_500, 'name' => 'volume 1').and_return([])
expect(item.retrieve!).to eq(false)
expect(described_class).to receive(:find_by).with(@client_500, 'name' => 'volume 1').and_return(item_found)
expect(item.retrieve!).to eq(true)
expect(item['name']).to eq(item_found.first['name'])
expect(item['uri']).to eq(item_found.first['uri'])
end

it 'raises an exception when uri or name and property is not set' do
item = described_class.new(@client_500, {})
expect { item.retrieve! }.to raise_error(OneviewSDK::IncompleteResource, /Must set resource name or uri before/)
end

it 'raises an exception name is not set and property is set' do
item = described_class.new(@client_500, properties: {})
expect { item.retrieve! }.to raise_error(OneviewSDK::IncompleteResource, /Must set resource name within the properties before/)
end
end

describe '#exists?' do
let(:item_found) { [described_class.new(@client_500, name: 'volume 1', uri: '/rest/fake')] }

it 'should call super method when properties is not set' do
item = described_class.new(@client_500, name: 'volume 1')
expect(described_class).to receive(:find_by).with(@client_500, { 'name' => 'volume 1' }, anything, anything).and_return([])
expect(item.exists?).to eq(false)
end

it 'should find by name when properties is set' do
item = described_class.new(@client_500, properties: { name: 'volume 1' })
expect(described_class).to receive(:find_by).with(@client_500, 'name' => 'volume 1').and_return([])
expect(item.exists?).to eq(false)
expect(described_class).to receive(:find_by).with(@client_500, 'name' => 'volume 1').and_return(item_found)
expect(item.exists?).to eq(true)
end

it 'raises an exception when uri or name and property is not set' do
item = described_class.new(@client_500, {})
expect { item.exists? } .to raise_error(OneviewSDK::IncompleteResource, /Must set resource name or uri before/)
end

it 'raises an exception name is not set and property is set' do
item = described_class.new(@client_500, properties: {})
expect { item.exists? } .to raise_error(OneviewSDK::IncompleteResource, /Must set resource name within the properties before/)
end
end
end

0 comments on commit ceb3514

Please sign in to comment.