From b3de58ff9c69d1ec140a123674916272fa703d58 Mon Sep 17 00:00:00 2001 From: Andreas Granig Date: Sat, 7 Dec 2013 14:14:47 +0100 Subject: [PATCH] MT#5299 API: Implement billing zone handling. --- .../Panel/Controller/API/BillingFeesItem.pm | 10 +- lib/NGCP/Panel/Controller/API/BillingZones.pm | 208 ++++++++++++++++++ .../Panel/Controller/API/BillingZonesItem.pm | 199 +++++++++++++++++ lib/NGCP/Panel/Role/API/BillingFees.pm | 8 +- lib/NGCP/Panel/Role/API/BillingProfiles.pm | 1 + lib/NGCP/Panel/Role/API/BillingZones.pm | 111 ++++++++++ 6 files changed, 532 insertions(+), 5 deletions(-) create mode 100644 lib/NGCP/Panel/Controller/API/BillingZones.pm create mode 100644 lib/NGCP/Panel/Controller/API/BillingZonesItem.pm create mode 100644 lib/NGCP/Panel/Role/API/BillingZones.pm diff --git a/lib/NGCP/Panel/Controller/API/BillingFeesItem.pm b/lib/NGCP/Panel/Controller/API/BillingFeesItem.pm index 99928edca9..6bc8a49a96 100644 --- a/lib/NGCP/Panel/Controller/API/BillingFeesItem.pm +++ b/lib/NGCP/Panel/Controller/API/BillingFeesItem.pm @@ -174,7 +174,15 @@ sub DELETE :Allow { { my $fee = $self->fee_by_id($c, $id); last unless $self->resource_exists($c, billingfee => $fee); - $fee->delete; + + try { + $fee->delete; + } catch($e) { + $c->log->error("Failed to delete billing fee with id '$id': $e"); + $self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Internal Server Error"); + last; + } + $guard->commit; $c->response->status(HTTP_NO_CONTENT); diff --git a/lib/NGCP/Panel/Controller/API/BillingZones.pm b/lib/NGCP/Panel/Controller/API/BillingZones.pm new file mode 100644 index 0000000000..cb21b68f23 --- /dev/null +++ b/lib/NGCP/Panel/Controller/API/BillingZones.pm @@ -0,0 +1,208 @@ +package NGCP::Panel::Controller::API::BillingZones; +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 NGCP::Panel::Form::BillingZone qw(); +use Path::Tiny qw(path); +BEGIN { extends 'Catalyst::Controller::ActionRole'; } +require Catalyst::ActionRole::ACL; +require Catalyst::ActionRole::CheckTrailingSlash; +require Catalyst::ActionRole::HTTPMethods; +require Catalyst::ActionRole::RequireSSL; + +with 'NGCP::Panel::Role::API'; +with 'NGCP::Panel::Role::API::BillingZones'; + +class_has('resource_name', is => 'ro', default => 'billingzones'); +class_has('dispatch_path', is => 'ro', default => '/api/billingzones/'); +class_has('relation', is => 'ro', default => 'http://purl.org/sipwise/ngcp-api/#rel-billingzones'); + +__PACKAGE__->config( + action => { + map { $_ => { + ACLDetachTo => '/api/root/invalid_user', + AllowedRole => 'api_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); +} + +sub GET :Allow { + my ($self, $c) = @_; + my $page = $c->request->params->{page} // 1; + my $rows = $c->request->params->{rows} // 10; + { + my $zones = $c->model('DB')->resultset('billing_zones'); + if($c->request->query_parameters->{billing_profile_id}) { + $zones = $zones->search({ + billing_profile_id => $c->request->query_parameters->{billing_profile_id}, + }); + }; + + if($c->user->roles eq "api_admin") { + } elsif($c->user->roles eq "api_reseller") { + $zones = $zones->search({ + 'billing_profile.reseller_id' => $c->user->reseller_id + }, { + join => 'billing_profile', + }); + } else { + $zones = $zones->search({ + 'billing_profile.reseller_id' => $c->user->contract->contact->reseller_id, + }, { + join => 'billing_profile', + }); + } + my $total_count = int($zones->count); + $zones = $zones->search(undef, { + page => $page, + rows => $rows, + }); + my (@embedded, @links); + my $form = NGCP::Panel::Form::BillingZone->new; + for my $zone ($zones->all) { + push @embedded, $self->hal_from_zone($c, $zone, $form); + push @links, Data::HAL::Link->new( + relation => 'ngcp:'.$self->resource_name, + href => sprintf('/%s%d', $c->request->path, $zone->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', $c->request->path, $page, $rows)); + + if(($total_count / $rows) > $page ) { + push @links, Data::HAL::Link->new(relation => 'next', href => sprintf('/%s?page=%d&rows=%d', $c->request->path, $page + 1, $rows)); + } + if($page > 1) { + push @links, Data::HAL::Link->new(relation => 'prev', href => sprintf('/%s?page=%d&rows=%d', $c->request->path, $page - 1, $rows)); + } + + my $hal = Data::HAL->new( + embedded => [@embedded], + links => [@links], + ); + $hal->resource({ + total_count => $total_count, + }); + my $rname = $self->resource_name; + 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-$rname)"|rel="item $1"|; + s/rel=self/rel="collection self"/; + $_ + } $hal->http_headers), + ), $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 POST :Allow { + my ($self, $c) = @_; + + my $guard = $c->model('DB')->txn_scope_guard; + { + my $schema = $c->model('DB'); + my $resource = $self->get_valid_post_data( + c => $c, + media_type => 'application/json', + ); + last unless $resource; + + my $reseller_id; + if($c->user->roles eq "api_admin") { + } elsif($c->user->roles eq "api_reseller") { + $reseller_id = $c->user->reseller_id; + } else { + $reseller_id = $c->user->contract->contact->reseller_id; + } + + my $form = NGCP::Panel::Form::BillingZone->new; + my $billing_profile_id = $resource->{billing_profile_id} // undef; + last unless $self->validate_form( + c => $c, + resource => $resource, + form => $form, + ); + $resource->{billing_profile_id} = $billing_profile_id; + + my $profile = $schema->resultset('billing_profiles')->find($resource->{billing_profile_id}); + unless($profile) { + $self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid 'billing_profile_id'."); + last; + } + if($c->user->roles ne "api_admin" && $profile->reseller_id != $reseller_id) { + $self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid 'billing_profile_id'."); + last; + } + + my $zone; + try { + $zone = $profile->billing_zones->create($resource); + } catch($e) { + $c->log->error("failed to create billing zone: $e"); # TODO: user, message, trace, ... + $self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Failed to create billing zone."); + last; + } + + $guard->commit; + + $c->response->status(HTTP_CREATED); + $c->response->header(Location => sprintf('%s%d', $self->dispatch_path, $zone->id)); + $c->response->body(q()); + } + return; +} + +sub end : Private { + my ($self, $c) = @_; + + $self->log_response($c); +} + +# vim: set tabstop=4 expandtab: diff --git a/lib/NGCP/Panel/Controller/API/BillingZonesItem.pm b/lib/NGCP/Panel/Controller/API/BillingZonesItem.pm new file mode 100644 index 0000000000..7aadc00651 --- /dev/null +++ b/lib/NGCP/Panel/Controller/API/BillingZonesItem.pm @@ -0,0 +1,199 @@ +package NGCP::Panel::Controller::API::BillingZonesItem; +use Sipwise::Base; +use namespace::sweep; +use HTTP::Headers qw(); +use HTTP::Status qw(:constants); +use MooseX::ClassAttribute qw(class_has); +use NGCP::Panel::Form::BillingZone qw(); +use NGCP::Panel::Utils::DateTime; +use NGCP::Panel::Utils::ValidateJSON qw(); +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'; +with 'NGCP::Panel::Role::API::BillingZones'; + +class_has('resource_name', is => 'ro', default => 'billingzones'); +class_has('dispatch_path', is => 'ro', default => '/api/billingzones/'); +class_has('relation', is => 'ro', default => 'http://purl.org/sipwise/ngcp-api/#rel-billingzones'); + +__PACKAGE__->config( + action => { + map { $_ => { + ACLDetachTo => '/api/root/invalid_user', + AllowedRole => 'api_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); +} + +sub GET :Allow { + my ($self, $c, $id) = @_; + { + last unless $self->valid_id($c, $id); + my $zone = $self->zone_by_id($c, $id); + last unless $self->resource_exists($c, billingzone => $zone); + + my $hal = $self->hal_from_zone($c, $zone); + + # TODO: we don't need reseller stuff here! + 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"|; + s/rel=self/rel="item self"/; + $_ + } $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', + ); + last unless $json; + + my $zone = $self->zone_by_id($c, $id); + last unless $self->resource_exists($c, billingzone => $zone); + my $old_resource = { $zone->get_inflated_columns }; + my $resource = $self->apply_patch($c, $old_resource, $json); + last unless $resource; + + my $form = NGCP::Panel::Form::BillingZone->new; + $zone = $self->update_zone($c, $zone, $old_resource, $resource, $form); + last unless $zone; + + $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_zone($c, $zone, $form); + 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 $zone = $self->zone_by_id($c, $id); + last unless $self->resource_exists($c, billingzone => $zone); + my $resource = $self->get_valid_put_data( + c => $c, + id => $id, + media_type => 'application/json', + ); + last unless $resource; + my $old_resource = { $zone->get_inflated_columns }; + + my $form = NGCP::Panel::Form::BillingZone->new; + $zone = $self->update_zone($c, $zone, $old_resource, $resource, $form); + last unless $zone; + + $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_zone($c, $zone, $form); + 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 $zone = $self->zone_by_id($c, $id); + last unless $self->resource_exists($c, billingzone => $zone); + try { + $zone->billing_fees->delete_all; + $zone->delete; + } catch($e) { + $c->log->error("Failed to delete billing zone 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); +} + +# vim: set tabstop=4 expandtab: diff --git a/lib/NGCP/Panel/Role/API/BillingFees.pm b/lib/NGCP/Panel/Role/API/BillingFees.pm index a7d9ba8b5d..be8c5a96c8 100644 --- a/lib/NGCP/Panel/Role/API/BillingFees.pm +++ b/lib/NGCP/Panel/Role/API/BillingFees.pm @@ -54,15 +54,15 @@ sub fee_by_id { if($c->user->roles eq "api_admin") { } elsif($c->user->roles eq "api_reseller") { $fees = $fees->search({ - 'billing_zone.reseller_id' => $c->user->reseller_id, + 'billing_profile.reseller_id' => $c->user->reseller_id, }, { - join => 'billin_zone', + join => 'billing_profile', }); } else { $fees = $fees->search({ - 'billing_zone.reseller_id' => $c->user->contract->contact->reseller_id, + 'billing_profile.reseller_id' => $c->user->contract->contact->reseller_id, }, { - join => 'billin_zone', + join => 'billin_profile', }); } diff --git a/lib/NGCP/Panel/Role/API/BillingProfiles.pm b/lib/NGCP/Panel/Role/API/BillingProfiles.pm index f57cb28508..c7e2f7e3e4 100644 --- a/lib/NGCP/Panel/Role/API/BillingProfiles.pm +++ b/lib/NGCP/Panel/Role/API/BillingProfiles.pm @@ -32,6 +32,7 @@ sub hal_from_profile { Data::HAL::Link->new(relation => 'profile', href => 'http://purl.org/sipwise/ngcp-api/'), Data::HAL::Link->new(relation => 'self', href => sprintf("%s%d", $self->dispatch_path, $profile->id)), map { Data::HAL::Link->new(relation => 'ngcp:billingfees', href => sprintf("/api/billingfees/%d", $_->id)) } $profile->billing_fees->all, + map { Data::HAL::Link->new(relation => 'ngcp:billingzones', href => sprintf("/api/billingzones/%d", $_->id)) } $profile->billing_zones->all, ], relation => 'ngcp:'.$self->resource_name, ); diff --git a/lib/NGCP/Panel/Role/API/BillingZones.pm b/lib/NGCP/Panel/Role/API/BillingZones.pm new file mode 100644 index 0000000000..2bd70fdcb4 --- /dev/null +++ b/lib/NGCP/Panel/Role/API/BillingZones.pm @@ -0,0 +1,111 @@ +package NGCP::Panel::Role::API::BillingZones; +use Moose::Role; +use Sipwise::Base; + +use boolean qw(true); +use Try::Tiny; +use Data::HAL qw(); +use Data::HAL::Link qw(); +use HTTP::Status qw(:constants); +use NGCP::Panel::Utils::DateTime; +use NGCP::Panel::Utils::Contract; +use NGCP::Panel::Form::BillingZone qw(); + +sub hal_from_zone { + my ($self, $c, $zone, $form) = @_; + + my %resource = $zone->get_inflated_columns; + + 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('/api/%s/', $self->resource_name)), + Data::HAL::Link->new(relation => 'profile', href => 'http://purl.org/sipwise/ngcp-api/'), + Data::HAL::Link->new(relation => 'self', href => sprintf("%s%d", $self->dispatch_path, $zone->id)), + Data::HAL::Link->new(relation => 'ngcp:billingprofiles', href => sprintf("/api/billingprofiles/%d", $zone->billing_profile->id)), + map { Data::HAL::Link->new(relation => 'ngcp:billingfees', href => sprintf("/api/billingfees/%d", $_->id)) } $zone->billing_fees->all, + ], + relation => 'ngcp:'.$self->resource_name, + ); + + $form //= NGCP::Panel::Form::BillingZone->new; + return unless $self->validate_form( + c => $c, + form => $form, + resource => \%resource, + run => 0, + ); + + $resource{id} = int($zone->id); + $resource{billing_profile_id} = int($zone->billing_profile_id); + $hal->resource({%resource}); + return $hal; +} + +sub zone_by_id { + my ($self, $c, $id) = @_; + + my $zones = $c->model('DB')->resultset('billing_zones'); + if($c->user->roles eq "api_admin") { + } elsif($c->user->roles eq "api_reseller") { + $zones = $zones->search({ + 'billing_profile.reseller_id' => $c->user->reseller_id, + }, { + join => 'billin_profile', + }); + } else { + $zones = $zones->search({ + 'billing_profile.reseller_id' => $c->user->contract->contact->reseller_id, + }, { + join => 'billin_profile', + }); + } + + return $zones->find($id); +} + +sub update_zone { + my ($self, $c, $zone, $old_resource, $resource, $form) = @_; + + my $reseller_id; + if($c->user->roles eq "api_admin") { + } elsif($c->user->roles eq "api_admin") { + $reseller_id = $c->user->reseller_id; + } else { + $reseller_id = $c->user->contract->contact->reseller_id; + } + $form //= NGCP::Panel::Form::BillingZone->new; + # TODO: for some reason, formhandler lets missing profile id + my $billing_profile_id = $resource->{billing_profile_id} // undef; + return unless $self->validate_form( + c => $c, + form => $form, + resource => $resource, + ); + $resource->{billing_profile_id} = $billing_profile_id; + + if($old_resource->{billing_profile_id} != $resource->{billing_profile_id}) { + my $profile = $c->model('DB')->resultset('billing_profiles') + ->find($resource->{billing_profile_id}); + unless($profile) { + $self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid 'billing_profile_id'"); + return; + } + if($c->user->roles ne "api_admin" && $profile->reseller->id != $reseller_id) { + $self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Invalid 'billing_profile_id'"); + return; + } + } + + $zone->update($resource); + + return $zone; +} + +1; +# vim: set tabstop=4 expandtab: