From ab516eb3674843e5b0411cef7fc9e6443226b517 Mon Sep 17 00:00:00 2001 From: Pranjal Walia Date: Sun, 4 Sep 2022 02:26:13 +0530 Subject: [PATCH] feat: add plugin usage examples --- examples/compute/ali-eci.js | 51 +++++++++++++++++++++ examples/compute/ali-ecs.js | 58 ++++++++++++++++++++++++ examples/database/ali-nosql.js | 55 ++++++++++++++++++++++ examples/database/ali-rdbms.js | 48 ++++++++++++++++++++ examples/network/ali-slb.js | 53 ++++++++++++++++++++++ examples/storage/ali-oss.js | 83 ++++++++++++++++++++++++++++++++++ 6 files changed, 348 insertions(+) create mode 100644 examples/compute/ali-eci.js create mode 100644 examples/compute/ali-ecs.js create mode 100644 examples/database/ali-nosql.js create mode 100644 examples/database/ali-rdbms.js create mode 100644 examples/network/ali-slb.js create mode 100644 examples/storage/ali-oss.js diff --git a/examples/compute/ali-eci.js b/examples/compute/ali-eci.js new file mode 100644 index 00000000..31fe5f97 --- /dev/null +++ b/examples/compute/ali-eci.js @@ -0,0 +1,51 @@ +const nodeCloud = require('../../lib/'); +const optionsProvider = { + overrideProviders: false, +}; +const ncProviders = nodeCloud.getProviders(optionsProvider); + +// get containerInstance object for AliCloud +const containers = ncProviders.alicloud.containerInstance(); + +async function createContainerGroup() { + const instanceParams = { + regionId: 'cn-hangzhou', + containerGroupName: 'test-group-1', + container: [ + { + command: ['/bin/sh', '-c', 'echo 1'], + cpu: 1, + memory: 512, + image: 'ubuntu', + name: 'test-container-1', + }, + ], + }; + try { + const res = await containers.create(instanceParams); + console.log('All done! ', res); + } catch (err) { + console.log(`Oops something happened ${err}`); + } +} + +async function listContainerGroup() { + try { + const res = await containers.list({ regionId: 'cn-hangzhou' }); + console.log('All done! ', res); + } catch (err) { + console.log(`Oops something happened ${err}`); + } +} + +async function deleteContainerGroup() { + try { + const res = await containers.delete({ + regionId: 'cn-hangzhou', + containerGroupId: 'eci-bp1ikor0s871wa5pahke', + }); + console.log('All done! ', res); + } catch (err) { + console.log(`Oops something happened ${err}`); + } +} diff --git a/examples/compute/ali-ecs.js b/examples/compute/ali-ecs.js new file mode 100644 index 00000000..78241b4a --- /dev/null +++ b/examples/compute/ali-ecs.js @@ -0,0 +1,58 @@ +const nodeCloud = require('../../lib/'); +const optionsProvider = { + overrideProviders: false, +}; +const ncProviders = nodeCloud.getProviders(optionsProvider); + +// get compute object for AliCloud +const compute = ncProviders.alicloud.computeInstance(); + +async function createInstance() { + const instanceParams = { + regionId: 'cn-hongkong', + imageId: 'ubuntu_20_04_x64_20G_alibase_20220215.vhd', + instanceType: 'ecs.n4.large', + }; + try { + const res = await compute.create(instanceParams); + console.log('All done! ', res); + } catch (err) { + console.log(`Oops something happened ${err}`); + } +} + +async function listInstances() { + try { + const res = await compute.list({ regionId: 'cn-hongkong' }); + console.log('All done! ', res); + } catch (err) { + console.log(`Oops something happened ${err}`); + } +} + +async function destroyInstance() { + try { + const res = await compute.destroy({ instanceId: 'i-2zmq8q8' }); + console.log('All done! ', res); + } catch (err) { + console.log(`Oops something happened ${err}`); + } +} + +async function describeInstanceTypes() { + try { + const res = await compute.listInstanceTypes({ maxResults: 10 }); + console.log('All done! ', res); + } catch (err) { + console.log(`Oops something happened ${err}`); + } +} + +async function describeImageTypes() { + try { + const res = await compute.describeImages({ regionId: 'cn-hongkong' }); + console.log('All done! ', res); + } catch (err) { + console.log(`Oops something happened ${err}`); + } +} diff --git a/examples/database/ali-nosql.js b/examples/database/ali-nosql.js new file mode 100644 index 00000000..eff7d9a9 --- /dev/null +++ b/examples/database/ali-nosql.js @@ -0,0 +1,55 @@ +const nodeCloud = require('../../lib/'); +const optionsProvider = { + overrideProviders: false, +}; +const ncProviders = nodeCloud.getProviders(optionsProvider); + +// get containerInstance object for AliCloud +const nosql = ncProviders.alicloud.nosql(); + +async function createInstance() { + const instanceParams = { + regionId: 'ap-southeast-1', + engine: 'MongoDB', + engineVersion: '4.2', + DBInstanceClass: 'dds.mongo.standard', + DBInstanceStorage: 10, // GB + }; + try { + const res = await nosql.createInstance(instanceParams); + console.log('All done! ', res); + } catch (err) { + console.log(`Oops something happened ${err}`); + } +} + +async function deleteInstance() { + try { + const res = await nosql.deleteInstance({ + DBInstanceId: 'dds-gs58537d0d9724c4', + }); + console.log('All done! ', res); + } catch (err) { + console.log(`Oops something happened ${err}`); + } +} + +async function listInstances() { + try { + const res = await nosql.listInstances({}); + console.log('All done! ', res); + } catch (err) { + console.log(`Oops something happened ${err}`); + } +} + +async function describeInstance() { + try { + const res = await nosql.describeInstance({ + DBInstanceId: 'dds-gs5581f86ac37b14', + }); + console.log('All done! ', res); + } catch (err) { + console.log(`Oops something happened ${err}`); + } +} diff --git a/examples/database/ali-rdbms.js b/examples/database/ali-rdbms.js new file mode 100644 index 00000000..02b4ade1 --- /dev/null +++ b/examples/database/ali-rdbms.js @@ -0,0 +1,48 @@ +const nodeCloud = require('../../lib/'); +const optionsProvider = { + overrideProviders: false, +}; +const ncProviders = nodeCloud.getProviders(optionsProvider); + +// get containerInstance object for AliCloud +const rdbms = ncProviders.alicloud.rdbms(); + +async function createInstance() { + const instanceParams = { + DBInstanceClass: 'rds.mysql.t1.small', + regionId: 'ap-southeast-1', + engine: 'MySQL', + engineVersion: '5.6', + DBInstanceStorage: 10, + DBInstanceNetType: 'Intranet', + securityIPList: '0.0.0.0/0', + payType: 'Postpaid', + DBInstanceStorageType: 'local_ssd', + }; + try { + const res = await rdbms.createInstance(instanceParams); + console.log('All done! ', res); + } catch (err) { + console.log(`Oops something happened ${err}`); + } +} + +async function deleteInstance() { + try { + const res = await rdbms.deleteInstance({ + DBInstanceId: 'rm-1udg1v5w25c8gmpx3', + }); + console.log('All done! ', res); + } catch (err) { + console.log(`Oops something happened ${err}`); + } +} + +async function listInstances() { + try { + const res = await rdbms.listInstances({ regionId: 'ap-southeast-1' }); + console.log('All done! ', res); + } catch (err) { + console.log(`Oops something happened ${err}`); + } +} diff --git a/examples/network/ali-slb.js b/examples/network/ali-slb.js new file mode 100644 index 00000000..ab53bc77 --- /dev/null +++ b/examples/network/ali-slb.js @@ -0,0 +1,53 @@ +const nodeCloud = require('../../lib/'); +const optionsProvider = { + overrideProviders: false, +}; +const ncProviders = nodeCloud.getProviders(optionsProvider); + +// get containerInstance object for AliCloud +const loadbalancer = ncProviders.alicloud.loadbalancer(); + +async function createInstance() { + try { + const res = await loadbalancer.create( + 'cn-hangzhou', + 'test-slb', + 'slb.s1.small', + {} + ); + console.log('All done! ', res); + } catch (err) { + console.log(`Oops something happened ${err}`); + } +} + +async function deleteInstance() { + try { + const res = await loadbalancer.delete('lb-1udjouk9sqkq5lvke5cd6', {}); + console.log('All done! ', res); + } catch (err) { + console.log(`Oops something happened ${err}`); + } +} + +async function listInstances() { + try { + const res = await loadbalancer.list('cn-hangzhou', {}); + console.log('All done! ', res); + } catch (err) { + console.log(`Oops something happened ${err}`); + } +} + +async function describeInstances() { + try { + const res = await loadbalancer.describe( + 'cn-hangzhou', + 'lb-1udjouk9sqkq5lvke5cd6', + {} + ); + console.log('All done! ', res); + } catch (err) { + console.log(`Oops something happened ${err}`); + } +} diff --git a/examples/storage/ali-oss.js b/examples/storage/ali-oss.js new file mode 100644 index 00000000..32a3f696 --- /dev/null +++ b/examples/storage/ali-oss.js @@ -0,0 +1,83 @@ +const nodeCloud = require('../../lib/'); +const path = require('path'); +const optionsProvider = { + overrideProviders: false, +}; +const ncProviders = nodeCloud.getProviders(optionsProvider); + +// get bucketStorage object for AliCloud +const bucketStorage = ncProviders.alicloud.bucketStorage(); + +async function createBucket() { + try { + const res = await bucketStorage.create('unique-test-bucket-1234', { + acl: 'public-read', + dataRedundancyType: 'LRS', + storageClass: 'Standard', + timeout: 30000, + }); + console.log('All done! ', res); + } catch (err) { + console.log(`Oops something happened ${err}`); + } +} + +async function listBuckets() { + try { + const res = await bucketStorage.listBuckets({}); + console.log('All done! ', res); + } catch (err) { + console.log(`Oops something happened ${err}`); + } +} + +async function listBucketObjects() { + try { + const res = await bucketStorage.listBucketObjects( + 'unique-test-bucket-1234', + { + 'max-keys': 100, + } + ); + console.log('All done! ', res); + } catch (err) { + console.log(`Oops something happened ${err}`); + } +} + +async function uploadLocalObject() { + try { + const headers = { + // Specify the caching behavior of the web page when the object is downloaded. + 'Cache-Control': 'no-cache', + // Specify the name of the object when the object is downloaded. + 'Content-Disposition': 'oss_download.txt', + // Specify the content encoding format of the object when the object is downloaded. + 'Content-Encoding': 'UTF-8', + // Specify the expiration time. + Expires: 'Wed, 08 Jul 2022 16:57:01 GMT', + // Specify the storage class of the object. + 'x-oss-storage-class': 'Standard', + // Specify the access control list (ACL) of the object. + 'x-oss-object-acl': 'private', + // Set tags for the object. You can set multiple tags at a time. + 'x-oss-tagging': 'Tag1=1&Tag2=2', + // Specify whether the CopyObject operation overwrites the object with the same name. In this example, this parameter is set to true, which indicates that the object with the same name cannot be overwritten. + 'x-oss-forbid-overwrite': 'true', + }; + + const res = await bucketStorage.uploadLocalObject( + 'unique-test-bucket-1234', + 'test.txt', + path.normalize('absolute-file-path'), + { + headers, //Optional, Specify the headers of the object on upload + mime: 'text/plain', //Optional, Specify the content type on upload + timeout: 30000, //Optional, Specify the timeout of the request on upload + } + ); + console.log('All done! ', res); + } catch (err) { + console.log(`Oops something happened ${err}`); + } +}