Skip to content

Commit b4f7cda

Browse files
committed
cli delete-service and reset
1 parent 902bc5a commit b4f7cda

5 files changed

Lines changed: 108 additions & 2 deletions

File tree

aggregator-platform/templates/instance-service-account.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ rules:
1515
- update
1616
- patch
1717
- delete
18+
- deletecollection
1819

1920
- apiGroups:
2021
- ""
@@ -26,4 +27,5 @@ rules:
2627
- create
2728
- update
2829
- patch
29-
- delete
30+
- delete
31+
- deletecollection

cli/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,14 @@ Show the currently active aggregator and its registered services.
128128
agg get-active
129129
```
130130

131+
#### `agg reset`
132+
133+
Remove all aggregators and services from the config. Use when you removed the aggregator server.
134+
135+
```bash
136+
agg reset
137+
```
138+
131139
---
132140

133141
### Service management
@@ -184,6 +192,31 @@ agg get-output --svc my-svc --agg <id> # specific service and aggregator
184192
| `--svc <name>` | Service name (overrides active) |
185193
| `--agg <id>` | Aggregator ID to use instead of the active one |
186194

195+
#### `agg delete-service`
196+
197+
Delete a service from the active aggregator.
198+
199+
```bash
200+
agg delete-service
201+
agg delete-service --svc my-svc # specific service
202+
agg delete-service --agg https://aggregator.example.org/id # specific aggregator
203+
```
204+
205+
| Option | Description |
206+
|---|---|
207+
| `--svc <name>` | Service name (overrides active) |
208+
| `--agg <id>` | Aggregator ID to use instead of the active one |
209+
210+
---
211+
agg get-service --svc my-svc # specific service
212+
agg get-service --agg https://aggregator.example.org/id # specific aggregator
213+
```
214+
215+
| Option | Description |
216+
|---|---|
217+
| `--svc <name>` | Service name (overrides active) |
218+
| `--agg <id>` | Aggregator ID to use instead of the active one |
219+
187220
---
188221
189222
### Config management

cli/src/cli.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ program
7676
await main({ agg: opts.agg, svc: opts.svc });
7777
});
7878

79+
program
80+
.command("delete-service")
81+
.description("Delete a service on the aggregator")
82+
.option("--agg <id>", "Aggregator ID to use instead of active")
83+
.option("--svc <name>", "Service name to use instead of active")
84+
.action(async (opts) => {
85+
const { main } = await import("./delete-service.js");
86+
await main({ agg: opts.agg, svc: opts.svc });
87+
});
88+
7989
program
8090
.command("get-output")
8191
.description("Fetch service outputs")
@@ -99,6 +109,14 @@ program
99109
console.log(`✅ Config loaded from ${file}`);
100110
});
101111

112+
program
113+
.command("reset")
114+
.description("Remove all aggregators and unset the active aggregator")
115+
.action(() => {
116+
updateConfig({ aggregators: {}, activeAggregator: null });
117+
console.log("✅ All aggregators removed and active aggregator unset.");
118+
});
119+
102120
program
103121
.command("where-config")
104122
.description("Show config file location")

cli/src/delete-service.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { KeycloakOIDCAuth } from "./util.js";
2+
import { config, updateConfig } from "./config.js";
3+
4+
export async function main(opts: { agg?: string, svc?: string } = {}) {
5+
const aggId = opts.agg ?? config.activeAggregator;
6+
if (!aggId) throw new Error("No active aggregator. Use --agg or run `agg set-active`.");
7+
8+
const agg = config.aggregators[aggId];
9+
if (!agg) throw new Error(`Aggregator "${aggId}" not found.`);
10+
11+
const svcName = opts.svc ?? config.service.name;
12+
if (!svcName) throw new Error("No service name provided. Use --svc");
13+
14+
const svc = agg.services[svcName];
15+
if (!svc) throw new Error(`Service "${svcName}" not found on Aggregator "${aggId}"`);
16+
17+
const SERVICE_ENDPOINT = `${agg.id}/${svc.name}`;
18+
19+
console.log("=== Initializing Keycloak Authentication ===");
20+
const auth = new KeycloakOIDCAuth();
21+
await auth.init(config.auth.idp);
22+
await auth.login(config.auth.username, config.auth.password, config.auth.clientId, config.auth.clientSecret);
23+
console.log("🔐 Auth initialized successfully.");
24+
const umaFetch = auth.createUMAFetch();
25+
26+
console.log("\n=== Deleting service ===");
27+
console.log(`➡️ Endpoint: ${SERVICE_ENDPOINT}\n`);
28+
29+
try {
30+
const response = await umaFetch(SERVICE_ENDPOINT, { method: "DELETE" });
31+
console.log(`📡 Response status: ${response.status}`);
32+
console.log("📄 Response body:\n");
33+
console.log(await response.text() || "(empty response)");
34+
35+
if (response.ok) {
36+
const { [svcName]: _, ...remainingServices } = agg.services;
37+
const updatedAgg = { ...agg, services: remainingServices };
38+
updateConfig({ aggregators: { ...config.aggregators, [aggId]: updatedAgg } });
39+
console.log(`✅ Service "${svcName}" removed from aggregator "${aggId}" in config.`);
40+
}
41+
} catch (err: any) {
42+
console.error("\n❌ Failed to delete service:");
43+
console.error(err?.message || err);
44+
}
45+
46+
console.log("\n=== Done ===");
47+
}

containers/aggregator/config/service_collection.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,13 @@ func (collec *ServiceCollection) postService(w http.ResponseWriter, r *http.Requ
279279
func (collec *ServiceCollection) deleteService(w http.ResponseWriter, _ *http.Request, service model.Service) {
280280
logrus.WithFields(logrus.Fields{"service_id": service.InstanceID}).Info("Request to delete service")
281281

282-
service.Stop()
282+
err := service.Stop()
283+
if err != nil {
284+
logrus.WithError(err).Error("Failed to stop service")
285+
http.Error(w, "Failed to stop service", http.StatusInternalServerError)
286+
return
287+
}
288+
283289
delete(collec.services, service.InstanceID)
284290

285291
collec.etagServices++

0 commit comments

Comments
 (0)