thankful-painting-31068
01/27/2025, 3:37 PMdevBackupStackSchedulerFunction
. We're going down this path to ensure we can support ephemeral deployments within a specific environment (i.e. account) for our cloud providers (helps us avoid naming collisions).
๐งตthankful-painting-31068
01/27/2025, 3:39 PMimport { ComponentResource, ComponentResourceOptions, Inputs } from "@pulumi/pulumi";
import { currentStack } from "../../core/common/stacks";
import { getEnvPrefix } from "../../core/common/utils";
/**
* Base VDC Component Resource. All other component resources should extend this component.
*
* A component resource must register a unique type name with the base constructor.
* To reduce the potential of other type name conflicts, this name contains the package and module name, in addition to the type: <package>:<module>:<type>.
* These names are namespaced alongside non-component resources, such as aws:lambda:Function.
*
* @param type
* The type of the resource. Pattern: <pkg>:<module>:
*/
export class VdcComponentResource extends ComponentResource {
readonly stack: string
readonly env: string
constructor(type: string, name: string, args?: Inputs, opts?: ComponentResourceOptions, remote?: boolean, packageRef?: Promise<string | undefined>) {
const stack = currentStack;
const env = getEnvPrefix(stack);
const resourceName = env + name;
// Customize the name of component resources to adhere to our naming convention
super(`pkg:${type}:${resourceName}`, resourceName, args, opts, remote, packageRef);
this.stack = stack;
this.env = env;
this.registerOutputs({ stack: this.stack, env: this.env })
}
}
export class AzureComponentResource extends VdcComponentResource {
constructor(name: string, args?: Inputs, opts?: ComponentResourceOptions, remote?: boolean, packageRef?: Promise<string | undefined>) {
super('azure', name, args, opts, remote, packageRef);
}
}
And so any additional component resources created that use the azure cloud should extend AzureComponentResource
which follow our established naming convention.thankful-painting-31068
01/27/2025, 3:41 PMthis.apiManagement = new ApiManagementService(
`${envPrefix}${name}Service`,...)
this.exampleSvcBackend = new Backend(
`${envPrefix}${name}ExampleSvcBackend`, ...)
And while it works, makes it easy for engineers to make mistakes and forget formatting the name in such a way.early-advantage-63854
01/27/2025, 4:03 PMpulumi:autonaming:
mode: default
providers:
azure-native:
resources:
"azure-native:resources:ResourceGroup":
pattern: "rg-${config.regionAbbreviation}-${stack}-${name}"
limited-rainbow-51650
01/27/2025, 4:43 PMthankful-painting-31068
01/27/2025, 4:55 PMthankful-painting-31068
01/27/2025, 6:04 PM/**
* Returns the prefix to use for resources based on the current stack.
*
* @param currentStack The name of the current stack.
* @returns The prefix to use for resources.
*/
function getEnvPrefix(currentStack: string): string {
return currentStack.includes("-personal")
? toTitleCase(currentStack)
.replace("something", "")
.replace("something-else", "")
.replace(/[^a-zA-Z0-9]/g, "")
.substring(0, 7)
: toTitleCase(currentStack).replace(/[^a-zA-Z0-9]/g, "");
}
Is there a way to accomplish something similar using the feature above? I see https://www.pulumi.com/docs/iac/concepts/resources/names/#custom-naming-pattern that can help with string length but what about transformations?late-airplane-27955
01/27/2025, 6:55 PMthankful-painting-31068
01/28/2025, 8:34 PMthankful-painting-31068
01/28/2025, 8:35 PMlate-airplane-27955
01/28/2025, 9:23 PM