/
Server.pm
286 lines (214 loc) · 8.52 KB
/
Server.pm
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package Net::RackSpace::CloudServers::Server;
use warnings;
use strict;
our $DEBUG = 0;
use Moose;
use MooseX::StrictConstructor;
use HTTP::Request;
use JSON;
use YAML;
has 'cloudservers' => ( is => 'rw', isa => 'Net::RackSpace::CloudServers', required => 1 );
has 'id' => ( is => 'ro', isa => 'Int', required => 1 );
has 'name' => ( is => 'ro', isa => 'Str', required => 1 );
has 'imageid' => ( is => 'ro', isa => 'Maybe[Int]', required => 1 );
has 'flavorid' => ( is => 'ro', isa => 'Maybe[Int]', required => 1 );
has 'hostid' => ( is => 'ro', isa => 'Maybe[Str]', required => 1 );
has 'status' => ( is => 'ro', isa => 'Maybe[Str]', required => 1 );
has 'adminpass' => ( is => 'ro', isa => 'Maybe[Str]', required => 1 );
has 'progress' => ( is => 'ro', isa => 'Maybe[Str]', required => 1 );
has 'public_address' => ( is => 'ro', isa => 'Maybe[ArrayRef[Str]]', required => 1 );
has 'private_address' => ( is => 'ro', isa => 'Maybe[ArrayRef[Str]]', required => 1 );
has 'metadata' => ( is => 'ro', isa => 'Maybe[HashRef]', required => 1 );
no Moose;
__PACKAGE__->meta->make_immutable();
sub change_root_password {
my $self = shift;
my $password = shift;
my $uri = '/servers/' . $self->id;
my $request = HTTP::Request->new(
'PUT',
$self->cloudservers->server_management_url . $uri,
[
'X-Auth-Token' => $self->cloudservers->token,
'Content-Type' => 'application/json',
],
to_json( { server => { adminPass => $password, } } )
);
my $response = $self->cloudservers->_request($request);
confess 'Unknown error' if $response->code != 202;
return $response;
}
sub change_name {
my $self = shift;
my $name = shift;
my $uri = '/servers/' . $self->id;
my $request = HTTP::Request->new(
'PUT',
$self->cloudservers->server_management_url . $uri,
[
'X-Auth-Token' => $self->cloudservers->token,
'Content-Type' => 'application/json',
],
to_json( { server => { name => $name, } } )
);
my $response = $self->cloudservers->_request($request);
confess 'Unknown error' if $response->code != 202;
return $response;
}
sub delete_server {
my $self = shift;
my $request = HTTP::Request->new(
'DELETE',
$self->cloudservers->server_management_url . '/servers/' . $self->id,
[
'X-Auth-Token' => $self->cloudservers->token,
'Content-Type' => 'application/json',
],
);
my $response = $self->cloudservers->_request($request);
confess 'Unknown error' if $response->code != 202;
return;
}
sub create_server {
my $self = shift;
my $request = HTTP::Request->new(
'POST',
$self->cloudservers->server_management_url . '/servers',
[
'X-Auth-Token' => $self->cloudservers->token,
'Content-Type' => 'application/json',
],
to_json(
{
server => {
name => $self->name,
imageId => int $self->imageid,
flavorId => int $self->flavorid,
}
}
)
);
my $response = $self->cloudservers->_request($request);
confess 'Unknown error' if $response->code != 202;
my $hash_response = from_json( $response->content );
warn Dump($hash_response) if $DEBUG;
confess 'response does not contain key "server"'
if ( !defined $hash_response->{server} );
confess 'response does not contain hashref of "server"'
if ( ref $hash_response->{server} ne 'HASH' );
my $hserver = $hash_response->{server};
return __PACKAGE__->new(
cloudservers => $self->cloudservers,
adminpass => $hserver->{adminPass},
id => $hserver->{id},
name => $hserver->{name},
imageid => $hserver->{imageId},
flavorid => $hserver->{flavorId},
hostid => $hserver->{hostId},
status => $hserver->{status},
progress => $hserver->{progress},
public_address => $hserver->{addresses}->{public},
private_address => $hserver->{addresses}->{private},
metadata => $hserver->{metadata},
);
}
=head1 NAME
Net::RackSpace::CloudServers::Server - a RackSpace CloudServers Server instance
=head1 SYNOPSIS
use Net::RackSpace::CloudServers;
use Net::RackSpace::CloudServers::Server;
my $cs = Net::RackSpace::CloudServers->new( user => 'myusername', key => 'mysecretkey' );
my $server = Net::RackSpace::CloudServers::Server->new(
cloudservers => $cs,
id => '1', name => 'test',
);
# get list:
my @servers = $cs->get_server;
foreach my $server ( @servers ) {
print 'Have server ', $server->name, ' id ', $server->id, "\n";
}
# get detailed list
my @servers = $cs->get_server_detail();
foreach my $server ( @servers) {
print 'Have server ', $server->name, ' id ', $server->id,
# ...
"\n";
}
## Create server from template
my $tmp = Net::Rackspace::CloudServer::Server->new(
cloudservers => $cs, name => 'myserver',
flavor => 2, image => 8,
# others
);
my $srv = $tmp->create_server;
print "root pass: ", $srv->adminpass, " IP: @{$srv->public_address}\n";
=head1 METHODS
=head2 new / BUILD
The constructor creates a Server object, see L<create_server> to create a server instance from a template:
my $server = Net::RackSpace::CloudServers::Server->new(
cloudserver => $cs
id => 'id', name => 'name',
);
This normally gets created for you by L<Net::RackSpace::Cloudserver>'s L<get_server> or L<get_server_detail> methods.
Needs a Net::RackSpace::CloudServers object as B<cloudserver> parameter.
=head2 create_server
This creates a real server based on a Server template object (TODO: will accept all the other build parameters).
=head2 delete_server
This will ask RackSpace to delete the cloud server instance specified in this object's ID from the system.
This operation is irreversible. Please notice that all images created from this server (if any) will also
be removed. This method doesn't return anything on success, and C<confess()>es on failure.
=head2 change_name
Changes the server's name to the new value given. Dies on error, or returns the response
$srv->change_name('newname');
=head2 change_root_password
Changes the server's root password to the new value given. Dies on error, or returns the response
$srv->change_root_password('toor');
=head1 ATTRIBUTES
=head2 id
The id is used for the creation of new cloudservers
=head2 name
The name which identifies the server
=head2 adminpass
When newly built ONLY, the automatically generated password for root
=head2 imageid
The ID of the L<Net::RackSpace::CloudServer::Image> from which the server has been created
=head2 flavorid
The ID of the L<Net::RackSpace::CloudServer::Flavor> the server is currently running as
=head2 hostid
An ID which univocally identifies a server on your account. May not be unique across accounts.
=head2 status
The status of the server: building, etc
=head2 progress
The progress of the current B<status> operation: 60%, etc.
=head2 public_address
Arrayref containing the list of public addresses the server is configured to use
=head2 private_address
Arrayref containing the list of private addresses the server is configured to use
=head2 metadata
Hashref containing any metadata that has been set for the server
=head1 AUTHOR
Marco Fontani, C<< <mfontani at cpan.org> >>
=head1 BUGS
Please report any bugs or feature requests to C<bug-net-rackspace-cloudservers at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-RackSpace-CloudServers>. I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.
=head1 SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Net::RackSpace::CloudServers::Server
You can also look for information at:
=over 4
=item * RT: CPAN's request tracker
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-RackSpace-CloudServers>
=item * AnnoCPAN: Annotated CPAN documentation
L<http://annocpan.org/dist/Net-RackSpace-CloudServers>
=item * CPAN Ratings
L<http://cpanratings.perl.org/d/Net-RackSpace-CloudServers>
=item * Search CPAN
L<http://search.cpan.org/dist/Net-RackSpace-CloudServers/>
=back
=head1 COPYRIGHT & LICENSE
Copyright 2009 Marco Fontani, all rights reserved.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=cut
1; # End of Net::RackSpace::CloudServers::Server