[oVirt] Added VM and Template network-interfaces crud.

This commit is contained in:
Amos Benari 2012-03-12 12:25:03 +02:00
parent 946707a264
commit 6eb1560abe
17 changed files with 291 additions and 10 deletions

View File

@ -54,7 +54,7 @@ Gem::Specification.new do |s|
s.add_development_dependency('rdoc')
s.add_development_dependency('thor')
s.add_development_dependency('rspec', '~>1.3.1')
s.add_development_dependency('rbovirt', '>=0.0.7')
s.add_development_dependency('rbovirt', '>=0.0.8')
s.add_development_dependency('shindo', '~>0.3.4')
s.add_development_dependency('virtualbox', '~>0.9.1')
s.add_development_dependency('fission')

View File

@ -13,6 +13,8 @@ module Fog
collection :templates
model :cluster
collection :clusters
model :interface
collection :interfaces
request_path 'fog/ovirt/requests/compute'
@ -28,6 +30,12 @@ module Fog
request :get_template
request :list_clusters
request :get_cluster
request :add_interface
request :destroy_interface
request :update_interface
request :list_vm_interfaces
request :list_template_interfaces
request :list_networks
module Shared
# converts an OVIRT object into an hash for fog to consume.
@ -42,6 +50,8 @@ module Fog
opts[key] = case value
when OVIRT::Link
value.id
when Array
value
when Hash
value
else
@ -54,7 +64,6 @@ module Fog
class Mock
include Shared
attr_reader :client
def initialize(options={})
require 'rbovirt'
@ -64,6 +73,7 @@ module Fog
end
private
attr_reader :client
#read mocks xml
def read_xml(file_name)
file_path = File.join(File.dirname(__FILE__),"requests","compute","mock_files",file_name)
@ -73,7 +83,7 @@ module Fog
class Real
include Shared
attr_reader :client
def initialize(options={})
require 'rbovirt'
@ -87,6 +97,9 @@ module Fog
@client = OVIRT::Client.new(username, password, url, datacenter)
end
private
attr_reader :client
end
end
end

View File

@ -1,5 +1,3 @@
require 'fog/compute/models/server'
module Fog
module Compute
class Ovirt
@ -12,7 +10,7 @@ module Fog
attribute :raw
def networks
connection.client.networks(:cluster_id => id)
connection.list_networks(id)
end
def to_s

View File

@ -0,0 +1,22 @@
module Fog
module Compute
class Ovirt
class Interface < Fog::Model
attr_accessor :raw
identity :id
attribute :name
attribute :network
attribute :interface
attribute :mac
def to_s
name
end
end
end
end
end

View File

@ -0,0 +1,32 @@
require 'fog/core/collection'
require 'fog/ovirt/models/compute/interface'
module Fog
module Compute
class Ovirt
class Interfaces < Fog::Collection
model Fog::Compute::Ovirt::Interface
attr_accessor :vm
def all(filters = {})
requires :vm
if vm.is_a? Fog::Compute::Ovirt::Server
load connection.list_vm_interfaces(vm.id)
elsif vm.is_a? Fog::Compute::Ovirt::Template
load connection.list_template_interfaces(vm.id)
else
raise 'interfaces should have vm or template'
end
end
def get(id)
new connection.get_interface(id)
end
end
end
end
end

View File

@ -24,18 +24,45 @@ module Fog
attribute :host
attribute :cluster
attribute :template
attribute :interfaces
attribute :raw
def ready?
!(status =~ /down/i)
end
def locked?
!!(status =~ /locked/i)
end
def stopped?
!!(status =~ /down/i)
end
def mac
raw.interfaces.first.mac if raw.interfaces
interfaces.first.mac unless interfaces.empty?
end
def interfaces
attributes[:interfaces] ||= id.nil? ? [] : Fog::Compute::Ovirt::Interfaces.new(
:connection => connection,
:vm => self
)
end
def add_interface attrs
wait_for { stopped? } if attrs[:blocking]
connection.add_interface(id, attrs)
end
def update_interface attrs
wait_for { stopped? } if attrs[:blocking]
connection.update_interface(id, attrs)
end
def destroy_interface attrs
wait_for { stopped? } if attrs[:blocking]
connection.destroy_interface(id, attrs)
end
def start(options = {})

View File

@ -6,6 +6,8 @@ module Fog
identity :id
attr_accessor :raw
attribute :name
attribute :description
attribute :profile
@ -17,6 +19,15 @@ module Fog
attribute :cores, :aliases => 'cpus'
attribute :memory
attribute :cluster
attribute :interfaces
def interfaces
attributes[:interfaces] ||= id.nil? ? [] : Fog::Compute::Ovirt::Interfaces.new(
:connection => connection,
:vm => self
)
end
def ready?
!(status =~ /down/i)

View File

@ -0,0 +1,23 @@
module Fog
module Compute
class Ovirt
class Real
def add_interface(id, options = {})
raise ArgumentError, "instance id is a required parameter" unless id
client.add_interface(id, options)
end
end
class Mock
def add_interface(id, options = {})
raise ArgumentError, "instance id is a required parameter" unless id
true
end
end
end
end
end

View File

@ -0,0 +1,25 @@
module Fog
module Compute
class Ovirt
class Real
def destroy_interface(id, options)
raise ArgumentError, "instance id is a required parameter" unless id
raise ArgumentError, "interface id is a required parameter for destroy-interface" unless options.has_key? :id
client.destroy_interface(id, options[:id])
end
end
class Mock
def destroy_interface(id, options)
raise ArgumentError, "instance id is a required parameter" unless id
raise ArgumentError, "interface id is a required parameter for destroy-interface" unless options.has_key? :id
true
end
end
end
end
end

View File

@ -0,0 +1,17 @@
module Fog
module Compute
class Ovirt
class Real
def list_networks(cluster_id)
client.networks(:cluster_id => cluster_id)
end
end
class Mock
def list_networks(cluster_id)
[]
end
end
end
end
end

View File

@ -0,0 +1,20 @@
module Fog
module Compute
class Ovirt
class Real
def list_template_interfaces(vm_id)
client.template_interfaces(vm_id).map {|ovirt_obj| ovirt_attrs ovirt_obj}
end
end
class Mock
def list_template_interfaces(vm_id)
xml = read_xml 'nics.xml'
Nokogiri::XML(xml).xpath('/nics/nic').collect do |nic|
ovirt_attrs OVIRT::Interface::new(self, nic)
end
end
end
end
end
end

View File

@ -0,0 +1,20 @@
module Fog
module Compute
class Ovirt
class Real
def list_vm_interfaces(vm_id)
client.vm_interfaces(vm_id).map {|ovirt_obj| ovirt_attrs ovirt_obj}
end
end
class Mock
def list_vm_interfaces(vm_id)
xml = read_xml 'nics.xml'
Nokogiri::XML(xml).xpath('/nics/nic').collect do |nic|
ovirt_attrs OVIRT::Interface::new(self, nic)
end
end
end
end
end
end

View File

@ -0,0 +1,10 @@
<nics>
<nic href="/api/vms/192cdd3f-3281-4e20-b81d-93bdc57ac387/nics/af743262-ebcf-4b18-ab5d-7218d788bdf3" id="af743262-ebcf-4b18-ab5d-7218d788bdf3">
<name>nic1</name>
<link href="/api/vms/192cdd3f-3281-4e20-b81d-93bdc57ac387/nics/af743262-ebcf-4b18-ab5d-7218d788bdf3/statistics" rel="statistics"/>
<vm href="/api/vms/192cdd3f-3281-4e20-b81d-93bdc57ac387" id="192cdd3f-3281-4e20-b81d-93bdc57ac387"/>
<network href="/api/networks/00000000-0000-0000-0000-000000000009" id="00000000-0000-0000-0000-000000000009"/>
<interface>virtio</interface>
<mac address="00:1a:4a:23:1b:8f"/>
</nic>
</nics>

View File

@ -0,0 +1,25 @@
module Fog
module Compute
class Ovirt
class Real
def update_interface(id, options)
raise ArgumentError, "instance id is a required parameter" unless id
raise ArgumentError, "interface id is a required parameter for update-interface" unless options.has_key? :id
client.update_interface(id, options)
end
end
class Mock
def update_interface(id, options)
raise ArgumentError, "instance id is a required parameter" unless id
raise ArgumentError, "interface id is a required parameter for update-interface" unless options.has_key? :id
true
end
end
end
end
end

View File

@ -3,19 +3,21 @@ Shindo.tests('Fog::Compute[:ovirt]', ['ovirt']) do
compute = Fog::Compute[:ovirt]
tests("Compute attributes") do
%w{ client }.each do |attr|
%w{ ovirt_attrs }.each do |attr|
test("it should respond to #{attr}") { compute.respond_to? attr }
end
end
tests("Compute collections") do
%w{ servers templates clusters }.each do |collection|
%w{ servers templates clusters interfaces }.each do |collection|
test("it should respond to #{collection}") { compute.respond_to? collection }
end
end
tests("Compute requests") do
%w{ datacenters storage_domains vm_action destroy_vm }.each do |collection|
%w{ add_interface create_vm datacenters destroy_interface destroy_vm get_cluster get_template
get_virtual_machine list_clusters list_networks list_template_interfaces list_templates
list_virtual_machines list_vm_interfaces storage_domains update_interface update_vm vm_action }.each do |collection|
test("it should respond to #{collection}") { compute.respond_to? collection }
end
end

View File

@ -0,0 +1,27 @@
Shindo.tests('Fog::Compute[:ovirt] | interface model', ['ovirt']) do
interfaces = Fog::Compute[:ovirt].servers.last.interfaces
interface = interfaces.last
tests('The interface model should') do
tests('have the action') do
test('reload') { interface.respond_to? 'reload' }
end
tests('have attributes') do
model_attribute_hash = interface.attributes
attributes = [ :id, :name, :network]
tests("The interface model should respond to") do
attributes.each do |attribute|
test("#{attribute}") { interface.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::Ovirt::Interface') { interface.kind_of? Fog::Compute::Ovirt::Interface }
end
end

View File

@ -0,0 +1,9 @@
Shindo.tests('Fog::Compute[:ovirt] | interfaces collection', ['ovirt']) do
interfaces = Fog::Compute[:ovirt].interfaces
tests('The interfaces collection') do
test('should be a kind of Fog::Compute::Ovirt::Interfaces') { interfaces.kind_of? Fog::Compute::Ovirt::Interfaces }
end
end