Skip to content

Commit

Permalink
Add support for updating Az modules with parameter AzureModuleClass (#6)
Browse files Browse the repository at this point in the history
* require automation module name param in Update-ProfileAndAutomationVersionToLatest

* introduce AzureModuleClass parameter to specify which module class to use for this script

* stick to existing param syntax

* support full use of Az module to perform updates based on param AzureModuleClass

* doco and param adjustments as per PR comments

* avoid importing conflicting modules

* locale independant documentation url

* earlier filtering of conflicting modules

* restrict Az to updating only Az modules

* repair tests for AzureRM module class
  • Loading branch information
ayoung012 authored and AnatoliB committed Jun 13, 2019
1 parent 7d56807 commit f9f4e2f
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 73 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ Import this runbook into your Automation account, and [start](https://docs.micro
run when the **Update Azure Modules** button is pushed or when this runbook is invoked directly
via ARM API for this Automation account. If this is not what you want, specify a different name
when importing this runbook.
* Only **Azure** and **AzureRM.\*** modules are currently supported. The new [Azure PowerShell Az modules](https://docs.microsoft.com/powershell/azure/new-azureps-module-az) are not supported yet.
Avoid starting this runbook on Automation accounts that contain Az modules.
* **Azure** and **AzureRM.\*** modules are currently supported by default.
* The new [Azure PowerShell Az modules](https://docs.microsoft.com/powershell/azure/new-azureps-module-az)
are also supported. You **must** supply the `AzureModuleClass` runbook parameter with `Az` if
your runbooks use only Az modules to avoid conflicts. More information can be found in the
[Microsoft Docs](https://docs.microsoft.com/azure/automation/az-modules) and
issue [#5](https://github.com/microsoft/AzureAutomation-Account-Modules-Update/issues/5).
Avoid starting this runbook on Automation accounts that contain both AzureRM and Az modules.
* Before starting this runbook, make sure your Automation account has an [Azure Run As account credential](https://docs.microsoft.com/azure/automation/manage-runas-account) created.
* You can use this code as a regular PowerShell script instead of a runbook: just login to Azure
using the [Connect-AzureRmAccount](https://docs.microsoft.com/powershell/module/azurerm.profile/connect-azurermaccount)
Expand Down
64 changes: 32 additions & 32 deletions Tests/Update-AutomationAzureModulesForAccount.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Describe 'Update-AutomationAzureModulesForAccount runbook' {
Context 'No overridden module versions' {
Mock Get-AzureRmAutomationModule {
@{
Name = 'FakeAzureModule'
Name = 'AzureRM.FakeAzureModule'
Version = '1.0.0'
ProvisioningState = 'Succeeded'
}
Expand Down Expand Up @@ -154,10 +154,10 @@ Describe 'Update-AutomationAzureModulesForAccount runbook' {
} -Verifiable

Mock Invoke-RestMethod -ParameterFilter {
$Uri -match '%27FakeAzureModule%27'
$Uri -match '%27AzureRM.FakeAzureModule%27'
} -MockWith {
$Method | Should be 'Get' > $null
Assert-CorrectSearchUri -Uri $Uri -ModuleName FakeAzureModule
Assert-CorrectSearchUri -Uri $Uri -ModuleName AzureRM.FakeAzureModule

@{
id = 'fake FakeAzureModule search result id'
Expand All @@ -181,21 +181,21 @@ Describe 'Update-AutomationAzureModulesForAccount runbook' {
} -Verifiable

Mock Invoke-WebRequest -ParameterFilter {
$Uri -match 'https://www.powershellgallery.com/api/v2/package/FakeAzureModule'
$Uri -match 'https://www.powershellgallery.com/api/v2/package/AzureRM.FakeAzureModule'
} -MockWith {
$Uri | Should be 'https://www.powershellgallery.com/api/v2/package/FakeAzureModule' > $null
$Uri | Should be 'https://www.powershellgallery.com/api/v2/package/AzureRM.FakeAzureModule' > $null

@{
Headers = @{
Location = 'Fake/FakeAzureModule/Content/Location.nupkg'
Location = 'Fake/AzureRM.FakeAzureModule/Content/Location.nupkg'
}
}
} -Verifiable

Mock New-AzureRmAutomationModule -ParameterFilter {
$Name -eq 'FakeAzureModule'
$Name -eq 'AzureRM.FakeAzureModule'
} -MockWith {
$ContentLink | Should be 'Fake/FakeAzureModule/Content/Location.nupkg' > $null
$ContentLink | Should be 'Fake/AzureRM.FakeAzureModule/Content/Location.nupkg' > $null
} -Verifiable

Invoke-Update-AutomationAzureModulesForAccount -OptionalParameters @{
Expand All @@ -207,7 +207,7 @@ Describe 'Update-AutomationAzureModulesForAccount runbook' {
}

It 'Updates fake Azure module' {
Assert-MockCalled New-AzureRmAutomationModule -ParameterFilter { $Name -eq 'FakeAzureModule' } -Times 1 -Exactly
Assert-MockCalled New-AzureRmAutomationModule -ParameterFilter { $Name -eq 'AzureRM.FakeAzureModule' } -Times 1 -Exactly
}

Assert-VerifiableMock
Expand All @@ -216,7 +216,7 @@ Describe 'Update-AutomationAzureModulesForAccount runbook' {
Context 'With overridden module versions' {
Mock Get-AzureRmAutomationModule {
@{
Name = 'FakeAzureModule'
Name = 'AzureRM.FakeAzureModule'
Version = '1.0.0'
ProvisioningState = 'Succeeded'
}
Expand Down Expand Up @@ -295,10 +295,10 @@ Describe 'Update-AutomationAzureModulesForAccount runbook' {
} -Verifiable

Mock Invoke-RestMethod -ParameterFilter {
$Uri -match '%27FakeAzureModule%27'
$Uri -match '%27AzureRM.FakeAzureModule%27'
} -MockWith {
$Method | Should be 'Get' > $null
Assert-CorrectSearchUri -Uri $Uri -ModuleName FakeAzureModule
Assert-CorrectSearchUri -Uri $Uri -ModuleName AzureRM.FakeAzureModule

@{
id = 'fake FakeAzureModule search result id'
Expand All @@ -322,21 +322,21 @@ Describe 'Update-AutomationAzureModulesForAccount runbook' {
} -Verifiable

Mock Invoke-WebRequest -ParameterFilter {
$Uri -match 'https://www.powershellgallery.com/api/v2/package/FakeAzureModule'
$Uri -match 'https://www.powershellgallery.com/api/v2/package/AzureRM.FakeAzureModule'
} -MockWith {
$Uri | Should be 'https://www.powershellgallery.com/api/v2/package/FakeAzureModule' > $null
$Uri | Should be 'https://www.powershellgallery.com/api/v2/package/AzureRM.FakeAzureModule' > $null

@{
Headers = @{
Location = 'Fake/FakeAzureModule/Content/Location.nupkg'
Location = 'Fake/AzureRM.FakeAzureModule/Content/Location.nupkg'
}
}
} -Verifiable

Mock New-AzureRmAutomationModule -ParameterFilter {
$Name -eq 'FakeAzureModule'
$Name -eq 'AzureRM.FakeAzureModule'
} -MockWith {
$ContentLink | Should be 'Fake/FakeAzureModule/Content/Location.nupkg' > $null
$ContentLink | Should be 'Fake/AzureRM.FakeAzureModule/Content/Location.nupkg' > $null
} -Verifiable

Invoke-Update-AutomationAzureModulesForAccount -OptionalParameters @{
Expand All @@ -350,7 +350,7 @@ Describe 'Update-AutomationAzureModulesForAccount runbook' {
}

It 'Updates fake Azure module' {
Assert-MockCalled New-AzureRmAutomationModule -ParameterFilter { $Name -eq 'FakeAzureModule' } -Times 1 -Exactly
Assert-MockCalled New-AzureRmAutomationModule -ParameterFilter { $Name -eq 'AzureRM.FakeAzureModule' } -Times 1 -Exactly
}

Assert-VerifiableMock
Expand Down Expand Up @@ -407,31 +407,31 @@ Describe 'Update-AutomationAzureModulesForAccount runbook' {

Mock Get-AzureRmAutomationModule {
@{
Name = 'FakeAzureModule'
Name = 'AzureRM.FakeAzureModule'
Version = '1.0.0'
ProvisioningState = 'Succeeded'
}
} -Verifiable

Mock Invoke-RestMethod -ParameterFilter {
$Uri -match '%27FakeAzureModule%27'
$Uri -match '%27AzureRM.FakeAzureModule%27'
} -MockWith {
$Method | Should be 'Get' > $null
Assert-CorrectSearchUri -Uri $Uri -ModuleName FakeAzureModule -Filter "Version%20eq%20'2.0.0'"
Assert-CorrectSearchUri -Uri $Uri -ModuleName AzureRM.FakeAzureModule -Filter "Version%20eq%20'2.0.0'"

@{
id = 'fake FakeAzureModule search result id 1'
title = @{ InnerText = 'FakeAzureModule.Different' }
title = @{ InnerText = 'AzureRM.FakeAzureModule.Different' }
}

@{
id = 'fake FakeAzureModule search result id 2'
title = @{ InnerText = 'FakeAzureModule' }
title = @{ InnerText = 'AzureRM.FakeAzureModule' }
}

@{
id = 'fake FakeAzureModule search result id 3'
title = @{ InnerText = 'FakeAzureModule.AnotherOne' }
title = @{ InnerText = 'AzureRM.FakeAzureModule.AnotherOne' }
}
} -Verifiable

Expand All @@ -452,33 +452,33 @@ Describe 'Update-AutomationAzureModulesForAccount runbook' {
} -Verifiable

Mock Invoke-WebRequest -ParameterFilter {
$Uri -match 'https://www.powershellgallery.com/api/v2/package/FakeAzureModule'
$Uri -match 'https://www.powershellgallery.com/api/v2/package/AzureRM.FakeAzureModule'
} -MockWith {
$Uri | Should be 'https://www.powershellgallery.com/api/v2/package/FakeAzureModule/2.0.0' > $null
$Uri | Should be 'https://www.powershellgallery.com/api/v2/package/AzureRM.FakeAzureModule/2.0.0' > $null

@{
Headers = @{
Location = 'Fake/FakeAzureModule/Content/Location.nupkg'
Location = 'Fake/AzureRM.FakeAzureModule/Content/Location.nupkg'
}
}
} -Verifiable

Mock New-AzureRmAutomationModule -ParameterFilter {
$Name -eq 'FakeAzureModule'
$Name -eq 'AzureRM.FakeAzureModule'
} -MockWith {
$ContentLink | Should be 'Fake/FakeAzureModule/Content/Location.nupkg' > $null
$ContentLink | Should be 'Fake/AzureRM.FakeAzureModule/Content/Location.nupkg' > $null
} -Verifiable

Invoke-Update-AutomationAzureModulesForAccount -OptionalParameters @{
ModuleVersionOverrides = "{
'FakeAzureModule' : '2.0.0'
'AzureRM.FakeAzureModule' : '2.0.0'
}"
}

It 'Updates fake Azure module' {
Assert-MockCalled New-AzureRmAutomationModule -ParameterFilter { $Name -eq 'FakeAzureModule' } -Times 1 -Exactly
Assert-MockCalled New-AzureRmAutomationModule -ParameterFilter { $Name -eq 'AzureRM.FakeAzureModule' } -Times 1 -Exactly
}

Assert-VerifiableMock
}
}
}
Loading

0 comments on commit f9f4e2f

Please sign in to comment.