-
Notifications
You must be signed in to change notification settings - Fork 308
/
tenant-recyclebinitem-restore.ts
113 lines (93 loc) · 3.15 KB
/
tenant-recyclebinitem-restore.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { Logger } from '../../../../cli/Logger.js';
import GlobalOptions from '../../../../GlobalOptions.js';
import request, { CliRequestOptions } from '../../../../request.js';
import { spo } from '../../../../utils/spo.js';
import { validation } from '../../../../utils/validation.js';
import SpoCommand from '../../../base/SpoCommand.js';
import commands from '../../commands.js';
import { setTimeout } from 'timers/promises';
interface CommandArgs {
options: Options;
}
interface Response {
HasTimedout: boolean,
IsComplete: boolean,
PollingInterval: number
}
interface Options extends GlobalOptions {
siteUrl: string;
wait: boolean;
}
class SpoTenantRecycleBinItemRestoreCommand extends SpoCommand {
public get name(): string {
return commands.TENANT_RECYCLEBINITEM_RESTORE;
}
public get description(): string {
return 'Restores the specified deleted site collection from tenant recycle bin';
}
constructor() {
super();
this.#initTelemetry();
this.#initOptions();
this.#initValidators();
}
#initTelemetry(): void {
this.telemetry.push((args: CommandArgs) => {
Object.assign(this.telemetryProperties, {
wait: args.options.wait
});
});
}
#initOptions(): void {
this.options.unshift(
{
option: '-u, --siteUrl <siteUrl>'
},
{
option: '--wait'
}
);
}
#initValidators(): void {
this.validators.push(
async (args: CommandArgs) => validation.isValidSharePointUrl(args.options.siteUrl)
);
}
public async commandAction(logger: Logger, args: CommandArgs): Promise<void> {
try {
const adminUrl: string = await spo.getSpoAdminUrl(logger, this.debug);
const requestOptions: CliRequestOptions = {
url: `${adminUrl}/_api/SPOInternalUseOnly.Tenant/RestoreDeletedSite`,
headers: {
accept: 'application/json;odata=nometadata',
'content-type': 'application/json;charset=utf-8'
},
data: {
siteUrl: args.options.siteUrl
}
};
const response: string = await request.post(requestOptions);
let responseContent: Response = JSON.parse(response);
if (args.options.wait && !responseContent.IsComplete) {
responseContent = await this.waitUntilTenantRestoreFinished(responseContent.PollingInterval, requestOptions, logger);
}
await logger.log(responseContent);
}
catch (err: any) {
this.handleRejectedODataJsonPromise(err);
}
}
private async waitUntilTenantRestoreFinished(pollingInterval: number, requestOptions: CliRequestOptions, logger: Logger): Promise<any> {
if (this.verbose) {
await logger.logToStderr(`Site collection still restoring. Retrying in ${pollingInterval / 1000} seconds...`);
}
await setTimeout(pollingInterval);
const response: string = await request.post(requestOptions);
const responseContent: Response = JSON.parse(response);
if (responseContent.IsComplete) {
return responseContent;
}
return await this.waitUntilTenantRestoreFinished(responseContent.PollingInterval, requestOptions, logger);
}
}
export default new SpoTenantRecycleBinItemRestoreCommand();