Skip to content

Commit

Permalink
Merge pull request #29 from coveo/add-index-resource
Browse files Browse the repository at this point in the history
Add index resource
  • Loading branch information
salvarez07 authored Oct 25, 2019
2 parents 839be1f + 32229a5 commit b2cc215
Show file tree
Hide file tree
Showing 6 changed files with 330 additions and 0 deletions.
64 changes: 64 additions & 0 deletions src/resources/Indexes/Indexes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import API from '../../APICore';
import {PageModel} from '../BaseInterfaces';
import Resource from '../Resource';
import {
CreateCoveoIndexModel,
IndexAttributes,
IndexBackupsItems,
IndexBackupsOptions,
IndexStatisticsModel,
} from './IndexesInterface';

export default class Indexes extends Resource {
static baseUrl = `/rest/organizations/${API.orgPlaceholder}/indexes`;
static indexBackupUrl = `/rest/organizations/${API.orgPlaceholder}/indexbackups/page`;

list() {
return this.api.get<IndexAttributes[]>(Indexes.baseUrl);
}

// Same for Copy Index but making sure to pass copyFromId and machineSpec
create(indexModal?: CreateCoveoIndexModel) {
return this.api.post<{id: string}>(Indexes.baseUrl, indexModal);
}

delete(indexId: string) {
return this.api.delete(`${Indexes.baseUrl}/${indexId}`);
}

get(indexId: string) {
return this.api.get<IndexAttributes>(`${Indexes.baseUrl}/${indexId}`);
}

backup(indexId: string) {
return this.api.post<{id: string}>(`${Indexes.baseUrl}/${indexId}/backup`, {});
}

getBackups(options?: IndexBackupsOptions) {
return this.api.get<PageModel<IndexBackupsItems>>(this.buildPath(`${Indexes.indexBackupUrl}`, options));
}

forceCommit(indexId: string) {
return this.api.post(`${Indexes.baseUrl}/${indexId}/commit`, {});
}

readOnly(indexId: string, isReadOnly: boolean) {
return this.api.put(this.buildPath(`${Indexes.baseUrl}/${indexId}/readonly`, {isReadOnly}), {});
}

resize(indexId: string, sizeInGibibytes: number) {
return this.api.post(`${Indexes.baseUrl}/${indexId}/resize`, {sizeInGibibytes});
}

stats(indexId: string) {
return this.api.get<IndexStatisticsModel>(`${Indexes.baseUrl}/${indexId}/stats`);
}

isOnline(indexId: string, isOnline: boolean) {
return this.api.put(this.buildPath(`${Indexes.baseUrl}/${indexId}/online`, {isOnline}), {});
}

restore(indexId: string, backupId: string) {
return this.api.post(this.buildPath(`${Indexes.baseUrl}/${indexId}/restore`, {backupId}), {});
}
}
103 changes: 103 additions & 0 deletions src/resources/Indexes/IndexesInterface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
export interface IndexStorageSpec {
numberOfIops: number;
sizeInGibibytes: number;
sizeInGigabytes: number;
storageType: string;
}

export interface IndexMachineSpec {
architecture: string;
storageSpec: IndexStorageSpec;
}

export interface IndexSourceStatistics {
collectionId: number;
documentCount: number;
documentTotalSize: number;
sourceId: number;
}

export interface IndexStatisticsModel {
diskSpaceUsed: number;
documentCount: number;
documentsTotalSize: number;
lastAppliedTransactionTimestamp: number;
lastCommitTimestamp: number;
numberOfQueries: number;
numberOfSlices: number;
pendingTransactions: number;
realtimeDiskSpaceUsed: number;
realtimeDocumentCount: number;
realtimeDocumentsTotalSize: number;
realtimePendingTransactions: number;
remainingDiskSpace: number;
sourceStatistics: IndexSourceStatistics[];
totalMemoryUsed: number;
totalPhysicalMemory: number;
}

export interface IndexStatus {
internalStatus: string;
online: boolean;
readOnlyStatus: string;
stats: IndexStatisticsModel;
}

export interface IndexAttributes {
adminUri: string;
agentId: string;
id: string;
logicalIndex: string;
machineSpec: IndexMachineSpec;
name: string;
organizationId: string;
region: string;
searchUri: string;
status: IndexStatus;
storages: IndexStorageSpec[];
type: string;
}

export interface CreateCoveoIndexModel {
copyFromId?: string;
logicalIndexId?: string;
machineSpec?: MachineSpecModel;
online?: boolean;
region?: string;
versions?: IndexVersionsModel;
}

export interface MachineSpecModel {
architecture: string;
storageSpec: IndexStorageSpecModel;
}

export interface IndexStorageSpecModel {
numberOfIops: number;
sizeInGibibytes: number;
storageType: string;
}

export interface IndexVersionsModel {
indexerVersion: string;
securityCacheVersion: string;
}

export interface IndexBackupsItems {
backupId: string;
creationDate: number;
indexId: string;
logicalIndex: string;
organizationId: string;
}

export interface IndexBackupsOptions {
backupId?: string;
from?: number;
indexId?: string;
order?: string;
page?: number;
perPage?: number;
sortBy?: string;
to?: number;
}
2 changes: 2 additions & 0 deletions src/resources/Indexes/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './Indexes';
export * from './IndexesInterface';
157 changes: 157 additions & 0 deletions src/resources/Indexes/tests/Indexes.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
import API from '../../../APICore';
import Indexes from '../Indexes';
import {CreateCoveoIndexModel} from '../IndexesInterface';

jest.mock('../../../APICore');

const APIMock: jest.Mock<API> = API as any;

describe('Cluster', () => {
let indexes: Indexes;
const api = new APIMock() as jest.Mocked<API>;

beforeEach(() => {
jest.clearAllMocks();
indexes = new Indexes(api);
});

describe('list', () => {
it('should make a GET call to the Cluster base url', () => {
indexes.list();
expect(api.get).toHaveBeenCalledTimes(1);
expect(api.get).toHaveBeenCalledWith(Indexes.baseUrl);
});
});

describe('get', () => {
it('should make a GET call for specific index', () => {
const indexId = 'ABC123';
indexes.get(indexId);
expect(api.get).toHaveBeenCalledTimes(1);
expect(api.get).toHaveBeenCalledWith(`${Indexes.baseUrl}/${indexId}`);
});
});

describe('delete', () => {
it('should make a DELETE call for specific index', () => {
const indexId = 'ABC123';
indexes.delete(indexId);
expect(api.delete).toHaveBeenCalledTimes(1);
expect(api.delete).toHaveBeenCalledWith(`${Indexes.baseUrl}/${indexId}`);
});
});

describe('create', () => {
it('should make a CREATE call with no arguments', () => {
indexes.create({});
expect(api.post).toHaveBeenCalledTimes(1);
expect(api.post).toHaveBeenCalledWith(Indexes.baseUrl, {});
});

it('should make a CREATE call to mimick COPY_INDEX', () => {
const indexToBeCopied: CreateCoveoIndexModel = {
copyFromId: 'originalIndexID',
machineSpec: {
architecture: 'T3_MEDIUM_UNLIMITED',
storageSpec: {
numberOfIops: 0,
sizeInGibibytes: 45,
storageType: 'STANDARD',
},
},
};

indexes.create(indexToBeCopied);
expect(api.post).toHaveBeenCalledTimes(1);
expect(api.post).toHaveBeenCalledWith(Indexes.baseUrl, indexToBeCopied);
});
});

describe('backup', () => {
it('should make a BACKUP call for specified index', () => {
const indexId = 'ABC123';
indexes.backup(indexId);
expect(api.post).toHaveBeenCalledTimes(1);
expect(api.post).toHaveBeenCalledWith(`${Indexes.baseUrl}/${indexId}/backup`, {});
});
});

describe('get all backups', () => {
it('should make a GETBACKUPS call', () => {
const options = {
backupId: 'backupId',
from: 0,
indexId: 'indexId',
order: 'asc',
page: 100,
perPage: 10,
sortBy: 'name',
to: 20,
};

indexes.getBackups(options);
expect(api.get).toHaveBeenCalledTimes(1);
expect(api.get).toHaveBeenCalledWith(
`${Indexes.indexBackupUrl}?backupId=backupId&from=0&indexId=indexId&order=asc&page=100&perPage=10&sortBy=name&to=20`
);
});
});

describe('forceCommit', () => {
it('should make a FORCECOMMIT call for specified index', () => {
const indexId = 'ABC123';
indexes.forceCommit(indexId);
expect(api.post).toHaveBeenCalledTimes(1);
expect(api.post).toHaveBeenCalledWith(`${Indexes.baseUrl}/${indexId}/commit`, {});
});
});

describe('readOnly', () => {
it('should make a READONLY call for specified index', () => {
const isReadOnly = true;
const indexId = 'ABC123';
indexes.readOnly(indexId, isReadOnly);
expect(api.put).toHaveBeenCalledTimes(1);
expect(api.put).toHaveBeenCalledWith(`${Indexes.baseUrl}/${indexId}/readonly?isReadOnly=true`, {});
});
});

describe('resize', () => {
it('should make a RESIZE call for specified index', () => {
const sizeInGibibytes = 100;
const indexId = 'ABC123';
indexes.resize(indexId, sizeInGibibytes);
expect(api.post).toHaveBeenCalledTimes(1);
expect(api.post).toHaveBeenCalledWith(`${Indexes.baseUrl}/${indexId}/resize`, {sizeInGibibytes});
});
});

describe('stats', () => {
it('should make a RESIZE call for specified index', () => {
const indexId = 'ABC123';
indexes.stats(indexId);
expect(api.get).toHaveBeenCalledTimes(1);
expect(api.get).toHaveBeenCalledWith(`${Indexes.baseUrl}/${indexId}/stats`);
});
});

describe('isOnline', () => {
it('should make a ISONLINE call for specified index', () => {
const isOnline = true;
const indexId = 'ABC123';
indexes.isOnline(indexId, isOnline);
expect(api.put).toHaveBeenCalledTimes(1);
expect(api.put).toHaveBeenCalledWith(`${Indexes.baseUrl}/${indexId}/online?isOnline=true`, {});
});
});

describe('restore', () => {
it('should make a RESTORE call for specified index', () => {
const backupId = 'backUpID';
const indexId = 'ABC123';
indexes.restore(indexId, backupId);
expect(api.post).toHaveBeenCalledTimes(1);
expect(api.post).toHaveBeenCalledWith(`${Indexes.baseUrl}/${indexId}/restore?backupId=backUpID`, {});
});
});
});
3 changes: 3 additions & 0 deletions src/resources/PlatformResources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import AWS from './AWS/AWS';
import Catalog from './Catalogs/Catalog';
import Cluster from './Clusters/Cluster';
import Group from './Groups/Groups';
import Index from './Indexes/Indexes';
import Organization from './Organizations/Organization';
import Resource from './Resource';
import SecurityCache from './SecurityCache/SecurityCache';
Expand All @@ -14,6 +15,7 @@ const resourcesMap: Array<{key: string; resource: typeof Resource}> = [
{key: 'cluster', resource: Cluster},
{key: 'group', resource: Group},
{key: 'organization', resource: Organization},
{key: 'indexes', resource: Index},
{key: 'securityCache', resource: SecurityCache},
{key: 'apiKey', resource: ApiKey},
];
Expand All @@ -26,6 +28,7 @@ class PlatformResources {
cluster: Cluster;
group: Group;
organization: Organization;
index: Index;
apiKey: ApiKey;
securityCache: SecurityCache;

Expand Down
1 change: 1 addition & 0 deletions src/resources/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export * from './Catalogs';
export * from './Clusters';
export * from './Groups';
export * from './Organizations';
export * from './Indexes';
export * from './License';
export * from './Organizations';
export * from './ApiKeys';

0 comments on commit b2cc215

Please sign in to comment.