From 9dd3a86147ee912e4246b37308b9573e65db7f11 Mon Sep 17 00:00:00 2001 From: Alex Yeung Date: Wed, 30 Oct 2024 14:30:00 +0000 Subject: [PATCH] CTP-3951 Set up AWS SQS connection for SORA --- classes/aws/sqs.php | 90 +++++++++++++++++++++++++++++++++ lang/en/local_sitsgradepush.php | 11 ++++ settings.php | 34 +++++++++++++ version.php | 2 +- 4 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 classes/aws/sqs.php diff --git a/classes/aws/sqs.php b/classes/aws/sqs.php new file mode 100644 index 0000000..3e8fff6 --- /dev/null +++ b/classes/aws/sqs.php @@ -0,0 +1,90 @@ +. + +namespace local_sitsgradepush\aws; + +use Aws\Sqs\SqsClient; +use core\aws\client_factory; + +defined('MOODLE_INTERNAL') || die(); + +require_once($CFG->dirroot . '/lib/aws-sdk/src/functions.php'); +require_once($CFG->dirroot . '/lib/guzzlehttp/guzzle/src/functions.php'); + +/** + * Class for SQS client. + * + * @package local_sitsgradepush + * @copyright 2024 onwards University College London {@link https://www.ucl.ac.uk/} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @author Alex Yeung + */ +class sqs { + + /** @var SqsClient AWS client */ + protected SqsClient $client; + + /** + * Constructor. + * + * @throws \moodle_exception + */ + public function __construct() { + // Check required configs are set. + $configs = $this->check_required_configs_are_set(); + + $this->client = client_factory::get_client('\Aws\Sqs\SqsClient', [ + 'region' => $configs->aws_region, + 'version' => 'latest', + 'credentials' => [ + 'key' => $configs->aws_key, + 'secret' => $configs->aws_secret, + ], + ]); + } + + /** + * Get the client. + * + * @return SqsClient + */ + public function get_client(): SqsClient { + return $this->client; + } + + /** + * Check required configs are set. + * + * @return object + * @throws \moodle_exception + */ + private function check_required_configs_are_set(): \stdClass { + $requiredfields = ['aws_region', 'aws_key', 'aws_secret']; + + $configs = []; + foreach ($requiredfields as $field) { + // Get the config value. + $config = get_config('local_sitsgradepush', $field); + + // Check if the config is empty. + if (empty($config)) { + throw new \moodle_exception('error:missingrequiredconfigs', 'local_sitsgradepush'); + } + $configs[$field] = $config; + } + return (object) $configs; + } +} diff --git a/lang/en/local_sitsgradepush.php b/lang/en/local_sitsgradepush.php index 3d107f5..23e5a2e 100644 --- a/lang/en/local_sitsgradepush.php +++ b/lang/en/local_sitsgradepush.php @@ -114,6 +114,7 @@ $string['error:mapassessment'] = 'You do not have permission to map assessment.'; $string['error:marks_transfer_failed'] = 'Marks transfer failed.'; $string['error:missingparams'] = 'Missing parameters.'; +$string['error:missingrequiredconfigs'] = 'Missing required configs.'; $string['error:mks_scheme_not_supported'] = 'Marking Scheme is not supported for marks transfer'; $string['error:multiplemappingsnotsupported'] = 'Multiple assessment component mappings is not supported by {$a}'; $string['error:no_mab_found'] = 'No assessment component found for this module delivery.'; @@ -194,6 +195,16 @@ $string['settings:apiclient'] = 'API client'; $string['settings:apiclient:desc'] = 'Choose which API client to use'; $string['settings:apiclientselect'] = 'Select API client'; +$string['settings:awskey'] = 'AWS Key'; +$string['settings:awskey:desc'] = 'AWS access key id'; +$string['settings:awsregion'] = 'AWS Region'; +$string['settings:awsregion:desc'] = 'AWS Server Region'; +$string['settings:awssecret'] = 'AWS Secret'; +$string['settings:awssecret:desc'] = 'AWS secret access key'; +$string['settings:awssettings'] = 'AWS'; +$string['settings:awssettings:desc'] = 'Settings for AWS'; +$string['settings:awssoraqueueurl'] = 'AWS SORA Queue URL'; +$string['settings:awssoraqueueurl:desc'] = 'URL for receiving SORA SQS messages'; $string['settings:concurrenttasks'] = 'Number of concurrent tasks allowed'; $string['settings:concurrenttasks:desc'] = 'Number of concurrent ad-hoc tasks allowed'; $string['settings:enable'] = 'Enable Marks Transfer'; diff --git a/settings.php b/settings.php index 77d92ce..c993d0f 100644 --- a/settings.php +++ b/settings.php @@ -157,6 +157,40 @@ get_string('settings:enableextension:desc', 'local_sitsgradepush'), '0' )); + + // Setting for AWS. + $settings->add(new admin_setting_heading('local_sitsgradepush_aws_settings', + get_string('settings:awssettings', 'local_sitsgradepush'), + get_string('settings:awssettings:desc', 'local_sitsgradepush') + )); + + // AWS region. + $settings->add(new admin_setting_configtext('local_sitsgradepush/aws_region', + get_string('settings:awsregion', 'local_sitsgradepush'), + get_string('settings:awsregion:desc', 'local_sitsgradepush'), + 'eu-west-2' + )); + + // AWS key. + $settings->add(new admin_setting_configtext('local_sitsgradepush/aws_key', + get_string('settings:awskey', 'local_sitsgradepush'), + get_string('settings:awskey:desc', 'local_sitsgradepush'), + 'AKIAX3UE2A7B2VLXHL2O' + )); + + // AWS secret. + $settings->add(new admin_setting_configpasswordunmask('local_sitsgradepush/aws_secret', + get_string('settings:awssecret', 'local_sitsgradepush'), + get_string('settings:awssecret:desc', 'local_sitsgradepush'), + 'CHANGEME' + )); + + // AWS SORA queue URL. + $settings->add(new admin_setting_configtext('local_sitsgradepush/aws_sora_sqs_queue_url', + get_string('settings:awssoraqueueurl', 'local_sitsgradepush'), + get_string('settings:awssoraqueueurl:desc', 'local_sitsgradepush'), + 'https://sqs.eu-west-2.amazonaws.com/540370667459/person-sora-dev' + )); } $subplugins = core_plugin_manager::instance()->get_plugins_of_type('sitsapiclient'); diff --git a/version.php b/version.php index 12897fa..20e1660 100644 --- a/version.php +++ b/version.php @@ -27,7 +27,7 @@ $plugin->component = 'local_sitsgradepush'; $plugin->release = '0.1.0'; -$plugin->version = 2024101100; +$plugin->version = 2024103000; $plugin->requires = 2024042200; $plugin->maturity = MATURITY_ALPHA; $plugin->dependencies = [