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

100% Statements 16/16
100% Branches 8/8
100% Functions 9/9
100% Lines 16/16

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    2x                                                                                 12x                                 6x     3x   3x             3x   4x 1x     3x 1x     2x 1x     1x       2x       3x       1x        
// @flow
 
import { finish } from './utils/jobs';
import { type Caller, type Async } from '.';
 
/**
 * Description of a snapshot.
 */
type Snapshot = {
  id: string,
  volumeId: string,
  lifeCycle: {
    state: string,
    details: string,
  },
  name: string,
  ownerId: string,
  region: string,
  usedBytes: number,
  created: Date,
};
 
/**
 * Parameters passed while creating a snapshot.
 */
type CreateSnapshotValues = any;
 
/**
 * Parameters passed while updating a snapshot.
 */
type UpdateSnapshotValues = any;
 
/**
 * Manage your snapshots.
 */
export interface SnapshotsAPI {
  list(volumeId?: ?string, id?: string): Promise<Snapshot | Snapshot[]>;
  create(values: CreateSnapshotValues): Promise<Async<Snapshot>>;
  update(id: string, values: UpdateSnapshotValues): Promise<Async<Snapshot>>;
  delete(id: string): Promise<Async<Snapshot>>;
}
 
function into(snapshot: any): Snapshot {
  return {
    id: snapshot.snapshotId,
    volumeId: snapshot.volumeId,
    lifeCycle: {
      state: snapshot.lifeCycleState,
      details: snapshot.lifeCycleStateDetails,
    },
    name: snapshot.name,
    ownerId: snapshot.ownerId,
    region: snapshot.region,
    usedBytes: snapshot.usedBytes,
    created: new Date(snapshot.created),
  };
}
 
export default function snapshots(call: Caller): SnapshotsAPI {
  function intoAsync(snapshot: any) {
    return {
      value: into(snapshot),
      finish(sideEffect, interval, timeout, interrupt) {
        return Promise.all(
          snapshot.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}/Snapshots/${id}`));
      }
 
      if (volumeId) {
        return (await call('GET', `Volumes/${volumeId}/Snapshots`)).map(into);
      }
 
      if (id) {
        return into(await call('GET', `Snapshots/${id}`));
      }
 
      return (await call('GET', 'Snapshots')).map(into);
    },
 
    async create(values) {
      return intoAsync(await call('POST', 'Snapshots', values));
    },
 
    async update(id, values) {
      return intoAsync(await call('PUT', `Snapshots/${id}`, values));
    },
 
    async delete(id) {
      return intoAsync(await call('DELETE', `Snapshots/${id}`));
    },
  };
}