Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Cloud Backup endpoints. #51

Merged
merged 5 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const response = await user.update("someUserName", body, options); // update use
Following entities are currently supported

- [User](#user)
- [CloudBackup](#cloudbackup)
- [Cluster](#cluster)
- [CustomDbRole](#customdbrole)
- [ProjectWhitelist](#projectwhitelist)
Expand Down Expand Up @@ -128,6 +129,59 @@ Function - Deletes the user name passed.

More details - https://docs.atlas.mongodb.com/reference/api/database-users-delete-a-user/

### CloudBackup

### cloudBackup.getReplicaSetCloudBackup(clustername, snapshotId, [options]) ⇒ <code>Promise</code>
Function - Returns the details of the specified snapshotId.

**Returns**: <code>Promise</code> - - promise which resolves on success and rejects on error

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| clustername | <code>String</code> | | name of the cluster for which details needs to be retrieved |
| snapshotId | <code>String</code> | | Id of the snapshot for which details needs to be retrieved |
| [options] | <code>Object</code> | <code>{}</code> | Optional object containing extra query strings which will be passed to atlas api. It can also include httpOptions which will be sent to `urllib`. More info can be found here - https://github.com/node-modules/urllib |

More details - https://www.mongodb.com/docs/atlas/reference/api-resources-spec/v1/#tag/Cloud-Backups/operation/getReplicaSetBackup

### cloudBackup.getAllReplicaSetCloudBackups(clustername, [options]) ⇒ <code>Promise</code>
Function - Returns the details of all snapshots of an specified clustername.

**Returns**: <code>Promise</code> - - promise which resolves on success and rejects on error

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| clustername | <code>String</code> | | name of the cluster for which details needs to be retrieved |
| [options] | <code>Object</code> | <code>{}</code> | Optional object containing extra query strings which will be passed to atlas api. It can also include httpOptions which will be sent to `urllib`. More info can be found here - https://github.com/node-modules/urllib |

More details - https://www.mongodb.com/docs/atlas/reference/api-resources-spec/v1/#tag/Cloud-Backups/operation/listReplicaSetBackups

### cloudBackup.getSnapshotRestoreJob(clustername, restoreJobId, [options]) ⇒ <code>Promise</code>
Function - Returns the details of all snapshots of an specified clustername.

**Returns**: <code>Promise</code> - - promise which resolves on success and rejects on error

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| clustername | <code>String</code> | | name of the cluster for which details needs to be retrieved |
| restoreJobId | <code>String</code> | | snapshot restore job id for which details needs to be retrieved |
| [options] | <code>Object</code> | <code>{}</code> | Optional object containing extra query strings which will be passed to atlas api. It can also include httpOptions which will be sent to `urllib`. More info can be found here - https://github.com/node-modules/urllib |

More details - https://www.mongodb.com/docs/atlas/reference/api-resources-spec/v1/#tag/Cloud-Backups/operation/getBackupRestoreJob

### cloudBackup.createSnapshotRestoreJob(clustername, body, [options]) ⇒ <code>Promise</code>
Function - Returns the details of all snapshots of an specified clustername.

**Returns**: <code>Promise</code> - - promise which resolves on success and rejects on error

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| clustername | <code>String</code> | | name of the cluster for which details needs to be retrieved |
| body | <code>Object</code> | | Body which has details for snapshot restore job which needs to be created |
| [options] | <code>Object</code> | <code>{}</code> | Optional object containing extra query strings which will be passed to atlas api. It can also include httpOptions which will be sent to `urllib`. More info can be found here - https://github.com/node-modules/urllib |

More details - https://www.mongodb.com/docs/atlas/reference/api-resources-spec/v1/#tag/Cloud-Backups/operation/createBackupRestoreJob

### Cluster

### cluster.get(clustername, [options]) ⇒ <code>Promise</code>
Expand Down
70 changes: 70 additions & 0 deletions src/cloudBackup.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import {ClusterName, Links, AtlasResultsResponse, AtlasClientOptions, AtlasError} from ".";

export interface GetReplicaSetCloudBackup {
cloudProvider: 'AWS' | 'GCP' | 'AZURE' | 'TENANT';
copyRegions: string[];
createdAt: string;
description?: string;
expiresAt: string;
frequencyType: 'hourly' | 'daily' | 'weekly' | 'monthly' | 'yearly';
id: string;
links: Links;
masterKeyUUID?: string;
mongodVersion: string;
policyItems: Array;
replicaSetName: string;
snapshotType: 'onDemand' | 'scheduled';
status: 'queued' | 'inProgress' | 'completed' | 'failed';
storageSizeBytes: number;
type: string
}

export type GetAllReplicaSetCloudBackups = AtlasResultsResponse<GetReplicaSetCloudBackup>;

export interface SnapshotRestoreJobComponent {
replicaSetName: string
}

export type SnapshotRestoreJobComponents = SnapshotRestoreJobComponent[]

export interface GetSnapshotRestoreJob {
cancelled?: boolean;
components?: SnapshotRestoreJobComponents;
deliveryType: 'automated' | 'download' | 'pointInTime';
deliveryUrl?: string[];
desiredTimestamp?: {
date: string;
increment: number
};
expired?: boolean;
expiresAt?: string;
failed?: boolean;
finishedAt?: string;
id?: string;
links?: Links;
oplogInc?: number;
oplogTs?: number;
pointInTimeUTCSeconds?: number;
snapshotId?: string;
targetClusterName?: string;
targetGroupId?: string;
timestamp?: string
}

export interface SnapshotRestoreJobRequest {
deliveryType: 'automated' | 'download' | 'pointInTime';
oplogInc?: number;
oplogTs?: number;
pointInTimeUTCSeconds?: number;
snapshotId?: string;
targetClusterName?: string;
targetGroupId?: string
}
export type CreateSnapshotRestoreJobResponse = GetSnapshotRestoreJob;

export interface CloudBackup {
getReplicaSetCloudBackup(clustername: ClusterName, snapshotId: string, options?: AtlasClientOptions): Promise<GetReplicaSetCloudBackup | AtlasError>;
getAllReplicaSetCloudBackups(clustername: ClusterName, options?: AtlasClientOptions): Promise<GetAllReplicaSetCloudBackups | AtlasError>;
getSnapshotRestoreJob(clustername: ClusterName, restoreJobId: string, options?: AtlasClientOptions): Promise<CreateSnapshotRestoreJobResponse | AtlasError>
createSnapshotRestoreJob(clustername: ClusterName, body: SnapshotRestoreJobRequest, options?: AtlasClientOptions): Promise<CreateSnapshotRestoreJobResponse | AtlasError>
}
53 changes: 53 additions & 0 deletions src/cloudBackup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const {getQueryStringFromOptions} = require("./helper");

class CloudBackup {

constructor(client, baseUrl, projectId) {
this.client_ = client;
this.baseUrl_ = baseUrl;
this.projectId_ = projectId;
}

async getReplicaSetCloudBackup(clustername, snapshotId, options = {}) {
const queryString = getQueryStringFromOptions(options);
const httpOptions = options.httpOptions;
const response = (
await this.client_.fetch(`${this.baseUrl_}/groups/${this.projectId_}/clusters/${clustername}/backup/snapshots/${snapshotId}?${queryString}`, httpOptions)
);
return response;
}

async getAllReplicaSetCloudBackups(clustername, options = {}) {
const queryString = getQueryStringFromOptions(options);
const httpOptions = options.httpOptions;
const response = (
await this.client_.fetch(`${this.baseUrl_}/groups/${this.projectId_}/clusters/${clustername}/backup/snapshots?${queryString}`, httpOptions)
);
return response;
}

async getSnapshotRestoreJob(clustername, restoreJobId, options = {}) {
const queryString = getQueryStringFromOptions(options);
const httpOptions = options.httpOptions;
const response = (
await this.client_.fetch(`${this.baseUrl_}/groups/${this.projectId_}/clusters/${clustername}/backup/restoreJobs/${restoreJobId}?${queryString}`, httpOptions)
);
return response;
}

async createSnapshotRestoreJob(clustername, body, options = {}) {
const queryString = getQueryStringFromOptions(options);
const httpOptions = options.httpOptions;
const response = (
await this.client_.fetch(`${this.baseUrl_}/groups/${this.projectId_}/clusters/${clustername}/backup/restoreJobs?${queryString}`, {
"method": "POST",
"data": body,
"headers": {"Content-Type": "application/json"},
...httpOptions
})
);
return response;
}
}

module.exports = CloudBackup;
3 changes: 3 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {Alert} from './alert'
import {AtlasUser} from './atlasUser';
import {Cluster} from './cluster';
import {CloudBackup} from './cloudBackup';
import {CustomDbRole} from './customDbRole';
import {Event} from './event';
import {Organization} from './organization';
Expand All @@ -15,6 +16,7 @@ import {AtlasSearch} from './atlasSearch';
export * from './alert'
export * from './atlasUser';
export * from './cluster';
export * from './cloudBackup';
export * from './customDbRole';
export * from './event';
export * from './organization';
Expand Down Expand Up @@ -66,6 +68,7 @@ export interface AtlasClient {
projectAccesslist: ProjectAccesslist;
customDbRole: CustomDbRole;
cluster: Cluster;
cloudBackup: CloudBackup;
event: Event;
dataLake: DataLake;
cloudProviderAccess: CloudProviderAccess;
Expand Down
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const User = require("./user");
const CloudBackup = require("./cloudBackup");
const Cluster = require("./cluster");
const CustomDbRole = require("./customDbRole");
const ProjectWhitelist = require("./projectWhitelist");
Expand Down Expand Up @@ -30,6 +31,7 @@ function getMongodbAtlasApiClient(options) {
const client = new HttpClient(urllibClient, options.publicKey, options.privateKey);
const user = new User(client, options.baseUrl, options.projectId);
const cluster = new Cluster(client, options.baseUrl, options.projectId);
const cloudBackup = new CloudBackup(client, options.baseUrl, options.projectId);
const customDbRole = new CustomDbRole(client, options.baseUrl, options.projectId);
const projectWhitelist = new ProjectWhitelist(client, options.baseUrl, options.projectId);
const projectAccesslist = new ProjectAccesslist(client, options.baseUrl, options.projectId);
Expand All @@ -45,6 +47,7 @@ function getMongodbAtlasApiClient(options) {
const functions = {};
functions.user = getFunctions(user);
functions.cluster = getFunctions(cluster);
functions.cloudBackup = getFunctions(cloudBackup);
functions.customDbRole = getFunctions(customDbRole);
functions.projectWhitelist = getFunctions(projectWhitelist);
functions.projectAccesslist = getFunctions(projectAccesslist);
Expand Down
70 changes: 70 additions & 0 deletions test/cloudbackup.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const {describe, it} = exports.lab = require("@hapi/lab").script();
const {expect} = require("@hapi/code");
const nock = require("nock");
const getClient = require("../src");

const baseUrl = "http://dummyBaseUrl";
const projectId = "dummyProjectId";

const client = getClient({
"publicKey": "dummuyPublicKey",
"privateKey": "dummyPrivateKey",
"baseUrl": baseUrl,
"projectId": projectId
});

describe("Mongo Atlas Api Client - CloudBackup", () => {

describe("When cluster is exported from index", () => {
it("should export cluster functions", async () => {
expect(client.cloudBackup.getReplicaSetCloudBackup).to.be.function();
expect(client.cloudBackup.getAllReplicaSetCloudBackups).to.be.function();
expect(client.cloudBackup.getSnapshotRestoreJob).to.be.function();
expect(client.cloudBackup.createSnapshotRestoreJob).to.be.function();
});
});

describe("When getReplicaSetCloudBackup is called with querystring parameters", () => {
it("should return response", async () => {
const expectedRequest = nock(baseUrl)
.get(`/groups/${projectId}/clusters/mycluster/backup/snapshots/mysnapshot?key1=value1&key2=value2`)
.reply(200, {"replicaSetName": "mycluster"});
const result = await client.cloudBackup.getReplicaSetCloudBackup("mycluster", "mysnapshot", {"key1": "value1", "key2": "value2"});
expect(result).to.equal({"replicaSetName": "mycluster"});
expect(expectedRequest.isDone()).to.be.true();
});
});

describe("When getAllReplicaSetCloudBackups is called with querystring parameters", () => {
it("should return response", async () => {
const expectedRequest = nock(baseUrl)
.get(`/groups/${projectId}/clusters/mycluster/backup/snapshots?key1=value1&key2=value2`)
.reply(200, [{"replicaSetName": "mycluster"}]);
const result = await client.cloudBackup.getAllReplicaSetCloudBackups("mycluster", {"key1": "value1", "key2": "value2"});
expect(result).to.equal([{"replicaSetName": "mycluster"}]);
expect(expectedRequest.isDone()).to.be.true();
});
});

describe("When getSnapshotRestoreJob is called with querystring parameters", () => {
it("should return response", async () => {
const expectedRequest = nock(baseUrl)
.get(`/groups/${projectId}/clusters/mycluster/backup/restoreJobs/myrestorejob?key1=value1&key2=value2`)
.reply(200, {"id": "myrestorejob"});
const result = await client.cloudBackup.getSnapshotRestoreJob("mycluster", "myrestorejob", {"key1": "value1", "key2": "value2"});
expect(result).to.equal({"id": "myrestorejob"});
expect(expectedRequest.isDone()).to.be.true();
});
});

describe("When createSnapshotRestoreJob is called with querystring parameters", () => {
it("should return response", async () => {
const expectedRequest = nock(baseUrl)
.post(`/groups/${projectId}/clusters/mycluster/backup/restoreJobs?key1=value1&key2=value2`)
.reply(200, {"id": "myrestorejob"});
const result = await client.cloudBackup.createSnapshotRestoreJob("mycluster", {"body": "value"}, {"key1": "value1", "key2": "value2"});
expect(result).to.equal({"id": "myrestorejob"});
expect(expectedRequest.isDone()).to.be.true();
});
});
});
Loading