Skip to content

Commit

Permalink
Connection validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Nguyen committed Oct 25, 2024
1 parent e0d97fe commit bf0e71e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
19 changes: 15 additions & 4 deletions classes/lifecycle/step.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

use admin_externalpage;
use backup_plan_dbops;
use core\output\notification;
use moodle_url;
use tool_lcbackupcoursestep\s3\helper;
use tool_lifecycle\local\manager\settings_manager;
Expand Down Expand Up @@ -279,7 +280,7 @@ public function extend_add_instance_form_definition($mform) {
$this->add_amazon_s3_settings($mform);
} else {
$mform->addElement('html', $OUTPUT->notification(get_string('s3_unmet_dependency', 'tool_lcbackupcoursestep'),
\core\output\notification::NOTIFY_WARNING));
notification::NOTIFY_WARNING));
}

}
Expand Down Expand Up @@ -365,6 +366,15 @@ public function extend_add_instance_form_validation(&$error, $data) {
if (empty($data['s3_region'])) {
$error['s3_region'] = get_string('required');
}

// Check connection if there is no error.
if (empty($error)) {
$connection = helper::check_connection($data);
if (!$connection->success) {
// We already show error on s3_status field, so no need to show it here.
$error['s3_status'] = '';
}
}
}

return $error;
Expand All @@ -379,14 +389,15 @@ public function extend_add_instance_form_validation(&$error, $data) {
public function extend_add_instance_form_definition_after_data($mform, $settings) {
global $OUTPUT;
if (!empty($settings['uses3'])) {
$connection = helper::check_connection($settings);
$data = $mform->exportValues();
$connection = helper::check_connection($data);
if (!$connection->success) {
$message = $OUTPUT->notification(get_string('s3_connection_error', 'tool_lcbackupcoursestep', $connection->details),
\core\output\notification::NOTIFY_ERROR);
notification::NOTIFY_ERROR);
$mform->setDefault('s3_status', $message);
} else {
$message = $OUTPUT->notification(get_string('s3_connection_success', 'tool_lcbackupcoursestep'),
\core\output\notification::NOTIFY_SUCCESS);
notification::NOTIFY_SUCCESS);
$mform->setDefault('s3_status', $message);
}
}
Expand Down
3 changes: 2 additions & 1 deletion classes/s3/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public static function met_dependency(): bool {
if (!file_exists($CFG->dirroot . '/local/aws/version.php')) {
return false;
}
require_once($CFG->dirroot . '/local/aws/sdk/aws-autoloader.php');
return true;
}

Expand Down Expand Up @@ -133,7 +134,7 @@ public static function upload_file(int $processid, int $instanceid, int $coursei
$connection = self::check_connection($settings);

if (!$connection->success) {
throw new \moodle_exception('s3_connection_error', 'tool_lcbackupcoursestep', $connection->details);
throw new \moodle_exception('s3_connection_error', 'tool_lcbackupcoursestep', '', $connection->details);
}

// Upload file.
Expand Down

0 comments on commit bf0e71e

Please sign in to comment.