Skip to content

Commit 0da6718

Browse files
authored
Support for backup api (#83)
* WVT-234: support for backup api * WVT-234: support for backup api (improved) * WVT-234: support for backup api (further improvements) * WVT-234: rename storageName -> backend * WVT-234: support for backup api (further improvements)
1 parent 13d9847 commit 0da6718

File tree

11 files changed

+1345
-2
lines changed

11 files changed

+1345
-2
lines changed

backup/backupCreateStatusGetter.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { validateBackupId, validateBackend } from "./validation";
2+
3+
export default class BackupCreateStatusGetter {
4+
5+
backend;
6+
backupId;
7+
errors;
8+
9+
constructor(client) {
10+
this.client = client;
11+
}
12+
13+
withBackend(backend) {
14+
this.backend = backend;
15+
return this;
16+
}
17+
18+
withBackupId(backupId) {
19+
this.backupId = backupId;
20+
return this;
21+
}
22+
23+
validate() {
24+
this.errors = [
25+
...validateBackend(this.backend),
26+
...validateBackupId(this.backupId),
27+
];
28+
}
29+
30+
do() {
31+
this.validate();
32+
if (this.errors.length > 0) {
33+
return Promise.reject(
34+
new Error("invalid usage: " + this.errors.join(", "))
35+
);
36+
}
37+
return this.client.get(this._path());
38+
}
39+
40+
_path() {
41+
return `/backups/${this.backend}/${this.backupId}`;
42+
}
43+
}

backup/backupCreator.js

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import { CreateStatus } from ".";
2+
import { validateBackupId, validateExcludeClassNames, validateIncludeClassNames, validateBackend } from "./validation";
3+
4+
const WAIT_INTERVAL = 1000;
5+
6+
export default class BackupCreator {
7+
8+
includeClassNames;
9+
excludeClassNames;
10+
backend;
11+
backupId;
12+
waitForCompletion;
13+
errors;
14+
15+
constructor(client, statusGetter) {
16+
this.client = client;
17+
this.statusGetter = statusGetter;
18+
}
19+
20+
withIncludeClassNames(...classNames) {
21+
let cls = classNames;
22+
if (classNames.length && Array.isArray(classNames[0])) {
23+
cls = classNames[0];
24+
}
25+
this.includeClassNames = cls;
26+
return this;
27+
}
28+
29+
withExcludeClassNames(...classNames) {
30+
let cls = classNames;
31+
if (classNames.length && Array.isArray(classNames[0])) {
32+
cls = classNames[0];
33+
}
34+
this.excludeClassNames = cls;
35+
return this;
36+
}
37+
38+
withBackend(backend) {
39+
this.backend = backend;
40+
return this;
41+
}
42+
43+
withBackupId(backupId) {
44+
this.backupId = backupId;
45+
return this;
46+
}
47+
48+
withWaitForCompletion(waitForCompletion) {
49+
this.waitForCompletion = waitForCompletion;
50+
return this;
51+
}
52+
53+
validate() {
54+
this.errors = [
55+
...validateIncludeClassNames(this.includeClassNames),
56+
...validateExcludeClassNames(this.excludeClassNames),
57+
...validateBackend(this.backend),
58+
...validateBackupId(this.backupId),
59+
];
60+
}
61+
62+
do() {
63+
this.validate();
64+
if (this.errors.length > 0) {
65+
return Promise.reject(
66+
new Error("invalid usage: " + this.errors.join(", "))
67+
);
68+
}
69+
70+
const payload = {
71+
id: this.backupId,
72+
config: {},
73+
include: this.includeClassNames,
74+
exclude: this.excludeClassNames,
75+
};
76+
77+
if (this.waitForCompletion) {
78+
return this._createAndWaitForCompletion(payload);
79+
}
80+
return this._create(payload);
81+
}
82+
83+
_create(payload) {
84+
return this.client.post(this._path(), payload);
85+
}
86+
87+
_createAndWaitForCompletion(payload) {
88+
return new Promise((resolve, reject) => {
89+
this._create(payload)
90+
.then(createResponse => {
91+
this.statusGetter
92+
.withBackend(this.backend)
93+
.withBackupId(this.backupId);
94+
95+
const loop = () => {
96+
this.statusGetter.do()
97+
.then(createStatusResponse => {
98+
if (createStatusResponse.status == CreateStatus.SUCCESS
99+
|| createStatusResponse.status == CreateStatus.FAILED
100+
) {
101+
resolve(this._merge(createStatusResponse, createResponse));
102+
} else {
103+
setTimeout(loop, WAIT_INTERVAL);
104+
}
105+
})
106+
.catch(reject);
107+
};
108+
109+
loop();
110+
})
111+
.catch(reject)
112+
});
113+
}
114+
115+
_path() {
116+
return `/backups/${this.backend}`;
117+
}
118+
119+
_merge(createStatusResponse, createResponse) {
120+
const merged = {};
121+
if ('id' in createStatusResponse) {
122+
merged.id = createStatusResponse.id;
123+
}
124+
if ('path' in createStatusResponse) {
125+
merged.path = createStatusResponse.path
126+
}
127+
if ('backend' in createStatusResponse) {
128+
merged.backend = createStatusResponse.backend
129+
}
130+
if ('status' in createStatusResponse) {
131+
merged.status = createStatusResponse.status
132+
}
133+
if ('error' in createStatusResponse) {
134+
merged.error = createStatusResponse.error
135+
}
136+
if ('classes' in createResponse) {
137+
merged.classes = createResponse.classes
138+
}
139+
return merged;
140+
}
141+
}

backup/backupGetter.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { validateBackend } from "./validation";
2+
3+
export default class BackupGetter {
4+
5+
backend;
6+
errors;
7+
8+
constructor(client) {
9+
this.client = client;
10+
}
11+
12+
withBackend(backend) {
13+
this.backend = backend;
14+
return this;
15+
}
16+
17+
validate() {
18+
this.errors = validateBackend(this.backend);
19+
}
20+
21+
do() {
22+
this.validate();
23+
if (this.errors.length > 0) {
24+
return Promise.reject(
25+
new Error("invalid usage: " + this.errors.join(", "))
26+
);
27+
}
28+
29+
return this.client.get(this._path());
30+
}
31+
32+
_path() {
33+
return `/backups/${this.backend}`;
34+
}
35+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { validateBackupId, validateBackend } from "./validation";
2+
3+
export default class BackupRestoreStatusGetter {
4+
5+
backend;
6+
backupId;
7+
errors;
8+
9+
constructor(client) {
10+
this.client = client;
11+
}
12+
13+
withBackend(backend) {
14+
this.backend = backend;
15+
return this;
16+
}
17+
18+
withBackupId(backupId) {
19+
this.backupId = backupId;
20+
return this;
21+
}
22+
23+
validate() {
24+
this.errors = [
25+
...validateBackend(this.backend),
26+
...validateBackupId(this.backupId),
27+
];
28+
}
29+
30+
do() {
31+
this.validate();
32+
if (this.errors.length > 0) {
33+
return Promise.reject(
34+
new Error("invalid usage: " + this.errors.join(", "))
35+
);
36+
}
37+
38+
return this.client.get(this._path());
39+
}
40+
41+
_path() {
42+
return `/backups/${this.backend}/${this.backupId}/restore`;
43+
}
44+
}

0 commit comments

Comments
 (0)