CloudDeploy/AMIDB.pm untested

filter: Statementstmt 0.0% 0 / 103 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 138 CC 4 SCAR 13.2
CC
4
Cov
0%
find
12.0
BUILD
6.0
mongo_id
2.0
CRAP 3.7
1 package CloudDeploy::AMI {
2 use Moose;
3 use Data::Dumper;
4 has _required_props => (
5 is => 'ro',
6 isa => 'ArrayRef[Str]',
7 default => sub { [ 'Name', 'ImageId' ] },
8 traits => [ 'Array' ],
9 handles => {
10 _number_of_required_props => 'count',
11 }
12 );
13  
14 has props => (
15 is => 'ro',
16 isa => 'HashRef[Any]',
17 traits => [ 'Hash' ],
18 handles => {
19 prop => 'get',
20 }
21 );
22
23 sub mongo_id { shift->prop('_id') }
24  
25 #fancy way to build props ;)
26 around BUILDARGS => sub {
27 my ($orig,$class, $hash_ref) = @_;
28
29 return $class->$orig(props => $hash_ref);
30 };
31
32 #props assuring that props have the _required_props.
33 sub BUILD {
34 my $self = shift;
35 my @required = grep {$self->props->{"$_"}} @{$self->_required_props};
36 die "Please provide the required attributes @required\n" if (@required < @{$self->_required_props});
37 }
38 }
39  
40 package CloudDeploy::AMIDB {
41 use Moose;
42 use Carp;
43 use DateTime;
44 use JSON;
45 use CloudDeploy::Config;
46 use Data::Dumper;
47 use Carp;
48  
49 has account => (is => 'ro', isa => 'Str', default => sub { CloudDeploy::Config->new->account; });
50  
51 has mongo => (is => 'rw', default => sub { CloudDeploy::Config->new->ami_mongo });
52 # has mongolog => (is => 'rw', default => sub { CloudDeploy::Config->new->amilog_mongo });
53  
54 has log_id => (is => 'rw', isa => 'Str|Undef');
55 has mongo_id => (is => 'rw', isa => 'Str|Undef');
56 has timestamp => (is => 'rw', isa => 'Str', default => sub { DateTime->now( time_zone => 'UTC' )->iso8601 });
57
58 #Todo: Need to review, the $self->stack_info param.
59 # sub copy_to_log {
60 # my $self = shift;
61 # my $id = $self->mongolog->insert($self->stack_info, { safe => 1 });
62 # $self->log_id($id->to_string);
63 # }
64  
65 sub add {
66 my ($self, %args) = @_;
67 my $ami = CloudDeploy::AMI->new({%args});
68 my $id = $self->mongo->insert($ami->props, {safe => 1});
69 return $id;
70 }
71  
72 sub replace {
73 my ($self, %args) = @_;
74 my $ami = CloudDeploy::AMI->new({%args});
75 $self->mongo->replace_one(
76 { ImageId => $ami->prop('ImageId') },
77 $ami->props,
78 { upsert => 1 }
79 );
80 }
81  
82 sub search {
83 my ($self, %criterion) = @_;
84  
85 my @results = $self->mongo->query({%criterion})->all;
86 return map { CloudDeploy::AMI->new($_) } @results;
87 }
88  
89 sub find {
90 my ($self, %criterion) = @_;
91 my @result = $self->search(%criterion);
92 die "Can't find a UNIQUE result for that criterion" if (@result > 1);
93 die "Can't find a ANY result for that criterion" if (@result == 0);
94
95 return $result[0];
96 }
97  
98 sub delete {
99 my ($self, $find_criterion) = @_;
100 my $doc = $self->find(%$find_criterion);
101 my $ret = $self->mongo->remove({ _id => $doc->mongo_id }, { safe => 1 });
102 }
103  
104 sub _update{
105 #update unique document, if it don“t find a unique document, it dies.
106 my ($self, $find_criterion,$update_hash) = @_;
107 my $doc = $self->find(%$find_criterion);
108 $self->mongo->update({_id => $doc->{'_id'}},{'$set' => $update_hash});
109 }
110  
111 sub unset_tag_from {
112 my ($self, $tag, %criterion) = @_;
113  
114 my @log = ();
115 my @tagged = $self->search(%criterion);
116  
117 # There should only be one tagged instance, but if a tagging fails, it
118 # may leave the old instance still tagged
119 foreach my $row (@tagged) {
120 $self->mongo->update(
121 { ImageId => $row->prop('ImageId') },
122 { '$pull' => { 'Tags' => $tag } }
123 );
124 push @log, $row->prop('ImageId');
125 }
126 return \@log;
127 }
128  
129 sub set_tag_to {
130 my ($self, $tag, %criterion) = @_;
131  
132 my @log = ();
133 my @tagged = $self->search(%criterion);
134  
135 foreach my $row (@tagged) {
136 $self->mongo->update(
137 { ImageId => $row->prop('ImageId') },
138 { '$addToSet' => { 'Tags' => $tag } }
139 );
140 push @log, $row->prop('ImageId');
141 }
142 return \@log;
143 }
144 }
145 1;