Skip to content

Commit

Permalink
Fixed bug when find volume template into volume resource
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardogpsf committed Aug 3, 2017
1 parent 1258cb1 commit ef0ffac
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 16 deletions.
36 changes: 26 additions & 10 deletions lib/oneview-sdk/resource/api500/c7000/volume.rb
Expand Up @@ -35,7 +35,8 @@ def create
isRoot: true,
family: family
}
@data['templateUri'] = OneviewSDK::API500::C7000::VolumeTemplate.find_by(@client, template_data).first['uri'] unless @data['templateUri']
@data['templateUri'] = get_volume_template_uri(template_data) unless @data['templateUri']

OneviewSDK::Resource.instance_method(:create).bind(self).call
@data.delete('properties')
@data.delete('templateUri')
Expand Down Expand Up @@ -88,19 +89,19 @@ def create_from_snapshot(snapshot_name, properties, volume_template = nil, is_pe
snapshot = get_snapshot(snapshot_name)
raise IncompleteResource, 'Snapshot not found!' unless snapshot
storage_pool_uri = nil
volume_template = if volume_template.nil?
storage_pool_uri = @data['storagePoolUri']
OneviewSDK::API500::C7000::VolumeTemplate.find_by(@client, isRoot: true, family: 'StoreServ').first
else
raise IncompleteResource, 'Volume Template not found!' unless volume_template.retrieve!
storage_pool_uri = volume_template['storagePoolUri']
volume_template
end
volume_template_uri = if volume_template.nil?
storage_pool_uri = @data['storagePoolUri']
get_volume_template_uri(isRoot: true, family: 'StoreServ')
else
raise IncompleteResource, 'Volume Template not found!' unless volume_template.retrieve!
storage_pool_uri = volume_template['storagePoolUri']
volume_template['uri']
end

data = {
'properties' => properties.merge('storagePool' => storage_pool_uri, 'snapshotPool' => storage_pool_uri),
'snapshotUri' => snapshot['uri'],
'templateUri' => volume_template['uri'],
'templateUri' => volume_template_uri,
'isPermanent' => is_permanent
}

Expand Down Expand Up @@ -128,6 +129,21 @@ def self.add(client, storage_system, volume_name, is_shareable = false, options

private

# Gets the storage volume template URI
# @param [Hash] template_data The data of storage volume template to filter the result
# @option options [Boolean] :isRoot True if storage volume template is root. False if not
# @option options [String] :family The family of storage volume template
# @return [String] the URI of storage volume template
def get_volume_template_uri(template_data)
storage_pool_uri = self['storagePoolUri'] || self['properties']['storagePool']
storage_pool = OneviewSDK::API500::C7000::StoragePool.new(@client, uri: storage_pool_uri)
raise 'StoragePool or snapshotPool must be set' unless storage_pool.retrieve!
storage_system = OneviewSDK::API500::C7000::StorageSystem.new(@client, uri: storage_pool['storageSystemUri'])
templates = storage_system.get_templates
template = templates.find { |item| item['isRoot'] == template_data[:isRoot] && item['family'] == template_data[:family] }
template['uri'] if template
end

# Generates the snapshot data
# @param [String] name The name of the snapshot
# @param [String] description The description of the snapshot
Expand Down
37 changes: 31 additions & 6 deletions spec/unit/resource/api500/c7000/volume_spec.rb
Expand Up @@ -32,8 +32,7 @@
it 'creating a volume - Store Serv' do
item = described_class.new(@client_500, properties: options)
vol_template = vol_template_class.new(@client_500, uri: '/rest/fake-template')
options_template = { isRoot: true, family: 'StoreServ' }
expect(vol_template_class).to receive(:find_by).with(@client_500, options_template).and_return([vol_template])
expect(item).to receive(:get_volume_template_uri).with(isRoot: true, family: 'StoreServ').and_return(vol_template['uri'])
data = { 'properties' => options, 'templateUri' => vol_template['uri'] }
allow_any_instance_of(OneviewSDK::Client).to receive(:rest_post)
.with(described_class::BASE_URI, { 'body' => data }, 500).and_return(fake_response)
Expand All @@ -55,8 +54,7 @@
it 'creating a volume - Store Virtual' do
item = described_class.new(@client_500, properties: options.merge(dataProtectionLevel: 'anyLevel'))
vol_template = vol_template_class.new(@client_500, uri: '/rest/fake-template')
options_template = { isRoot: true, family: 'StoreVirtual' }
expect(vol_template_class).to receive(:find_by).with(@client_500, options_template).and_return([vol_template])
expect(item).to receive(:get_volume_template_uri).with(isRoot: true, family: 'StoreVirtual').and_return(vol_template['uri'])
data = { 'properties' => options.merge(dataProtectionLevel: 'anyLevel'), 'templateUri' => vol_template['uri'] }
allow_any_instance_of(OneviewSDK::Client).to receive(:rest_post)
.with(described_class::BASE_URI, { 'body' => data }, 500).and_return(fake_response)
Expand Down Expand Up @@ -162,8 +160,7 @@

it 'creating from snapshot with root template' do
item = described_class.new(@client_500, uri: '/rest/fake', storagePoolUri: '/rest/storage-pool/fake')
options_template = { isRoot: true, family: 'StoreServ' }
expect(vol_template_class).to receive(:find_by).with(@client_500, options_template).and_return([@vol_template])
expect(item).to receive(:get_volume_template_uri).with(isRoot: true, family: 'StoreServ').and_return(@vol_template['uri'])
expect(@client_500).to receive(:rest_get).with("#{item['uri']}/snapshots", {}).and_return(@fake_response1)
allow_any_instance_of(OneviewSDK::Client).to receive(:response_handler).with(@fake_response1).and_return('members' => @snapshots)
allow_any_instance_of(OneviewSDK::Client).to receive(:rest_post)
Expand Down Expand Up @@ -240,4 +237,32 @@
@item.create_snapshot(@snapshot_options)
end
end

describe '#get_volume_template_uri' do
let(:instance_item) { described_class.new(@client_500) }

before do
storage_pool = OneviewSDK::API500::C7000::StoragePool.new(@client_500, storageSystemUri: '/storage-systems/1', uri: '/storage-pools/1')
instance_item.set_storage_pool(storage_pool)
expect(storage_pool).to receive(:retrieve!).and_return(true)
expect(OneviewSDK::API500::C7000::StoragePool).to receive(:new).and_return(storage_pool)
storage_system = OneviewSDK::API500::C7000::StorageSystem.new(@client_500)
expect(OneviewSDK::API500::C7000::StorageSystem).to receive(:new).and_return(storage_system)
templates = [
{ 'isRoot' => true, 'family' => 'StoreServ', 'uri' => '/rest/template/1' },
{ 'isRoot' => true, 'family' => 'StoreVirtual', 'uri' => '/rest/template/2' }
]
expect(storage_system).to receive(:get_templates).and_return(templates)
end

it 'should returns the URI correctly when family is StoreServ' do
parameters = { isRoot: true, family: 'StoreServ' }
expect(instance_item.send(:get_volume_template_uri, parameters)).to eq('/rest/template/1')
end

it 'should returns the URI correctly when family is StoreVirtual' do
parameters = { isRoot: true, family: 'StoreVirtual' }
expect(instance_item.send(:get_volume_template_uri, parameters)).to eq('/rest/template/2')
end
end
end

0 comments on commit ef0ffac

Please sign in to comment.