redis-cloud-api-sdk
Version:
This is a client for the Redislabs Cloud API
258 lines (257 loc) • 11.9 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.Subscription = void 0;
const task_1 = require("../api/task");
class Subscription {
constructor(client) {
this.client = client;
this.task = new task_1.Task(client);
}
/**
* Returning a lookup list of current account's subscriptions
*/
getSubscriptions() {
return __awaiter(this, void 0, void 0, function* () {
try {
const response = yield this.client.get('/subscriptions');
return response.data.subscriptions;
}
catch (error) {
return error;
}
});
}
/**
* Creating a subscription
* @param createParameters The given parameters given for the subscription creation
*/
createSubscription(createParameters) {
return __awaiter(this, void 0, void 0, function* () {
try {
const response = yield this.client.post('/subscriptions', createParameters);
const taskId = response.data.taskId;
const taskResponse = yield this.task.waitForTaskStatus(taskId, 'processing-completed');
return taskResponse.response;
}
catch (error) {
return error;
}
});
}
/**
* Returning a subscription
* @param subscriptionId The id of the subscription
*/
getSubscription(subscriptionId) {
return __awaiter(this, void 0, void 0, function* () {
try {
const response = yield this.client.get(`/subscriptions/${subscriptionId}`);
return response.data;
}
catch (error) {
return error;
}
});
}
/**
* Updating a subscription
* @param subscriptionId The id of the subscription
* @param updateParameters The given update parameters to update the subscription with
*/
updateSubscription(subscriptionId, updateParameters) {
return __awaiter(this, void 0, void 0, function* () {
try {
const response = yield this.client.put(`/subscriptions/${subscriptionId}`, updateParameters);
const taskId = response.data.taskId;
const taskResponse = yield this.task.waitForTaskStatus(taskId, 'processing-completed');
return taskResponse.response;
}
catch (error) {
return error;
}
});
}
/**
* Deleting a subscription
* @param subscriptionId The id of the subscription
*/
deleteSubscription(subscriptionId) {
return __awaiter(this, void 0, void 0, function* () {
try {
const response = yield this.client.delete(`/subscriptions/${subscriptionId}`);
const taskId = response.data.taskId;
const taskResponse = yield this.task.waitForTaskStatus(taskId, 'processing-completed');
return taskResponse.response;
}
catch (error) {
return error;
}
});
}
/**
* Returning a lookup list of a subscription CIDR whitelists
* @param subscriptionId The id of the subscription
*/
getSubscriptionCidrWhitelist(subscriptionId) {
return __awaiter(this, void 0, void 0, function* () {
try {
const response = yield this.client.get(`/subscriptions/${subscriptionId}/cidr`);
const taskId = response.data.taskId;
const taskResponse = yield this.task.waitForTaskStatus(taskId, 'processing-completed');
return taskResponse.response.resource;
}
catch (error) {
return error;
}
});
}
/**
* Updating a subscription CIDR whitelists
* @param subscriptionId The id of the subscription
* @param updateParameters The parameters to update the subscription with
*/
updateSubscriptionCidrWhitelists(subscriptionId, updateParameters) {
return __awaiter(this, void 0, void 0, function* () {
try {
const response = yield this.client.put(`/subscriptions/${subscriptionId}/cidr`, updateParameters);
const taskId = response.data.taskId;
const taskResponse = yield this.task.waitForTaskStatus(taskId, 'processing-completed');
return taskResponse.response;
}
catch (error) {
return error;
}
});
}
/**
* Returning a lookup list of the subscription VPC Peerings
* @param subscriptionId The id of the subscription
*/
getVpcPeerings(subscriptionId) {
return __awaiter(this, void 0, void 0, function* () {
try {
const response = yield this.client.get(`/subscriptions/${subscriptionId}/peerings`);
const taskId = response.data.taskId;
const taskResponse = yield this.task.waitForTaskStatus(taskId, 'processing-completed');
return taskResponse.response.resource.peerings;
}
catch (error) {
return error;
}
});
}
/**
* Creating a subscription VPC peering
* @param subscriptionId The id of the subscription
* @param createParameters The create parameters to create the VPC peering with
*/
createSubscriptionVpcPeering(subscriptionId, createParameters) {
return __awaiter(this, void 0, void 0, function* () {
try {
const response = yield this.client.post(`/subscriptions/${subscriptionId}/peerings`, createParameters);
const taskId = response.data.taskId;
const taskResponse = yield this.task.waitForTaskStatus(taskId, 'processing-completed');
return taskResponse.response;
}
catch (error) {
return error;
}
});
}
/**
* Deleting a subscription VPC peering
* @param subscriptionId The id of the subscription
* @param vpcPeeringId The id of the VPC peering
*/
deleteSubscriptionVpcPeering(subscriptionId, vpcPeeringId) {
return __awaiter(this, void 0, void 0, function* () {
try {
const response = yield this.client.delete(`/subscriptions/${subscriptionId}/peerings/${vpcPeeringId}`);
const taskId = response.data.taskId;
const taskResponse = yield this.task.waitForTaskStatus(taskId, 'processing-completed');
return taskResponse.response;
}
catch (error) {
return error;
}
});
}
/**
* Waiting for the subscription status to change to a given status
* @param subscriptionId The id of the subscription
* @param expectedStatus The expected status
* @param timeoutInSeconds The timeout of waiting for the status. Default: 20 minutes
* @param sleepTimeInSeconds The sleep time between requests. Default: 5 seconds
*/
waitForSubscriptionStatus(subscriptionId, expectedStatus, timeoutInSeconds = 20 * 60, sleepTimeInSeconds = 5) {
return __awaiter(this, void 0, void 0, function* () {
let subscription = yield this.getSubscription(subscriptionId);
let timePassedInSeconds = 0;
while (subscription.status !== expectedStatus && subscription.status !== 'error' && subscription.status !== undefined && timePassedInSeconds <= timeoutInSeconds) {
this.client.log('debug', `Waiting for subscription ${subscription.id} status '${subscription.status}' to be become status '${expectedStatus}' (${timePassedInSeconds}/${timeoutInSeconds})`);
yield this.client.sleep(sleepTimeInSeconds);
timePassedInSeconds += sleepTimeInSeconds;
subscription = yield this.getSubscription(subscriptionId);
}
this.client.log('debug', `Subscription ${subscription.id} ended up as '${subscription.status}' status after ${timePassedInSeconds}/${timeoutInSeconds}`);
return subscription;
});
}
/**
* Waiting for existing subscriptions statuses to change to a given status
* @param expectedStatus The expected status
* @param timeoutInSeconds The timeout of waiting for the status. Default: 20 minutes
* @param sleepTimeInSeconds The sleep time between requests. Default: 5 seconds
* @returns A batch of subscription responses
*/
waitForSubscriptionsStatus(expectedStatus, timeoutInSeconds = 20 * 60, sleepTimeInSeconds = 5) {
return __awaiter(this, void 0, void 0, function* () {
const subscriptions = yield this.getSubscriptions();
const subscriptionResponses = [];
for (const subscription of subscriptions) {
const response = yield this.waitForSubscriptionStatus(subscription.id, expectedStatus, timeoutInSeconds, sleepTimeInSeconds);
subscriptionResponses.push(response);
}
return subscriptionResponses;
});
}
/**
* Waiting for the subscription VPC peering status to change to a given status
* @param subscriptionId The id of the subscription
* @param vpcPeeringId The id of the subscription VPC peering
* @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
*/
waitForVpcPeeringStatus(subscriptionId, vpcPeeringId, expectedStatus, timeoutInSeconds = 5 * 60, sleepTimeInSeconds = 5) {
return __awaiter(this, void 0, void 0, function* () {
let vpcPeerings = yield this.getVpcPeerings(subscriptionId);
let vpcPeering = vpcPeerings.find((vpcPeering) => vpcPeering.id === vpcPeeringId);
let timePassedInSeconds = 0;
if (vpcPeering !== undefined) {
let status = vpcPeering.status;
while (status !== expectedStatus && status !== 'failed' && status !== undefined && timePassedInSeconds <= timeoutInSeconds) {
this.client.log('debug', `Waiting for VPC peering ${vpcPeeringId} status '${status}' to be become status '${expectedStatus}' (${timePassedInSeconds}/${timeoutInSeconds}`);
yield this.client.sleep(sleepTimeInSeconds);
timePassedInSeconds += sleepTimeInSeconds;
vpcPeerings = yield this.getVpcPeerings(subscriptionId);
vpcPeering = vpcPeerings.find((vpcPeering) => vpcPeering.id === vpcPeeringId);
if (vpcPeering !== undefined)
status = vpcPeering.status;
}
}
this.client.log('debug', `VPC peering ${vpcPeeringId} ended up as '${status}' status after ${timePassedInSeconds}/${timeoutInSeconds}`);
return vpcPeering;
});
}
}
exports.Subscription = Subscription;