-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathupdate.ts
More file actions
158 lines (131 loc) · 5.19 KB
/
update.ts
File metadata and controls
158 lines (131 loc) · 5.19 KB
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
/*
* Copyright (c) 2025, Salesforce, Inc.
* SPDX-License-Identifier: Apache-2
* For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0
*/
import {Args, Flags} from '@oclif/core';
import {OdsCommand} from '@salesforce/b2c-tooling-sdk/cli';
import {getApiErrorMessage, type OdsComponents} from '@salesforce/b2c-tooling-sdk';
import {t, withDocs} from '../../../i18n/index.js';
type RealmConfigurationUpdateRequestModel = OdsComponents['schemas']['RealmConfigurationUpdateRequestModel'];
type RealmConfigurationResponse = OdsComponents['schemas']['RealmConfigurationResponse'];
/**
* Update realm-level ODS configuration (TTL and schedulers).
*/
export default class SandboxRealmUpdate extends OdsCommand<typeof SandboxRealmUpdate> {
static aliases = ['ods:realm:update'];
static args = {
realm: Args.string({
description: 'Realm ID (four-letter ID) to update',
required: true,
}),
};
static description = withDocs(
t('commands.realm.update.description', 'Update realm configuration'),
'/cli/realm.html#b2c-realm-update',
);
static enableJsonFlag = true;
static examples = [
'<%= config.bin %> <%= command.id %> zzzz --max-sandbox-ttl 72',
'<%= config.bin %> <%= command.id %> zzzz --default-sandbox-ttl 24',
'<%= config.bin %> <%= command.id %> zzzz --start-scheduler \'{"weekdays":["MONDAY"],"time":"08:00:00Z"}\'',
'<%= config.bin %> <%= command.id %> zzzz --stop-scheduler "null"',
];
static flags = {
'max-sandbox-ttl': Flags.integer({
description: 'Maximum sandbox TTL in hours (0 for unlimited, subject to quotas)',
}),
'default-sandbox-ttl': Flags.integer({
description: 'Default sandbox TTL in hours when no TTL is specified at creation',
}),
'start-scheduler': Flags.string({
description:
'Start schedule JSON for sandboxes in this realm (use "null" to remove). Format: {"weekdays":[...],"time":"..."}',
}),
'stop-scheduler': Flags.string({
description:
'Stop schedule JSON for sandboxes in this realm (use "null" to remove). Format: {"weekdays":[...],"time":"..."}',
}),
} as const;
async run(): Promise<RealmConfigurationResponse | undefined> {
const {args, flags} = await this.parse(SandboxRealmUpdate);
const realm = args.realm;
const host = this.odsHost;
const hasAnyUpdateFlag =
flags['max-sandbox-ttl'] !== undefined ||
flags['default-sandbox-ttl'] !== undefined ||
flags['start-scheduler'] !== undefined ||
flags['stop-scheduler'] !== undefined;
if (!hasAnyUpdateFlag) {
this.error(
t(
'commands.realm.update.noChanges',
'No update flags specified. Use --max-sandbox-ttl, --default-sandbox-ttl, --start-scheduler, or --stop-scheduler.',
),
);
}
this.log(
t('commands.realm.update.updating', 'Updating realm {{realm}} on {{host}}...', {
realm,
host,
}),
);
const body: RealmConfigurationUpdateRequestModel = {};
if (flags['max-sandbox-ttl'] !== undefined || flags['default-sandbox-ttl'] !== undefined) {
body.sandbox = body.sandbox ?? {};
body.sandbox.sandboxTTL = body.sandbox.sandboxTTL ?? {};
if (flags['max-sandbox-ttl'] !== undefined) {
body.sandbox.sandboxTTL.maximum = flags['max-sandbox-ttl'];
}
if (flags['default-sandbox-ttl'] !== undefined) {
body.sandbox.sandboxTTL.defaultValue = flags['default-sandbox-ttl'];
}
}
// Helper to parse scheduler flags (JSON or "null")
const parseScheduler = (value: string | undefined) => {
if (!value) return;
if (value === 'null') return null;
try {
return JSON.parse(value) as OdsComponents['schemas']['WeekdaySchedule'];
} catch {
this.error(
t('commands.realm.update.schedulerParseError', 'Invalid JSON for scheduler. Use valid JSON or "null".'),
);
}
};
const startScheduler = parseScheduler(flags['start-scheduler']);
const stopScheduler = parseScheduler(flags['stop-scheduler']);
if (startScheduler !== undefined) {
body.sandbox = body.sandbox ?? {};
// null removes the existing schedule
// eslint-disable-next-line @typescript-eslint/no-explicit-any
body.sandbox.startScheduler = startScheduler as any;
}
if (stopScheduler !== undefined) {
body.sandbox = body.sandbox ?? {};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
body.sandbox.stopScheduler = stopScheduler as any;
}
const result = await this.odsClient.PATCH('/realms/{realm}/configuration', {
params: {path: {realm}},
body,
});
if (result.error) {
this.error(
t('commands.realm.update.error', 'Failed to update realm {{realm}}: {{message}}', {
realm,
message: getApiErrorMessage(result.error, result.response),
}),
);
}
if (this.jsonEnabled()) {
return result.data as RealmConfigurationResponse | undefined;
}
this.log(
t('commands.realm.update.success', 'Successfully updated realm {{realm}}.', {
realm,
}),
);
return result.data as RealmConfigurationResponse | undefined;
}
}