File Coverage

blib/lib/WebService/ProfitBricks.pm
Criterion Covered Total %
statement 16 18 88.8
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 22 24 91.6


line stmt bran cond sub pod time code
1             #
2             # (c) Jan Gehring
3             #
4             # vim: set ts=3 sw=3 tw=0:
5             # vim: set expandtab:
6              
7             =head1 NAME
8              
9             WebService::ProfitBricks - ProfitBricks Base Class
10              
11             =head1 DESCRIPTION
12              
13             Profitbricks API
14              
15             This is the first version of the API implementation. This is currently work-in-progress.
16              
17             With this library it is possible to provision your ProftBricks datacenter with perl. This library will connect to the SOAP webservice of ProfitBricks.
18              
19             =head1 HELP
20              
21             If you need help or want to report bugs please feel free to use our issue tracker.
22              
23             =over 4
24              
25             =item *
26              
27             http://github.com/Krimdomu/p5-webservice-profitbricks/issues
28              
29             =back
30              
31             =head1 SYNOPSIS
32              
33             use WebService::ProfitBricks qw/DataCenter Image IpBlock/;
34             WebService::ProfitBricks->auth($user, $password);
35            
36             Image->list;
37             my $dc = DataCenter->new(dataCenterName => "DC1", region => "EUROPE");
38             $dc->save;
39             $dc->wait_for_provisioning;
40            
41             my $stor1 = $dc->storage->new(size => 50, storageName => "store01", mountImageId => $use_image, profitBricksImagePassword => $root_pw);
42             $stor1->save;
43             $dc->wait_for_provisioning;
44            
45             my $srv1 = $dc->server->new(cores => 1, ram => 512, serverName => "srv01", lanId => 1, bootFromStorageId => $stor1->storageId, internetAccess => 'true');
46             $srv1->save;
47             $dc->wait_for_provisioning;
48              
49              
50             =head1 METHODS
51              
52             This class inherits from WebService::ProfitBricks::Base.
53             This is the base class for all the other ProfitBricks classes.
54              
55             =over 4
56              
57             =cut
58            
59             package WebService::ProfitBricks;
60              
61 1     1   1294 use strict;
  1         3  
  1         42  
62 1     1   6 use warnings;
  1         3  
  1         34  
63              
64 1     1   3679 use Data::Dumper;
  1         10922  
  1         81  
65 1     1   849 use WebService::ProfitBricks::Class;
  1         53  
  1         92  
66              
67 1     1   647 use WebService::ProfitBricks::Base;
  1         2  
  1         29  
68 1     1   700 use WebService::ProfitBricks::Connection;
  0            
  0            
69              
70             use base qw(WebService::ProfitBricks::Base);
71              
72             our $VERSION = "0.0.1";
73              
74             my $user;
75             my $password;
76              
77             sub construct {
78             my ($self, @data) = @_;
79              
80             $self->connection(WebService::ProfitBricks::Connection->new(user => $user, password => $password));
81              
82             if(! @data) {
83             return;
84             }
85              
86             my ($pkg_name) = [ split(/::/, ref($self)) ]->[-1];
87             my $get_data_func_name = "get$pkg_name";
88             my $get_data_func_key = lcfirst($pkg_name) . "Id";
89              
90             if(! exists $self->{__data__}->{$get_data_func_key}) {
91             return;
92             }
93              
94             # later, this should be rewritten so it will only call the soap iface
95             # if the data someone wanted to use is not present yet
96             $self->find_by_id($self->$get_data_func_key);
97              
98             return $self;
99             }
100              
101             =item find_by_id($id)
102              
103             Tries to find a thing with the given $id.
104              
105             my $server = $dc->server->find_by_id("a-b-c-d");
106              
107             =cut
108             sub find_by_id {
109             my ($self, $id) = @_;
110            
111             my ($pkg_name) = [ split(/::/, ref($self)) ]->[-1];
112             my $get_data_func_name = "get$pkg_name";
113             my $get_data_func_key = lcfirst($pkg_name) . "Id";
114              
115             my $data = $self->connection->call($get_data_func_name, $get_data_func_key => $id);
116             $self->set_data($data);
117              
118             return $self;
119             }
120              
121             =item save()
122              
123             This method created the current object at ProfitBricks. Don't call this method if you only want to update an object. Use I instead.
124              
125             my $dc = DataCenter->new(dataCenterName => "DC1", region => "EUROPE");
126             $dc->save;
127              
128             =cut
129             sub save {
130             my ($self) = @_;
131              
132             my ($pkg_name) = [ split(/::/, ref($self)) ]->[-1];
133             my $create_func_name = "create" . $pkg_name;
134              
135             my $ret_data = $self->connection->call($create_func_name, xml => $self->to_xml);
136              
137             $self->set_data($ret_data);
138             $self->update_data;
139              
140             # get and save relations
141             my @relations = $self->get_relations;
142             for my $rel (@relations) {
143             my $rel_name = pluralize($rel->{name});
144             #print "(" . ref($self) . ") finding relations through: $rel_name\n";
145             for my $child_obj ($self->$rel_name()) {
146             my $update_ref_key = lcfirst($pkg_name) . "Id";
147             $child_obj->$update_ref_key($self->$update_ref_key);
148             $child_obj->save;
149             }
150             }
151              
152             return $self;
153             }
154              
155             =item update()
156              
157             Updates an exisisting object at ProfitBricks. If you want to create a new object use the I method instead.
158              
159             my $dc = DataCenter->find_by_name("DC1");
160             $dc->dataCenterName("new_name");
161             $dc->update;
162              
163             =cut
164             sub update {
165             my ($self) = @_;
166              
167             my ($pkg_name) = [ split(/::/, ref($self)) ]->[-1];
168             my $update_func_name = "update" . $pkg_name;
169              
170             my $ret_data = $self->connection->call($update_func_name, xml => $self->to_xml);
171              
172             return $self;
173             }
174              
175             sub update_data {
176             my ($self) = @_;
177              
178             my ($pkg_name) = [ split(/::/, ref($self)) ]->[-1];
179             my $get_func_name = "get" . $pkg_name;
180             my $get_key = lcfirst($pkg_name) . "Id";
181              
182             my $ret_data = $self->connection->call($get_func_name, $get_key => $self->$get_key);
183              
184             #print Dumper($ret_data);
185              
186             $self->set_data($ret_data);
187             }
188              
189             =item delete();
190              
191             This function delete the current object.
192              
193             =cut
194             sub delete {
195             my ($self) = @_;
196              
197             my ($pkg_name) = [ split(/::/, ref($self)) ]->[-1];
198             my $delete_func_name = "delete" . $pkg_name;
199             my $delete_param_name = lcfirst($pkg_name) . "Id";
200              
201             my $ret_data = $self->connection->call($delete_func_name, $delete_param_name => $self->$delete_param_name);
202              
203             return 1;
204             }
205              
206             =item auth($user, $password)
207              
208             Sets the authentication.
209              
210             =cut
211             sub auth {
212             my ($class, $_user, $pass) = @_;
213              
214             $user = $_user;
215             $password = $pass;
216             }
217              
218             sub import {
219             my ($class, @names) = @_;
220              
221             my ($caller_pkg) = caller;
222              
223             no strict 'refs';
224              
225             for my $name (@names) {
226            
227             *{ $caller_pkg . "::" . $name } = sub {
228             my $pkg = __PACKAGE__ . "::$name";
229             eval "use $pkg";
230             if($@) {
231             die($@);
232             }
233              
234             shift;
235             return $pkg->new(@_);
236             };
237              
238             }
239              
240             }
241              
242             =back
243              
244             =cut
245              
246             "For the Horde!";