/
siteclassification-set.ts
177 lines (161 loc) · 6.12 KB
/
siteclassification-set.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import { Logger } from '../../../../cli';
import {
CommandOption
} from '../../../../Command';
import GlobalOptions from '../../../../GlobalOptions';
import request from '../../../../request';
import GraphCommand from '../../../base/GraphCommand';
import commands from '../../commands';
import { DirectorySetting, UpdateDirectorySetting } from './DirectorySetting';
import { DirectorySettingValue } from './DirectorySettingValue';
interface CommandArgs {
options: Options;
}
interface Options extends GlobalOptions {
classifications?: string;
defaultClassification?: string;
usageGuidelinesUrl?: string;
guestUsageGuidelinesUrl?: string;
}
class AadSiteClassificationSetCommand extends GraphCommand {
public get name(): string {
return commands.SITECLASSIFICATION_SET;
}
public get description(): string {
return 'Updates site classification configuration';
}
public getTelemetryProperties(args: CommandArgs): any {
const telemetryProps: any = super.getTelemetryProperties(args);
telemetryProps.classifications = typeof args.options.classifications !== 'undefined';
telemetryProps.defaultClassification = typeof args.options.defaultClassification !== 'undefined';
telemetryProps.usageGuidelinesUrl = typeof args.options.usageGuidelinesUrl !== 'undefined';
telemetryProps.guestUsageGuidelinesUrl = typeof args.options.guestUsageGuidelinesUrl !== 'undefined';
return telemetryProps;
}
public commandAction(logger: Logger, args: CommandArgs, cb: (err?: any) => void): void {
const requestOptions: any = {
url: `${this.resource}/beta/settings`,
headers: {
accept: 'application/json;odata.metadata=none'
},
responseType: 'json'
};
request
.get<{ value: DirectorySetting[]; }>(requestOptions)
.then((res: { value: DirectorySetting[]; }): Promise<void> => {
const unifiedGroupSetting: DirectorySetting[] = res.value.filter((directorySetting: DirectorySetting): boolean => {
return directorySetting.displayName === 'Group.Unified';
});
if (!unifiedGroupSetting ||
unifiedGroupSetting.length === 0) {
return Promise.reject("There is no previous defined site classification which can updated.");
}
const updatedDirSettings: UpdateDirectorySetting = new UpdateDirectorySetting();
unifiedGroupSetting[0].values.forEach((directorySetting: DirectorySettingValue) => {
switch (directorySetting.name) {
case "ClassificationList":
if (args.options.classifications) {
updatedDirSettings.values.push({
"name": directorySetting.name,
"value": args.options.classifications as string
});
}
else {
updatedDirSettings.values.push({
"name": directorySetting.name,
"value": directorySetting.value as string
});
}
break;
case "DefaultClassification":
if (args.options.defaultClassification) {
updatedDirSettings.values.push({
"name": directorySetting.name,
"value": args.options.defaultClassification as string
});
}
else {
updatedDirSettings.values.push({
"name": directorySetting.name,
"value": directorySetting.value as string
});
}
break;
case "UsageGuidelinesUrl":
if (args.options.usageGuidelinesUrl) {
updatedDirSettings.values.push({
"name": directorySetting.name,
"value": args.options.usageGuidelinesUrl as string
});
}
else {
updatedDirSettings.values.push({
"name": directorySetting.name,
"value": directorySetting.value as string
});
}
break;
case "GuestUsageGuidelinesUrl":
if (args.options.guestUsageGuidelinesUrl) {
updatedDirSettings.values.push({
"name": directorySetting.name,
"value": args.options.guestUsageGuidelinesUrl as string
});
}
else {
updatedDirSettings.values.push({
"name": directorySetting.name,
"value": directorySetting.value as string
});
}
break;
default:
updatedDirSettings.values.push({
"name": directorySetting.name,
"value": directorySetting.value as string
});
break;
}
});
const requestOptions: any = {
url: `${this.resource}/beta/settings/${unifiedGroupSetting[0].id}`,
headers: {
accept: 'application/json;odata.metadata=none',
'content-type': 'application/json'
},
responseType: 'json',
data: updatedDirSettings
};
return request.patch(requestOptions);
})
.then(_ => cb(), (err: any) => this.handleRejectedODataJsonPromise(err, logger, cb));
}
public options(): CommandOption[] {
const options: CommandOption[] = [
{
option: '-c, --classifications [classifications]'
},
{
option: '-d, --defaultClassification [defaultClassification]'
},
{
option: '-u, --usageGuidelinesUrl [usageGuidelinesUrl]'
},
{
option: '-g, --guestUsageGuidelinesUrl [guestUsageGuidelinesUrl]'
}
];
const parentOptions: CommandOption[] = super.options();
return options.concat(parentOptions);
}
public validate(args: CommandArgs): boolean | string {
if (!args.options.classifications &&
!args.options.defaultClassification &&
!args.options.usageGuidelinesUrl &&
!args.options.guestUsageGuidelinesUrl) {
return 'Specify at least one property to update';
}
return true;
}
}
module.exports = new AadSiteClassificationSetCommand();