redis-cloud-api-sdk
Version:
This is a client for the Redislabs Cloud API
123 lines (122 loc) • 5.36 kB
JavaScript
;
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.CloudAccount = void 0;
const task_1 = require("./task");
class CloudAccount {
constructor(client) {
this.client = client;
this.task = new task_1.Task(client);
}
/**
* Returning a lookup list of cloud accounts owned by the account
*/
getCloudAccounts() {
return __awaiter(this, void 0, void 0, function* () {
try {
const response = yield this.client.get('/cloud-accounts');
return response.data.cloudAccounts;
}
catch (error) {
return error;
}
});
}
/**
* Creating a cloud account
* @param createParameters The create parameters to create a cloud account
*/
createCloudAccount(createParameters) {
return __awaiter(this, void 0, void 0, function* () {
try {
const response = yield this.client.post('/cloud-accounts', createParameters);
const taskId = response.data.taskId;
const taskResponse = yield this.task.waitForTaskStatus(taskId, 'processing-completed');
return taskResponse.response;
}
catch (error) {
return error;
}
});
}
/**
* Returning a cloud account
* @param cloudAccountId The id of the cloud account
*/
getCloudAccount(cloudAccountId) {
return __awaiter(this, void 0, void 0, function* () {
try {
const response = yield this.client.get(`/cloud-accounts/${cloudAccountId}`);
return response.data;
}
catch (error) {
return error;
}
});
}
/**
* Updating a cloud account
* @param cloudAccountId The id of the cloud account
* @param updateParameters The update parameters to update a cloud account
*/
updateCloudAccount(cloudAccountId, updateParameters) {
return __awaiter(this, void 0, void 0, function* () {
try {
const response = yield this.client.put(`/cloud-accounts/${cloudAccountId}`, updateParameters);
const taskId = response.data.taskId;
const taskResponse = yield this.task.waitForTaskStatus(taskId, 'processing-completed');
return taskResponse.response;
}
catch (error) {
return error;
}
});
}
/**
* Deleting a cloud account
* @param cloudAccountId The id of the cloud account
*/
deleteCloudAccount(cloudAccountId) {
return __awaiter(this, void 0, void 0, function* () {
try {
const response = yield this.client.delete(`/cloud-accounts/${cloudAccountId}`);
const taskId = response.data.taskId;
const taskResponse = yield this.task.waitForTaskStatus(taskId, 'processing-completed');
return taskResponse.response;
}
catch (error) {
return error;
}
});
}
/**
* Waiting for cloud account status to change to a given status
* @param cloudAccountId The id of the cloud account
* @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
*/
waitForCloudAccountStatus(cloudAccountId, expectedStatus, timeoutInSeconds = 5 * 60, sleepTimeInSeconds = 5) {
return __awaiter(this, void 0, void 0, function* () {
let cloudAccount = yield this.getCloudAccount(cloudAccountId);
let timePassedInSeconds = 0;
while (cloudAccount.status !== expectedStatus && cloudAccount.status !== 'error' && cloudAccount.status !== undefined && timePassedInSeconds <= timeoutInSeconds) {
this.client.log('debug', `Waiting for cloud account ${cloudAccountId} status '${cloudAccount.status}' to be become status '${expectedStatus}' (${timePassedInSeconds}/${timeoutInSeconds}`);
yield this.client.sleep(sleepTimeInSeconds);
timePassedInSeconds += sleepTimeInSeconds;
cloudAccount = yield this.getCloudAccount(cloudAccountId);
}
this.client.log('debug', `Cloud account ${cloudAccountId} ended up as '${cloudAccount.status}' status after ${timePassedInSeconds}/${timeoutInSeconds}`);
return cloudAccount;
});
}
}
exports.CloudAccount = CloudAccount;