|
| 1 | +/** |
| 2 | + * Cleans up AI Core test resource groups. |
| 3 | + * |
| 4 | + * Required environment variables: |
| 5 | + * RESOURCE_GROUP_PREFIX - The prefix identifying resource groups owned by this run |
| 6 | + * (e.g. "itest-12345-1-j17" or "sonar-12345-1") |
| 7 | + * |
| 8 | + * Optional environment variables: |
| 9 | + * STALE_PREFIXES - Comma-separated list of additional prefixes to clean up |
| 10 | + * (defaults to "itest-rg-,cds-itest-") |
| 11 | + * |
| 12 | + * Credentials are resolved from VCAP_SERVICES (via cds bind) or AICORE_SERVICE_KEY. |
| 13 | + */ |
| 14 | +const https = require("https"); |
| 15 | + |
| 16 | +const DEFAULT_STALE_PREFIXES = ["itest-rg-", "cds-itest-"]; |
| 17 | + |
| 18 | +function getCredentials() { |
| 19 | + const vcap = JSON.parse(process.env.VCAP_SERVICES || "{}"); |
| 20 | + return ( |
| 21 | + (vcap.aicore || vcap["ai-core"] || [{}])[0].credentials || |
| 22 | + JSON.parse(process.env.AICORE_SERVICE_KEY || "null") |
| 23 | + ); |
| 24 | +} |
| 25 | + |
| 26 | +function request(url, opts = {}) { |
| 27 | + return new Promise((resolve, reject) => { |
| 28 | + const u = new URL(url); |
| 29 | + const req = https.request( |
| 30 | + { |
| 31 | + hostname: u.hostname, |
| 32 | + path: u.pathname + u.search, |
| 33 | + method: opts.method || "GET", |
| 34 | + headers: opts.headers || {}, |
| 35 | + }, |
| 36 | + (res) => { |
| 37 | + let data = ""; |
| 38 | + res.on("data", (chunk) => (data += chunk)); |
| 39 | + res.on("end", () => resolve({ status: res.statusCode, body: data })); |
| 40 | + } |
| 41 | + ); |
| 42 | + req.on("error", reject); |
| 43 | + if (opts.body) req.write(opts.body); |
| 44 | + req.end(); |
| 45 | + }); |
| 46 | +} |
| 47 | + |
| 48 | +async function getAccessToken(credentials) { |
| 49 | + const tokenUrl = credentials.url + "/oauth/token"; |
| 50 | + const params = new URLSearchParams({ grant_type: "client_credentials" }); |
| 51 | + const authHeader = |
| 52 | + "Basic " + |
| 53 | + Buffer.from(credentials.clientid + ":" + credentials.clientsecret).toString( |
| 54 | + "base64" |
| 55 | + ); |
| 56 | + const res = await request(tokenUrl + "?" + params.toString(), { |
| 57 | + headers: { Authorization: authHeader }, |
| 58 | + }); |
| 59 | + return JSON.parse(res.body).access_token; |
| 60 | +} |
| 61 | + |
| 62 | +async function deleteResourceGroups(apiUrl, headers, prefixes) { |
| 63 | + const res = await request(apiUrl + "/v2/admin/resourceGroups", { headers }); |
| 64 | + const groups = JSON.parse(res.body).resources || []; |
| 65 | + const toDelete = groups.filter( |
| 66 | + (rg) => |
| 67 | + rg.resourceGroupId && |
| 68 | + prefixes.some((p) => rg.resourceGroupId.startsWith(p)) |
| 69 | + ); |
| 70 | + |
| 71 | + for (const rg of toDelete) { |
| 72 | + const delRes = await request( |
| 73 | + apiUrl + "/v2/admin/resourceGroups/" + rg.resourceGroupId, |
| 74 | + { method: "DELETE", headers } |
| 75 | + ); |
| 76 | + console.log("Delete", rg.resourceGroupId, "->", delRes.status); |
| 77 | + } |
| 78 | + |
| 79 | + console.log("Cleaned up", toDelete.length, "resource groups"); |
| 80 | +} |
| 81 | + |
| 82 | +async function main() { |
| 83 | + const ownPrefix = process.env.RESOURCE_GROUP_PREFIX; |
| 84 | + if (!ownPrefix) { |
| 85 | + console.error("RESOURCE_GROUP_PREFIX environment variable is required"); |
| 86 | + process.exit(1); |
| 87 | + } |
| 88 | + |
| 89 | + const credentials = getCredentials(); |
| 90 | + if (!credentials) { |
| 91 | + console.log("No AI Core credentials found, skipping cleanup"); |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + const stalePrefixes = process.env.STALE_PREFIXES |
| 96 | + ? process.env.STALE_PREFIXES.split(",").map((s) => s.trim()) |
| 97 | + : DEFAULT_STALE_PREFIXES; |
| 98 | + |
| 99 | + const prefixes = [ownPrefix, ...stalePrefixes]; |
| 100 | + |
| 101 | + const apiUrl = credentials.serviceurls.AI_API_URL; |
| 102 | + const token = await getAccessToken(credentials); |
| 103 | + const headers = { |
| 104 | + Authorization: "Bearer " + token, |
| 105 | + "AI-Resource-Group": "default", |
| 106 | + }; |
| 107 | + |
| 108 | + console.log("Cleaning resource groups matching prefixes:", prefixes); |
| 109 | + await deleteResourceGroups(apiUrl, headers, prefixes); |
| 110 | +} |
| 111 | + |
| 112 | +main().catch((e) => { |
| 113 | + console.error(e.message); |
| 114 | + process.exit(0); |
| 115 | +}); |
0 commit comments