From a56268161da5edb6d90f9a9ad876596189a1c607 Mon Sep 17 00:00:00 2001 From: Karen Etheridge Date: Mon, 26 Aug 2019 11:31:12 -0700 Subject: [PATCH] add DELETE /relay/:serial closes #868. (backported from #872 in v3.) --- docs/modules/Conch::Controller::Relay.md | 2 ++ docs/modules/Conch::Route::Relay.md | 5 +++++ lib/Conch/Controller/Relay.pm | 22 ++++++++++++++++++++++ lib/Conch/Route/Relay.pm | 13 +++++++++++++ t/integration/crud/relay.t | 19 +++++++++++++++++++ 5 files changed, 61 insertions(+) diff --git a/docs/modules/Conch::Controller::Relay.md b/docs/modules/Conch::Controller::Relay.md index fa7a3b1f4..6b1d27fd3 100644 --- a/docs/modules/Conch::Controller::Relay.md +++ b/docs/modules/Conch::Controller::Relay.md @@ -15,6 +15,8 @@ If the user is a system admin, retrieve a list of all active relays in the datab Response uses the Relays json schema. +## delete + # LICENSING Copyright Joyent, Inc. diff --git a/docs/modules/Conch::Route::Relay.md b/docs/modules/Conch::Route::Relay.md index 84a4ec0bb..a4239deeb 100644 --- a/docs/modules/Conch::Route::Relay.md +++ b/docs/modules/Conch::Route::Relay.md @@ -20,6 +20,11 @@ Unless otherwise noted, all routes require authentication. - Requires System Admin Authentication - Response: response.yaml#/Relays +## `DELETE /relay/:relay_serial_number` + +- Requires system admin authorization +- Response: `204 NO CONTENT` + # LICENSING Copyright Joyent, Inc. diff --git a/lib/Conch/Controller/Relay.pm b/lib/Conch/Controller/Relay.pm index 329b79f4e..a64355b9f 100644 --- a/lib/Conch/Controller/Relay.pm +++ b/lib/Conch/Controller/Relay.pm @@ -53,6 +53,28 @@ sub list ($c) { $c->status(200, [ $c->db_relays->active->all ]); } +=head2 delete + +=cut + +sub delete ($c) { + my $identifier = $c->stash('relay_serial_number'); + + my $rs = $c->db_relays + ->active + ->search({ id => $identifier }); + return $c->status(404) if not $rs->exists; + + my $drc_count = $rs->related_resultset('device_relay_connections')->delete; + my $urc_count = $rs->related_resultset('user_relay_connections')->delete; + $rs->deactivate; + + $c->log->debug('Deactivated relay '.$identifier + .', removing '.$drc_count.' associated device connections and ' + .$urc_count.' associated user connections'); + return $c->status(204); +} + 1; __END__ diff --git a/lib/Conch/Route/Relay.pm b/lib/Conch/Route/Relay.pm index 5fda668be..36c5ff29a 100644 --- a/lib/Conch/Route/Relay.pm +++ b/lib/Conch/Route/Relay.pm @@ -27,6 +27,9 @@ sub routes { # GET /relay $relay->get('/')->to('#list'); + + # DELETE /relay/:relay_serial_number + $relay->require_system_admin->delete('/:relay_serial_number')->to('#delete'); } 1; @@ -60,6 +63,16 @@ Unless otherwise noted, all routes require authentication. =cut +=head2 C + +=over 4 + +=item * Requires system admin authorization + +=item * Response: C<204 NO CONTENT> + +=back + =head1 LICENSING Copyright Joyent, Inc. diff --git a/t/integration/crud/relay.t b/t/integration/crud/relay.t index bfc42b060..916ed91d4 100644 --- a/t/integration/crud/relay.t +++ b/t/integration/crud/relay.t @@ -259,5 +259,24 @@ subtest get_relay_devices => sub { ]); }; +subtest delete => sub { + $t->authenticate(user => $t->load_fixture('null_user')->email); + + $t->delete_ok('/relay/'.$relay0->id) + ->status_is(403); + + $t->authenticate; # back to superuser + + $t->delete_ok('/relay/'.$relay0->id) + ->status_is(204) + ->log_debug_is('Deactivated relay '.$relay0->id.', removing 2 associated device connections and 1 associated user connections'); + + $t->get_ok('/relay/'.$relay0->id) + ->status_is(404); + + $t->delete_ok('/relay/'.$relay0->id) + ->status_is(404); +}; + done_testing; # vim: set ts=4 sts=4 sw=4 et :