File Coverage

blib/lib/WebService/IdoitAPI/Object.pm
Criterion Covered Total %
statement 33 93 35.4
branch 2 28 7.1
condition 2 6 33.3
subroutine 7 12 58.3
pod 4 4 100.0
total 48 143 33.5


line stmt bran cond sub pod time code
1             package WebService::IdoitAPI::Object;
2              
3             # vim: set sw=4 ts=4 et ai si:
4              
5 2     2   335005 use warnings;
  2         6  
  2         164  
6 2     2   15 use strict;
  2         6  
  2         96  
7 2     2   15 use Carp;
  2         8  
  2         213  
8              
9 2     2   1262 use WebService::IdoitAPI;
  2         8  
  2         3244  
10              
11             our $VERSION = '0.4.6'; # VERSION
12              
13             sub new {
14 1     1 1 8 my ($class, $api, $id) = @_;
15 1         7 my $self = {
16             api => $api,
17             };
18 1 50       4 $self->{id} = $id if ( $id );
19 1         2 bless($self, $class);
20 1         3 return $self;
21             } # new()
22              
23             sub get_addresses {
24 1     1 1 2777 my ($self,$id) = @_;
25 1         3 my $addresses = {};
26              
27 1         4 my $obj_data = $self->get_information($id);
28              
29 1         3 for my $ha (@{$obj_data->{catg}->{C__CATG__IP}}) {
  1         4  
30 8         17 my $addr = $ha->{hostaddress}->{ref_title};
31             $addresses->{$addr} = {
32             active => $ha->{active}->{value},
33             net_id => $ha->{net}->{id},
34             primary => $ha->{active}->{value},
35             type => $ha->{net_type}->{title},
36             type_const => $ha->{net_type}->{const},
37             standard_gateway => $ha->{use_standard_gateway}->{value},
38 8         51 };
39 8         16 next;
40             }
41 1         5 return $addresses;
42             } # get_addresses()
43              
44             sub get_data {
45 0     0 1 0 my ($self,$id) = @_;
46              
47 0         0 my ($data,$obj_data);
48              
49 0         0 $obj_data = $self->get_information($id);
50             $data = {
51             obj => $obj_data->{obj},
52 0         0 };
53 0         0 for my $c (qw{ catg cats }) {
54 0         0 my $hc = $obj_data->{$c};
55 0         0 $data->{$c} = {};
56 0         0 for my $cc (keys %$hc) {
57 0 0       0 next if ('C__CATG__LOGBOOK' eq $cc);
58 0         0 my @acc = ();
59 0         0 for my $accr (@{$hc->{$cc}}) {
  0         0  
60 0         0 my $record = {};
61 0         0 for my $accrk (keys %$accr) {
62 0         0 my $type = ref($accr->{$accrk});
63 0 0       0 if ('' eq $type) {
    0          
    0          
64 0 0       0 if ($accr->{$accrk}) {
65 0         0 $record->{$accrk} = $accr->{$accrk};
66             }
67             }
68             elsif ('HASH' eq $type) {
69 0 0       0 if (exists $accr->{$accrk}->{title}) {
70 0         0 $record->{$accrk} = $accr->{$accrk}->{title};
71             }
72             else {
73 0         0 $record->{$accrk} = $accr->{$accrk};
74             }
75             }
76             elsif ('ARRAY' eq $type) {
77 0 0       0 if (0 < scalar @{$accr->{$accrk}}) {
  0         0  
78 0         0 $record->{$accrk} = $accr->{$accrk};
79             }
80             }
81             else {
82 0         0 warn "get_data(): can't handle type '$type'";
83             }
84 0         0 next;
85             }
86 0         0 push(@acc, $record);
87             }
88 0 0       0 if (0 < scalar @acc) {
89 0         0 $data->{$c}->{$cc} = \@acc;
90             }
91             }
92             }
93 0         0 return $data;
94             } # get_data();
95              
96             sub get_information {
97 1     1 1 3 my ($self, $id) = @_;
98 1         2 my ($api,$info,$obj,$res,$tcat);
99 1         10 $api = $self->{api};
100              
101             my $get_it = sub {
102 0     0   0 my ($wanted_id) = @_;
103 0         0 $info = {};
104 0         0 $res = _cmdb_object_read($api,$wanted_id);
105             # TODO: check for errors
106 0         0 $obj = $res->{content}->{result};
107 0         0 $info->{obj} = $obj;
108 0         0 $res = _cmdb_object_type_categories_read($api, $obj->{objecttype});
109             # TODO: check for errors
110 0         0 $tcat = $res->{content}->{result};
111             # $info->{catg} = [ map { { $_->{const} => {}, } } @{$tcat->{catg}} ];
112             # $info->{cats} = [ map { { $_->{const} => {}, } } @{$tcat->{cats}} ];
113 0         0 for my $ct ( qw( catg cats ) ) {
114 0         0 for my $c (@{$tcat->{$ct}}) {
  0         0  
115 0         0 $res = _cmdb_category_read($api, $obj->{id}, $c->{const});
116 0 0       0 if ( $res->{is_success} ) {
117 0         0 $info->{$ct}->{$c->{const}} = $res->{content}->{result};
118             }
119             else {
120 0         0 my $error = $res->{content}->{error};
121 0 0       0 if ( $error->{code} eq '-32099' ) {
122             # ignore 'Virtual category ... cannot be handled by the API.
123             }
124             else {
125 0         0 die "Error: code: $error->{code}, $error->{message}";
126             }
127             }
128             }
129             }
130 0         0 $self->{id} = $wanted_id;
131 0         0 $self->{info} = $info;
132 1         7 };
133              
134 1 50 33     11 if (defined $self->{info}
    0 33        
    0          
135             and (not defined $id
136             or $self->{id} == $id)) {
137 1         3 $info = $self->{info};
138             }
139             elsif (defined $id) {
140 0         0 $info = $get_it->($id);
141             }
142             elsif (defined $self->{id}) {
143 0         0 $info = $get_it->($self->{id});
144             }
145             else {
146 0         0 die "get_information() called without ID on an object without ID";
147             }
148 1         11 return $info;
149             } # get_information()
150              
151             sub _cmdb_category_read {
152 0     0     my ($api, $objID, $category) = @_;
153 0           my $res = $api->request( {
154             method => 'cmdb.category.read',
155             params => {
156             objID => $objID,
157             category => $category,
158             },
159             });
160 0           return $res;
161             } # _cmdb_category_read()
162              
163             sub _cmdb_object_read {
164 0     0     my ($api, $id) = @_;
165 0           my $res = $api->request( {
166             method => 'cmdb.object.read',
167             params => { id => $id },
168             });
169 0           return $res;
170             } # _cmdb_object_read()
171              
172             sub _cmdb_object_type_categories_read {
173 0     0     my ($api, $type) = @_;
174 0           my $res = $api->request( {
175             method => 'cmdb.object_type_categories.read',
176             params => { type => $type },
177             });
178 0           return $res;
179             } # _cmdb_object_type_categories_read()
180              
181             1; # Magic true value required at end of module
182             __END__