This is an working class library of static functions which can be used using the Build Tools.
Over the course of the next 12 months I will be adding functions to this page so bookmark it and come back regularly for new cool features and updates.
GitHub Repository: https://github.com/SimonSparksUK/Orchestrator
Filename: Utilities.ts
import { Logger } from "com.vmware.pscoe.library.ts.logging/Logger";
import { ConfigElementAccessor } from "com.vmware.pscoe.library.ts.util/ConfigElementAccessor";
import { ResourceElementAccessor } from "com.vmware.pscoe.library.ts.util/ResourceElementAccessor";
export class Utilities {
public static logExecutionContext(objLogger: Logger): void {
let strActionName: string = arguments.callee.name.substring(6, arguments.callee.name.length);
// let objWorkflowItemInfo: WorkflowItemInfo = System.currentWorkflowItem();
// System.setLogMarker("SCRIPT: " + objWorkflowItemInfo.getDisplayName());
let objExecutionContext: ExecutionContext = System.getContext();
let arrParameterName: string[] = objExecutionContext.parameterNames();
arrParameterName.forEach((strParameterName: string): void => {
let anyParameterValue: any = objExecutionContext.getParameter(strParameterName);
let strParameterType: string = System.getObjectType(anyParameterValue);
if (strParameterType === "Properties") {
let objResourceProperties: Properties = anyParameterValue as Properties;
let arrKey: string[] = objResourceProperties.keys;
arrKey.forEach((strKey: string): void => {
if (strKey === "tags") {
let objResourceProperties: Properties = objExecutionContext.getParameter("tags");
let arrKey: string[] = objResourceProperties.keys;
arrKey.forEach((strKey: string): void => {
let anyValue: any = objResourceProperties.get(strKey);
let strResourcePropertyType: string = System.getObjectType(anyValue);
objLogger.info(`Resource Properties - Tags: ${strKey}:${strResourcePropertyType} = ${anyValue}`);
});
}
else {
let anyResourcePropertyValue: any = objResourceProperties.get(strKey);
let strResourcePropertyType: string = System.getObjectType(anyResourcePropertyValue);
objLogger.info(`Resource Properties: ${strKey}:${strResourcePropertyType} = ${objResourceProperties.get(strKey)}`);
}
});
}
else if (strParameterType === "Array") {
let arr: [] = anyParameterValue as [];
}
else {
objLogger.info(`Execution Context: Key - ${strParameterName} - Value - ${anyParameterValue as string}`);
}
});
}
public static logInputPropertiesForSubscription(objLogger: Logger, inputProperties: Properties, strWorkflowName: string): void {
this.logInputProperties(objLogger, inputProperties, "Subscription", strWorkflowName);
}
public static logInputPropertiesForWorkflow(objLogger: Logger, inputProperties: Properties, strWorkflowName: string): void {
this.logInputProperties(objLogger, inputProperties, "Resource Action", strWorkflowName);
}
private static logInputProperties(objLogger: Logger, inputProperties: Properties, strLogTypeName: string, strObjectName: string): void {
let arrInputPropertyKey: string[] = inputProperties.keys;
arrInputPropertyKey.forEach((strInputPropertyKey: string): void => {
if (strInputPropertyKey === "customProperties") {
let objCustomProperties: Properties = inputProperties.get("customProperties");
let arrCustomPropertyKey: string[] = objCustomProperties.keys;
arrCustomPropertyKey.forEach((strCustomPropertyKey: string): void => {
let objCustomPropertyValue: string = objCustomProperties.get<string>(strCustomPropertyKey);
let strType: string = typeof objCustomPropertyValue;
objLogger.info(`${strLogTypeName} - ${strObjectName} - Custom Property - ${strCustomPropertyKey}:${strType} = '${objCustomPropertyValue}'.`);
});
}
else {
let objInputPropertyValue: string = inputProperties.get<string>(strInputPropertyKey);
let strType: string = typeof objInputPropertyValue;
objLogger.info(`${strLogTypeName} - ${strObjectName} - Input Property - ${strInputPropertyKey}:${strType} = '${objInputPropertyValue}'.`);
}
});
}
public static validateFunctionInputs(inputProperties: Properties, strResourceActionType: string, strResourceActionName: string, objWorkflowToken: WorkflowToken): void {
let strWorkflowName: string = `${strResourceActionType} - ${strResourceActionName}`;
let objLogger: Logger = Logger.getLogger(`${strWorkflowName} - validateFunctionInputs`);
let objProperties: Properties = objWorkflowToken.getInputParameters();
let objWorkflow: Workflow = objWorkflowToken.currentWorkflow;
let arrParameter: Parameter[] = objWorkflow.inParameters;
arrParameter.forEach((objParameter: Parameter): void => {
objLogger.info(`Input Parameter: ${objParameter.name}:${objParameter.type} ( ${objParameter.description} ).`);
let objValue: any = objProperties.get(objParameter.name);
let strType: string = System.getObjectType(objValue);
strType = strType.toLowerCase();
if (strType === "string" && objValue === null) {
throw new Error_RequiredParameter_Null(objParameter.name);
} else if (strType === "string" && objValue === "") {
throw new Error_RequiredParameter_EmptyString(objParameter.name);
} else if (strType === "number" && objValue === null) {
throw new Error_RequiredParameter_Null(objParameter.name);
} else if (strType === "number" && Number.isNaN(objValue)) {
throw new Error_RequiredParameter_NotANumber(objParameter.name);
} else if (strType === "boolean" && objValue === null) {
throw new Error_RequiredParameter_Null(objParameter.name);
} else if (strType === "boolean" && (objValue !== true || objValue !== false)) {
throw new Error_RequiredParameter_NotTrueOrFalse(objParameter.name);
} else if (strType === "date" && objValue === null) {
throw new Error_RequiredParameter_Null(objParameter.name);
} else if (strType === "date" && !(objValue instanceof Date)) {
throw new Error_RequiredParameter_NotADate(objParameter.name);
} else if (strType === "array" && objValue === null) {
throw new Error_RequiredParameter_Null(objParameter.name);
} else if (strType === "array" && objValue.length === 0) {
throw new Error_RequiredParameter_EmptyArray(objParameter.name);
} else if (strType === "array" && Array.isArray(objValue) === false) {
throw new Error_RequiredParameter_NotAnArray(objParameter.name);
} else if (strType === "object" && objValue === null) {
throw new Error_RequiredParameter_Null(objParameter.name);
} else if (strType === "object" && objValue instanceof eval(strType)) {
throw new Error_RequiredParameter_Missing(objParameter.name);
}
});
}
public static validateWorkflowInputs(strResourceActionType: string, strResourceActionName: string, objWorkflowToken: WorkflowToken): void {
let strWorkflowName: string = `${strResourceActionType} - ${strResourceActionName}`;
let objLogger: Logger = Logger.getLogger(`${strWorkflowName} - validateFunctionInputs`);
let objProperties: Properties = objWorkflowToken.getInputParameters();
let objWorkflow: Workflow = objWorkflowToken.currentWorkflow;
let arrParameter: Parameter[] = objWorkflow.inParameters;
arrParameter.forEach((objParameter: Parameter): void => {
objLogger.info(`Input Parameter: ${objParameter.name}:${objParameter.type} ( ${objParameter.description} ).`);
let objValue: any = objProperties.get(objParameter.name);
let strType: string = System.getObjectType(objValue);
strType = strType.toLowerCase();
if (strType === "string" && objValue === null) {
throw new Error_RequiredParameter_Null(objParameter.name);
} else if (strType === "string" && objValue === "") {
throw new Error_RequiredParameter_EmptyString(objParameter.name);
} else if (strType === "number" && objValue === null) {
throw new Error_RequiredParameter_Null(objParameter.name);
} else if (strType === "number" && Number.isNaN(objValue)) {
throw new Error_RequiredParameter_NotANumber(objParameter.name);
} else if (strType === "boolean" && objValue === null) {
throw new Error_RequiredParameter_Null(objParameter.name);
} else if (strType === "boolean" && (objValue !== true || objValue !== false)) {
throw new Error_RequiredParameter_NotTrueOrFalse(objParameter.name);
} else if (strType === "date" && objValue === null) {
throw new Error_RequiredParameter_Null(objParameter.name);
} else if (strType === "date" && !(objValue instanceof Date)) {
throw new Error_RequiredParameter_NotADate(objParameter.name);
} else if (strType === "array" && objValue === null) {
throw new Error_RequiredParameter_Null(objParameter.name);
} else if (strType === "array" && objValue.length === 0) {
throw new Error_RequiredParameter_EmptyArray(objParameter.name);
} else if (strType === "array" && Array.isArray(objValue) === false) {
throw new Error_RequiredParameter_NotAnArray(objParameter.name);
} else if (strType === "object" && objValue === null) {
throw new Error_RequiredParameter_Null(objParameter.name);
} else if (strType === "object" && objValue instanceof eval(strType)) {
throw new Error_RequiredParameter_Missing(objParameter.name);
}
});
}
}Discover more from Cloud Build Tools
Subscribe to get the latest posts sent to your email.
