Azure / template-specs

MIT License
31 stars 4 forks source link

Proposal: The root of any RelativePath type Deployment should be able to be specified above the TemplateURI #52

Closed brwilkinson closed 3 years ago

brwilkinson commented 3 years ago

This is a proposal that should be relevant for the following 2 scenarios

Currently when deploying a Template that uses RelativePath you have the following 2 parameters.

This is a proposal to provide an additional "URIBase" property.

There are several ways to facilitate this capability, however, first lets set a baseline.


$StorageAccountName = 'acu1brwhaag1saglobal'
$StorageContainerName = 'project'
$StagingDirectory = 'D:\project'
$TemplateFile = 'D:\project\deploy\deploy1.json'
$StorageAccount = Get-AzStorageAccount | Where-Object { $_.StorageAccountName -eq $StorageAccountName }
$SASParams = @{
    Container  = $StorageContainerName 
    Context    = $StorageAccount.Context
    Permission = 'r'
    ExpiryTime = (Get-Date).AddHours(4)
}
$queryString = (New-AzStorageContainerSASToken @SASParams).Substring(1)
$TemplateArgs = @{ }
$TemplateArgs.Add('queryString', $queryString)
Write-Warning -Message "Using queryString: [$queryString]"

$TemplateURIBase = $StorageAccount.Context.BlobEndPoint + $StorageContainerName
Write-Warning -Message "Using template base: [$TemplateURIBase]"

$TemplateFile = Get-Item -Path $TemplateFile | ForEach-Object FullName
$TemplateFile = $TemplateFile -replace '\\', '/'
$TemplateURI = $TemplateFile -replace ($StagingDirectory -replace '\\', '/'), ''
Write-Warning -Message "Using template file: [$TemplateURI]"

$TemplateURI = $TemplateURIBase + $TemplateURI
$TemplateArgs.Add('TemplateURI', $TemplateURI)
Write-Warning -Message "Using template file: [$TemplateURI]"

WARNING: Using queryString: [sv=2019-07-07&sr=c&sig=FX9szdADBtNHBujUxRU6OsDyIw4OkAVhBIsrZ7MRk%3D&se=2021-03-31T11%3A17%3A32Z&sp=r]
WARNING: Using template base: [https://acu1brwhaag1saglobal.blob.core.windows.net/project]
WARNING: Using template file: [/deploy/deploy1.json]
WARNING: Using template uri: [https://acu1brwhaag1saglobal.blob.core.windows.net/project/deploy/deploy1.json]

Now that I have the above information I can deploy from storage.

note I left off uploading the blob file to storage, however assume that occured.

# Template args contains both querystring and also TemplateURI

$TemplateArgs

Name                           Value
----                           -----
TemplateURI                    https://acu1brwhaag1saglobal.blob.core.windows.net/project/deploy/deploy1.json
queryString                    sv=2019-07-07&sr=c&sig=FX9szdADBtNHBujUxRU6OsDyIw4OkAVhBIsrZ7MRk%3D&se=2021-03-31T11%3A17%3A32Z&sp=r

New-AzResourceGroupDeployment -Name $DeploymentName @TemplateArgs @OptionalParameters `
                    -ResourceGroupName $ResourceGroupName `
                    -Verbose -ErrorVariable ErrorMessages

There could be several ways to facilitate this new capability.

  1. querystring now becomes a combination of the storageURI root & the queryString
$TemplateURIBase = $StorageAccount.Context.BlobEndPoint + $StorageContainerName
Write-Warning -Message "Using template base: [$TemplateURIBase]"
$queryString = New-AzStorageContainerSASToken @SASParams
$queryString = $TemplateURIBase + $queryString
$TemplateArgs = @{ }
$TemplateArgs.Add('queryString', $queryString)
Write-Warning -Message "Using queryString: [$queryString]"

WARNING: Using template base: [https://acu1brwhaag1saglobal.blob.core.windows.net/project]
WARNING: Using queryString: [https://acu1brwhaag1saglobal.blob.core.windows.net/project?sv=2019-07-07&sr=c&sig=FX9szdADBtNHBujUxRU6OsDyIw4OkAVhBIsrZ7MRk%3D&se=2021-03-31T11%3A17%3A32Z&sp=r]

# The TemplateURI File is now actually a relativePath
$StagingDirectory = 'D:\project\'
$TemplateFile = Get-Item -Path $TemplateFile | ForEach-Object FullName
$TemplateFile = $TemplateFile -replace '\\', '/'
$TemplateURI = $TemplateFile -replace ($StagingDirectory -replace '\\', '/'), ''
Write-Warning -Message "Using template file: [$TemplateURI]"
WARNING: Using template file: [deploy/deploy1.json]
  1. There could be new parameters added that basically do a similar thing

However in the end they facilitate being able to define a project root that is lower than a specific template file, that may not exist in the root of any project. This also facilitates references to other side by side template files in storage or TemplateSpec reference paths.

brwilkinson commented 3 years ago

Closing due to age.