Skip to content

Add ResourceGroup to DefaultParameterValues #4911

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Nov 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if there is no context (the user hasn't logged in yet)? Wnat to make sure that we are not pushing errors into the Error stream.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a null check

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 "@()"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we return a string representing the code for an array?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, when put into the template it will be interpreted as an array.

}
}
}

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