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

92.31% Statements 12/13
95.45% Branches 21/22
90% Functions 9/10
92.31% Lines 12/13

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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215    2x                                                                                                                                                                                                                                                                                 9x                                                                         6x     3x   3x             3x   3x 1x     2x       2x       2x       2x                
// @flow
 
import { finish } from './utils/jobs';
import { type Caller, type Async } from '.';
 
/**
 * The service level of the volume
 */
type ServiceLevel = 'basic' | 'standard' | 'extreme';
 
/**
 * The protocol type of volume/mountPoint
 */
type ProtocolType = 'NFSv3' | 'NFSv4' | 'CIFS';
 
/**
 * Description of scheduled snaphsots taken from a volume.
 */
type SnapshotPolicy = {
  dailySchedule: {
    hour: number,
    minute: number,
    snapshotsToKeep: number,
  },
  hourlySchedule: { minute: number, snapshotsToKeep: number },
  monthlySchedule: {
    daysOfMonth: string,
    hour: number,
    minute: number,
    snapshotsToKeep: number,
  },
  weeklySchedule: {
    day: string,
    hour: number,
    minute: number,
    snapshotsToKeep: number,
  },
  enabled: boolean,
};
 
/**
 * Description of exporting rules for a volume.
 */
type ExportPolicy = {
  rules: [
    {
      allowedClients: string,
      v3: boolean,
      v4: boolean,
      ruleIndex: number,
      unixReadOnly: boolean,
      unixReadWrite: boolean,
    },
  ],
};
 
/**
 * Description of backup policy for a volume.
 */
type BackupPolicy = {
  enabled: boolean,
  dailyBackupsToKeep: number,
  weeklyBackupsToKeep: number,
  monthlyBackupsToKeep: number,
};
 
/**
 * Description of mount point for a volume.
 */
type MountPoint = {
  server: string,
  vlanId: number,
  export: string,
  exportFull: string,
  protocolType: ProtocolType,
  instructions: string,
};
 
/**
 * Description of a volume.
 */
type Volume = {
  id: string,
  lifeCycle: {
    state: string,
    details: string,
  },
  name: string,
  ownerId: string,
  region: string,
  creationToken: string,
  protocolTypes: ?[ProtocolType],
  quotaInBytes: number,
  securityStyle: ?string,
  serviceLevel: ServiceLevel,
  snapReserve: number,
  timezone: string,
  totalInodes: ?number,
  usedBytes: number,
  usedBytesBySnapshots: number,
  usedInodes: ?number,
  labels: ?[string],
  created: Date,
  snapshotDirectory: boolean,
  mountPoints: ?[MountPoint],
  backupPolicy: ?BackupPolicy,
  exportPolicy: ?ExportPolicy,
  snapshotPolicy: ?SnapshotPolicy,
};
 
/**
 * Parameters passed while creating a volume.
 */
type CreateVolumeValues = any;
 
/**
 * Parameters passed while updating a volume.
 */
type UpdateVolumeValues = any;
 
/**
 * Optional parameters passed while deleting a volume.
 */
type DeleteBackupValues = {
  deleteAssociatedBackups: boolean,
};
 
/**
 * Manage your volumes.
 */
export interface VolumesAPI {
  list(id?: string): Promise<Volume | Volume[]>;
  create(values: CreateVolumeValues): Promise<Async<Volume>>;
  update(id: string, values: UpdateVolumeValues): Promise<Async<Volume>>;
  delete(id: string, values?: DeleteBackupValues): Promise<Async<Volume>>;
  revert(id: string, snapshotId: string): Promise<Volume>;
}
 
function into(volume: any): Volume {
  return {
    id: volume.volumeId,
    lifeCycle: {
      state: volume.lifeCycleState,
      details: volume.lifeCycleStateDetails,
    },
    name: volume.name,
    ownerId: volume.ownerId,
    region: volume.region,
    creationToken: volume.creationToken,
    protocolTypes: volume.protocolTypes || null,
    quotaInBytes: volume.quotaInBytes,
    securityStyle: volume.securityStyle || null,
    serviceLevel: volume.serviceLevel,
    snapReserve: volume.snapReserve,
    snapshotDirectory:
      typeof volume.snapshotDirectory === 'boolean'
        ? volume.snapshotDirectory
        : true,
    timezone: volume.timezone,
    totalInodes:
      typeof volume.totalInodes === 'number' ? volume.totalInodes : null,
    usedBytes: volume.usedBytes,
    usedBytesBySnapshots: volume.usedBytesBySnapshots,
    usedInodes:
      typeof volume.usedInodes === 'number' ? volume.usedInodes : null,
    labels: volume.labels || null,
    created: new Date(volume.created),
    mountPoints: volume.mountPoints || null,
    backupPolicy: volume.backupPolicy || null,
    exportPolicy: volume.exportPolicy || null,
    snapshotPolicy: volume.snapshotPolicy || null,
  };
}
 
export default function volumes(call: Caller): VolumesAPI {
  function intoAsync(volume: any) {
    return {
      value: into(volume),
      finish(sideEffect, interval, timeout, interrupt) {
        return Promise.all(
          volume.jobs.map(job =>
            finish(call, job.jobId, sideEffect, interval, timeout, interrupt),
          ),
        );
      },
    };
  }
 
  return {
    async list(id) {
      if (id) {
        return into(await call('GET', `Volumes/${id}`));
      }
 
      return (await call('GET', 'Volumes')).map(into);
    },
 
    async create(values) {
      return intoAsync(await call('POST', 'Volumes', values));
    },
 
    async update(id, values) {
      return intoAsync(await call('PUT', `Volumes/${id}`, values));
    },
 
    async delete(id, values) {
      return intoAsync(await call('DELETE', `Volumes/${id}`, values));
    },
 
    async revert(id, snapshotId) {
      return into(await call('POST', `Volumes/${id}/Revert`, { snapshotId }));
    },
  };
}