1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

[xenserver|compute] added VLAN model and tests

This commit is contained in:
Sergio Rubio 2013-03-26 17:16:56 +01:00
parent b70f5806dc
commit 4c3713db9e
2 changed files with 156 additions and 0 deletions

View file

@ -0,0 +1,80 @@
require 'fog/core/model'
module Fog
module Compute
class XenServer
class VLAN < Fog::Model
# API Reference here:
# @see http://docs.vmd.citrix.com/XenServer/5.6.0/1.0/en_gb/api/?c=VLAN
identity :reference
attribute :uuid
attribute :tag, :type => :integer
attribute :__untagged_pif, :aliases => :untagged_PIF
attribute :__tagged_pif, :aliases => :tagged_PIF
# @return [Fog::Compute::XenServer::PIF] interface on which traffic is tagged
#
# @see http://docs.vmd.citrix.com/XenServer/6.1.0/1.0/en_gb/api/?c=VLAN
#
def untagged_pif
service.pifs.get __untagged_pif
end
# @return [Fog::Compute::XenServer::PIF] interface on which traffic is untagged
#
# @see http://docs.vmd.citrix.com/XenServer/6.1.0/1.0/en_gb/api/?c=VLAN
#
def tagged_pif
service.pifs.get __tagged_pif
end
# Creates a new VLAN.
#
# service = Fog::Compute[:xenserver]
#
# # create a network 'foo-net'
# net = service.networks.create :name => 'foo-net'
#
# # get the eth0 physical interface where the
# # VLAN subinterface will be added
# pif = service.pifs.find { |p| p.device == 'eth0' and p.physical }
#
# Fog::Compute[:xenserver].vlans.create :tag => 123,
# :network => net,
# :pif => pif
def save
requires :tag
pif = attributes[:pif]
net = attributes[:network]
unless pif and net
raise Fog::Error.new 'save requires :pif and :network attributes'
end
ref = service.create_vlan attributes[:pif].reference,
tag,
attributes[:network].reference
data = service.get_record ref, 'VLAN'
merge_attributes data
true
end
# Destroys a VLAN.
#
# service = Fog::Compute[:xenserver]
#
# # Find VLAN 123 and destroy it
# (service.vlans.find { |v| v.tag == 123 }).destroy
#
def destroy
requires :reference
service.destroy_vlan reference
true
end
end
end
end
end

View file

@ -0,0 +1,76 @@
Shindo.tests('Fog::Compute[:xenserver] | VLAN model', ['xenserver']) do
service = Fog::Compute[:xenserver]
vlans = service.vlans
vlan = vlans.first
tests('The VLAN model should') do
tests('have the action') do
%w{ reload destroy save untagged_pif tagged_pif }.each do |action|
test(action) { vlan.respond_to? action }
end
end
tests('have attributes') do
model_attribute_hash = vlan.attributes
attributes = [
:reference,
:uuid,
:__untagged_pif,
:__tagged_pif,
:tag
]
tests("The VLAN model should respond to") do
attributes.each do |attribute|
test("#{attribute}") { vlan.respond_to? attribute }
end
end
tests("The attributes hash should have key") do
attributes.each do |attribute|
test("#{attribute}") { model_attribute_hash.has_key? attribute }
end
end
end
test('be a kind of Fog::Compute::XenServer::VLAN') { vlan.kind_of? Fog::Compute::XenServer::VLAN}
end
tests("#save") do
test 'should create a VLAN' do
@net = service.networks.create :name => 'test-net'
# try to use a bonded interface first
pif = service.pifs.find { |p| p.device == 'bond0' and p.vlan == "-1" }
unless pif
pif = compute.pifs.find { |p| p.device == 'eth0' and p.vlan == "-1" }
end
@vlan = vlans.create :tag => 1499,
:pif => pif,
:network => @net
@vlan.is_a? Fog::Compute::XenServer::VLAN
end
test 'VLAN ID should be 1449' do
@vlan.tag == 1499
end
end
tests("#destroy") do
test 'should destroy the network' do
@vlan.destroy
@net.destroy
(vlans.reload.find { |v| v.reference == @vlan.reference }).nil?
end
end
tests("#tagged_pif") do
test 'should return a PIF' do
vlans.find.first.tagged_pif.is_a? Fog::Compute::XenServer::PIF
end
end
tests("#untagged_pif") do
test 'should return a PIF' do
vlans.find.first.untagged_pif.is_a? Fog::Compute::XenServer::PIF
end
end
end