-
Notifications
You must be signed in to change notification settings - Fork 78
WIP check api #592
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
matthewhilton
wants to merge
5
commits into
MOODLE_310_STABLE
Choose a base branch
from
310-stable-add-check-api
base: MOODLE_310_STABLE
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
WIP check api #592
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| <?php | ||
| // This file is part of Moodle - http://moodle.org/ | ||
| // | ||
| // Moodle is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
| // | ||
| // Moodle is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU General Public License | ||
| // along with Moodle. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| namespace tool_objectfs\check; | ||
|
|
||
| use action_link; | ||
| use core\check\check; | ||
| use core\check\result; | ||
| use moodle_url; | ||
| use tool_objectfs\local\manager; | ||
|
|
||
| /** | ||
| * Configuration check. | ||
| * | ||
| * @package tool_objectfs | ||
| * @author Matthew Hilton <[email protected]> | ||
| * @copyright Catalyst IT | ||
| * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
| */ | ||
| class configuration extends check { | ||
| /** | ||
| * Gets the result of the check | ||
| * @return result | ||
| */ | ||
| public function get_result(): result { | ||
| // Load objectfs and run a test. | ||
| $config = manager::get_objectfs_config(); | ||
| $client = manager::get_client($config); | ||
|
|
||
| // Something is very wrong if this is false. | ||
| if (empty($client)) { | ||
| return new result(result::UNKNOWN, get_string('check:configuration:empty', 'tool_objectfs')); | ||
| } | ||
|
|
||
| return $client->test_configuration(); | ||
| } | ||
|
|
||
| /** | ||
| * Return action link | ||
| * @return action_link | ||
| */ | ||
| public function get_action_link(): ?action_link { | ||
| $str = get_string('check:settings', 'tool_objectfs'); | ||
| $url = new moodle_url('/admin/category.php', ['category' => 'tool_objectfs']); | ||
| return new action_link($url, $str); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| <?php | ||
| // This file is part of Moodle - http://moodle.org/ | ||
| // | ||
| // Moodle is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
| // | ||
| // Moodle is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU General Public License | ||
| // along with Moodle. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| namespace tool_objectfs\check; | ||
|
|
||
| use action_link; | ||
| use coding_exception; | ||
| use core\check\check; | ||
| use core\check\result; | ||
| use moodle_url; | ||
| use Throwable; | ||
| use tool_objectfs\local\manager; | ||
|
|
||
| /** | ||
| * Store check. | ||
| * | ||
| * @package tool_objectfs | ||
| * @author Matthew Hilton <[email protected]> | ||
| * @copyright Catalyst IT | ||
| * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
| */ | ||
| class store extends check { | ||
| /** @var string The selected type of store check **/ | ||
| private $type; | ||
|
|
||
| /** @var string Connection test type **/ | ||
| public const TYPE_CONNECTION = 'connection'; | ||
|
|
||
| /** @var string Permissions test type **/ | ||
| public const TYPE_PERMISSIONS = 'permissions'; | ||
|
|
||
| /** @var string Range request test type **/ | ||
| public const TYPE_RANGEREQUEST = 'rangerequest'; | ||
|
|
||
| /** @var array Available test types **/ | ||
| public const TYPES = [self::TYPE_CONNECTION, self::TYPE_PERMISSIONS, self::TYPE_RANGEREQUEST]; | ||
|
|
||
| /** | ||
| * Create a store check for a given type | ||
| * @param string $type one of TYPES | ||
| */ | ||
| public function __construct(string $type) { | ||
| if (!in_array($type, self::TYPES)) { | ||
| throw new coding_exception("Given test type " . $type . " is not valid."); | ||
| } | ||
|
|
||
| $this->type = $type; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the id - differs based on the type | ||
| * @return string | ||
| */ | ||
| public function get_id(): string { | ||
| return "store_check_" . $this->type; | ||
| } | ||
|
|
||
| /** | ||
| * Return the result | ||
| * @return result | ||
| */ | ||
| public function get_result(): result { | ||
| try { | ||
| // Check if configured first, and report NA if not configured. | ||
| if (!\tool_objectfs\local\manager::check_file_storage_filesystem()) { | ||
| return new result(result::NA, get_string('check:notenabled', 'tool_objectfs')); | ||
| } | ||
|
|
||
| // Load objectfs and run a test. | ||
| $config = manager::get_objectfs_config(); | ||
| $client = manager::get_client($config); | ||
|
|
||
| // Something is very wrong if this is empty. | ||
| if (empty($client)) { | ||
| return new result(result::UNKNOWN, get_string('check:configuration:empty', 'tool_objectfs')); | ||
| } | ||
|
|
||
| // If not configured yet, don't bother testing connection or permissions. | ||
| if ($client->test_configuration()->get_status() != result::OK) { | ||
| return new result(result::NA, get_string('check:storecheck:notconfiguredskip', 'tool_objectfs')); | ||
| } | ||
|
|
||
| switch($this->type) { | ||
| case self::TYPE_CONNECTION: | ||
| return $client->test_connection(false); | ||
|
|
||
| case self::TYPE_RANGEREQUEST: | ||
| return $client->test_range_request(new $config->filesystem()); | ||
|
|
||
| case self::TYPE_PERMISSIONS: | ||
| return $client->test_permissions(false); | ||
| } | ||
| } catch (Throwable $e) { | ||
| // Usually the SDKs will throw exceptions if something doesn't work, so we want to catch these. | ||
| return new result(result::CRITICAL, get_string('check:storecheck:error', 'tool_objectfs') | ||
| . $this->type . ': ' . $e->getMessage(), $e->getTraceAsString()); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Return action link | ||
| * @return action_link | ||
| */ | ||
| public function get_action_link(): ?action_link { | ||
| $str = get_string('check:settings', 'tool_objectfs'); | ||
| $url = new moodle_url('/admin/category.php', ['category' => 'tool_objectfs']); | ||
| return new action_link($url, $str); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,8 @@ | |
|
|
||
| namespace tool_objectfs\local\store; | ||
|
|
||
| use core\check\result; | ||
|
|
||
| abstract class object_client_base implements object_client { | ||
|
|
||
| protected $autoloader; | ||
|
|
@@ -89,25 +91,40 @@ public function generate_presigned_url($contenthash, $headers = array()) { | |
| */ | ||
| public function define_client_check() { | ||
| global $OUTPUT; | ||
|
|
||
| // TODO use MDL check api admin setting if available ??? | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO use https://tracker.moodle.org/browse/MDL-67898 if available, since it uses Ajax so page loading times are very quick |
||
|
|
||
| $checks = tool_objectfs_status_checks(); | ||
|
|
||
| $output = ''; | ||
| $connection = $this->test_connection(); | ||
| if ($connection->success) { | ||
| $output .= $OUTPUT->notification(get_string('settings:connectionsuccess', 'tool_objectfs'), 'notifysuccess'); | ||
| // Check permissions if we can connect. | ||
| $permissions = $this->test_permissions($this->testdelete); | ||
| if ($permissions->success) { | ||
| $output .= $OUTPUT->notification(key($permissions->messages), 'notifysuccess'); | ||
| } else { | ||
| foreach ($permissions->messages as $message => $type) { | ||
| $output .= $OUTPUT->notification($message, $type); | ||
| } | ||
|
|
||
| foreach ($checks as $check) { | ||
| // This is fixed in 4.4 by MDL-67898. | ||
| // But until that is more common, the component must be set manually, | ||
| // as the default is incorrect when viewed in the admin tree. | ||
| $check->set_component('tool_objectfs'); | ||
|
|
||
| $result = $check->get_result(); | ||
|
|
||
| $status = $result->get_status(); | ||
|
|
||
| // If status was N/A - ignore it. | ||
| if ($status == result::NA) { | ||
| continue; | ||
| } | ||
| } else { | ||
| $output .= $OUTPUT->notification( | ||
| get_string('settings:connectionfailure', 'tool_objectfs', $connection->details), | ||
| 'notifyproblem' | ||
| ); | ||
|
|
||
| $str = $check->get_name() . ": " . $result->get_summary(); | ||
|
|
||
| // Only include details if failure. | ||
| if ($status != result::OK) { | ||
| $str .= "\n" . $result->get_details(); | ||
| } | ||
|
|
||
| $type = $status == result::OK ? 'notifysuccess' : 'notifyerror'; | ||
|
|
||
| $output .= $OUTPUT->notification(nl2br($str), $type); | ||
| } | ||
|
|
||
| return $output; | ||
| } | ||
|
|
||
|
|
@@ -135,20 +152,20 @@ public function proxy_range_request(\stored_file $file, $ranges) { | |
| * Test proxy range request. | ||
| * | ||
| * @param object $filesystem Filesystem to be tested. | ||
| * @return object | ||
| * @return result | ||
| */ | ||
| public function test_range_request($filesystem) { | ||
| return (object)['result' => false, 'error' => '']; | ||
| public function test_range_request($filesystem): result { | ||
| return new result(result::UNKNOWN, ''); | ||
matthewhilton marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
| * Tests connection to external storage. | ||
| * Override this method in client class. | ||
| * | ||
| * @return object | ||
| * @return result | ||
| */ | ||
| public function test_connection() { | ||
| return (object)['success' => false, 'details' => '']; | ||
| public function test_connection(): result { | ||
| return new result(result::UNKNOWN, ''); | ||
matthewhilton marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
|
|
@@ -158,7 +175,17 @@ public function test_connection() { | |
| * @param bool $testdelete Test delete permission and fail the test if could delete object from the storage. | ||
| * @return object | ||
| */ | ||
| public function test_permissions($testdelete) { | ||
| return (object)['success' => false, 'details' => '']; | ||
| public function test_permissions($testdelete): result { | ||
| return new result(result::UNKNOWN, ''); | ||
matthewhilton marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
| * Tests configuration is OK. | ||
| * Override this method in client class. | ||
| * | ||
| * @return result | ||
| */ | ||
| public function test_configuration(): result { | ||
| return new result(result::UNKNOWN, ''); | ||
matthewhilton marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.