diff --git a/.gitignore b/.gitignore index 6fc18b0..9a0b0ca 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ node_modules yarn-error.log package-lock.json -yarn.lock \ No newline at end of file +yarn.lock +.DS_Store \ No newline at end of file diff --git a/README.md b/README.md index 1c1ebb8..6681f41 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,12 @@ shark create droplet ``` +To create snapshot +```sh +shark create snapshot +``` + + To create ssh_key ```sh shark create ssh_key @@ -79,6 +85,11 @@ To delete droplet shark delete droplet ``` +To delete snapshot +```sh +shark delete snapshot +``` + To delete ssh_key ```sh shark delete ssh_key @@ -108,6 +119,11 @@ To list droplets shark list droplets ``` +To list droplets +``` +shark list snapshots +``` + To list ssh_keys ``` shark list ssh_keys diff --git a/cmds/create.js b/cmds/create.js index 6b3ff9b..bc644e3 100644 --- a/cmds/create.js +++ b/cmds/create.js @@ -82,6 +82,29 @@ module.exports.floating_ip = async () => { } }; +module.exports.snapshot = async () => { + try { + const answers = await Create.snapshot(); + spinner.start('Creating snapshot..'); + const { + body: { action } + } = await DoAPI.dropletsRequestAction( + answers.droplet.id, + { + type: 'snapshot', + name: answers.snapshot_name + } + ); + + if (action) { + spinner.succeed('Snapshot is being created!'); + } + } catch (error) { + spinner.stop(); + console.log(error.message); + } +}; + module.exports.volume = async () => { try { const answers = await Create.volume(); diff --git a/cmds/delete.js b/cmds/delete.js index 7c0c70a..7c6796f 100644 --- a/cmds/delete.js +++ b/cmds/delete.js @@ -49,6 +49,36 @@ module.exports.droplet = async () => { } }; +module.exports.snapshot = async () => { + try { + const answers = await deletePrompts.snapshot(); + if (answers.snapshots.length > 0) { + const { delete_snapshot } = await deletePrompts.confirmDelete('snapshot'); + if (delete_snapshot) { + spinner.start('Deleting your snapshot...'); + answers.snapshots.map(async snapshot => { + try { + const data = await DoAPI.snapshotsDeleteById(snapshot); + if (data.response.statusCode === 204) { + spinner.succeed(`${snapshot} is deleted!`); + } + } catch (error) { + spinner.fail(`failed to delete ${snapshot}`); + console.log(error.message); + } + }); + } + } else { + console.log( + 'Please select atleast one snapshot to perform this operation!' + ); + } + } catch (error) { + spinner.stop(); + console.error(error); + } +}; + module.exports.ssh_key = async () => { try { const answers = await deletePrompts.ssh_key(); diff --git a/cmds/list.js b/cmds/list.js index 8071306..4650a98 100644 --- a/cmds/list.js +++ b/cmds/list.js @@ -75,6 +75,41 @@ ${chalk.bold(' CE:')} ${calData.totalCost.toLocaleString('en-US', { } }; +module.exports.snapshots = async () => { + try { + spinner.start('Loading Snapshot...'); + const list = await DoAPI.snapshots(); + spinner.stop(); + if (list.body.meta.total === 0) { + /* prettier-ignore */ + console.log('You don\'t have any snapshots'); + } else { + console.log( + `You have ${chalk.magenta(list.body.meta.total)} ${ + list.body.meta.total > 1 ? 'Snapshots' : 'Snapshot' + }` + ); + list.body.snapshots.map(snapshot => { + /* eslint-disable indent */ + return console.log( + `---------------------- +${chalk.bold(' Name:')} ${chalk.blue(snapshot.name)} +${chalk.bold(' Id:')} ${snapshot.id} +${chalk.bold(' Created at:')} ${snapshot.created_at} +${chalk.bold('Ressource Id:')} ${snapshot.resource_id} +${chalk.bold(' Size:')} ${snapshot.size_gigabytes} +${chalk.bold(' Regions:')} ${snapshot.regions} + ` + ); + /* eslint-enable */ + }); + } + } catch (error) { + spinner.stop(); + console.error(error); + } +}; + module.exports.ssh_keys = async () => { try { spinner.start('Loading sshkeys...'); diff --git a/loaders.js b/loaders.js index a571039..d14d3c9 100644 --- a/loaders.js +++ b/loaders.js @@ -158,6 +158,29 @@ module.exports.loadAvailableImages = async () => { } }; +module.exports.loadAvailableSnapshots = async () => { + try { + spinner.start('Loading available snapshots...'); + const data = await DoAPI.snapshots(); + spinner.stop(); + const availableSnapshots = []; + if (data.body.meta.total === 0) { + console.log('You donn\'t have any snapshots'); + process.exit(); + } else { + data.body.snapshots.map(snapshot => + availableSnapshots.push({ + name: snapshot.name, + value: snapshot.id + }) + ); + return availableSnapshots; + } + } catch (error) { + console.error(error); + } +}; + module.exports.loadAvailableSSHKEYS = async () => { try { spinner.start('Loading your ssh_keys...'); diff --git a/prompts/create.js b/prompts/create.js index abecc76..25af0cb 100644 --- a/prompts/create.js +++ b/prompts/create.js @@ -5,7 +5,8 @@ const { loadAvailableRegions, loadAvailableSizes, loadAvailableImages, - loadAvailableSSHKEYS + loadAvailableSSHKEYS, + loadAvailableDroplets } = require('../loaders'); module.exports.init = () => { @@ -16,6 +17,7 @@ module.exports.init = () => { message: 'What do you want to create?', choices: [ { name: 'Droplet', value: 'droplet' }, + { name: 'Snapshot', value: 'snapshot' }, { name: 'Floating Ip', value: 'floating_ip' }, { name: 'SSH Key', value: 'ssh_key' }, { name: 'Volume', value: 'volume' }, @@ -138,6 +140,24 @@ module.exports.floating_ip = async () => { return inquirer.prompt(questions); }; +module.exports.snapshot = async () => { + const questions = [ + { + type: 'list', + name: 'droplet', + message: 'Select of which droplet you want to create a snapshot', + choices: await loadAvailableDroplets() + }, + { + type: 'input', + name: 'snapshot_name', + message: 'Input your Snapshot Name' + } + ]; + + return inquirer.prompt(questions); +}; + module.exports.volume = async () => { const questions = [ { diff --git a/prompts/delete.js b/prompts/delete.js index 11a39ff..0975427 100644 --- a/prompts/delete.js +++ b/prompts/delete.js @@ -3,6 +3,7 @@ const inquirer = require('inquirer'); const chalk = require('chalk'); const { loadAvailableDroplets, + loadAvailableSnapshots, loadAvailableSSHKEYS, loadAvailableFloatingIps, loadAvailableVolumes @@ -16,6 +17,7 @@ module.exports.init = () => { message: 'What do you want to Delete?', choices: [ { name: 'Droplet', value: 'droplet' }, + { name: 'Snapshot', value: 'snapshot' }, { name: 'Floating Ip', value: 'floating_ip' }, { name: 'SSH Key', value: 'ssh_key' }, { name: 'Volume', value: 'volume' }, @@ -41,6 +43,19 @@ module.exports.droplet = async () => { return inquirer.prompt(questions); }; +module.exports.snapshot = async () => { + const questions = [ + { + type: 'checkbox', + name: 'snapshots', + message: 'Select the snapshot you want to delete:', + choices: await loadAvailableSnapshots() + } + ]; + + return inquirer.prompt(questions); +}; + module.exports.ssh_key = async () => { const questions = [ { diff --git a/prompts/list.js b/prompts/list.js index 4c452c7..f273e78 100644 --- a/prompts/list.js +++ b/prompts/list.js @@ -9,6 +9,7 @@ module.exports.init = () => { message: 'What do you want to list?', choices: [ { name: 'Droplets', value: 'droplets' }, + { name: 'Snapshots', value: 'snapshots' }, { name: 'SSH Keys', value: 'ssh_keys' }, { name: 'Floating Ips', value: 'floating_ips' }, { name: 'Volumes', value: 'volumes' },