diff --git a/lib/vmpooler/providers/base.rb b/lib/vmpooler/providers/base.rb index 579124b..23b0bfb 100644 --- a/lib/vmpooler/providers/base.rb +++ b/lib/vmpooler/providers/base.rb @@ -18,7 +18,15 @@ module Vmpooler @metrics = metrics @provider_name = name + # Ensure that there is not a nil provider configuration + @config[:providers] = {} if @config[:providers].nil? + @config[:providers][@provider_name] = {} if provider_config.nil? + + # Ensure that there is not a nil pool configuration + @config[:pools] = {} if @config[:pools].nil? + @provider_options = options + logger.log('s', "[!] Creating provider '#{name}'") end # Helper Methods @@ -41,7 +49,7 @@ module Vmpooler def provider_config @config[:providers].each do |provider| # Convert the symbol from the config into a string for comparison - return provider[1] if provider[0].to_s == @provider_name + return (provider[1].nil? ? {} : provider[1]) if provider[0].to_s == @provider_name end nil @@ -60,6 +68,16 @@ module Vmpooler @provider_name end + # returns + # Array[String] : Array of pool names this provider services + def provided_pools + list = [] + @config[:pools].each do |pool| + list << pool['name'] if pool['provider'] == name + end + list + end + # Pool Manager Methods # inputs @@ -146,8 +164,8 @@ module Vmpooler # [String] new_snapshot_name : Name of the new snapshot to create # returns # [Boolean] : true if success, false if snapshot could not be created + # Raises RuntimeError if the Pool does not exist # Raises RuntimeError if the VM does not exist - # Raises RuntimeError if the snapshot already exists def create_snapshot(_pool_name, _vm_name, _new_snapshot_name) raise("#{self.class.name} does not implement create_snapshot") end @@ -158,8 +176,9 @@ module Vmpooler # [String] snapshot_name : Name of the snapshot to restore to # returns # [Boolean] : true if success, false if snapshot could not be revertted + # Raises RuntimeError if the Pool does not exist # Raises RuntimeError if the VM does not exist - # Raises RuntimeError if the snapshot already exists + # Raises RuntimeError if the snapshot does not exist def revert_snapshot(_pool_name, _vm_name, _snapshot_name) raise("#{self.class.name} does not implement revert_snapshot") end diff --git a/spec/unit/providers/base_spec.rb b/spec/unit/providers/base_spec.rb index b9b5155..5e7feb6 100644 --- a/spec/unit/providers/base_spec.rb +++ b/spec/unit/providers/base_spec.rb @@ -84,7 +84,7 @@ EOT ) } - context 'Given a misconfigured provider name' do + context 'Given a provider with no configuration' do let(:config) { YAML.load(<<-EOT --- :providers: @@ -94,8 +94,8 @@ EOT EOT ) } - it 'should return nil' do - expect(subject.provider_config).to be_nil + it 'should return empty hash' do + expect(subject.provider_config).to eq({}) end end @@ -120,6 +120,26 @@ EOT end end + describe '#provided_pools' do + let(:config) { YAML.load(<<-EOT +--- +:pools: + - name: 'pool1' + provider: 'base' + - name: 'pool2' + provider: 'base' + - name: 'otherpool' + provider: 'other provider' + - name: 'no name' +EOT + ) + } + + it "should return pools serviced by this provider" do + expect(subject.provided_pools).to eq(['pool1','pool2']) + end + end + describe '#vms_in_pool' do it 'should raise error' do expect{subject.vms_in_pool('pool')}.to raise_error(/does not implement vms_in_pool/)