-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmdb.pl
More file actions
executable file
·99 lines (87 loc) · 2.39 KB
/
Copy pathcmdb.pl
File metadata and controls
executable file
·99 lines (87 loc) · 2.39 KB
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
#!/usr/bin/perl
use strict;
use FindBin::libs qw( base=moosex-storage-dbi-pgdoc subdir=lib subonly );
use FindBin::libs;
use CMDB::BaseCI;
use CMDB::Schema;
use DBI;
use Data::Dumper;
use JSON;
use Mojolicious::Lite;
use Moose;
my $dbh = DBI->connect_cached(
'dbi:Pg:db=cmdb;host=localhost;port=5432',
'cmdb',
'cmdb',
);
die $DBI::errstr unless $dbh;
helper db => sub { $dbh };
# Set up the data
app->log->info("Schema: loading");
my $cmdb_schema = CMDB::Schema->new({ files => [ glob "$FindBin::Bin/etc/*.json" ] });
app->log->info("Schema: loaded");
app->log->info("Setting up routes");
get '/cmdb/schema' => sub {
my $c = shift;
my $pointer = $c->cim;
for my $key (split /:/, $c->req->param('path')) {
$pointer = $pointer->{$key};
}
if (ref $pointer eq 'HASH') {
$c->render(text => join q{<br/>}, Dumper keys %{$pointer});
}
else {
$c->render(text => join q{<br/>}, Dumper $pointer);
}
};
get '/cmdb/get_properties' => sub {
my $c = shift;
my $class = $c->req->param('class');
$c->render(
template => 'properties',
metaclass => eval { "$class"->meta },
);
};
get '/cmdb/:uuid' => { uuid => undef } => sub {
my $c = shift;
if (defined $c->param('uuid')) {
my $ci = eval { CMDB::BaseCI->load( $c->db, encode_json( { uuid => $c->param('uuid') } ) ) };
if ($@) {
app->log->error($@);
$c->render(
text => 'There was an error, please see application log',
);
}
else {
$c->render(
template => 'ci',
ci => $ci,
);
}
}
else {
$c->render( template => 'index', classes => [ keys %{ $cmdb_schema->schema->{classes} } ], uuids => $c->db->selectall_arrayref(q{select uuid from data}) );
}
};
post '/cmdb/:uuid' => { uuid => undef } => sub {
my $c = shift;
my %params = %{$c->req->params->to_hash};
app->log->debug(Dumper \%params);
my $class = delete $params{_class};
my %init = ();
for my $key (keys %params) {
if ($params{$key} ne q{}) {
$init{$key} = $params{$key};
}
}
my $obj = eval "$class->new(%init)";
if ($@) {
$c->render( text => $@ );
}
else {
$obj->store($c->db);
$c->redirect_to('/cmdb/'.$obj->uuid);
}
};
app->log->info("Starting main loop");
app->start;