All files / src/api/cloud-volumes backups.js

94.74% Statements 18/19
100% Branches 14/14
90% Functions 9/10
94.74% Lines 18/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117    2x                                                                                     13x                                     7x     3x   3x             3x   4x 1x     3x 1x     2x 1x     1x       3x 1x         2x       2x       2x                
// @flow
 
import { finish } from './utils/jobs';
import { type Caller, type Async } from '.';
 
/**
 * Description of volume backups.
 */
export type Backup = {
  id: string,
  created: Date,
  lifeCycle: {
    state: string,
    details: string,
  },
  ownerId: string,
  region: string,
  tag: ?string,
  type: ?string,
  name: string,
  usedBytes: number,
  volumeId: string,
};
 
/**
 * Parameters passed while creating a volume.
 */
type CreateBackupValues = any;
 
/**
 * Parameters passed while updating a volume.
 */
type UpdateBackupValues = any;
 
/**
 * Manage your backups.
 */
export interface BackupsAPI {
  list(volumeId?: ?string, id?: string): Promise<Backup | Backup[]>;
  create(volumeId: ?string, values: CreateBackupValues): Promise<Async<Backup>>;
  update(id: string, values: UpdateBackupValues): Promise<Async<Backup>>;
  delete(id: string): Promise<any>;
}
 
function into(backup: any): Backup {
  return {
    id: backup.backupId,
    name: backup.name,
    tag: backup.tag || null,
    type: backup.type || null,
    ownerId: backup.ownerId,
    region: backup.region,
    usedBytes: backup.usedBytes,
    volumeId: backup.volumeId,
    lifeCycle: {
      state: backup.lifeCycleState,
      details: backup.lifeCycleStateDetails,
    },
    created: new Date(backup.created),
  };
}
 
export default function backups(call: Caller): BackupsAPI {
  function intoAsync(backup: any) {
    return {
      value: into(backup),
      finish(sideEffect, interval, timeout, interrupt) {
        return Promise.all(
          backup.jobs.map(job =>
            finish(call, job.jobId, sideEffect, interval, timeout, interrupt),
          ),
        );
      },
    };
  }
 
  return {
    async list(volumeId, id) {
      if (volumeId && id) {
        return into(await call('GET', `Volumes/${volumeId}/Backups/${id}`));
      }
 
      if (volumeId) {
        return (await call('GET', `Volumes/${volumeId}/Backups`)).map(into);
      }
 
      if (id) {
        return into(await call('GET', `Backups/${id}`));
      }
 
      return (await call('GET', 'Backups')).map(into);
    },
 
    async create(volumeId, values) {
      if (volumeId) {
        return intoAsync(
          await call('POST', `Volumes/${volumeId}/Backups`, values),
        );
      }
 
      return intoAsync(await call('POST', 'Backups', values));
    },
 
    async update(id, values) {
      return intoAsync(await call('PUT', `Backups/${id}`, values));
    },
 
    async delete(id) {
      return intoAsync(await call('DELETE', `Backups/${id}`));
    },
 
    async restore(values) {
      return intoAsync(await call('POST', 'Backups/Restore', values));
    },
  };
}