UNPKG

armpit

Version:

Another resource manager programming interface toolkit.

88 lines 3.09 kB
import { validate as uuidValidate } from "uuid"; import { isStringValueArrayEqual } from "./tsUtils.js"; import { applyObjectKeyProperties, applySourceToTargetObject } from "./optionsUtils.js"; export function isSubscriptionId(value) { return uuidValidate(value); } export function isSubscriptionIdOrName(value) { return typeof value === "string" && value.length > 0; } export function isTenantId(value) { return uuidValidate(value); } export function hasNameAndLocation(resource) { return hasName(resource) && hasLocation(resource); } export function hasName(resource) { // eslint-disable-next-line @typescript-eslint/no-explicit-any return resource != null && typeof resource.name === "string"; } export function hasLocation(resource) { // eslint-disable-next-line @typescript-eslint/no-explicit-any return resource != null && typeof resource.location === "string"; } export function isResourceId(resourceId) { return resourceId != null && typeof resourceId === "string" && /\/subscriptions\/([^/]+)\//i.test(resourceId); } export function extractSubscriptionFromId(resourceId) { if (!resourceId) { return null; } const match = resourceId.match(/\/subscriptions\/([^/]+)\//i); return (match && match[1]) ?? null; } export function constructId(subscriptionId, resourceGroupName, resourceType, ...names) { let result = ""; if (subscriptionId != null) { result += `/subscriptions/${subscriptionId}`; } if (resourceGroupName != null) { result += `/resourceGroups/${resourceGroupName}`; } if (resourceType != null) { result += `/provider/${resourceType}`; } if (names && names.length > 0) { for (const name of names) { result += `/${name}`; } } return result; } export function isScope(value) { return typeof value === "string" && /^[0-9a-zA-Z-_.:/]+$/.test(value); } export function idsEquals(a, b, sort) { if (a == null) { return b == null; } if (b == null) { return false; } return isStringValueArrayEqual(a.map(e => e.id), b.map(e => e.id), { sort }); } export function locationNameOrCodeEquals(a, b) { return a.replace(/\s/g, "").localeCompare(b.replace(/\s/g, ""), undefined, { sensitivity: "base" }) === 0; } export function applyManagedServiceIdentity(target, source, context) { let appliedChanges = false; const { userAssignedIdentities, ...rest } = source; if (userAssignedIdentities == null) { if (userAssignedIdentities === null && target.userAssignedIdentities != null) { delete target.userAssignedIdentities; } } else { target.userAssignedIdentities ??= {}; if (applyObjectKeyProperties(target.userAssignedIdentities, userAssignedIdentities, (k, t, s) => { t[k] = s[k] ?? {}; }, true)) { appliedChanges = true; } } if (applySourceToTargetObject(target, rest, context)) { appliedChanges = true; } return appliedChanges; } //# sourceMappingURL=azureUtils.js.map