Skip to content
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

WIP check api #592

Draft
wants to merge 5 commits into
base: MOODLE_310_STABLE
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions classes/check/configuration.php
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 <matthewhilton@catalyst-au.net>
* @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);
}
}
69 changes: 0 additions & 69 deletions classes/check/proxy_range_request.php

This file was deleted.

127 changes: 127 additions & 0 deletions classes/check/store.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?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 <matthewhilton@catalyst-au.net>
* @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:
// Range requests require presigned url support. If not supported, return N/A.
if (!$client->support_presigned_urls()) {
return new result(result::NA, get_string('check:storecheck:unsupportedrangerequest', 'tool_objectfs'));
}

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::ERROR, 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);
}
}
3 changes: 2 additions & 1 deletion classes/local/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
namespace tool_objectfs\local;

use stdClass;
use tool_objectfs\local\store\object_client;
use tool_objectfs\local\store\object_file_system;

defined('MOODLE_INTERNAL') || die();
Expand Down Expand Up @@ -115,7 +116,7 @@ public static function get_objectfs_config() {

/**
* @param $config
* @return bool
* @return bool|object_client
*/
public static function get_client($config) {
$clientclass = self::get_client_classname_from_fs($config->filesystem);
Expand Down
34 changes: 19 additions & 15 deletions classes/local/store/azure/client.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

namespace tool_objectfs\local\store\azure;

use core\check\result;
use SimpleXMLElement;
use stdClass;
use tool_objectfs\local\store\azure\stream_wrapper;
Expand Down Expand Up @@ -202,25 +203,27 @@ protected function get_filepath_from_hash($contenthash) {
return "$l1/$l2/$contenthash";
}

public function test_connection() {
$connection = new \stdClass();
$connection->success = true;
$connection->details = '';

/**
* Tests the connection
* @return result
*/
public function test_connection(): result {
try {
$this->client->createBlockBlob($this->container, 'connection_check_file', 'connection_check_file');
} catch (\MicrosoftAzure\Storage\Common\Exceptions\ServiceException $e) {
$connection->success = false;
$connection->details = $this->get_exception_details($e);
return new result(result::ERROR, get_string('check:failed', 'tool_objectfs'), $this->get_exception_details($e));
} catch (\GuzzleHttp\Exception\ConnectException $e) {
$connection->success = false;
$connection->details = $e->getMessage();
return new result(result::ERROR, get_string('check:failed', 'tool_objectfs'), $e->getMessage());
}

return $connection;
return new result(result::OK, get_string('check:passed', 'tool_objectfs'));
}

public function test_permissions($testdelete) {
/**
* Tests permission and returns result
* @return result
*/
public function test_permissions($testdelete): result {
matthewhilton marked this conversation as resolved.
Show resolved Hide resolved
$permissions = new \stdClass();
$permissions->success = true;
$permissions->messages = array();
Expand Down Expand Up @@ -261,11 +264,12 @@ public function test_permissions($testdelete) {
}
}

if ($permissions->success) {
$permissions->messages[get_string('settings:permissioncheckpassed', 'tool_objectfs')] = 'notifysuccess';
}
$status = $permissions->success ? result::OK : result::ERROR;
$summarystr = result::OK ? 'check:passed' : 'check:failed';
$summary = get_string($summarystr, 'tool_objectfs');
$details = implode("\n", $permissions->messages);

return $permissions;
return new result($status, $summary, $details);
}

protected function get_exception_details(\MicrosoftAzure\Storage\Common\Exceptions\ServiceException $exception) {
Expand Down
9 changes: 6 additions & 3 deletions classes/local/store/object_client.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

namespace tool_objectfs\local\store;

use core\check\result;

interface object_client {
public function __construct($config);
public function register_stream_wrapper();
Expand All @@ -37,10 +39,11 @@ public function get_maximum_upload_size();
public function verify_object($contenthash, $localpath);
public function generate_presigned_url($contenthash, $headers = array());
public function support_presigned_urls();
public function test_connection();
public function test_permissions($testdelete);
public function test_connection(): result;
public function test_permissions($testdelete): result;
public function proxy_range_request(\stored_file $file, $ranges);
public function test_range_request($filesystem);
public function test_range_request($filesystem): result;
public function test_configuration(): result;
}


Loading
Loading