From 1e7762e0b78c7c6b4e6aa27ca3e4e62f8cbccce9 Mon Sep 17 00:00:00 2001 From: Flaviu Mates Date: Mon, 27 Jan 2020 17:45:23 +0200 Subject: [PATCH] TT#74525 - Updgrade /api/headerrulesets * Introduce posibility to provision rule sets, rules, condition and actions at the same time using only the /api/headerrulesets endpoint; also rules can be modified with PUT/PATCH on /api/headerrulesets Change-Id: I8c054f72a2632d45fec76166774521f8c22aea05 --- .../Panel/Controller/API/HeaderRuleSets.pm | 41 +++++++++++++ lib/NGCP/Panel/Form/Header/AdminRuleSetAPI.pm | 11 +++- .../Panel/Form/Header/ResellerRuleSetAPI.pm | 11 +++- lib/NGCP/Panel/Role/API/HeaderRuleSets.pm | 60 ++++++++++++++++++- 4 files changed, 120 insertions(+), 3 deletions(-) diff --git a/lib/NGCP/Panel/Controller/API/HeaderRuleSets.pm b/lib/NGCP/Panel/Controller/API/HeaderRuleSets.pm index 5e7a7f555f..1c01e96928 100644 --- a/lib/NGCP/Panel/Controller/API/HeaderRuleSets.pm +++ b/lib/NGCP/Panel/Controller/API/HeaderRuleSets.pm @@ -51,7 +51,48 @@ sub create_item { my $item; my $schema = $c->model('DB'); try { + my $header_rules = delete $resource->{rules}; $item = $schema->resultset('voip_header_rule_sets')->create($resource); + if ($header_rules) { + foreach my $rule (@$header_rules) { + my $header_actions = delete $rule->{actions}; + my $header_conditions = delete $rule->{conditions}; + $rule->{set_id} = $item->id; + last unless $self->validate_form( + c => $c, + resource => $rule, + form => (NGCP::Panel::Form::get("NGCP::Panel::Form::Header::RuleAPI", $c)), + ); + my $rule_result = $schema->resultset('voip_header_rules')->create($rule); + if ($header_actions) { + foreach my $action (@$header_actions) { + $action->{rule_id} = $rule_result->id; + last unless $self->validate_form( + c => $c, + resource => $action, + form => (NGCP::Panel::Form::get("NGCP::Panel::Form::Header::ActionAPI", $c)), + ); + last unless NGCP::Panel::Role::API::HeaderRuleActions->check_resource($c, undef, undef, $action, undef, undef); + my $action_result = $schema->resultset('voip_header_rule_actions')->create($action); + } + } + if ($header_conditions) { + foreach my $condition (@$header_conditions) { + $condition->{rule_id} = $rule_result->id; + last unless $self->validate_form( + c => $c, + resource => $condition, + form => (NGCP::Panel::Form::get("NGCP::Panel::Form::Header::ConditionAPI", $c)), + ); + last unless NGCP::Panel::Role::API::HeaderRuleConditions->check_resource($c, undef, undef, $condition, undef, undef); + my $condition_result = $schema->resultset('voip_header_rule_conditions')->create($condition); + } + } + } + NGCP::Panel::Utils::HeaderManipulations::invalidate_ruleset( + c => $c, set_id => $item->id + ); + } } catch($e) { $c->log->error("failed to create a header rule set: $e"); $self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Failed to create a header rule set."); diff --git a/lib/NGCP/Panel/Form/Header/AdminRuleSetAPI.pm b/lib/NGCP/Panel/Form/Header/AdminRuleSetAPI.pm index 35c82e7487..e809e02db3 100644 --- a/lib/NGCP/Panel/Form/Header/AdminRuleSetAPI.pm +++ b/lib/NGCP/Panel/Form/Header/AdminRuleSetAPI.pm @@ -12,10 +12,19 @@ has_field 'reseller_id' => ( }, ); +has_field 'rules' => ( + type => 'Compound', + required => 0, + element_attr => { + rel => ['tooltip'], + title => ['The list of rules in the set.'], + }, +); + has_block 'fields' => ( tag => 'div', class => [qw/modal-body/], - render_list => [qw/reseller_id subscriber_id name description/], + render_list => [qw/reseller_id subscriber_id name description rules/], ); 1; diff --git a/lib/NGCP/Panel/Form/Header/ResellerRuleSetAPI.pm b/lib/NGCP/Panel/Form/Header/ResellerRuleSetAPI.pm index 0f118f551f..e14043bcc2 100644 --- a/lib/NGCP/Panel/Form/Header/ResellerRuleSetAPI.pm +++ b/lib/NGCP/Panel/Form/Header/ResellerRuleSetAPI.pm @@ -12,10 +12,19 @@ has_field 'subscriber_id' => ( fif_from_value => 1, ); +has_field 'rules' => ( + type => 'Compound', + required => 0, + element_attr => { + rel => ['tooltip'], + title => ['The list of rules in the set.'], + }, +); + has_block 'fields' => ( tag => 'div', class => [qw/modal-body/], - render_list => [qw/subscriber_id name description/], + render_list => [qw/subscriber_id name description rules/], ); 1; diff --git a/lib/NGCP/Panel/Role/API/HeaderRuleSets.pm b/lib/NGCP/Panel/Role/API/HeaderRuleSets.pm index 38502b6d95..d7b394732d 100644 --- a/lib/NGCP/Panel/Role/API/HeaderRuleSets.pm +++ b/lib/NGCP/Panel/Role/API/HeaderRuleSets.pm @@ -116,7 +116,46 @@ sub check_duplicate { sub update_item_model { my ($self, $c, $item, $old_resource, $resource, $form) = @_; - $item = $self->SUPER::update_item_model($c, $item, $old_resource, $resource, $form); + my $header_rules = delete $resource->{rules}; + $item->update($resource); + if ($header_rules) { + $item->voip_header_rules->delete; + foreach my $rule (@$header_rules) { + my $header_actions = delete $rule->{actions}; + my $header_conditions = delete $rule->{conditions}; + $rule->{set_id} = $item->id; + last unless $self->validate_form( + c => $c, + resource => $rule, + form => (NGCP::Panel::Form::get("NGCP::Panel::Form::Header::RuleAPI", $c)), + ); + my $rule_result = $item->voip_header_rules->create($rule); + if ($header_actions) { + foreach my $action (@$header_actions) { + $action->{rule_id} = $rule_result->id; + last unless $self->validate_form( + c => $c, + resource => $action, + form => (NGCP::Panel::Form::get("NGCP::Panel::Form::Header::ActionAPI", $c)), + ); + last unless NGCP::Panel::Role::API::HeaderRuleActions->check_resource($c, undef, undef, $action, undef, undef); + my $action_result = $rule_result->actions->create($action); + } + } + if ($header_conditions) { + foreach my $condition (@$header_conditions) { + $condition->{rule_id} = $rule_result->id; + last unless $self->validate_form( + c => $c, + resource => $condition, + form => (NGCP::Panel::Form::get("NGCP::Panel::Form::Header::ConditionAPI", $c)), + ); + last unless NGCP::Panel::Role::API::HeaderRuleConditions->check_resource($c, undef, undef, $condition, undef, undef); + my $condition_result = $rule_result->conditions->create($condition); + } + } + } + } NGCP::Panel::Utils::HeaderManipulations::invalidate_ruleset( c => $c, set_id => $item->id @@ -125,5 +164,24 @@ sub update_item_model { return $item; } +sub resource_from_item { + my ($self, $c, $item, $form) = @_; + + my %resource = $item->get_inflated_columns; + my @rules = (); + + foreach my $r ($item->voip_header_rules->all) { + my @conditions = map { {$_->get_inflated_columns} } $r->conditions->all; + my @actions = map { {$_->get_inflated_columns} } $r->actions->all; + push @rules, { $r->get_inflated_columns, + conditions => \@conditions, + actions => \@actions + }; + } + $resource{rules} = \@rules; + + return \%resource; +} + 1; # vim: set tabstop=4 expandtab: