redis-cloud-api-sdk
Version:
This is a client for the Redislabs Cloud API
181 lines (180 loc) • 8.25 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Database = void 0;
const task_1 = require("./task");
class Database {
constructor(client) {
this.client = client;
this.task = new task_1.Task(client);
}
/**
* Returning a lookup list of databases owned by the account
* @param subscriptionId The id of the subscription
*/
getDatabases(subscriptionId) {
return __awaiter(this, void 0, void 0, function* () {
try {
const response = yield this.client.get(`/subscriptions/${subscriptionId}/databases`);
return response.data.subscription[0].databases;
}
catch (error) {
return error;
}
});
}
/**
* Creating a database
* @param subscriptionId The id of the subscription
* @param createParameters The create parameters to create the database
*/
createDatabase(subscriptionId, createParameters) {
return __awaiter(this, void 0, void 0, function* () {
try {
const response = yield this.client.post(`/subscriptions/${subscriptionId}/databases`, createParameters);
const taskId = response.data.taskId;
const taskResponse = yield this.task.waitForTaskStatus(taskId, 'processing-completed');
return taskResponse.response;
}
catch (error) {
return error;
}
});
}
/**
* Returning a database
* @param subscriptionId The id of the subscription
* @param databaseId The id of the database
*/
getDatabase(subscriptionId, databaseId) {
return __awaiter(this, void 0, void 0, function* () {
try {
const response = yield this.client.get(`/subscriptions/${subscriptionId}/databases/${databaseId}`);
return response.data;
}
catch (error) {
return error;
}
});
}
/**
* Updating a database
* @param subscriptionId The id of the subscription
* @param databaseId The id of the database
* @param updateParameters The update parameters to update the database
*/
updateDatabase(subscriptionId, databaseId, updateParameters) {
return __awaiter(this, void 0, void 0, function* () {
try {
const response = yield this.client.put(`/subscriptions/${subscriptionId}/databases/${databaseId}`, updateParameters);
const taskId = response.data.taskId;
const taskResponse = yield this.task.waitForTaskStatus(taskId, 'processing-completed');
return taskResponse.response;
}
catch (error) {
return error;
}
});
}
/**
* Deleting a database
* @param subscriptionId The id of the subscription
* @param databaseId The id of the database
*/
deleteDatabase(subscriptionId, databaseId) {
return __awaiter(this, void 0, void 0, function* () {
try {
const response = yield this.client.delete(`/subscriptions/${subscriptionId}/databases/${databaseId}`);
const taskId = response.data.taskId;
const taskResponse = yield this.task.waitForTaskStatus(taskId, 'processing-completed');
return taskResponse.response;
}
catch (error) {
return error;
}
});
}
/**
* Backing up a database
* @param subscriptionId The id of the subscription
* @param databaseId The id of the database
*/
backupDatabase(subscriptionId, databaseId) {
return __awaiter(this, void 0, void 0, function* () {
try {
const response = yield this.client.post(`/subscriptions/${subscriptionId}/databases/${databaseId}/backup`);
const taskId = response.data.taskId;
const taskResponse = yield this.task.waitForTaskStatus(taskId, 'processing-completed');
return taskResponse.response;
}
catch (error) {
return error;
}
});
}
/**
* Importing a dataset into a database
* @param subscriptionId The id of the subscription
* @param databaseId The id of the database
* @param importParameters The import parameters to import into a database
*/
importIntoDatabase(subscriptionId, databaseId, importParameters) {
return __awaiter(this, void 0, void 0, function* () {
try {
const response = yield this.client.post(`/subscriptions/${subscriptionId}/databases/${databaseId}/import`, importParameters);
const taskId = response.data.taskId;
const taskResponse = yield this.task.waitForTaskStatus(taskId, 'processing-completed');
return taskResponse.response;
}
catch (error) {
return error;
}
});
}
/**
* Waiting for database status to change to a given status
* @param subscriptionId The id of the subscription
* @param databaseId The id of the database
* @param expectedStatus The expected status
* @param timeoutInSeconds The timeout of waiting for the status. Default: 5 minutes
* @param sleepTimeInSeconds The sleep time between requests. Default: 5 seconds
*/
waitForDatabaseStatus(subscriptionId, databaseId, expectedStatus, timeoutInSeconds = 5 * 60, sleepTimeInSeconds = 5) {
return __awaiter(this, void 0, void 0, function* () {
let database = yield this.getDatabase(subscriptionId, databaseId);
let timePassedInSeconds = 0;
while (database.status !== expectedStatus && database.status !== 'error' && database.status !== undefined && timePassedInSeconds <= timeoutInSeconds) {
this.client.log('debug', `Waiting for database ${databaseId} status '${database.status}' to be become status '${expectedStatus}' (${timePassedInSeconds}/${timeoutInSeconds} (Subscription ${subscriptionId})`);
yield this.client.sleep(sleepTimeInSeconds);
timePassedInSeconds += sleepTimeInSeconds;
database = yield this.getDatabase(subscriptionId, databaseId);
}
this.client.log('debug', `Database ${databaseId} ended up as '${database.status}' status after ${timePassedInSeconds}/${timeoutInSeconds} (Subscription ${subscriptionId})`);
return database;
});
}
/**
* Waiting for all databases status under subscription to change to the expected status
* @param subscriptionId The id of the subscription
* @param expectedStatus The expected status
* @param timeoutInSeconds The timeout of waiting for the status. Default: 5 minutes
* @param sleepTimeInSeconds The sleep time between requests. Default: 5 seconds
*/
waitForSubscriptionDatabasesStatus(subscriptionId, expectedStatus = 'active', timeoutInSeconds = 5 * 60, sleepTimeInSeconds = 5) {
return __awaiter(this, void 0, void 0, function* () {
let databases = yield this.getDatabases(subscriptionId);
for (const database of databases) {
yield this.waitForDatabaseStatus(subscriptionId, database.databaseId, expectedStatus, timeoutInSeconds, sleepTimeInSeconds);
}
});
}
}
exports.Database = Database;