typo on POD
[p5sagit/Oyster.git] / lib / Oyster / Provision / Rackspace.pm
index ed14944..fd1ce28 100644 (file)
@@ -4,37 +4,70 @@ use Net::RackSpace::CloudServers;
 use Net::RackSpace::CloudServers::Server;
 use MIME::Base64;
 
+# TODO http://failverse.com/manually-creating-a-cloud-server-from-a-cloud-files-image/
+# in order to use an already created image to build the server, a la EC2 way
+
 requires 'config';
 
-has 'api_username' => ( is => 'ro', isa => 'Str', required => 1, lazy_build => 1);
-sub _build_api_username {
+has 'api_username' => ( is => 'ro', isa => 'Str', required => 1, lazy => 1, default => sub {
+    my $self = shift;
     return $ENV{CLOUDSERVERS_USER} if exists $ENV{CLOUDSERVERS_USER};
     return $self->config->{api_username}
         or die "Need api_username or CLOUDSERVERS_USER in environment";
-}
+});
 
-has 'api_password' => ( is => 'ro', isa => 'Str', required => 1, lazy_build => 1);
-sub _build_api_password {
+has 'api_password' => ( is => 'ro', isa => 'Str', required => 1, lazy => 1, default => sub {
+    my $self = shift;
     return $ENV{CLOUDSERVERS_KEY} if exists $ENV{CLOUDSERVERS_KEY};
     return $self->config->{api_password}
         or die "Need api_password or CLOUDSERVERS_KEY in environment";
-}
+});
 
-has '_rs' => ( is => 'rw', isa => 'Net::RackSpace::CloudServers', default => sub {
+has '_rs' => ( is => 'rw', isa => 'Net::RackSpace::CloudServers', lazy => 1, default => sub {
     my $self = shift;
-    my $rs = Net::RackSpace::CloudServers->new(
-        user => $self->api_username,
-        key  => $self->api_password,
-    );
+    my $rs = eval {
+        Net::RackSpace::CloudServers->new(
+            user => $self->api_username,
+            key  => $self->api_password,
+        );
+    };
+    if ( $@ ) {
+        die
+            "Could not instantiate a backend connection to RackSpace CloudServers.\n",
+            "Check the api_username and api_password on the configuration file\n";
+    }
     $rs;
 });
 
 sub create {
    my $self = shift;
 
+   die "Rackspace Provisioning backend requires a server name\n" if !defined $self->name;
+
    # Do nothing if the server named $self->name already exists
    return if scalar grep { $_->name eq $self->name } $self->_rs->get_server();
 
+   # Validate size and image
+   {
+       die "Rackspace Provisioning backend requires a server image\n"  if !defined $self->image;
+       my @allowed_images = $self->_rs->get_image();
+       my $image_id = $self->image;
+       if ( !scalar grep { $_->{id} eq $image_id } @allowed_images ) {
+           die "Rackspace Provisioning backend requires a valid image id\nValid images:\n",
+               (map { sprintf("id %-10s -- %s\n", $_->{id}, $_->{name}) } @allowed_images),
+               "\n";
+       }
+
+       die "Rackspace Provisioning backend requires a server size\n"  if !defined $self->size;
+       my @allowed_flavors = $self->_rs->get_flavor();
+       my $flavor_id = $self->size;
+       if ( !scalar grep { $_->{id} eq $flavor_id } @allowed_flavors ) {
+           die "Rackspace Provisioning backend requires a valid size id\nValid flavors:\n",
+               (map { sprintf("id %-10s -- %s\n", $_->{id}, $_->{name}) } @allowed_flavors),
+               "\n";
+       }
+   }
+
    # Check the ssh pub key exists and is <10K
    die "SSH pubkey needs to exist" if !-f $self->pub_ssh;
    my $pub_ssh = do {
@@ -119,7 +152,7 @@ The following are required to instantiate a backend:
 The rackspace API username, or C<$ENV{RACKSPACE_USER}> will be used if that is
 not given
 
-=item password
+=item api_password
 
 This is your rackspace API Key