import { axios } from '../axios-instance' ; import { BlockStorage , BlockStorageRequest } from '../models/block-storage' ; import { Snapshot } from '../models/snapshot' ; export class BlockStorageService { constructor ( ) { } /**
     * List all of the Block Storage volumes available on your account
     *
     * ### Example
     * ```js
     * import { DigitalOcean } from 'digitalocean-js';
     *
     * const client = new DigitalOcean('your-api-key');
     * const volumes = await client.blockStorage.getAllBlockStorage();
     * ```
     */ public getAllBlockStorage ( ) { return new Promise ( ( resolve , reject ) => { axios . get ( `/volumes` ) . then ( response => { resolve ( response . data . volumes ) ; } ) . catch ( error => { reject ( error ) ; } ) ; } ) ; } /**
     * Create a new Block Storage volume
     *
     * ### Example
     * ```js
     * import { DigitalOcean } from 'digitalocean-js';
     *
     * const client = new DigitalOcean('your-api-key');
     * const request = {
     *   size_gigabytes: 10,
     *   name: 'example',
     *   description: 'Block store for examples',
     *   region: 'nyc1'
     * };
     * const volume = await client.blockStorage.createBlockStorage(request);
     * ```
     */ public createBlockStorage ( volume ) { return new Promise ( ( resolve , reject ) => { if ( ! this . volumeIsValid ( volume ) ) { throw new Error ( 'Required fields missing from Block Storage Object' ) ; } axios . post ( `/volumes` , volume ) . then ( response => { resolve ( response . data . volume ) ; } ) . catch ( error => { reject ( error ) ; } ) ; } ) ; } /**
     * Get a single Block Storage volume by ID
     *
     * ### Example
     * ```js
     * import { DigitalOcean } from 'digitalocean-js';
     *
     * const client = new DigitalOcean('your-api-key');
     * const volume = await client.blockStorage.getBlockStorageById('volume-id');
     * ```
     */ public getBlockStorageById ( id ) { return new Promise ( ( resolve , reject ) => { axios . get ( `/volumes/${ id }` ) . then ( response => { resolve ( response . data . volume ) ; } ) . catch ( error => { reject ( error ) ; } ) ; } ) ; } /**
     * Get Block Storage volumes by name and region
     *
     * ### Example
     * ```js
     * import { DigitalOcean } from 'digitalocean-js';
     *
     * const client = new DigitalOcean('your-api-key');
     * const volume = await client.blockStorage
     *    .getBlockStorageByName('volume-name', 'nyc1');
     * ```
     */ public getBlockStorageByName ( name , regionSlug ) { return new Promise ( ( resolve , reject ) => { axios . get ( `/volumes?name=${ name }&region=${ regionSlug }` ) . then ( response => { resolve ( response . data . volumes ) ; } ) . catch ( error => { reject ( error ) ; } ) ; } ) ; } /**
     * Get snapshots that have been created from a Block Storage volume
     *
     * ### Example
     * ```js
     * import { DigitalOcean } from 'digitalocean-js';
     *
     * const client = new DigitalOcean('your-api-key');
     * const snapshots = await client.blockStorage
     *    .getSnapshotsForVolume('volume-id');
     * ```
     */ public getSnapshotsForVolume ( id ) { return new Promise ( ( resolve , reject ) => { axios . get ( `/volumes/${ id }/snapshots` ) . then ( response => { resolve ( response . data . snapshots ) ; } ) . catch ( error => { reject ( error ) ; } ) ; } ) ; } /**
     * Create a snapshot from a Block Storage volume
     *
     * ### Example
     * ```js
     * import { DigitalOcean } from 'digitalocean-js';
     *
     * const client = new DigitalOcean('your-api-key');
     * const snapshot = await client.blockStorage
     *    .createSnapshotFromVolume('volume-id', 'my-new-snapshot');
     * ```
     */ public createSnapshotFromVolume ( id , name ) { return new Promise ( ( resolve , reject ) => { axios . post ( `/volumes/${ id }/snapshots` , { name } ) . then ( response => { resolve ( response . data . snapshot ) ; } ) . catch ( error => { reject ( error ) ; } ) ; } ) ; } /**
     * Delete a Block Storage volume by ID
     *
     * ### Example
     * ```js
     * import { DigitalOcean } from 'digitalocean-js';
     *
     * const client = new DigitalOcean('your-api-key');
     * await client.blockStorage.deleteBlockStorageById('volume-id');
     * ```
     */ public deleteBlockStorageById ( id ) { return new Promise ( ( resolve , reject ) => { axios . delete ( `/volumes/${ id }` ) . then ( ( ) => { resolve ( ) ; } ) . catch ( error => { reject ( error ) ; } ) ; } ) ; } /**
     * Delete a Block Storage volume by name and region
     *
     * ### Example
     * ```js
     * import { DigitalOcean } from 'digitalocean-js';
     *
     * const client = new DigitalOcean('your-api-key');
     * await client.blockStorage.deleteBlockStorageByName('volume-name', 'nyc1');
     * ```
     */ public deleteBlockStorageByName ( name , regionSlug ) { return new Promise ( ( resolve , reject ) => { axios . delete ( `/volumes?name=${ name }&region=${ regionSlug }` ) . then ( ( ) => { resolve ( ) ; } ) . catch ( error => { reject ( error ) ; } ) ; } ) ; } private volumeIsValid ( vol ) { if ( ! vol . size_gigabytes || ! vol . name ) { return false ; } return true ; } }