From bc5f003c9c504dbf8d19c59b1292588fa0f65473 Mon Sep 17 00:00:00 2001 From: Ramkumar Ethiraj <64294490+rmkr-dev@users.noreply.github.com> Date: Tue, 8 Sep 2026 12:44:52 +0000 Subject: [PATCH] Add Azure Resource ID parser tool Parse ARM resource IDs into subscription, resource group, type, and name with unit tests wired into site checks. --- README.md | 1 + assets/site.js | 1 + azure-id/app.js | 153 ++++++++++++++++++++++++++++++++++ azure-id/index.html | 32 +++++++ scripts/azure-id-lib.mjs | 149 +++++++++++++++++++++++++++++++++ scripts/azure-id-lib.test.mjs | 73 ++++++++++++++++ scripts/site-check.mjs | 7 ++ 7 files changed, 416 insertions(+) create mode 100644 azure-id/app.js create mode 100644 azure-id/index.html create mode 100644 scripts/azure-id-lib.mjs create mode 100644 scripts/azure-id-lib.test.mjs diff --git a/README.md b/README.md index 72f1478..3e070fe 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,7 @@ Skillbook and Promptbook load JSON over `fetch`, so use a local server (opening - **UUID Generator** - **PEM / SSH Viewer** - **CIDR Calculator** — IPv4 network, broadcast, mask, host range +- **Azure Resource ID** — parse ARM IDs into subscription, RG, type, name ### Time diff --git a/assets/site.js b/assets/site.js index 4d88519..f4b83e8 100644 --- a/assets/site.js +++ b/assets/site.js @@ -16,6 +16,7 @@ { id: "uuid", path: "uuid/", name: "UUID Generator", blurb: "Create v4 UUIDs in bulk and copy them.", cat: "Security" }, { id: "cert", path: "cert/", name: "PEM / SSH Viewer", blurb: "Inspect PEM blocks and SSH key fingerprints.", cat: "Security" }, { id: "cidr", path: "cidr/", name: "CIDR Calculator", blurb: "Network, broadcast, mask, and host range from an IPv4 CIDR.", cat: "Security" }, + { id: "azure-id", path: "azure-id/", name: "Azure Resource ID", blurb: "Split subscription, resource group, type, and name from an ARM ID.", cat: "Security" }, { id: "time", path: "time/", name: "Time Overlay", blurb: "Compare working hours across time zones.", cat: "Time" }, { id: "timestamp", path: "timestamp/", name: "Unix Timestamp", blurb: "Convert Unix time to human dates and back.", cat: "Time" }, { id: "cron", path: "cron/", name: "Cron Explainer", blurb: "Read cron expressions and preview next runs.", cat: "Time" }, diff --git a/azure-id/app.js b/azure-id/app.js new file mode 100644 index 0000000..4e21d53 --- /dev/null +++ b/azure-id/app.js @@ -0,0 +1,153 @@ +(function () { + var GUID_RE = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/; + function isGuid(value) { + return GUID_RE.test(String(value || "").trim()); + } + function parseAzureResourceId(input) { + var raw = String(input || "").trim(); + if (!raw) throw new Error("Paste an Azure resource ID"); + var path = raw; + if ((path.charAt(0) === '"' && path.charAt(path.length - 1) === '"') || + (path.charAt(0) === "'" && path.charAt(path.length - 1) === "'")) { + path = path.slice(1, -1).trim(); + } + if (path.charAt(0) !== "/") throw new Error("Resource ID must start with /"); + var segments = path.split("/").filter(Boolean); + if (!segments.length) throw new Error("Resource ID has no segments"); + var result = { + input: raw, + normalized: "/" + segments.join("/"), + kind: "unknown", + subscriptionId: null, + resourceGroup: null, + providers: [], + resourceType: null, + name: null, + parent: null, + segments: segments.slice() + }; + var i = 0; + function lower(s) { return String(s).toLowerCase(); } + if (lower(segments[0]) === "subscriptions") { + if (segments.length < 2) throw new Error("Missing subscription ID"); + result.subscriptionId = segments[1]; + if (!isGuid(result.subscriptionId)) throw new Error("Subscription ID should be a GUID"); + i = 2; + result.kind = "subscription"; + if (i < segments.length && lower(segments[i]) === "resourcegroups") { + if (i + 1 >= segments.length) throw new Error("Missing resource group name"); + result.resourceGroup = segments[i + 1]; + i += 2; + result.kind = "resourceGroup"; + } + } else if (lower(segments[0]) === "providers") { + i = 0; + result.kind = "providerScoped"; + } else { + throw new Error("Expected /subscriptions/{id}/... or /providers/{namespace}/..."); + } + if (i < segments.length) { + if (lower(segments[i]) !== "providers") { + throw new Error('Unexpected segment "' + segments[i] + '" (expected providers)'); + } + if (i + 1 >= segments.length) throw new Error("Missing provider namespace"); + var namespace = segments[i + 1]; + i += 2; + var typeParts = []; + var nameParts = []; + var expectingType = true; + while (i < segments.length) { + if (expectingType) typeParts.push(segments[i]); + else nameParts.push(segments[i]); + expectingType = !expectingType; + i += 1; + } + if (!typeParts.length) throw new Error("Provider present but resource type is missing"); + if (typeParts.length !== nameParts.length) { + throw new Error("Each resource type needs a matching name segment"); + } + result.providers = [{ namespace: namespace, types: typeParts.slice(), names: nameParts.slice() }]; + result.resourceType = namespace + "/" + typeParts.join("/"); + result.name = nameParts[nameParts.length - 1]; + if (nameParts.length > 1) { + result.parent = { + resourceType: namespace + "/" + typeParts.slice(0, -1).join("/"), + name: nameParts[nameParts.length - 2] + }; + } + result.kind = "resource"; + } + return result; + } + function formatAzureIdSummary(info) { + var lines = ["Normalized: " + info.normalized, "Kind: " + info.kind]; + if (info.subscriptionId) lines.push("Subscription: " + info.subscriptionId); + if (info.resourceGroup) lines.push("Resource group: " + info.resourceGroup); + if (info.resourceType) lines.push("Type: " + info.resourceType); + if (info.name) lines.push("Name: " + info.name); + if (info.parent) lines.push("Parent: " + info.parent.resourceType + " / " + info.parent.name); + if (info.providers.length) { + var p = info.providers[0]; + lines.push("Provider: " + p.namespace); + for (var i = 0; i < p.types.length; i++) { + lines.push(" " + p.types[i] + " = " + p.names[i]); + } + } + return lines.join("\n"); + } + function esc(s) { + return String(s) + .replace(/&/g, "&") + .replace(//g, ">") + .replace(/"/g, """); + } + function card(title, value) { + return '

' + esc(title) + + '

' + + esc(value) + "

"; + } + function ready() { + var input = document.getElementById("idInput"); + var output = document.getElementById("output"); + var status = document.getElementById("status"); + var cards = document.getElementById("cards"); + function render(info) { + output.value = formatAzureIdSummary(info); + var bits = []; + if (info.subscriptionId) bits.push(card("Subscription", info.subscriptionId)); + if (info.resourceGroup) bits.push(card("Resource group", info.resourceGroup)); + if (info.resourceType) bits.push(card("Type", info.resourceType)); + if (info.name) bits.push(card("Name", info.name)); + if (info.parent) bits.push(card("Parent", info.parent.resourceType + " / " + info.parent.name)); + bits.push(card("Kind", info.kind)); + cards.innerHTML = bits.join(""); + RMKR.setStatus(status, "Parsed " + info.kind + ".", "ok"); + } + function run() { + try { render(parseAzureResourceId(input.value)); } + catch (e) { + cards.innerHTML = ""; + output.value = ""; + RMKR.setStatus(status, e.message || String(e), "err"); + } + } + document.getElementById("run").onclick = run; + document.getElementById("clear").onclick = function () { + input.value = ""; + output.value = ""; + cards.innerHTML = ""; + RMKR.setStatus(status, "", ""); + input.focus(); + }; + document.getElementById("copyOut").onclick = function () { + var v = output.value; + if (!v) return RMKR.setStatus(status, "Parse first.", "err"); + RMKR.copy(v).then(function () { RMKR.setStatus(status, "Copied.", "ok"); }); + }; + input.addEventListener("keydown", function (e) { if (e.key === "Enter") run(); }); + run(); + } + if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", ready); + else ready(); +})(); diff --git a/azure-id/index.html b/azure-id/index.html new file mode 100644 index 0000000..693d2b5 --- /dev/null +++ b/azure-id/index.html @@ -0,0 +1,32 @@ + + + + + + Azure Resource ID | RMKR Toolkit + + + + + + + + +
+

Azure Resource ID

+

Paste an ARM resource ID. Split subscription, resource group, provider, type, and name without leaving this tab.

+
+ + + + + +
+

+
+ + +
+ + + diff --git a/scripts/azure-id-lib.mjs b/scripts/azure-id-lib.mjs new file mode 100644 index 0000000..59d5857 --- /dev/null +++ b/scripts/azure-id-lib.mjs @@ -0,0 +1,149 @@ +/** + * Azure Resource ID helpers for the toolkit parser. + * Pure functions — safe from Node tests and the browser. + */ + +const GUID_RE = + /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/; + +export function isGuid(value) { + return GUID_RE.test(String(value || "").trim()); +} + +/** + * Parse a full or partial Azure resource ID into structured parts. + * Accepts subscription, resource group, provider, and nested type segments. + */ +export function parseAzureResourceId(input) { + const raw = String(input || "").trim(); + if (!raw) throw new Error("Paste an Azure resource ID"); + + let path = raw; + // Allow accidental wrapping quotes or ARM ID prefix noise + if ( + (path.startsWith('"') && path.endsWith('"')) || + (path.startsWith("'") && path.endsWith("'")) + ) { + path = path.slice(1, -1).trim(); + } + if (!path.startsWith("/")) { + throw new Error("Resource ID must start with /"); + } + + const segments = path.split("/").filter(Boolean); + if (!segments.length) throw new Error("Resource ID has no segments"); + + const result = { + input: raw, + normalized: "/" + segments.join("/"), + kind: "unknown", + subscriptionId: null, + resourceGroup: null, + providers: [], + resourceType: null, + name: null, + parent: null, + segments: segments.slice(), + }; + + let i = 0; + const lower = (s) => String(s).toLowerCase(); + + if (lower(segments[0]) === "subscriptions") { + if (segments.length < 2) throw new Error("Missing subscription ID"); + result.subscriptionId = segments[1]; + if (!isGuid(result.subscriptionId)) { + throw new Error("Subscription ID should be a GUID"); + } + i = 2; + result.kind = "subscription"; + + if (i < segments.length && lower(segments[i]) === "resourcegroups") { + if (i + 1 >= segments.length) throw new Error("Missing resource group name"); + result.resourceGroup = segments[i + 1]; + i += 2; + result.kind = "resourceGroup"; + } + } else if (lower(segments[0]) === "providers") { + // Tenant- or management-group scoped + i = 0; + result.kind = "providerScoped"; + } else { + throw new Error( + "Expected /subscriptions/{id}/... or /providers/{namespace}/..." + ); + } + + if (i < segments.length) { + if (lower(segments[i]) !== "providers") { + throw new Error(`Unexpected segment "${segments[i]}" (expected providers)`); + } + if (i + 1 >= segments.length) throw new Error("Missing provider namespace"); + + const namespace = segments[i + 1]; + i += 2; + + const typeParts = []; + const nameParts = []; + let expectingType = true; + while (i < segments.length) { + if (expectingType) { + typeParts.push(segments[i]); + expectingType = false; + } else { + nameParts.push(segments[i]); + expectingType = true; + } + i += 1; + } + if (!typeParts.length) { + throw new Error("Provider present but resource type is missing"); + } + if (typeParts.length !== nameParts.length) { + throw new Error("Each resource type needs a matching name segment"); + } + + result.providers = [ + { + namespace, + types: typeParts.slice(), + names: nameParts.slice(), + }, + ]; + result.resourceType = namespace + "/" + typeParts.join("/"); + result.name = nameParts[nameParts.length - 1]; + if (nameParts.length > 1) { + result.parent = { + resourceType: namespace + "/" + typeParts.slice(0, -1).join("/"), + name: nameParts[nameParts.length - 2], + }; + } + result.kind = "resource"; + } + + return result; +} + +export function formatAzureIdSummary(info) { + const lines = [ + `Normalized: ${info.normalized}`, + `Kind: ${info.kind}`, + ]; + if (info.subscriptionId) lines.push(`Subscription: ${info.subscriptionId}`); + if (info.resourceGroup) lines.push(`Resource group: ${info.resourceGroup}`); + if (info.resourceType) lines.push(`Type: ${info.resourceType}`); + if (info.name) lines.push(`Name: ${info.name}`); + if (info.parent) { + lines.push( + `Parent: ${info.parent.resourceType} / ${info.parent.name}` + ); + } + if (info.providers.length) { + const p = info.providers[0]; + lines.push(`Provider: ${p.namespace}`); + for (let i = 0; i < p.types.length; i++) { + lines.push(` ${p.types[i]} = ${p.names[i]}`); + } + } + return lines.join("\n"); +} diff --git a/scripts/azure-id-lib.test.mjs b/scripts/azure-id-lib.test.mjs new file mode 100644 index 0000000..f7ae2fd --- /dev/null +++ b/scripts/azure-id-lib.test.mjs @@ -0,0 +1,73 @@ +import assert from "node:assert/strict"; +import { + isGuid, + parseAzureResourceId, + formatAzureIdSummary, +} from "./azure-id-lib.mjs"; + +export function runAzureIdTests() { + assert.equal(isGuid("00000000-0000-0000-0000-000000000000"), true); + assert.equal(isGuid("not-a-guid"), false); + + const sub = parseAzureResourceId( + "/subscriptions/11111111-1111-1111-1111-111111111111" + ); + assert.equal(sub.kind, "subscription"); + assert.equal(sub.subscriptionId, "11111111-1111-1111-1111-111111111111"); + assert.equal(sub.resourceGroup, null); + + const rg = parseAzureResourceId( + "/subscriptions/11111111-1111-1111-1111-111111111111/resourceGroups/demo-rg" + ); + assert.equal(rg.kind, "resourceGroup"); + assert.equal(rg.resourceGroup, "demo-rg"); + + const vm = parseAzureResourceId( + "/subscriptions/11111111-1111-1111-1111-111111111111/resourceGroups/demo-rg/providers/Microsoft.Compute/virtualMachines/web-01" + ); + assert.equal(vm.kind, "resource"); + assert.equal(vm.resourceType, "Microsoft.Compute/virtualMachines"); + assert.equal(vm.name, "web-01"); + assert.equal(vm.parent, null); + + const nested = parseAzureResourceId( + "/subscriptions/11111111-1111-1111-1111-111111111111/resourceGroups/demo-rg/providers/Microsoft.Compute/virtualMachines/web-01/extensions/CustomScript" + ); + assert.equal( + nested.resourceType, + "Microsoft.Compute/virtualMachines/extensions" + ); + assert.equal(nested.name, "CustomScript"); + assert.equal(nested.parent.resourceType, "Microsoft.Compute/virtualMachines"); + assert.equal(nested.parent.name, "web-01"); + + const quoted = parseAzureResourceId( + '"/subscriptions/11111111-1111-1111-1111-111111111111/resourceGroups/rg1"' + ); + assert.equal(quoted.resourceGroup, "rg1"); + + const summary = formatAzureIdSummary(vm); + assert.match(summary, /Microsoft\.Compute\/virtualMachines/); + assert.match(summary, /web-01/); + + assert.throws(() => parseAzureResourceId(""), /Paste/); + assert.throws(() => parseAzureResourceId("subscriptions/x"), /must start/); + assert.throws( + () => parseAzureResourceId("/subscriptions/not-a-guid"), + /GUID/ + ); + assert.throws( + () => + parseAzureResourceId( + "/subscriptions/11111111-1111-1111-1111-111111111111/resourceGroups/rg/providers/Microsoft.Compute/virtualMachines" + ), + /matching name/ + ); +} + +const isDirect = + process.argv[1] && process.argv[1].endsWith("azure-id-lib.test.mjs"); +if (isDirect) { + runAzureIdTests(); + console.log("azure-id-lib.test: OK"); +} diff --git a/scripts/site-check.mjs b/scripts/site-check.mjs index 1899d24..4665921 100644 --- a/scripts/site-check.mjs +++ b/scripts/site-check.mjs @@ -174,6 +174,13 @@ try { fail(`cidr-lib tests: ${e.message}`); } +const { runAzureIdTests } = await import("./azure-id-lib.test.mjs"); +try { + runAzureIdTests(); +} catch (e) { + fail(`azure-id-lib tests: ${e.message}`); +} + if (warnings.length) { console.log("Warnings:"); for (const w of warnings) console.log(" - " + w);