diff --git a/lib/NGCP/Panel/Controller/API/CCMapEntries.pm b/lib/NGCP/Panel/Controller/API/CCMapEntries.pm new file mode 100644 index 0000000000..fe204d179c --- /dev/null +++ b/lib/NGCP/Panel/Controller/API/CCMapEntries.pm @@ -0,0 +1,137 @@ +package NGCP::Panel::Controller::API::CCMapEntries; +use Sipwise::Base; +use namespace::sweep; +use boolean qw(true); +use Data::HAL qw(); +use Data::HAL::Link qw(); +use HTTP::Headers qw(); +use HTTP::Status qw(:constants); +use MooseX::ClassAttribute qw(class_has); +use NGCP::Panel::Utils::DateTime; +use Path::Tiny qw(path); +use Safe::Isa qw($_isa); +BEGIN { extends 'Catalyst::Controller::ActionRole'; } +require Catalyst::ActionRole::ACL; +require Catalyst::ActionRole::CheckTrailingSlash; +require Catalyst::ActionRole::HTTPMethods; +require Catalyst::ActionRole::RequireSSL; + +class_has 'api_description' => ( + is => 'ro', + isa => 'Str', + default => + 'Creates a calling card mapping entry. For call through, it uses the UUID of the subscriber to attach allowed '. + 'CLIs able to perform a call through.', +); + +class_has 'query_params' => ( + is => 'ro', + isa => 'ArrayRef', + default => sub {[ + ]}, +); + +with 'NGCP::Panel::Role::API::CCMapEntries'; + +class_has('resource_name', is => 'ro', default => 'ccmapentries'); +class_has('dispatch_path', is => 'ro', default => '/api/ccmapentries/'); +class_has('relation', is => 'ro', default => 'http://purl.org/sipwise/ngcp-api/#rel-ccmapentries'); + +__PACKAGE__->config( + action => { + map { $_ => { + ACLDetachTo => '/api/root/invalid_user', + AllowedRole => 'admin', + Args => 0, + Does => [qw(ACL CheckTrailingSlash RequireSSL)], + Method => $_, + Path => __PACKAGE__->dispatch_path, + } } @{ __PACKAGE__->allowed_methods }, + }, + action_roles => [qw(HTTPMethods)], +); + +sub auto :Private { + my ($self, $c) = @_; + + $self->set_body($c); + $self->log_request($c); + return 1; +} + +sub GET :Allow { + my ($self, $c) = @_; + my $page = $c->request->params->{page} // 1; + my $rows = $c->request->params->{rows} // 10; + { + my $cfs = $self->item_rs($c); + (my $total_count, $cfs) = $self->paginate_order_collection($c, $cfs); + my (@embedded, @links); + for my $cf ($cfs->all) { + try { + push @embedded, $self->hal_from_item($c, $cf, "callforwards"); + push @links, Data::HAL::Link->new( + relation => 'ngcp:'.$self->resource_name, + href => sprintf('%s%s', $self->dispatch_path, $cf->id), + ); + } + } + push @links, + Data::HAL::Link->new( + relation => 'curies', + href => 'http://purl.org/sipwise/ngcp-api/#rel-{rel}', + name => 'ngcp', + templated => true, + ), + Data::HAL::Link->new(relation => 'profile', href => 'http://purl.org/sipwise/ngcp-api/'), + Data::HAL::Link->new(relation => 'self', href => sprintf('%s?page=%s&rows=%s', $self->dispatch_path, $page, $rows)); + if(($total_count / $rows) > $page ) { + push @links, Data::HAL::Link->new(relation => 'next', href => sprintf('%s?page=%d&rows=%d', $self->dispatch_path, $page + 1, $rows)); + } + if($page > 1) { + push @links, Data::HAL::Link->new(relation => 'prev', href => sprintf('%s?page=%d&rows=%d', $self->dispatch_path, $page - 1, $rows)); + } + + my $hal = Data::HAL->new( + embedded => [@embedded], + links => [@links], + ); + $hal->resource({ + total_count => $total_count, + }); + my $response = HTTP::Response->new(HTTP_OK, undef, + HTTP::Headers->new($hal->http_headers(skip_links => 1)), $hal->as_json); + $c->response->headers($response->headers); + $c->response->body($response->content); + return; + } + return; +} + +sub HEAD :Allow { + my ($self, $c) = @_; + $c->forward(qw(GET)); + $c->response->body(q()); + return; +} + +sub OPTIONS :Allow { + my ($self, $c) = @_; + my $allowed_methods = $self->allowed_methods; + $c->response->headers(HTTP::Headers->new( + Allow => $allowed_methods->join(', '), + Accept_Post => 'application/hal+json; profile=http://purl.org/sipwise/ngcp-api/#rel-'.$self->resource_name, + )); + $c->response->content_type('application/json'); + $c->response->body(JSON::to_json({ methods => $allowed_methods })."\n"); + return; +} + +sub end : Private { + my ($self, $c) = @_; + + $self->log_response($c); + return 1; +} + +# vim: set tabstop=4 expandtab: diff --git a/lib/NGCP/Panel/Controller/API/CCMapEntriesItem.pm b/lib/NGCP/Panel/Controller/API/CCMapEntriesItem.pm new file mode 100644 index 0000000000..ce235fe8bc --- /dev/null +++ b/lib/NGCP/Panel/Controller/API/CCMapEntriesItem.pm @@ -0,0 +1,202 @@ +package NGCP::Panel::Controller::API::CCMapEntriesItem; +use Sipwise::Base; +use namespace::sweep; +use boolean qw(true); +use Data::HAL qw(); +use Data::HAL::Link qw(); +use HTTP::Headers qw(); +use HTTP::Status qw(:constants); +use MooseX::ClassAttribute qw(class_has); +use NGCP::Panel::Utils::ValidateJSON qw(); +use NGCP::Panel::Utils::DateTime; +use Path::Tiny qw(path); +use Safe::Isa qw($_isa); +BEGIN { extends 'Catalyst::Controller::ActionRole'; } +require Catalyst::ActionRole::ACL; +require Catalyst::ActionRole::HTTPMethods; +require Catalyst::ActionRole::RequireSSL; + +with 'NGCP::Panel::Role::API::CCMapEntries'; + +class_has('resource_name', is => 'ro', default => 'ccmapentries'); +class_has('dispatch_path', is => 'ro', default => '/api/ccmapentries/'); +class_has('relation', is => 'ro', default => 'http://purl.org/sipwise/ngcp-api/#rel-ccmapentries'); + +__PACKAGE__->config( + action => { + map { $_ => { + ACLDetachTo => '/api/root/invalid_user', + AllowedRole => 'admin', + Args => 1, + Does => [qw(ACL RequireSSL)], + Method => $_, + Path => __PACKAGE__->dispatch_path, + } } @{ __PACKAGE__->allowed_methods }, + }, + action_roles => [qw(HTTPMethods)], +); + +sub auto :Private { + my ($self, $c) = @_; + + $self->set_body($c); + $self->log_request($c); + return 1; +} + +sub GET :Allow { + my ($self, $c, $id) = @_; + { + last unless $self->valid_id($c, $id); + my $cfm = $self->item_by_id($c, $id, "callforwards"); + last unless $self->resource_exists($c, cfm => $cfm); + + my $hal = $self->hal_from_item($c, $cfm, "callforwards"); + + my $response = HTTP::Response->new(HTTP_OK, undef, HTTP::Headers->new( + (map { # XXX Data::HAL must be able to generate links with multiple relations + s|rel="(http://purl.org/sipwise/ngcp-api/#rel-resellers)"|rel="item $1"|r + =~ s/rel=self/rel="item self"/r; + } $hal->http_headers), + ), $hal->as_json); + $c->response->headers($response->headers); + $c->response->body($response->content); + return; + } + return; +} + +sub HEAD :Allow { + my ($self, $c, $id) = @_; + $c->forward(qw(GET)); + $c->response->body(q()); + return; +} + +sub OPTIONS :Allow { + my ($self, $c, $id) = @_; + my $allowed_methods = $self->allowed_methods; + $c->response->headers(HTTP::Headers->new( + Allow => $allowed_methods->join(', '), + Accept_Patch => 'application/json-patch+json', + )); + $c->response->content_type('application/json'); + $c->response->body(JSON::to_json({ methods => $allowed_methods })."\n"); + return; +} + +sub PATCH :Allow { + my ($self, $c, $id) = @_; + my $guard = $c->model('DB')->txn_scope_guard; + { + my $preference = $self->require_preference($c); + last unless $preference; + + my $json = $self->get_valid_patch_data( + c => $c, + id => $id, + media_type => 'application/json-patch+json', + ops => [qw/add replace remove copy/], + ); + last unless $json; + + my $callforward = $self->item_by_id($c, $id, "callforwards"); + last unless $self->resource_exists($c, callforward => $callforward); + my $old_resource = $self->hal_from_item($c, $callforward, "callforwards")->resource; + my $resource = $self->apply_patch($c, $old_resource, $json); + last unless $resource; + + my $form = $self->get_form($c); + $callforward = $self->update_item($c, $callforward, $old_resource, $resource, $form); + last unless $callforward; + + $guard->commit; + + if ('minimal' eq $preference) { + $c->response->status(HTTP_NO_CONTENT); + $c->response->header(Preference_Applied => 'return=minimal'); + $c->response->body(q()); + } else { + my $hal = $self->hal_from_item($c, $callforward, "callforwards"); + my $response = HTTP::Response->new(HTTP_OK, undef, HTTP::Headers->new( + $hal->http_headers, + ), $hal->as_json); + $c->response->headers($response->headers); + $c->response->header(Preference_Applied => 'return=representation'); + $c->response->body($response->content); + } + } + return; +} + +sub PUT :Allow { + my ($self, $c, $id) = @_; + my $guard = $c->model('DB')->txn_scope_guard; + { + my $preference = $self->require_preference($c); + last unless $preference; + + my $callforward = $self->item_by_id($c, $id); + last unless $self->resource_exists($c, callforward => $callforward); + my $resource = $self->get_valid_put_data( + c => $c, + id => $id, + media_type => 'application/json', + ); + last unless $resource; + my $old_resource = undef; + + my $form = $self->get_form($c); + $callforward = $self->update_item($c, $callforward, $old_resource, $resource, $form); + last unless $callforward; + + $guard->commit; + + if ('minimal' eq $preference) { + $c->response->status(HTTP_NO_CONTENT); + $c->response->header(Preference_Applied => 'return=minimal'); + $c->response->body(q()); + } else { + my $hal = $self->hal_from_item($c, $callforward, "callforwards"); + my $response = HTTP::Response->new(HTTP_OK, undef, HTTP::Headers->new( + $hal->http_headers, + ), $hal->as_json); + $c->response->headers($response->headers); + $c->response->header(Preference_Applied => 'return=representation'); + $c->response->body($response->content); + } + } + return; +} + +sub DELETE :Allow { + my ($self, $c, $id) = @_; + my $guard = $c->model('DB')->txn_scope_guard; + { + my $subscriber = $self->item_by_id($c, $id); + last unless $self->resource_exists($c, ccmapentry => $subscriber); + my $prov_subs = $subscriber->provisioning_voip_subscriber; + last unless $self->resource_exists($c, prov_subs => $prov_subs); + try { + $prov_subs->voip_cc_mappings->delete; + } catch($e) { + $c->log->error("Failed to delete ccmapentries with id '$id': $e"); + $self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Internal Server Error"); + last; + } + $guard->commit; + + $c->response->status(HTTP_NO_CONTENT); + $c->response->body(q()); + } + return; +} + +sub end : Private { + my ($self, $c) = @_; + + $self->log_response($c); + return 1; +} + +# vim: set tabstop=4 expandtab: diff --git a/lib/NGCP/Panel/Form/CCMapEntriesAPI.pm b/lib/NGCP/Panel/Form/CCMapEntriesAPI.pm new file mode 100644 index 0000000000..f29012893a --- /dev/null +++ b/lib/NGCP/Panel/Form/CCMapEntriesAPI.pm @@ -0,0 +1,39 @@ +package NGCP::Panel::Form::CCMapEntriesAPI; +use HTML::FormHandler::Moose; +use Moose::Util::TypeConstraints; +extends 'HTML::FormHandler'; + +sub build_render_list {return [qw/fields actions/]} +sub build_form_element_class {return [qw(form-horizontal)]} + +has_field 'subscriber_id' => ( + type => 'Repeatable', + required => 0, + element_attr => { + rel => ['tooltip'], + title => ['The subscriber ID.'], + }, +); + +has_field 'mappings' => ( + type => 'Repeatable', + required => 1, + element_attr => { + rel => ['tooltip'], + title => ['An Array of mappings, each entry containing the mandatory key "auth_key".'], + }, +); + +has_field 'mappings.auth_key' => ( + type => 'Text', +); + +has_block 'fields' => ( + tag => 'div', + class => [qw(modal-body)], + render_list => [qw(cfu cfb cft cfna)], +); + +1; + +# vim: set tabstop=4 expandtab: diff --git a/lib/NGCP/Panel/Role/API/CCMapEntries.pm b/lib/NGCP/Panel/Role/API/CCMapEntries.pm new file mode 100644 index 0000000000..721fbfde53 --- /dev/null +++ b/lib/NGCP/Panel/Role/API/CCMapEntries.pm @@ -0,0 +1,134 @@ +package NGCP::Panel::Role::API::CCMapEntries; +use Moose::Role; +use Sipwise::Base; +with 'NGCP::Panel::Role::API' => { + -alias =>{ item_rs => '_item_rs', }, + -excludes => [ 'item_rs' ], +}; + +use boolean qw(true); +use TryCatch; +use Data::HAL qw(); +use Data::HAL::Link qw(); +use HTTP::Status qw(:constants); +use JSON::Types; +use NGCP::Panel::Form::CFSimpleAPI; +use NGCP::Panel::Utils::Subscriber; +use NGCP::Panel::Form::CCMapEntriesAPI; + +sub get_form { + my ($self, $c) = @_; + + return NGCP::Panel::Form::CCMapEntriesAPI->new; +} + +sub hal_from_item { + my ($self, $c, $item, $type) = @_; + my $form = $self->get_form($c); + + my $prov_subs = $item->provisioning_voip_subscriber; + + die "no provisioning_voip_subscriber" unless $prov_subs; + + my %resource = (subscriber_id => $item->id); + + my $hal = Data::HAL->new( + links => [ + Data::HAL::Link->new( + relation => 'curies', + href => 'http://purl.org/sipwise/ngcp-api/#rel-{rel}', + name => 'ngcp', + templated => true, + ), + Data::HAL::Link->new(relation => 'collection', href => sprintf("%s", $self->dispatch_path)), + Data::HAL::Link->new(relation => 'profile', href => 'http://purl.org/sipwise/ngcp-api/'), + Data::HAL::Link->new(relation => 'self', href => sprintf("%s%s", $self->dispatch_path, $item->id)), + Data::HAL::Link->new(relation => "ngcp:$type", href => sprintf("/api/%s/%s", $type, $item->id)), + Data::HAL::Link->new(relation => 'ngcp:subscribers', href => sprintf("/api/subscribers/%d", $item->id)), + ], + relation => 'ngcp:'.$self->resource_name, + ); + + $resource{mappings} = []; + for my $mapping ($item->provisioning_voip_subscriber->voip_cc_mappings->all) { + push $resource{mappings}, { + auth_key => $mapping->auth_key, + }; + } + + return unless $self->validate_form( + c => $c, + form => $form, + resource => \%resource, + run => 0, + exceptions => ['subscriber_id'], + ); + + $hal->resource(\%resource); + return $hal; +} + +sub item_rs { + my ($self, $c) = @_; + my $item_rs; + + $item_rs = $c->model('DB')->resultset('voip_subscribers') + ->search( + { status => { '!=' => 'terminated' } }, + { prefetch => 'provisioning_voip_subscriber',}, + ); + if($c->user->roles eq "reseller") { + $item_rs = $item_rs->search({ + 'contact.reseller_id' => $c->user->reseller_id, + }, { + join => { 'contract' => 'contact' }, + }); + } + + return $item_rs; +} + +sub item_by_id { + my ($self, $c, $id) = @_; + + my $item_rs = $self->item_rs($c); + return $item_rs->search_rs({'me.id' => $id})->first; +} + +sub update_item { + my ($self, $c, $item, $old_resource, $resource, $form) = @_; + + delete $resource->{id}; + my $billing_subscriber_id = $item->id; + my $prov_subs = $item->provisioning_voip_subscriber; + die "need provisioning_voip_subscriber" unless $prov_subs; + my $prov_subscriber_id = $prov_subs->id; + + return unless $self->validate_form( + c => $c, + form => $form, + resource => $resource, + run => 1, + ); + + my $uuid = $item->uuid; + + try { + $prov_subs->voip_cc_mappings->delete; + for my $mapping (@{ $resource->{mappings} }) { + $prov_subs->voip_cc_mappings->create({ + source_uuid => $uuid, + auth_key => $mapping->{auth_key}, + }); + } + } catch($e) { + $c->log->error("Error Updating ccmapentry for $uuid: $e"); + $self->error($c, HTTP_INTERNAL_SERVER_ERROR, "CCMapEntry could not be updated."); + return; + }; + + return $item; +} + +1; +# vim: set tabstop=4 expandtab: