Skip to content

Commit

Permalink
More tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewhilton committed Jan 12, 2024
1 parent 3df4d04 commit 13b8497
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 30 deletions.
16 changes: 9 additions & 7 deletions classes/check/configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,23 @@
use core\check\result;

class configuration extends check {
public function get_result(): result
{
public function get_result(): result {
// Load objectfs and run a test.
$config = \tool_objectfs\local\manager::get_objectfs_config();
$client = \tool_objectfs\local\manager::get_client($config);

if (empty($client)) {
return new result(result::WARNING, "TODO lang Client was empty");
return new result(result::UNKNOWN, get_string('check:configuration:empty', 'tool_objectfs'));
}

$configstatus = $client->get_configuration_check_status();
$status = $configstatus['ok'] ? result::OK : result::ERROR;
$summary = $configstatus['ok'] ? "OK" : "ERRORS"; // TODO lang.
$details = nl2br($configstatus['details']);
$configstatus = $client->test_configuration();
$status = $configstatus->success ? result::OK : result::ERROR;
$summary = $configstatus->success ? get_string('check:configuration:ok', 'tool_objectfs')
: get_string('check:configuration:error', 'tool_objectfs');
$details = nl2br($configstatus->details);

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

// TODO action link
}
24 changes: 16 additions & 8 deletions classes/check/store_check.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,28 @@ 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, "todo lang Not configured.");
return new result(result::NA, get_string('check:notenabled', 'tool_objectfs'));
}

// Load objectfs and run a test.
$config = \tool_objectfs\local\manager::get_objectfs_config();
$client = \tool_objectfs\local\manager::get_client($config);

if (empty($client)) {
return new result(result::UNKNOWN, "TODO lang client not configured");
return new result(result::UNKNOWN, get_string('check:configuration:empty', 'tool_objectfs'));
}

$results = [];
if (!$client->test_configuration()->success) {
// Not confingured yet, don't bother testing connection or permissions.
return new result(result::NA, get_string('check:storecheck:notconfiguredskip', 'tool_objectfs'));
}

$results = (object) [];

// TODO test delete.
switch($this->type) {
case self::TYPE_CONNECTION:
$results = $client->test_permissions(false);
$results = $client->test_connection(false);
break;

case self::TYPE_RANGEREQUEST:
Expand All @@ -63,13 +68,16 @@ public function get_result(): result {
}

if (empty($results)) {
return new result(result::UNKNOWN, "TODO lang Test type " . $this->type . " was configured, but no test was executed.");
return new result(result::UNKNOWN, get_string('check:storecheck:nothingexecuted', 'tool_objectfs'));
}

$status = $results['success'] ? result::OK : result::ERROR;
return new result($status, $results['details']);
$status = $results->success ? result::OK : result::ERROR;
return new result($status, $results->details ?? '');
} catch (Throwable $e) {
return new result(result::CRITICAL, "TODO lang Error while executing store check type " . $this->type . ': ' . $e->getMessage());
return new result(result::CRITICAL, get_string('check:storecheck:error', 'tool_objectfs')
. $this->type . ': ' . $e->getMessage(), $e->getTraceAsString());
}
}

// TODO action link
}
2 changes: 1 addition & 1 deletion classes/local/store/object_client.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function test_connection();
public function test_permissions($testdelete);
public function proxy_range_request(\stored_file $file, $ranges);
public function test_range_request($filesystem);
public function get_configuration_check_status();
public function test_configuration();
}


16 changes: 11 additions & 5 deletions classes/local/store/object_client_base.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,6 @@ public function generate_presigned_url($contenthash, $headers = array()) {
throw new \coding_exception("Pre-signed URLs not supported");
}

public function get_configuration_check_status() {
return ['ok' => false, 'details' => ''];
}

/**
* Moodle admin settings form to display connection details for the client service.
*
Expand Down Expand Up @@ -142,7 +138,7 @@ public function proxy_range_request(\stored_file $file, $ranges) {
* @return object
*/
public function test_range_request($filesystem) {
return (object)['result' => false, 'error' => ''];
return (object)['result' => false, 'details' => ''];
}

/**
Expand All @@ -165,4 +161,14 @@ public function test_connection() {
public function test_permissions($testdelete) {
return (object)['success' => false, 'details' => ''];
}

/**
* Tests configuration is OK.
* Override this method in client class.
*
* @return object
*/
public function test_configuration() {
return (object)['success' => false, 'details' => ''];
}
}
22 changes: 13 additions & 9 deletions classes/local/store/s3/client.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,25 +89,29 @@ public function __wakeup() {
* @return bool
*/
protected function is_functional() {
return isset($this->client);
return !empty($this->client);
}

public function get_configuration_check_status() {
/**
* Tests that the configuration is ok.
* @return object with 'success' and 'details' values.
*/
public function test_configuration() {
$ok = $this->is_configuration_valid($this->config);

$configcheck = $this->check_configuration($this->config);
$details = '';

$lookup = [ // TODO lang strings
true => 'GOOD',
false => "Missing",
null => "N/A",
$lookup = [
true => get_string('settings:config:exists', 'tool_objectfs'),
false => get_string('settings:config:missing', 'tool_objectfs'),
null => get_string('settings:config:na', 'tool_objectfs'),
];

foreach ($configcheck as $check => $result) {
$details .= $check . ":" . $lookup[$result] . "\n";
}
return ['ok' => $ok, 'details' => $details];
return (object) ['success' => $ok, 'details' => $details];
}

/**
Expand All @@ -129,7 +133,7 @@ protected function check_configuration($config) {
* @return bool
*/
protected function is_configuration_valid($config) {
return !in_array(false, $this->check_configuration($config));
return !in_array(false, $this->check_configuration($config), true);
}

/**
Expand Down Expand Up @@ -304,7 +308,7 @@ public function test_permissions($testdelete) {
$permissions->success = true;
$permissions->messages = array();

if ($this->is_functional()) {
if (!$this->is_functional()) {
$permissions->success = false;
$permissions->messages = array();
return $permissions;
Expand Down
19 changes: 19 additions & 0 deletions lang/en/tool_objectfs.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,22 @@

$string['check:proxyrangerequestsdisabled'] = 'The proxy range request setting is disabled.';
$string['checkproxy_range_request'] = 'Pre-signed URL range request proxy';

$string['settings:config:missing'] = 'Missing';
$string['settings:config:na'] = 'N/A';
$string['settings:config:exists'] = 'Exists';

$string['check:notenabled'] = 'Object storage not enabled';

$string['checkconfiguration'] = 'Object storage configuration';
$string['check:configuration:ok'] = 'Configuration exists';
$string['check:configuration:error'] = 'Configuration error';
$string['check:configuration:empty'] = 'Client created from config was empty';

$string['checkstore_check_connection'] = 'Object storage connection';
$string['checkstore_check_permissions'] = 'Object storage permissions';
$string['checkstore_check_rangerequest'] = 'Object storage range requests';
$string['check:storecheck:error'] = 'Error while trying to run check: ';
$string['check:storecheck:notconfiguredskip'] = 'Object storage not configured - skipping';
$string['check:storecheck:nothingexecuted'] = 'No results were returned from the tests';

0 comments on commit 13b8497

Please sign in to comment.