From eeeb2199b767bd1d281508729c4b5a1c6e920b0d Mon Sep 17 00:00:00 2001 From: Jimmy Lin Date: Sun, 22 Sep 2024 14:11:46 -0400 Subject: [PATCH] Add Linode object storage support --- classes/linode_file_system.php | 35 ++++++ classes/local/manager.php | 1 + classes/local/store/linode/client.php | 139 +++++++++++++++++++++ classes/local/store/linode/file_system.php | 54 ++++++++ lang/en/tool_objectfs.php | 10 ++ 5 files changed, 239 insertions(+) create mode 100644 classes/linode_file_system.php create mode 100644 classes/local/store/linode/client.php create mode 100644 classes/local/store/linode/file_system.php diff --git a/classes/linode_file_system.php b/classes/linode_file_system.php new file mode 100644 index 00000000..33041090 --- /dev/null +++ b/classes/linode_file_system.php @@ -0,0 +1,35 @@ +. + +/** + * File system for Linode Ocean Storage. + * + * @package tool_objectfs + * @author Brian Yanosik + * @copyright Catalyst IT + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace tool_objectfs; + +use tool_objectfs\local\store\linode\file_system; + +/** + * Unknown? + */ +class linode_file_system extends file_system { + +} diff --git a/classes/local/manager.php b/classes/local/manager.php index acd2b30e..afaa3e52 100644 --- a/classes/local/manager.php +++ b/classes/local/manager.php @@ -308,6 +308,7 @@ public static function get_available_fs_list() { $filesystems['\tool_objectfs\azure_file_system'] = '\tool_objectfs\azure_file_system'; $filesystems['\tool_objectfs\digitalocean_file_system'] = '\tool_objectfs\digitalocean_file_system'; + $filesystems['\tool_objectfs\linode_file_system'] = '\tool_objectfs\linode_file_system'; $filesystems['\tool_objectfs\s3_file_system'] = '\tool_objectfs\s3_file_system'; $filesystems['\tool_objectfs\swift_file_system'] = '\tool_objectfs\swift_file_system'; diff --git a/classes/local/store/linode/client.php b/classes/local/store/linode/client.php new file mode 100644 index 00000000..71761ba1 --- /dev/null +++ b/classes/local/store/linode/client.php @@ -0,0 +1,139 @@ +. + +/** + * Linode Object Spaces client. + * + * @package tool_objectfs + * @author Brian Yanosik + * @copyright Brian Yanosik + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace tool_objectfs\local\store\linode; + +use tool_objectfs\local\store\s3\client as s3_client; + +/** + * client + */ +class client extends s3_client { + + /** + * construct + * @param \stdClass $config + * @return void + */ + public function __construct($config) { + global $CFG; + $this->autoloader = $CFG->dirroot . '/local/aws/sdk/aws-autoloader.php'; + $this->testdelete = false; + + if ($this->get_availability() && !empty($config)) { + require_once($this->autoloader); + $this->bucket = $config->linode_bucket; + $this->set_client($config); + } else { + parent::__construct($config); + } + } + + /** + * Check if the client configured properly. + * + * @param \stdClass $config Client config. + * @return bool + */ + protected function is_configured($config) { + if (empty($config->linode_key) || empty($config->linode_secret) || empty($config->linode_region)) { + return false; + } + + return true; + } + + /** + * set_client + * @param \stdClass $config + * + * @return void + */ + public function set_client($config) { + if (!$this->is_configured($config)) { + $this->client = null; + return; + } + + $this->client = \Aws\S3\S3Client::factory([ + 'credentials' => ['key' => $config->linode_key, 'secret' => $config->linode_secret], + 'region' => $config->linode_region, + 'endpoint' => 'https://' . $config->linode_region . '.linodeobjects.com', + 'version' => AWS_API_VERSION, + ]); + } + + /** + * define_client_section + * @param admin_settingpage $settings + * @param \stdClass $config + * @return admin_settingpage + */ + public function define_client_section($settings, $config) { + + $regionoptions = [ + 'nl-ams-1' => 'nl-ams-1 (Amsterdam, Netherlands)', + 'us-southeast-1' => 'us-southeast-1 (Atlanta, GA, USA)', + 'in-maa-1' => 'in-maa-1 (Chennai, India)', + 'us-ord-1' => 'us-ord-1 (Chicago, IL, USA)', + 'eu-central-1' => 'eu-central-1 (Frankfurt, Germany)', + 'id-cgk-1' => 'id-cgk-1 (Jakarta, Indonesia)', + 'us-lax-1' => 'us-lax-1 (Los Angeles, CA, USA)', + 'es-mad-1' => 'es-mad-1 (Madrid, Spain)', + 'us-mia-1' => 'us-mia-1 (Miami, FL, USA)', + 'it-mil-1' => 'it-mil-1 (Milan, Italy)', + 'us-east-1' => 'us-east-1 (Newark, NJ, USA)', + 'jp-osa-1' => 'jp-osa-1 (Osaka, Japan)', + 'fr-par-1' => 'fr-par-1 (Paris, France)', + 'br-gru-1' => 'br-gru-1 (São Paulo, Brazil)', + 'us-sea-1' => 'us-sea-1 (Seattle, WA, USA)', + 'ap-south-1' => 'ap-south-1 (Singapore)', + 'se-sto-1' => 'se-sto-1 (Stockholm, Sweden)', + 'us-iad-1' => 'us-iad-1 (Washington, DC, USA)', + ]; + + $settings->add(new \admin_setting_heading('tool_objectfs/linode', + new \lang_string('settings:linode:header', 'tool_objectfs'), '')); + + $settings->add(new \admin_setting_configtext('tool_objectfs/linode_key', + new \lang_string('settings:linode:key', 'tool_objectfs'), + new \lang_string('settings:linode:key_help', 'tool_objectfs'), '')); + + $settings->add(new \admin_setting_configpasswordunmask('tool_objectfs/linode_secret', + new \lang_string('settings:linode:secret', 'tool_objectfs'), + new \lang_string('settings:linode:secret_help', 'tool_objectfs'), '')); + + $settings->add(new \admin_setting_configtext('tool_objectfs/linode_bucket', + new \lang_string('settings:linode:bucket', 'tool_objectfs'), + new \lang_string('settings:linode:bucket_help', 'tool_objectfs'), '')); + + $settings->add(new \admin_setting_configselect('tool_objectfs/linode_region', + new \lang_string('settings:linode:region', 'tool_objectfs'), + new \lang_string('settings:linode:region_help', 'tool_objectfs'), '', $regionoptions)); + + return $settings; + } + +} diff --git a/classes/local/store/linode/file_system.php b/classes/local/store/linode/file_system.php new file mode 100644 index 00000000..a685e684 --- /dev/null +++ b/classes/local/store/linode/file_system.php @@ -0,0 +1,54 @@ +. + +/** + * object_file_system abstract class. + * + * Remote object storage providers extent this class. + * At minimum you need to implement get_remote_client. + * + * @package tool_objectfs + * @author Brian Yanosik + * @copyright Brian Yanosik + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace tool_objectfs\local\store\linode; + +defined('MOODLE_INTERNAL') || die(); + +use tool_objectfs\local\store\s3\file_system as s3_file_system; + +require_once($CFG->dirroot . '/admin/tool/objectfs/lib.php'); + +/** + * [Description file_system] + */ +class file_system extends s3_file_system { + + /** + * initialise_external_client + * @param \stdClass $config + * + * @return client + */ + protected function initialise_external_client($config) { + $linodeclient = new client($config); + + return $linodeclient; + } + +} diff --git a/lang/en/tool_objectfs.php b/lang/en/tool_objectfs.php index 74b383a5..e1c806fb 100644 --- a/lang/en/tool_objectfs.php +++ b/lang/en/tool_objectfs.php @@ -138,6 +138,16 @@ $string['settings:do:region'] = 'Region'; $string['settings:do:region_help'] = 'DO Spaces API gateway region.'; +$string['settings:linode:header'] = 'Linode Object Storage Settings'; +$string['settings:linode:key'] = 'Key'; +$string['settings:linode:key_help'] = 'Linode Object Storage key credential.'; +$string['settings:linode:secret'] = 'Secret'; +$string['settings:linode:secret_help'] = 'Linode Object Storage secret credential.'; +$string['settings:linode:bucket'] = 'Space'; +$string['settings:linode:bucket_help'] = 'Linode Object Storage to store files in.'; +$string['settings:linode:region'] = 'Region'; +$string['settings:linode:region_help'] = 'Linode Object Storage API gateway region.'; + $string['settings:azure:header'] = 'Azure Blob Storage Settings'; $string['settings:azure:accountname'] = 'Account name'; $string['settings:azure:accountname_help'] = 'The name of the storage account.';