Skip to content

Commit

Permalink
Merge pull request Azure#4911 from maddieclayton/DefaultScript
Browse files Browse the repository at this point in the history
Add ResourceGroup to DefaultParameterValues
  • Loading branch information
maddieclayton authored Nov 15, 2017
2 parents 5193770 + 0fa53f9 commit 9b78152
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 10 deletions.
14 changes: 13 additions & 1 deletion tools/AzureRM.Example.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,16 @@
$PSDefaultParameterValues.Clear()
Set-StrictMode -Version Latest

%IMPORTED-DEPENDENCIES%
%IMPORTED-DEPENDENCIES%

$FilteredCommands = %COMMANDS%

$FilteredCommands | ForEach-Object {
$global:PSDefaultParameterValues.Add($_,
{
$context = Get-AzureRmContext
if (($context -ne $null) -and $context.ExtendedProperties.ContainsKey("Default Resource Group")) {
$context.ExtendedProperties["Default Resource Group"]
}
})
}
96 changes: 87 additions & 9 deletions tools/UpdateModules.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ function Create-ModulePsm1
[CmdletBinding()]
param(
[string]$ModulePath,
[string]$TemplatePath
[string]$TemplatePath,
[bool]$AddDefaultParameters
)

PROCESS
Expand Down Expand Up @@ -65,12 +66,89 @@ function Create-ModulePsm1
$template = $template -replace "%MODULE-NAME%", $file.BaseName
$template = $template -replace "%DATE%", [string](Get-Date)
$template = $template -replace "%IMPORTED-DEPENDENCIES%", $importedModules

$contructedCommands = Find-DefaultResourceGroupCmdlets -AddDefaultParameters $AddDefaultParameters -ModuleMetadata $ModuleMetadata -ModulePath $ModulePath
$template = $template -replace "%COMMANDS%", $contructedCommands

Write-Host "Writing psm1 manifest to $templateOutputPath"
$template | Out-File -FilePath $templateOutputPath -Force
$file = Get-Item -Path $templateOutputPath
}
}

function Find-DefaultResourceGroupCmdlets
{
[CmdletBinding()]
param(
[bool]$AddDefaultParameters,
[Hashtable]$ModuleMetadata,
[string]$ModulePath
)
PROCESS
{
if ($AddDefaultParameters)
{
$nestedModules = $ModuleMetadata.NestedModules
$AllCmdlets = @()
$nestedModules | ForEach-Object {
$dllPath = Join-Path -Path $ModulePath -ChildPath $_
$Assembly = [Reflection.Assembly]::LoadFrom($dllPath)
$dllCmdlets = $Assembly.GetTypes() | Where-Object {$_.CustomAttributes.AttributeType.Name -contains "CmdletAttribute"}
$AllCmdlets += $dllCmdlets
}

$FilteredCommands = $AllCmdlets | Where-Object {Test-CmdletRequiredParameter -Cmdlet $_ -Parameter "ResourceGroupName"}

if ($FilteredCommands.Length -eq 0) {
$contructedCommands = "@()"
}
else {
$contructedCommands = "@("
$FilteredCommands | ForEach-Object {
$contructedCommands += "'" + $_.GetCustomAttributes("System.Management.Automation.CmdletAttribute").VerbName + "-" + $_.GetCustomAttributes("System.Management.Automation.CmdletAttribute").NounName + ":ResourceGroupName" + "',"
}
$contructedCommands = $contructedCommands -replace ".$",")"
}

return $contructedCommands
}

else {
return "@()"
}
}
}

function Test-CmdletRequiredParameter
{
[CmdletBinding()]
param(
[Object]$Cmdlet,
[string]$Parameter
)

PROCESS
{
$rgParameter = $Cmdlet.GetProperties() | Where-Object {$_.Name -eq $Parameter}
if ($rgParameter -ne $null) {
$parameterAttributes = $rgParameter.CustomAttributes | Where-Object {$_.AttributeType.Name -eq "ParameterAttribute"}
$isMandatory = $true
$parameterAttributes | ForEach-Object {
$hasParameterSet = $_.NamedArguments | Where-Object {$_.MemberName -eq "ParameterSetName"}
$MandatoryParam = $_.NamedArguments | Where-Object {$_.MemberName -eq "Mandatory"}
if (($hasParameterSet -ne $null) -or (!$MandatoryParam.TypedValue.Value)) {
$isMandatory = $false
}
}
if ($isMandatory) {
return $true
}
}

return $false
}
}

function Create-MinimumVersionEntry
{
[CmdletBinding()]
Expand Down Expand Up @@ -122,22 +200,22 @@ $templateLocation = "$PSScriptRoot\AzureRM.Example.psm1"
if (($scope -eq 'All') -or $publishToLocal ) {
# If we publish 'All' or to local folder, publish AzureRM.Profile first, becasue it is the common dependency
Write-Host "Updating profile module"
Create-ModulePsm1 -ModulePath "$resourceManagerRootFolder\AzureRM.Profile" -TemplatePath $templateLocation
Create-ModulePsm1 -ModulePath "$resourceManagerRootFolder\AzureRM.Profile" -TemplatePath $templateLocation $true
Write-Host "Updated profile module"
}

if (($scope -eq 'All') -or ($scope -eq 'AzureStorage')) {
$modulePath = "$packageFolder\$buildConfig\Storage\Azure.Storage"
# Publish AzureStorage module
Write-Host "Updating AzureStorage module from $modulePath"
Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation
Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation $false
}

if (($scope -eq 'All') -or ($scope -eq 'ServiceManagement')) {
$modulePath = "$packageFolder\$buildConfig\ServiceManagement\Azure"
# Publish Azure module
Write-Host "Updating ServiceManagement(aka Azure) module from $modulePath"
Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation
Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation $false
}

$resourceManagerModules = Get-ChildItem -Path $resourceManagerRootFolder -Directory
Expand All @@ -148,15 +226,15 @@ if ($scope -eq 'All') {
if (($module.Name -ne "AzureRM.Profile") -and ($module.Name -ne "Azure.Storage")) {
$modulePath = $module.FullName
Write-Host "Updating $module module from $modulePath"
Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation
Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation $true
Write-Host "Updated $module module"
}
}
} elseif ($scope -ne 'AzureRM') {
$modulePath = Join-Path $resourceManagerRootFolder "AzureRM.$scope"
if (Test-Path $modulePath) {
Write-Host "Updating $scope module from $modulePath"
Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation
Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation $false
Write-Host "Updated $scope module"
} else {
Write-Error "Can not find module with name $scope to publish"
Expand All @@ -169,17 +247,17 @@ if (($scope -eq 'All') -or ($scope -eq 'AzureRM')) {
{
$modulePath = "$PSScriptRoot\..\src\StackAdmin\AzureRM"
Write-Host "Updating AzureRM module from $modulePath"
Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation
Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation $false
Write-Host "Updated AzureRM module"
$modulePath = "$PSScriptRoot\..\src\StackAdmin\AzureStack"
Write-Host "Updating AzureRM module from $modulePath"
Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation
Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation $false
Write-Host "Updated AzureStack module"
}
else {
$modulePath = "$PSScriptRoot\AzureRM"
Write-Host "Updating AzureRM module from $modulePath"
Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation
Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation $false
Write-Host "Updated Azure module"
}
}
Expand Down

0 comments on commit 9b78152

Please sign in to comment.