-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathapplications.ts
More file actions
148 lines (120 loc) · 5.3 KB
/
applications.ts
File metadata and controls
148 lines (120 loc) · 5.3 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
/**
* Application API Journey — Full lifecycle of applications and access keys
*
* Demonstrates all ApplicationClient APIs:
* - Application CRUD (create, get, update, list, delete)
* - Access key management (create, list, toggle, delete)
* - Role management (add, remove)
* - Tag management (add, get, delete)
*
* Run:
* CONDUCTOR_SERVER_URL=http://localhost:8080 npx ts-node examples/api-journeys/applications.ts
*/
import { OrkesClients } from "../../src/sdk";
async function main() {
const clients = await OrkesClients.from();
const appClient = clients.getApplicationClient();
const appName = `example_app_${Date.now()}`;
let applicationId = "";
let accessKeyId = "";
try {
// ── 1. Application CRUD ───────────────────────────────────────────
console.log("=== Application CRUD ===\n");
// Create application
const app = await appClient.createApplication(appName);
if (!app.id) throw new Error("Expected application id");
applicationId = app.id;
console.log(`1. Created application: ${app.name} (id: ${applicationId})`);
// Get application
const fetched = await appClient.getApplication(applicationId);
console.log(`2. Fetched application: ${fetched.name}`);
// Update application name
const updatedName = `${appName}_updated`;
const updated = await appClient.updateApplication(applicationId, updatedName);
console.log(`3. Updated application name: ${updated.name}`);
// List all applications
const allApps = await appClient.getAllApplications();
console.log(`4. Total applications: ${allApps.length}`);
// ── 2. Access Key Management ──────────────────────────────────────
console.log("\n=== Access Key Management ===\n");
// Create access key
const accessKey = await appClient.createAccessKey(applicationId);
accessKeyId = accessKey.id;
console.log(`5. Created access key: ${accessKey.id}`);
console.log(` Secret: ${accessKey.secret?.substring(0, 10)}...`);
// List access keys
const keys = await appClient.getAccessKeys(applicationId);
console.log(`6. Access keys for app: ${keys.length}`);
// Toggle access key status (active -> inactive)
const toggled = await appClient.toggleAccessKeyStatus(applicationId, accessKeyId);
console.log(`7. Toggled access key status: ${toggled.status}`);
// Toggle back (inactive -> active)
const toggledBack = await appClient.toggleAccessKeyStatus(applicationId, accessKeyId);
console.log(`8. Toggled back: ${toggledBack.status}`);
// ── 3. Role Management ────────────────────────────────────────────
console.log("\n=== Role Management ===\n");
// Add role
await appClient.addApplicationRole(applicationId, "WORKER");
console.log("9. Added WORKER role");
// Remove role
await appClient.removeRoleFromApplicationUser(applicationId, "WORKER");
console.log("10. Removed WORKER role");
// ── 4. Tag Management ─────────────────────────────────────────────
console.log("\n=== Tag Management ===\n");
// Add tags
await appClient.addApplicationTags(applicationId, [
{ key: "environment", value: "staging" },
{ key: "team", value: "platform" },
]);
console.log("11. Added tags");
// Get tags
const tags = await appClient.getApplicationTags(applicationId);
console.log(`12. Tags: ${JSON.stringify(tags)}`);
// Delete a single tag
await appClient.deleteApplicationTag(applicationId, {
key: "team",
value: "platform",
});
console.log("13. Deleted 'team' tag");
// Verify remaining tags
const remainingTags = await appClient.getApplicationTags(applicationId);
console.log(`14. Remaining tags: ${JSON.stringify(remainingTags)}`);
// Lookup by access key
const lookedUp = await appClient.getAppByAccessKeyId(accessKeyId);
console.log(`15. Looked up app by access key: ${lookedUp.name}`);
// ── 5. Cleanup ──────────────────────────────────────────────────
console.log("\n=== Cleanup ===\n");
// Delete access key
await appClient.deleteAccessKey(applicationId, accessKeyId);
console.log("16. Deleted access key");
// Delete remaining tags
await appClient.deleteApplicationTags(applicationId, remainingTags);
console.log("17. Deleted remaining tags");
// Delete application
await appClient.deleteApplication(applicationId);
console.log("18. Deleted application");
} catch (err) {
console.error("Error:", err);
// Best-effort cleanup
try {
if (accessKeyId && applicationId) {
await appClient.deleteAccessKey(applicationId, accessKeyId);
}
} catch {
/* ignore */
}
try {
if (applicationId) {
await appClient.deleteApplication(applicationId);
}
} catch {
/* ignore */
}
}
console.log("\nDone.");
process.exit(0);
}
main().catch((err) => {
console.error(err);
process.exit(1);
});