All files / src/api/storage accounts.js

77.78% Statements 7/9
100% Branches 9/9
71.43% Functions 5/7
77.78% Lines 7/9

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                                                                                                                        4x                                     3x   2x 1x     1x       1x               1x        
// @flow
 
import { type Caller } from '.';
 
export type StorageAccountSettings = {
  type: string,
  region: string,
  value: string,
};
 
export type StorageAccountAllowedHosts = {
  name: string,
  vendorID: string,
  publicAccess: boolean,
  maintenanceMode: boolean,
  ipAddress: boolean,
  port: number,
  protocol: string,
  username: string,
  stampId: string,
};
 
/**
 * A description of a storage account.
 */
export type StorageAccount = {
  id: string,
  name: string,
  state: string,
  publicAccess: boolean,
  accountSettings: StorageAccountSettings[],
  allowedHosts: ?(StorageAccountAllowedHosts[]),
  created: Date,
  updated: Date,
  deleted: ?Date,
};
 
export type SetHostsValues = {
  hostIds: string[],
};
 
/**
 * Parameters passed in updating storage account.
 */
export type UpdateStorageAccountValues = any;
 
/**
 * Manage storage accounts.
 */
export interface StorageAccountsAPI {
  list(id?: string): Promise<StorageAccount | StorageAccount[]>;
  update(
    id: string,
    values: UpdateStorageAccountValues,
  ): Promise<StorageAccount>;
  setHosts(id: string, values: SetHostsValues): Promise<any>;
  delete(id: string): Promise<any>;
}
 
function into(account: any): StorageAccount {
  return {
    id: account.uuid,
    name: account.name,
    state: account.state,
    publicAccess: !!account.publicAccess,
    accountSettings: account.accountSettings || [],
    allowedHosts: (account.allowedHosts || []).map(
      ({ stampUUID: stampId, password, ...rest }) => ({
        ...rest,
        stampId,
      }),
    ),
    created: new Date(account.createdAt),
    updated: new Date(account.updatedAt),
    deleted: (account.deletedAt && new Date(account.deletedAt)) || null,
  };
}
 
export default function accounts(call: Caller): StorageAccountsAPI {
  return {
    async list(id) {
      if (id) {
        return into(await call('GET', `account/${id}`));
      }
 
      return (await call('GET', 'account')).map(into);
    },
 
    async update(id, values) {
      return into(await call('PUT', `account/${id}`, values));
    },
 
    async setHosts(id, values) {
      return call('PUT', `account/${id}/hosts`, values);
    },
 
    async delete(id) {
      return call('DELETE', `account/${id}`);
    },
  };
}