This is an automated email from the ASF dual-hosted git repository.

rob pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-trafficcontrol.git

commit 58ab7c2aeb6e7c7a97a5afbdaf463562ea00e20d
Author: ASchmidt <andrew_schm...@comcast.com>
AuthorDate: Mon May 7 12:18:20 2018 -0600

    reverted changes removing PERL api
---
 traffic_ops/app/lib/API/Types.pm        | 234 ++++++++++++++++++++++++++++++++
 traffic_ops/app/lib/TrafficOpsRoutes.pm |   9 ++
 2 files changed, 243 insertions(+)

diff --git a/traffic_ops/app/lib/API/Types.pm b/traffic_ops/app/lib/API/Types.pm
new file mode 100644
index 0000000..8906b51
--- /dev/null
+++ b/traffic_ops/app/lib/API/Types.pm
@@ -0,0 +1,234 @@
+package API::Types;
+#
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+#
+#
+
+# JvD Note: you always want to put Utils as the first use. Sh*t don't work if 
it's after the Mojo lines.
+use UI::Utils;
+
+use Mojo::Base 'Mojolicious::Controller';
+use Data::Dumper;
+
+sub index {
+       my $self         = shift;
+       my $use_in_table = $self->param('useInTable');
+       my $orderby      = $self->param('orderby') || "name";
+
+       my @data;
+       my %criteria;
+
+       if ( defined $use_in_table ) {
+               $criteria{'me.use_in_table'} = $use_in_table;
+       }
+
+       my $rs_data = $self->db->resultset("Type")->search( \%criteria, { 
order_by => $orderby } );
+
+       while ( my $row = $rs_data->next ) {
+               push(
+                       @data, {
+                               "id"          => $row->id,
+                               "name"        => $row->name,
+                               "description" => $row->description,
+                               "useInTable"  => $row->use_in_table,
+                               "lastUpdated" => $row->last_updated
+                       }
+               );
+       }
+       $self->success( \@data );
+}
+
+sub index_trimmed {
+       my $self = shift;
+       my @data;
+       my $orderby = $self->param('orderby') || "name";
+       my $rs_data = $self->db->resultset("Type")->search( undef, { order_by 
=> $orderby } );
+       while ( my $row = $rs_data->next ) {
+               push(
+                       @data, {
+                               "name" => $row->name
+                       }
+               );
+       }
+       $self->success( \@data );
+}
+
+sub show {
+       my $self = shift;
+       my $id   = $self->param('id');
+
+       my $rs_data = $self->db->resultset("Type")->search( { id => $id } );
+       my @data = ();
+       while ( my $row = $rs_data->next ) {
+               push(
+                       @data, {
+                               "id"          => $row->id,
+                               "name"        => $row->name,
+                               "description" => $row->description,
+                               "useInTable"  => $row->use_in_table,
+                               "lastUpdated" => $row->last_updated
+                       }
+               );
+       }
+       $self->success( \@data );
+}
+
+sub update {
+       my $self   = shift;
+       my $id     = $self->param('id');
+       my $params = $self->req->json;
+
+       if ( !&is_oper($self) ) {
+               return $self->forbidden();
+       }
+
+       my $type = $self->db->resultset('Type')->find( { id => $id } );
+       if ( !defined($type) ) {
+               return $self->not_found();
+       }
+
+       if ( !defined( $params->{name} ) ) {
+               return $self->alert("Type name is required.");
+       }
+
+       my $values = {
+               name                    => $params->{name},
+               description     => $params->{description},
+               use_in_table    => $params->{useInTable}
+       };
+
+       my $rs = $type->update($values);
+       if ($rs) {
+               my $response;
+               $response->{id}          = $rs->id;
+               $response->{name}        = $rs->name;
+               $response->{description} = $rs->description;
+               $response->{useInTable} = $rs->description;
+               $response->{lastUpdated} = $rs->use_in_table;
+
+               &log( $self, "Updated Type name '" . $rs->name . "' for id: " . 
$rs->id, "APICHANGE" );
+
+               return $self->success( $response, "Type update was successful." 
);
+       }
+       else {
+               return $self->alert("Type update failed.");
+       }
+
+}
+
+sub create {
+       my $self   = shift;
+       my $params = $self->req->json;
+
+       if ( !&is_oper($self) ) {
+               return $self->forbidden();
+       }
+
+       my $name = $params->{name};
+       if ( !defined($name) ) {
+               return $self->alert("Type name is required.");
+       }
+
+       my $existing = $self->db->resultset('Type')->search( { name => $name } 
)->get_column('name')->single();
+       if ( defined($existing) ) {
+               return $self->alert( "Type with that name already exists." );
+       }
+
+       my $values = {
+               name                    => $params->{name} ,
+               description     => $params->{description},
+               use_in_table    => $params->{useInTable}
+       };
+
+       my $insert = $self->db->resultset('Type')->create($values);
+       my $rs = $insert->insert();
+       if ($rs) {
+               my $response;
+               $response->{id}                 = $rs->id;
+               $response->{name}               = $rs->name;
+               $response->{description}    = $rs->description;
+               $response->{useInTable}     = $rs->use_in_table;
+               $response->{lastUpdated}        = $rs->last_updated;
+
+               &log( $self, "Created Type name '" . $rs->name . "' for id: " . 
$rs->id, "APICHANGE" );
+
+               return $self->success( $response, "Type create was successful." 
);
+       }
+       else {
+               return $self->alert("Type create failed.");
+       }
+
+}
+
+sub delete {
+       my $self = shift;
+       my $id     = $self->param('id');
+
+       if ( !&is_oper($self) ) {
+               return $self->forbidden();
+       }
+
+       my $type = $self->db->resultset('Type')->find( { id => $id } );
+       if ( !defined($type) ) {
+               return $self->not_found();
+       }
+
+       my $servers = $self->db->resultset('Server')->find( { type => $type->id 
} );
+       if ( defined($servers) ) {
+               return $self->alert("This type is currently used by servers.");
+       }
+
+       my $cachegroups = $self->db->resultset('Cachegroup')->find( { type => 
$type->id } );
+       if ( defined($cachegroups) ) {
+               return $self->alert("This type is currently used by 
cachegroups.");
+       }
+
+       my $deliveryservices = $self->db->resultset('Deliveryservice')->find( { 
type => $type->id } );
+       if ( defined($deliveryservices) ) {
+               return $self->alert("This type is currently used by 
deliveryservices.");
+       }
+
+       my $regexes = $self->db->resultset('Regex')->find( { type => $type->id 
} );
+       if ( defined($regexes) ) {
+               return $self->alert("This type is currently used by regexes.");
+       }
+
+       my $staticdnsentries = $self->db->resultset('Staticdnsentry')->find( { 
type => $type->id } );
+       if ( defined($staticdnsentries) ) {
+               return $self->alert("This type is currently used by 
staticdnsentries.");
+       }
+
+       my $federations = $self->db->resultset('FederationResolver')->find( { 
type => $type->id } );
+       if ( defined($federations) ) {
+               return $self->alert("This type is currently used by 
federations.");
+       }
+
+       my $to_extensions = $self->db->resultset('ToExtension')->find( { type 
=> $type->id } );
+       if ( defined($to_extensions) ) {
+               return $self->alert("This type is currently used by to 
extensions.");
+       }
+
+       my $rs = $type->delete();
+       if ($rs) {
+               return $self->success_message("Status deleted.");
+       } else {
+               return $self->alert( "Status delete failed." );
+       }
+}
+
+
+
+
+1;
diff --git a/traffic_ops/app/lib/TrafficOpsRoutes.pm 
b/traffic_ops/app/lib/TrafficOpsRoutes.pm
index 7f04a4b..5c944b9 100644
--- a/traffic_ops/app/lib/TrafficOpsRoutes.pm
+++ b/traffic_ops/app/lib/TrafficOpsRoutes.pm
@@ -807,6 +807,15 @@ sub api_routes {
        $r->post("/api/$version/tenants")->over( authenticated => 1, not_ldap 
=> 1 )->to( 'Tenant#create', namespace => $namespace );
        $r->delete("/api/$version/tenants/:id" => [ id => qr/\d+/ ] )->over( 
authenticated => 1, not_ldap => 1 )->to( 'Tenant#delete', namespace => 
$namespace );
 
+       # -- TYPES
+       # Supports ?orderby=key
+       $r->get("/api/$version/types")->over( authenticated => 1, not_ldap => 1 
)->to( 'Types#index', namespace => $namespace );
+       $r->get("/api/$version/types/trimmed")->over( authenticated => 1, 
not_ldap => 1 )->to( 'Types#index_trimmed', namespace => $namespace );
+       $r->get( "/api/$version/types/:id" => [ id => qr/\d+/ ] )->over( 
authenticated => 1, not_ldap => 1 )->to( 'Types#show', namespace => $namespace 
);
+       $r->post("/api/$version/types")->over( authenticated => 1, not_ldap => 
1 )->to( 'Types#create', namespace => $namespace );
+       $r->put("/api/$version/types/:id" => [ id => qr/\d+/ ] )->over( 
authenticated => 1, not_ldap => 1 )->to( 'Types#update', namespace => 
$namespace );
+       $r->delete("/api/$version/types/:id" => [ id => qr/\d+/ ] )->over( 
authenticated => 1, not_ldap => 1 )->to( 'Types#delete', namespace => 
$namespace );
+
        # -- USERS
        $r->get("/api/$version/users")->over( authenticated => 1, not_ldap => 1 
)->to( 'User#index', namespace => $namespace );
        $r->get( "/api/$version/users/:id" => [ id => qr/\d+/ ] )->over( 
authenticated => 1, not_ldap => 1 )->to( 'User#show', namespace => $namespace );

-- 
To stop receiving notification emails like this one, please contact
r...@apache.org.

Reply via email to