import * as azure from "@pulumi/azure" ; import * as pulumi from "@pulumi/pulumi" ; import { RunError } from "@pulumi/pulumi/errors" ; import * as crypto from "crypto" ; import * as config from "./config" ; /**
 * Helper to create a name for resources with a name that should be unique to this stack.
 */ export function createNameWithStackInfo ( requiredInfo , maxLength , delim ) { if ( requiredInfo . length > maxLength ) { throw new RunError ( `'${ requiredInfo }' cannot be longer then ${ maxLength } characters.` ) ; } if ( requiredInfo . length === 0 ) { throw new RunError ( `[requiredInfo] must be non-empty` ) ; } const stackName = pulumi . getStack ( ) ; if ( requiredInfo . length >= maxLength - delim . length ) { return requiredInfo ; } const suffix = delim + requiredInfo ; const result = stackName . substr ( 0 , maxLength - suffix . length ) + suffix ; return result ; } class InfrastructureResource extends pulumi . ComponentResource { constructor ( ) { super ( "cloud:global:infrastructure" , "global-infrastructure" ) ; this . registerOutputs ( ) ; } } let globalInfrastructureResource ; /**
 * Get's the resource that any global infrastructure resource for this stack can use as a parent.
 */ export function getGlobalInfrastructureResource ( ) { if ( ! globalInfrastructureResource ) { globalInfrastructureResource = new InfrastructureResource ( ) ; } return globalInfrastructureResource ; } export const location = config . location ; const azureConfig = new pulumi . Config ( "cloud-azure" ) ; /**
 * The Azure Resource Group to use for all resources if a specific one is not specified. To use an
 * existing Resource Group provide the [cloud-azure:resourceGroupName] config value. Otherwise, a
 * new group will be created.
 */ export const globalResourceGroup = getGlobalResourceGroup ( ) ; export const globalResourceGroupName = globalResourceGroup . apply ( g => g . name ) ; function getGlobalResourceGroup ( ) { const resourceGroupPromise = getOrCreateGlobalResourceGroup ( ) ; return pulumi . output ( resourceGroupPromise ) ; async function getOrCreateGlobalResourceGroup ( ) { const resourceGroupName = azureConfig . get ( "resourceGroupName" ) ; if ( resourceGroupName ) { const result = await azure . core . getResourceGroup ( { name : resourceGroupName , } ) ; return azure . core . ResourceGroup . get ( "global" , result . id ) ; } return new azure . core . ResourceGroup ( "global" , { name : createNameWithStackInfo ( "global-" + sha1hash ( pulumi . getStack ( ) ) , 90 , "-" ) , location : location , } , { parent : getGlobalInfrastructureResource ( ) } ) ; } } let globalStorageAccount ; /**
 * The Azure Storage Account to use for all resources that need to store data if not specific
 * account is specified. To use an existing Storage Account provide the
 * [cloud-azure:storageAccountId] config value. Otherwise, a new account will be created.
 */ export function getGlobalStorageAccount ( ) { if ( ! globalStorageAccount ) { globalStorageAccount = getOrCreateGlobalStorageAccount ( ) ; } return globalStorageAccount ; } function getOrCreateGlobalStorageAccount ( ) { const storageAccountId = azureConfig . get ( "storageAccountId" ) ; if ( storageAccountId ) { return azure . storage . Account . get ( "global" , storageAccountId ) ; } const storageAccountName = makeSafeStorageAccountName ( createNameWithStackInfo ( "global" + sha1hash ( pulumi . getStack ( ) ) , 24 , "" ) ) ; return new azure . storage . Account ( "global" , { resourceGroupName : globalResourceGroupName , location : location , name : storageAccountName , accountKind : "StorageV2" , accountTier : "Standard" , accountReplicationType : "LRS" , } , { parent : getGlobalInfrastructureResource ( ) } ) ; } let globalStorageContainer ; function getGlobalStorageContainer ( ) { if ( ! globalStorageContainer ) { globalStorageContainer = new azure . storage . Container ( "global" , { storageAccountName : getGlobalStorageAccount ( ) . name , containerAccessType : "private" , } , { parent : getGlobalInfrastructureResource ( ) } ) ; } return globalStorageContainer ; } let globalAppServicePlan ; let globalFunctionAppServicePlan ; export function getGlobalAppServicePlan ( ) { if ( ! globalAppServicePlan ) { globalAppServicePlan = new azure . appservice . Plan ( "global-app" , { resourceGroupName : globalResourceGroupName , location : location , kind : "App" , sku : { tier : "Standard" , size : "S1" , } , } , { parent : getGlobalInfrastructureResource ( ) } ) ; } return globalAppServicePlan ; } export function getGlobalFunctionAppServicePlan ( ) { if ( ! globalFunctionAppServicePlan ) { globalFunctionAppServicePlan = new azure . appservice . Plan ( "global-function-app" , { resourceGroupName : globalResourceGroupName , location : location , kind : "FunctionApp" , sku : { tier : "Dynamic" , size : "Y1" , } , } , { parent : getGlobalInfrastructureResource ( ) } ) ; } return globalFunctionAppServicePlan ; } function makeSafeStorageAccountName ( prefix ) { return prefix . replace ( /[^a-zA-Z0-9]/g , "" ) . toLowerCase ( ) ; } export function sha1hash ( s ) { const shasum = crypto . createHash ( "sha1" ) ; shasum . update ( s ) ; return shasum . digest ( "hex" ) . substring ( 0 , 8 ) ; } export const defaultSubscriptionArgs = { includePaths : config . functionIncludePaths , includePackages : config . functionIncludePackages , resourceGroupName : globalResourceGroupName , location : location , storageAccount : getGlobalStorageAccount ( ) , storageContainer : getGlobalStorageContainer ( ) , appServicePlanId : getGlobalFunctionAppServicePlan ( ) . id , } ;