File Coverage

blib/lib/WWW/MediaTemple.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package WWW::MediaTemple;
2              
3 1     1   29297 use Carp qw(croak);
  1         3  
  1         85  
4 1     1   1818 use JSON;
  1         25334  
  1         8  
5 1     1   874 use XML::Simple;
  0            
  0            
6              
7             use base qw(REST::Client);
8              
9             our $VERSION = 0.02;
10              
11             sub new {
12             my ( $class, %args ) = @_;
13              
14             if ( !$args{api_key} ) { croak "new method requires an api key"; }
15              
16             my %parent_args = (
17             host => 'https://api.mediatemple.net/api/v1',
18             pretty_print => 'true',
19             wrap_root => 'true',
20             format => 'json',
21             raw_data => '',
22             key => ''
23             );
24              
25             # we prefer the following verbage
26             $parent_args{host} = $args{base_host} if $args{base_host};
27             $parent_args{key} = $args{api_key} if $args{api_key};
28              
29             # assign args to parent
30             while ( my ( $key, $value ) = each %args ) {
31             if ( exists $parent_args{$key} ) {
32             $parent_args{$key} = $value;
33             }
34             }
35              
36             my $self = $class->SUPER::new( { host => $parent_args{host} } );
37             bless $self, $class;
38              
39             foreach my $params ( keys %parent_args ) {
40             $self->{$params} = $parent_args{$params};
41             }
42              
43             $self->getUseragent()->agent("perl-WWW-MediaTemple/$VERSION");
44             $self->{headers} = {
45             'Content-type' => "application/$self->{format}",
46             Accept => "application/$self->{format}",
47             Authorization => "MediaTemple $self->{key}"
48             };
49              
50             $self->{params} =
51             "prettyPrint=$self->{pretty_print}&wrapRoot=$self->{wrap_root}";
52              
53             return $self;
54             }
55              
56             sub services {
57             my ($self) = @_;
58              
59             $self->GET( "/services?" . $self->{params}, $self->{headers} );
60              
61             return _dumper($self);
62             }
63              
64             sub serviceIds {
65             my ($self) = @_;
66              
67             $self->GET( "/services/ids?" . $self->{params}, $self->{headers} );
68              
69             return _dumper($self);
70             }
71              
72             sub service {
73             my ( $self, %args ) = @_;
74              
75             if ( !$args{serviceId} ) {
76             croak "service method requires a serviceId parameter";
77             }
78              
79             $self->GET( "/services/$args{serviceId}?" . $self->{params},
80             $self->{headers} );
81              
82             return _dumper($self);
83             }
84              
85             sub serviceTypes {
86             my ($self) = @_;
87              
88             $self->GET( "/services/types?" . $self->{params}, $self->{headers} );
89              
90             return _dumper($self);
91             }
92              
93             sub serviceOSTypes {
94             my ($self) = @_;
95              
96             $self->GET( '/services/types/os?' . $self->{params}, $self->{headers} );
97              
98             return _dumper($self);
99             }
100              
101             sub addService {
102             my ( $self, %args ) = @_;
103              
104             my $b_content = undef;
105              
106             # prepare xml/json
107             if ( $self->{format} eq 'xml' ) {
108             $b_content = "$args{serviceType}"
109             . "$args{primaryDomain};";
110              
111             if ( $args{operatingSystem} ) {
112             $param .=
113             "$args{operatingSystem}";
114             }
115              
116             $param .= "";
117             }
118             else {
119             $b_content = "{ \"serviceType\": $args{serviceType}, "
120             . "\"primaryDomain\": \"$args{primaryDomain};\"";
121              
122             if ( $args{operatingSystem} ) {
123             $param .= ", \"operatingSystem\": \"$args{operatingSystem}\"";
124             }
125              
126             $param .= " }";
127             }
128              
129             $self->POST( '/services' . $self->{params}, $b_content, $self->{headers} );
130              
131             return _dumper($self);
132             }
133              
134             sub reboot {
135             my ( $self, %args ) = @_;
136              
137             if ( !$args{serviceId} ) {
138             croak 'reboot method requires a serviceId parameter';
139             }
140              
141             $self->POST( "/services/$args{serviceId}/reboot?" . $self->{params},
142             '', $self->{headers} );
143              
144             return _dumper($self);
145             }
146              
147             sub flushFirewall {
148             my ( $self, %args ) = @_;
149              
150             if ( !$args{serviceId} ) {
151             croak 'flushFirewall method requires a serviceId parameter';
152             }
153              
154             $self->POST( "/services/$args{serviceId}/firewall/flush?" . $self->{params},
155             '', $self->{headers} );
156              
157             return _dumper($self);
158             }
159              
160             sub addTempDisk {
161             my ( $self, %args ) = @_;
162              
163             if ( !$args{serviceId} ) {
164             croak 'addTempDisk method requires a serviceId parameter';
165             }
166              
167             $self->POST( "/services/$args{serviceId}/disk/temp?" . $self->{params},
168             '', $self->{headers} );
169              
170             return _dumper($self);
171             }
172              
173             sub pleskPassword {
174             my ( $self, %args ) = @_;
175              
176             if ( !$args{serviceId} ) {
177             croak 'pleskPassword method requires a serviceId parameter';
178             }
179              
180             my $b_content = undef;
181              
182             if ( $self->{format} eq 'xml' ) {
183             $b_content = "$args{password}";
184             }
185             else {
186             $b_content = "{\"password\": \"$args{password}\"}";
187             }
188              
189             $self->PUT( "/services/$args{serviceId}/pleskPassword?" . $self->{params},
190             $b_content, $self->{headers} );
191              
192             return _dumper($self);
193             }
194              
195             sub setRootPass {
196             my ( $self, %args ) = @_;
197              
198             if ( !$args{serviceId} ) {
199             croak 'setRootPass method requires a serviceId parameter';
200             }
201              
202             my $b_content = undef;
203              
204             if ( $self->{format} eq 'xml' ) {
205             $b_content = "$args{password}";
206             }
207             else {
208             $b_content = "{\"password\": \"$args{password}\"}";
209             }
210              
211             $self->PUT( "/services/$args{serviceId}/rootPassword?" . $self->{params},
212             $b_content, $self->{headers} );
213              
214             return _dumper($self);
215             }
216              
217             sub stats {
218             my ( $self, %args ) = @_;
219              
220             my ( $added_param, $extra_param ) = undef;
221              
222             if ( $args{precision} ) {
223             $added_param .= "&precision=" . $arg{precision};
224             }
225             if ( $args{resolution} ) {
226             $added_param .= "&resolution=" . $arg{resolution};
227             }
228             if ( $args{start} && $arg{end} ) {
229             $added_param .= "&start=" . $arg{start} . "&end=" . $arg{end};
230             }
231             if ( $args{predefined} ) {
232             $extra_param = "/$args{predefined}";
233             }
234              
235             $self->GET(
236             "/stats/$args{serviceId}"
237             . $extra_param . "?"
238             . $self->{params}
239             . $added_param,
240             $self->{headers}
241             );
242              
243             return _dumper($self);
244             }
245              
246             sub warnings {
247             my ($self) = @_;
248              
249             $self->GET( "/stats/warnings?" . $self->{params}, $self->{headers} );
250              
251             return _dumper($self);
252             }
253              
254             sub thresholds {
255             my ($self) = @_;
256              
257             $self->GET( "/stats/warnings/thresholds?" . $self->{params},
258             $self->{headers} );
259              
260             return _dumper($self);
261             }
262              
263             sub _dumper {
264             my ($self) = @_;
265              
266             my ( $init_mod, $ret_val );
267              
268             # return object based on constructor
269             # ( xml object, json object, or raw xml/json text)
270             if ( $self->{format} eq 'xml' and !$self->{raw_data} ) {
271             $init_mod = XML::Simple->new();
272             $ret_val = $init_mod->XMLin(
273             $self->responseContent,
274             KeyAttr => [],
275             ForceArray => 0
276             );
277             }
278              
279             if ( $self->{format} eq 'json' and !$self->{raw_data} ) {
280             $ret_val = JSON->new->utf8->decode( $self->responseContent );
281             }
282              
283             if ( $self->{raw_data} ) {
284             $ret_val = $self->responseContent;
285             }
286              
287             return $ret_val;
288             }
289              
290             1;
291              
292             __END__