-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtenants.ts
More file actions
227 lines (212 loc) · 6.88 KB
/
Copy pathtenants.ts
File metadata and controls
227 lines (212 loc) · 6.88 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import type { Command } from "commander";
import { withErrorHandler, createClient, getFormat, outputResponse } from "../../helpers.js";
import { parseJsonInput } from "../../input.js";
import { printSuccess } from "../../output.js";
import { addExamples } from "../help.js";
export function registerTenantsCommand(parent: Command): void {
const tenants = parent
.command("tenants")
.description("Manage tenants");
// tenants list
const list = tenants
.command("list")
.description("List all tenants in the system, including their status and configuration")
.action(
withErrorHandler(async (_opts: unknown, cmd: Command) => {
const client = createClient(cmd);
const format = getFormat(cmd);
const response = await client.rawRequest("GET", "/admin/tenants");
outputResponse(response, format);
}),
);
addExamples(list, [
{
description: "List all tenants",
command: "geonic admin tenants list",
},
{
description: "List tenants in table format",
command: "geonic admin tenants list --format table",
},
]);
// tenants get
const get = tenants
.command("get <id>")
.description("Get a tenant's details — name, description, status, and creation date")
.action(
withErrorHandler(async (id: unknown, _opts: unknown, cmd: Command) => {
const client = createClient(cmd);
const format = getFormat(cmd);
const response = await client.rawRequest(
"GET",
`/admin/tenants/${encodeURIComponent(String(id))}`,
);
outputResponse(response, format);
}),
);
addExamples(get, [
{
description: "Get tenant details by ID",
command: "geonic admin tenants get <tenant-id>",
},
{
description: "Get tenant details in table format",
command: "geonic admin tenants get <tenant-id> --format table",
},
]);
// tenants create
const create = tenants
.command("create [json]")
.description(
"Create a new tenant\n\n" +
"JSON payload example:\n" +
" {\n" +
' "name": "production",\n' +
' "description": "Production environment tenant"\n' +
" }",
)
.action(
withErrorHandler(async (json: unknown, _opts: unknown, cmd: Command) => {
const body = await parseJsonInput(json as string | undefined);
const client = createClient(cmd);
const format = getFormat(cmd);
const response = await client.rawRequest("POST", "/admin/tenants", {
body,
});
outputResponse(response, format);
printSuccess("Tenant created.");
}),
);
addExamples(create, [
{
description: "Create with inline JSON",
command: `geonic admin tenants create '{"name":"my-tenant","description":"My first tenant"}'`,
},
{
description: "Minimal (name only)",
command: `geonic admin tenants create '{"name":"production"}'`,
},
{
description: "Create from a JSON file",
command: "geonic admin tenants create @tenant.json",
},
{
description: "Create from stdin pipe",
command: "cat tenant.json | geonic admin tenants create",
},
{
description: "Interactive mode (omit JSON argument)",
command: "geonic admin tenants create",
},
]);
// tenants update
const update = tenants
.command("update <id> [json]")
.description(
"Update a tenant\n\n" +
"JSON payload: only specified fields are updated.\n" +
' e.g. {"name": "new-name", "description": "Updated description"}',
)
.action(
withErrorHandler(
async (id: unknown, json: unknown, _opts: unknown, cmd: Command) => {
const body = (await parseJsonInput(json as string | undefined)) as Record<string, unknown>;
const client = createClient(cmd);
const format = getFormat(cmd);
const response = await client.rawRequest(
"PATCH",
`/admin/tenants/${encodeURIComponent(String(id))}`,
{ body },
);
outputResponse(response, format);
printSuccess("Tenant updated.");
},
),
);
addExamples(update, [
{
description: "Update description with inline JSON",
command: `geonic admin tenants update <tenant-id> '{"description":"Updated description"}'`,
},
{
description: "Rename a tenant",
command: `geonic admin tenants update <tenant-id> '{"name":"new-name"}'`,
},
{
description: "Update from a JSON file",
command: "geonic admin tenants update <tenant-id> @patch.json",
},
{
description: "Update from stdin pipe",
command: "cat patch.json | geonic admin tenants update <tenant-id>",
},
{
description: "Interactive mode",
command: "geonic admin tenants update <tenant-id>",
},
]);
// tenants delete
const del = tenants
.command("delete <id>")
.description("Permanently delete a tenant and all its associated data. This action cannot be undone")
.action(
withErrorHandler(async (id: unknown, _opts: unknown, cmd: Command) => {
const client = createClient(cmd);
await client.rawRequest(
"DELETE",
`/admin/tenants/${encodeURIComponent(String(id))}`,
);
printSuccess("Tenant deleted.");
}),
);
addExamples(del, [
{
description: "Delete a tenant by ID",
command: "geonic admin tenants delete <tenant-id>",
},
{
description: "Delete with verbose output to confirm",
command: "geonic admin tenants delete <tenant-id> --verbose",
},
]);
// tenants activate
const activate = tenants
.command("activate <id>")
.description("Activate a tenant, restoring API access for all its users")
.action(
withErrorHandler(async (id: unknown, _opts: unknown, cmd: Command) => {
const client = createClient(cmd);
await client.rawRequest(
"POST",
`/admin/tenants/${encodeURIComponent(String(id))}/activate`,
);
printSuccess("Tenant activated.");
}),
);
addExamples(activate, [
{
description: "Activate a deactivated tenant",
command: "geonic admin tenants activate <tenant-id>",
},
]);
// tenants deactivate
const deactivate = tenants
.command("deactivate <id>")
.description("Deactivate a tenant, blocking API access for all its users until reactivated")
.action(
withErrorHandler(async (id: unknown, _opts: unknown, cmd: Command) => {
const client = createClient(cmd);
await client.rawRequest(
"POST",
`/admin/tenants/${encodeURIComponent(String(id))}/deactivate`,
);
printSuccess("Tenant deactivated.");
}),
);
addExamples(deactivate, [
{
description: "Deactivate a tenant to temporarily suspend access",
command: "geonic admin tenants deactivate <tenant-id>",
},
]);
}