Skip to content
This repository has been archived by the owner on Nov 3, 2020. It is now read-only.

Commit

Permalink
feat(snapshots): add create and delete
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderProd authored and Satya Rohith committed Dec 19, 2018
1 parent 3e0ef5a commit 94e864e
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
yarn-error.log
package-lock.json
yarn.lock
yarn.lock
.DS_Store
23 changes: 23 additions & 0 deletions cmds/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
30 changes: 30 additions & 0 deletions cmds/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
23 changes: 23 additions & 0 deletions loaders.js
Original file line number Diff line number Diff line change
Expand Up @@ -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...');
Expand Down
22 changes: 21 additions & 1 deletion prompts/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const {
loadAvailableRegions,
loadAvailableSizes,
loadAvailableImages,
loadAvailableSSHKEYS
loadAvailableSSHKEYS,
loadAvailableDroplets
} = require('../loaders');

module.exports.init = () => {
Expand All @@ -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' },
Expand Down Expand Up @@ -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 = [
{
Expand Down
15 changes: 15 additions & 0 deletions prompts/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const inquirer = require('inquirer');
const chalk = require('chalk');
const {
loadAvailableDroplets,
loadAvailableSnapshots,
loadAvailableSSHKEYS,
loadAvailableFloatingIps,
loadAvailableVolumes
Expand All @@ -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' },
Expand All @@ -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 = [
{
Expand Down

0 comments on commit 94e864e

Please sign in to comment.