OpenNebula.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« OpenNebula/Template.pm ↑ Summary
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4 # vim: set ts=3 sw=3 tw=0:
5 # vim: set expandtab:
6 #
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Net::OpenNebula - Access OpenNebula RPC via Perl.
13  
14 =head1 DESCRIPTION
15  
16 With this module you can access the OpenNebula XML-RPC service.
17  
18 =head1 SYNOPSIS
19  
20 use Net::OpenNebula;
21 my $one = Net::OpenNebula->new(
22 url => "http://server:2633/RPC2",
23 user => "oneadmin",
24 password => "onepass",
25 );
26  
27 my @vms = $one->get_vms();
28  
29 =cut
30  
31 package Net::OpenNebula;
32 $Net::OpenNebula::VERSION = '0.317.0';
33 0 ▶use Net::OpenNebula::RPCClient;
Subroutine BEGIN: called CC=1 SCAR=2.6
34 push our @ISA , qw(Net::OpenNebula::RPCClient);
35  
36 use Data::Dumper;
37  
38 use Net::OpenNebula::Cluster;
39 use Net::OpenNebula::Datastore;
40 use Net::OpenNebula::Group;
41 use Net::OpenNebula::Host;
42 use Net::OpenNebula::Image;
43 use Net::OpenNebula::Template;
44 use Net::OpenNebula::User;
45 use Net::OpenNebula::VM;
46 use Net::OpenNebula::VMGroup;
47 use Net::OpenNebula::VNet;
48  
49 sub get_clusters {
50 my ($self, $nameregex) = @_;
51  
52 my $new = Net::OpenNebula::Cluster->new(rpc => $self);
53 return $new->_get_instances($nameregex);
54 }
55  
56 sub get_datastores {
57 my ($self, $nameregex) = @_;
58  
59 my $new = Net::OpenNebula::Datastore->new(rpc => $self);
60 return $new->_get_instances($nameregex);
61 }
62  
63 sub get_users {
64 my ($self, $nameregex) = @_;
65  
66 my $new = Net::OpenNebula::User->new(rpc => $self);
67 return $new->_get_instances($nameregex);
68 }
69  
70 sub get_groups {
71 my ($self, $nameregex) = @_;
72  
73 my $new = Net::OpenNebula::Group->new(rpc => $self);
74 return $new->_get_instances($nameregex);
75 }
76  
77 sub get_hosts {
78 my ($self, $nameregex) = @_;
79  
80 my $new = Net::OpenNebula::Host->new(rpc => $self);
81 return $new->_get_instances($nameregex);
82 }
83  
84 sub get_host {
85 my ($self, $id) = @_;
86  
87 if(! defined $id) {
88 my $msg = "You have to define the ID => Usage: \$obj->get_host(\$host_id)";
89  
90 $self->error($msg);
91 if( $self->{fail_on_rpc_fail}) {
92 die($msg);
93 } else {
94 return;
95 }
96 }
97  
98 my $data = $self->_rpc("one.host.info", [ int => $id ]);
99 return Net::OpenNebula::Host->new(rpc => $self, data => $data, extended_data => $data);
100 }
101  
102 sub get_vms {
103 my ($self, $nameregex) = @_;
104  
105 my $new = Net::OpenNebula::VM->new(rpc => $self);
106 return $new->_get_instances($nameregex,
107 [ int => -2 ], # always get all resources
108 [ int => -1 ], # range from (begin)
109 [ int => -1 ], # range to (end)
110 [ int => -1 ], # all states, except DONE
111 );
112 }
113  
114 sub get_vm {
115 my ($self, $id) = @_;
116  
117 if(! defined $id) {
118 my $msg = "You have to define the ID => Usage: \$obj->\$obj->get_vm(\$vm_id)";
119  
120 $self->error($msg);
121 if( $self->{fail_on_rpc_fail}) {
122 die($msg);
123 } else {
124 return;
125 }
126 }
127  
128 if($id =~ m/^\d+$/) {
129 my $data = $self->_rpc("one.vm.info", [ int => $id ]);
130 return Net::OpenNebula::VM->new(rpc => $self, data => $data, extended_data => $data);
131 }
132 else {
133 # try to find vm by name
134 my ($vm) = grep { $_->name eq $id } $self->get_vms;
135 return $vm;
136 }
137  
138 }
139  
140 sub get_vmgroups {
141 my ($self, $nameregex) = @_;
142  
143 my $new = Net::OpenNebula::VMGroup->new(rpc => $self);
144 return $new->_get_instances($nameregex,
145 [ int => -2 ], # all VM groups
146 [ int => -1 ], # range start
147 [ int => -1 ], # range end
148 );
149 }
150  
151 sub get_templates {
152 my ($self, $nameregex) = @_;
153  
154 my $new = Net::OpenNebula::Template->new(rpc => $self);
155 return $new->_get_instances($nameregex,
156 [ int => -2 ], # all templates
157 [ int => -1 ], # range start
158 [ int => -1 ], # range end
159 );
160 }
161  
162 sub get_vnets {
163 my ($self, $nameregex) = @_;
164  
165 my $new = Net::OpenNebula::VNet->new(rpc => $self);
166 return $new->_get_instances($nameregex,
167 [ int => -2 ], # all VNets
168 [ int => -1 ], # range start
169 [ int => -1 ], # range end
170 );
171 }
172  
173 sub get_images {
174 my ($self, $nameregex) = @_;
175  
176 my $new = Net::OpenNebula::Image->new(rpc => $self);
177 return $new->_get_instances($nameregex,
178 [ int => -2 ], # all templates
179 [ int => -1 ], # range start
180 [ int => -1 ], # range end
181 );
182 }
183  
184 sub create_cluster {
185 my ($self, $name) = @_;
186  
187 my $new = Net::OpenNebula::Cluster->new(rpc => $self, data => undef);
188 $new->create($name);
189  
190 return $new;
191 }
192  
193 sub create_vm {
194 my ($self, %option) = @_;
195  
196 my $template;
197  
198 if($option{template} =~ m/^\d+$/) {
199 ($template) = grep { $_->id == $option{template} } $self->get_templates;
200 }
201 else {
202 ($template) = grep { $_->name eq $option{template} } $self->get_templates;
203 }
204  
205 my $hash_ref = $template->get_template_ref;
206 $hash_ref->{TEMPLATE}->[0]->{NAME}->[0] = $option{name};
207  
208 my $s = XMLout($hash_ref, RootName => undef, NoIndent => 1 );
209  
210 my $id = $self->_rpc("one.vm.allocate", [ string => $s ]);
211  
212 if(! defined($id)) {
213 $self->error("Create vm failed");
214 return;
215 }
216  
217 return $self->get_vm($id);
218 }
219  
220 sub create_host {
221 my ($self, %option) = @_;
222  
223 my @args = (
224 "one.host.allocate",
225 [ string => $option{name} ],
226 [ string => $option{im_mad} ],
227 [ string => $option{vmm_mad} ],
228 );
229  
230 if ($self->version() < version->new('5.0.0')) {
231 push(@args, [ string => $option{vnm_mad} ]);
232 };
233 push(@args, [ int => (exists $option{cluster} ? $option{cluster} : -1) ]);
234  
235 my $id = $self->_rpc(@args);
236  
237 if(! defined($id)) {
238 $self->error("Create host failed");
239 return;
240 }
241  
242 return $self->get_host($id);
243 }
244  
245  
246 sub create_datastore {
247 my ($self, $txt) = @_;
248  
249 my $new = Net::OpenNebula::Datastore->new(rpc => $self, data => undef);
250 $new->create($txt);
251  
252 return $new;
253 }
254  
255 sub create_user {
256 my ($self, $name, $password, $driver) = @_;
257  
258 my $new = Net::OpenNebula::User->new(rpc => $self, data => undef);
259 $new->create($name, $password, $driver);
260  
261 return $new;
262 }
263  
264 sub create_group {
265 my ($self, $name) = @_;
266  
267 my $new = Net::OpenNebula::Group->new(rpc => $self, data => undef);
268 $new->create($name);
269  
270 return $new;
271 }
272  
273 sub create_template {
274 my ($self, $txt) = @_;
275  
276 my $new = Net::OpenNebula::Template->new(rpc => $self, data => undef);
277 $new->create($txt);
278  
279 return $new;
280 }
281  
282 sub create_vmgroup {
283 my ($self, $txt) = @_;
284  
285 my $new = Net::OpenNebula::VMGroup->new(rpc => $self, data => undef);
286 $new->create($txt);
287  
288 return $new;
289 }
290  
291 sub create_vnet {
292 my ($self, $txt) = @_;
293  
294 my $new = Net::OpenNebula::VNet->new(rpc => $self, data => undef);
295 $new->create($txt);
296  
297 return $new;
298 }
299  
300  
301 sub create_image {
302 my ($self, $txt, $datastore) = @_;
303  
304 my $datastoreid;
305 if($datastore =~ m/^\d+$/) {
306 $datastoreid = $datastore;
307 }
308 else {
309 my @datastores = $self->get_datastores(qr{^$datastore$});
310 $datastoreid = $datastores[0]->id if (@datastores); # take the first one
311 }
312  
313 my $new = Net::OpenNebula::Image->new(rpc => $self, data => undef);
314 $new->create($txt, $datastoreid);
315  
316 return $new;
317 }
318  
319 1;
« OpenNebula/Template.pm ↑ Summary