From bf35080b8125dc2b0887a5bb826c81e2191501bd Mon Sep 17 00:00:00 2001 From: Sergio Rubio Date: Tue, 26 Mar 2013 17:29:17 +0100 Subject: [PATCH] [xenserver|compute] Added Vlans collection and tests --- lib/fog/xenserver/models/compute/vlans.rb | 39 +++++++++++++++++++ tests/xenserver/models/compute/vlans_tests.rb | 36 +++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 lib/fog/xenserver/models/compute/vlans.rb create mode 100644 tests/xenserver/models/compute/vlans_tests.rb diff --git a/lib/fog/xenserver/models/compute/vlans.rb b/lib/fog/xenserver/models/compute/vlans.rb new file mode 100644 index 000000000..fba9fbb1a --- /dev/null +++ b/lib/fog/xenserver/models/compute/vlans.rb @@ -0,0 +1,39 @@ +require 'fog/core/collection' +require 'fog/xenserver/models/compute/vlan' + +module Fog + module Compute + class XenServer + + class Vlans < Fog::Collection + + model Fog::Compute::XenServer::VLAN + + # Return the list of VLANs available + # + # @return [Array] a list of Fog::Compute::XenServer::VLAN + # + def all(options = {}) + data = service.get_records 'VLAN' + load(data) + end + + # Retrieve a VLAN object by by reference. + # + # @return [Fog::Compute::XenServer::VLAN] + # + # Returns nil if not found + # + def get( ref ) + if ref && obj = service.get_record( ref, 'VLAN' ) + new(obj) + end + rescue Fog::XenServer::NotFound + nil + end + + end + + end + end +end diff --git a/tests/xenserver/models/compute/vlans_tests.rb b/tests/xenserver/models/compute/vlans_tests.rb new file mode 100644 index 000000000..3050693c7 --- /dev/null +++ b/tests/xenserver/models/compute/vlans_tests.rb @@ -0,0 +1,36 @@ +Shindo.tests('Fog::Compute[:xenserver] | Vlans collection', ['xenserver']) do + + service = Fog::Compute[:xenserver] + + tests('The Vlans collection') do + + test('should not be empty') { !service.vlans.empty? } + + test('should be a kind of Fog::Compute::XenServer::Vlans') do + service.vlans.kind_of? Fog::Compute::XenServer::Vlans + end + + tests('should be able to reload itself').succeeds { service.vlans.reload } + + tests('should be able to get a model') do + tests('by reference').succeeds { + service.vlans.get(service.vlans.first.reference).is_a? \ + Fog::Compute::XenServer::VLAN + } + end + + end + + tests('failures') do + test 'with an invalid reference' do + raises = false + begin + service.vlans.get('OpaqueRef:foo') + rescue Fog::XenServer::RequestFailed => e + raises = true if e.message =~ /HANDLE_INVALID/ + end + raises + end + end + +end