Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

use post instead of put to create new resources. added function to ad…

…d services to servers
  • Loading branch information...
commit 68d05e5e5f2cef4b5e2abf314a3bd27632da4b20 1 parent eb1c302
@krimdomu authored
View
8 lib/Rex/IO/CMDB.pm
@@ -16,14 +16,18 @@ sub startup {
$r->delete("/server/:name")->to("server#delete");
$r->delete("/service/:name")->to("service#delete");
- $r->put("/server")->to("server#put");
- $r->put("/service")->to("service#put");
+ $r->post("/server")->to("server#post");
+ $r->post("/service")->to("service#post");
$r->get("/server/:name")->to("server#get");
$r->get("/service/:name")->to("service#get");
$r->route("/server")->via("LIST")->to("server#list");
$r->route("/service")->via("LIST")->to("service#list");
+
+ # link service/server to server/service
+ $r->route("/service/:name")->via("LINK")->to("service#link");
+ $r->route("/server/:name")->via("LINK")->to("server#link");
}
1;
View
26 lib/Rex/IO/CMDB/Driver/CouchDB.pm
@@ -195,6 +195,32 @@ sub delete_service {
return {ok => Mojo::JSON->false};
}
+sub add_service_to_server {
+ my ($self, $server, $service) = @_;
+
+ $service = $self->get_service($service, full => 1);
+ $server = $self->get_server($server, full => 1);
+
+ if(exists $server->{service} && exists $server->{service}->{$service->{name}} ) {
+ return 304;
+ }
+
+ for my $section (keys %{ $service->{variables} }) {
+ for my $var (keys %{ $service->{variables}->{$section} } ) {
+ $server->{service}->{$service->{name}}->{$section}->{$var} = $service->{variables}->{$section}->{$var}->{default};
+ }
+ }
+
+ my $id = $server->{_id};
+ my $tx = $self->_ua->put("$couch_db/$id", { "Content-Type" => "application/json" }, $self->_json->encode($server));
+ if($tx->success) {
+ return $self->get_server($server->{name});
+ }
+ else {
+ return {ok => Mojo::JSON->false};
+ }
+}
+
sub _ua {
my ($self) = @_;
my $ua = Mojo::UserAgent->new;
View
16 lib/Rex/IO/CMDB/Server.pm
@@ -4,7 +4,7 @@ use Mojo::JSON;
use Data::Dumper;
-sub put {
+sub post {
my $self = shift;
my $ref = $self->req->json;
@@ -46,4 +46,18 @@ sub list {
$self->render_json($data);
}
+sub link {
+ my $self = shift;
+
+ my $data = $self->req->json;
+ my $status = $self->cmdb->add_service_to_server($self->stash("name"), $data->{service});
+
+ if(ref($status)) {
+ $self->render_json($status, status => 200);
+ }
+ else {
+ $self->render_json({}, status => $status);
+ }
+}
+
1;
View
2  lib/Rex/IO/CMDB/Service.pm
@@ -3,7 +3,7 @@ use Mojo::Base 'Mojolicious::Controller';
use Data::Dumper;
-sub put {
+sub post {
my $self = shift;
my $ref = $self->req->json;
Please sign in to comment.
Something went wrong with that request. Please try again.