1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
#
# Copyright (C) 2009, 2010 Red Hat, Inc.
# Copyright (C) 2009 Daniel P. Berrange
#
# This program is free software; You can redistribute it and/or modify
# it under the GNU General Public License as published by the Free
# Software Foundation; either version 2, or (at your option) any
# later version
#
# The file "LICENSE" distributed along with this file provides full
# details of the terms and conditions
#
package Sys::Virt::TCK::NetworkBuilder;
use strict;
use warnings;
use Sys::Virt;
use IO::String;
use XML::Writer;
sub new {
my $proto = shift;
my $class = ref($proto) || $proto;
my %params = @_;
my $self = {
name => $params{name} ? $params{name} : "tck" ,
};
bless $self, $class;
return $self;
}
sub uuid {
my $self = shift;
$self->{uuid} = shift;
return $self;
}
sub bridge {
my $self = shift;
my $name = shift;
$self->{bridge} = { name => $name,
@_ };
return $self;
}
sub forward {
my $self = shift;
$self->{forward} = { @_ };
return $self;
}
sub ipaddr {
my $self = shift;
my $address = shift;
my $netmask = shift;
$self->{ipaddr} = [$address, $netmask];
return $self;
}
sub dhcp_range {
my $self = shift;
my $start = shift;
my $end = shift;
$self->{dhcp} = [] unless $self->{dhcp};
push @{$self->{dhcp}}, [$start, $end];
return $self;
}
sub as_xml {
my $self = shift;
my $data;
my $fh = IO::String->new(\$data);
my $w = XML::Writer->new(OUTPUT => $fh,
DATA_MODE => 1,
DATA_INDENT => 2);
$w->startTag("network");
foreach (qw(name uuid)) {
$w->dataElement("$_" => $self->{$_}) if $self->{$_};
}
$w->emptyTag("bridge", %{$self->{bridge}})
if $self->{bridge};
$w->emptyTag("forward", %{$self->{forward}})
if exists $self->{forward};
if ($self->{ipaddr}) {
$w->startTag("ip",
address => $self->{ipaddr}->[0],
netmask => $self->{ipaddr}->[1]);
if ($self->{dhcp}) {
$w->startTag("dhcp");
foreach my $range (@{$self->{dhcp}}) {
$w->emptyTag("range",
start => $range->[0],
end => $range->[1]);
}
$w->endTag("dhcp");
}
$w->endTag("ip");
}
$w->endTag("network");
return $data;
}
1;
|