| 1 | package Net::Cisco::FMC::v1::Role::ObjectMethods; | ||||||||||
| 2 | 3 | $Net::Cisco::FMC::v1::Role::ObjectMethods::VERSION = '0.009001'; | |||||||||
| 3 | # ABSTRACT: Role for Cisco Firepower Management Center (FMC) API version 1 method generation | ||||||||||
| 4 | |||||||||||
| 5 | 3 | ▶ | use 5.024; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 3 | ▶ | use feature 'signatures'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 3 | ▶ | use MooX::Role::Parameterized; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 3 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 3 | ▶ | use Clone qw( clone ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 3 | ▶ | use Moo::Role; # last for cleanup | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 3 | ▶ | no warnings "experimental::signatures"; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 3 | requires qw( _create _list _get _update _delete ); | |||||||||
| 15 | |||||||||||
| 16 | |||||||||||
| 17 | |||||||||||
| 18 | |||||||||||
| 19 | |||||||||||
| 20 | |||||||||||
| 21 | |||||||||||
| 22 | |||||||||||
| 23 | role { | ||||||||||
| 24 | 50 | ▶ | my $params = shift; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=4.1
| |||||||||||
| 25 | 50 | my $mop = shift; | |||||||||
| 26 | |||||||||||
| 27 | ▶ | $mop->method('create_' . $params->{singular} => sub ($self, $object_data) { | |||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 28 | return $self->_create(join('/', | ||||||||||
| 29 | '/api/fmc_config/v1/domain', | ||||||||||
| 30 | $self->domain_uuid, | ||||||||||
| 31 | $params->{path}, | ||||||||||
| 32 | $params->{object} | ||||||||||
| 33 | 0 | ), $object_data); | |||||||||
| 34 | 50 | }); | |||||||||
| 35 | |||||||||||
| 36 | 0 | ▶ | $mop->method('list_' . $params->{object} => sub ($self, $query_params = {}) { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 37 | return $self->_list(join('/', | ||||||||||
| 38 | '/api/fmc_config/v1/domain', | ||||||||||
| 39 | $self->domain_uuid, | ||||||||||
| 40 | $params->{path}, | ||||||||||
| 41 | $params->{object} | ||||||||||
| 42 | 0 | ), $query_params); | |||||||||
| 43 | 50 | }); | |||||||||
| 44 | |||||||||||
| 45 | 0 | ▶ | $mop->method('get_' . $params->{singular} => sub ($self, $id, $query_params = {}) { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 46 | return $self->_get(join('/', | ||||||||||
| 47 | '/api/fmc_config/v1/domain', | ||||||||||
| 48 | $self->domain_uuid, | ||||||||||
| 49 | $params->{path}, | ||||||||||
| 50 | $params->{object}, | ||||||||||
| 51 | 0 | $id | |||||||||
| 52 | ), $query_params); | ||||||||||
| 53 | 50 | }); | |||||||||
| 54 | |||||||||||
| 55 | ▶ | $mop->method('update_' . $params->{singular} => sub ($self, $object, $object_data) { | |||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 56 | 0 | my $id = $object->{id}; | |||||||||
| 57 | return $self->_update(join('/', | ||||||||||
| 58 | '/api/fmc_config/v1/domain', | ||||||||||
| 59 | $self->domain_uuid, | ||||||||||
| 60 | $params->{path}, | ||||||||||
| 61 | $params->{object}, | ||||||||||
| 62 | 0 | $id | |||||||||
| 63 | ), $object, $object_data); | ||||||||||
| 64 | 50 | }); | |||||||||
| 65 | |||||||||||
| 66 | ▶ | $mop->method('delete_' . $params->{singular} => sub ($self, $id) { | |||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 67 | return $self->_delete(join('/', | ||||||||||
| 68 | '/api/fmc_config/v1/domain', | ||||||||||
| 69 | $self->domain_uuid, | ||||||||||
| 70 | $params->{path}, | ||||||||||
| 71 | $params->{object}, | ||||||||||
| 72 | 0 | $id | |||||||||
| 73 | )); | ||||||||||
| 74 | 50 | }); | |||||||||
| 75 | |||||||||||
| 76 | 0 | ▶ | $mop->method('find_' . $params->{singular} => sub ($self, $query_params = {}) { | ||||||||
|
Subroutine
__ANON__: not called CC=8 SCAR=42.8
| |||||||||||
| 77 | 0 | my $listname = 'list_' . $params->{object}; | |||||||||
| 78 | 0 | for my $object ($self->$listname({ expanded => 'true' })->{items}->@*) { | |||||||||
| 79 | 0 | my $identical = 1; | |||||||||
| 80 | 0 | for my $key (keys $query_params->%*) { | |||||||||
| 81 | 0 | ▶ | if ( ref $query_params->{$key} eq 'Regexp') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | 0 | ▶ | if ($object->{$key} !~ $query_params->{$key}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | 0 | $identical = 0; | |||||||||
| 84 | 0 | last; | |||||||||
| 85 | } | ||||||||||
| 86 | } | ||||||||||
| 87 | else { | ||||||||||
| 88 | 0 | ▶ | if ($object->{$key} ne $query_params->{$key}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | 0 | $identical = 0; | |||||||||
| 90 | 0 | last; | |||||||||
| 91 | } | ||||||||||
| 92 | } | ||||||||||
| 93 | } | ||||||||||
| 94 | 0 | ▶ | if ($identical) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 95 | 0 | return $object; | |||||||||
| 96 | } | ||||||||||
| 97 | } | ||||||||||
| 98 | 0 | croak "object not found"; | |||||||||
| 99 | 50 | }); | |||||||||
| 100 | 3 | }; | |||||||||
| 101 | |||||||||||
| 102 | 3 | 1; | |||||||||
| 103 | |||||||||||
| 104 | __END__ | ||||||||||
| 105 | |||||||||||
| 106 | =pod | ||||||||||
| 107 | |||||||||||
| 108 | =encoding UTF-8 | ||||||||||
| 109 | |||||||||||
| 110 | =head1 NAME | ||||||||||
| 111 | |||||||||||
| 112 | Net::Cisco::FMC::v1::Role::ObjectMethods - Role for Cisco Firepower Management Center (FMC) API version 1 method generation | ||||||||||
| 113 | |||||||||||
| 114 | =head1 VERSION | ||||||||||
| 115 | |||||||||||
| 116 | version 0.009001 | ||||||||||
| 117 | |||||||||||
| 118 | =head1 SYNOPSIS | ||||||||||
| 119 | |||||||||||
| 120 | package Net::Cisco::FMC::v1; | ||||||||||
| 121 | use Moo; | ||||||||||
| 122 | use Net::Cisco::FMC::v1::Role::ObjectMethods; | ||||||||||
| 123 | |||||||||||
| 124 | Net::Cisco::FMC::v1::Role::ObjectMethods->apply([ | ||||||||||
| 125 | { | ||||||||||
| 126 | path => 'object', | ||||||||||
| 127 | object => 'portobjectgroups', | ||||||||||
| 128 | singular => 'portobjectgroup', | ||||||||||
| 129 | }, | ||||||||||
| 130 | { | ||||||||||
| 131 | path => 'object', | ||||||||||
| 132 | object => 'protocolportobjects', | ||||||||||
| 133 | singular => 'protocolportobject', | ||||||||||
| 134 | } | ||||||||||
| 135 | ]); | ||||||||||
| 136 | |||||||||||
| 137 | 1; | ||||||||||
| 138 | |||||||||||
| 139 | =head1 DESCRIPTION | ||||||||||
| 140 | |||||||||||
| 141 | This role adds methods for the REST methods of a specific object named. | ||||||||||
| 142 | |||||||||||
| 143 | =head1 METHODS | ||||||||||
| 144 | |||||||||||
| 145 | =head2 create_$singular | ||||||||||
| 146 | |||||||||||
| 147 | Takes a hashref of attributes. | ||||||||||
| 148 | |||||||||||
| 149 | Returns the created object as hashref. | ||||||||||
| 150 | |||||||||||
| 151 | Throws an exception on error. | ||||||||||
| 152 | |||||||||||
| 153 | =head2 list_$object | ||||||||||
| 154 | |||||||||||
| 155 | Takes optional query parameters. | ||||||||||
| 156 | |||||||||||
| 157 | Returns a hashref with a single key 'items' that has a list of hashrefs | ||||||||||
| 158 | similar to the FMC API. | ||||||||||
| 159 | |||||||||||
| 160 | Throws an exception on error. | ||||||||||
| 161 | |||||||||||
| 162 | As the API only allows fetching 1000 objects at a time it works around that by | ||||||||||
| 163 | making multiple API calls. | ||||||||||
| 164 | |||||||||||
| 165 | =head2 get_$singular | ||||||||||
| 166 | |||||||||||
| 167 | Takes an object id and optional query parameters. | ||||||||||
| 168 | |||||||||||
| 169 | Returns the object as hashref. | ||||||||||
| 170 | |||||||||||
| 171 | Throws an exception on error. | ||||||||||
| 172 | |||||||||||
| 173 | =head2 update_$singular | ||||||||||
| 174 | |||||||||||
| 175 | Takes an object and a hashref of attributes. | ||||||||||
| 176 | |||||||||||
| 177 | Returns the updated object as hashref. | ||||||||||
| 178 | |||||||||||
| 179 | Throws an exception on error. | ||||||||||
| 180 | |||||||||||
| 181 | =head2 delete_$singular | ||||||||||
| 182 | |||||||||||
| 183 | Takes an object id. | ||||||||||
| 184 | |||||||||||
| 185 | Returns true on success. | ||||||||||
| 186 | |||||||||||
| 187 | Throws an exception on error. | ||||||||||
| 188 | |||||||||||
| 189 | =head2 find_$singular | ||||||||||
| 190 | |||||||||||
| 191 | Takes query parameters. | ||||||||||
| 192 | |||||||||||
| 193 | Returns the object as hashref on success. | ||||||||||
| 194 | |||||||||||
| 195 | Throws an exception on error. | ||||||||||
| 196 | |||||||||||
| 197 | As there is no API for searching by all attributes this method emulates this | ||||||||||
| 198 | by fetching all objects using the L<list_$object> method and performing the | ||||||||||
| 199 | search on the client. | ||||||||||
| 200 | |||||||||||
| 201 | =head1 AUTHOR | ||||||||||
| 202 | |||||||||||
| 203 | Alexander Hartmaier <abraxxa@cpan.org> | ||||||||||
| 204 | |||||||||||
| 205 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 206 | |||||||||||
| 207 | This software is copyright (c) 2018 - 2023 by Alexander Hartmaier. | ||||||||||
| 208 | |||||||||||
| 209 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 210 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 211 | |||||||||||
| 212 | =cut | ||||||||||