Skip to content

Commit

Permalink
Merge pull request #3 from qbeyond/hotfix/resource-povider-call
Browse files Browse the repository at this point in the history
Hotfix/resource povider call
  • Loading branch information
qby-chhol authored Jan 12, 2024
2 parents a255d40 + e9fd4e9 commit 0c3bf6e
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 8 deletions.
68 changes: 62 additions & 6 deletions AzFunction/SubMover/run.ps1
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<#
.SYNOPSIS
Moves VSE Subscriptions from Management Group "New" to "Sandbox"
Moves VSE Subscriptions from a source Management Group to another Management Group (target)
.DESCRIPTION
This script moves in management group "New" created VSE Subscriptions to "Sandbox" every 5 minutes.
This script moves in the source Management Group created VSE Subscriptions to the target Management Group every 5 minutes.
This is done via checking for the Quota Id, which correlates with the Offer Id for VSE Subscriptions.
.EXAMPLE
#>
Expand All @@ -12,25 +12,81 @@ param($Timer)
#region Global variables
$azQuotaID = 'MSDN_2014-09-01'
$sourceManagementGroupName = $env:source_management_group_name
$targetManagementGroup = $env:target_management_group_name
$targetManagementGroup = $env:target_management_group_name
$ErrorActionPreference = "Stop"
#endregion

#region functions
function Get-QAzManagementGroupSubscription {
param (
[parameter(Mandatory)]
[string] $GroupName
)

$apiPath = "/providers/Microsoft.Management/managementGroups/$GroupName/subscriptions?api-version=2021-04-01"
$content = Invoke-QAzRestMethod -Method GET -Path $apiPath

return $content.value
}

function New-QAzManagementGroupSubscription {
param (
[parameter(Mandatory)]
[string] $GroupId,
[parameter(Mandatory)]
[string] $subscriptionId
)

$apiPath = "/providers/Microsoft.Management/managementGroups/$GroupId/subscriptions/$($subscriptionId)?api-version=2021-04-01"
$content = Invoke-QAzRestMethod -Method PUT -Path $apiPath

return $content
}

function Invoke-QAzRestMethod {
param (
[parameter(Mandatory)]
[string] $Method,
[parameter(Mandatory)]
[string] $Path
)

$apiResponse = Invoke-AzRestMethod -Method $Method -Path $Path
$content = ConvertFrom-Json $apiResponse.Content

switch ($apiResponse.StatusCode) {
200 {
return $content
}
Default {
$message = "Failed to $Method $Path : $($result.StatusCode)"
if ($content.error.message) {
$message = $content.error.message
}
$exception = [Microsoft.Rest.Azure.CloudException]::new($message)
$cloudError = [Microsoft.Rest.Azure.CloudError]::new()
$cloudError.Code = $content.error.code
$cloudError.Message = $content.error.message
$exception.Body = $cloudError
throw $exception
}
}
}
#endregion

#region move subscriptions matching the Quota ID from source management group to target management group
$mgmtSubs = Get-AzManagementGroupSubscription -GroupName $sourceManagementGroupName
$mgmtSubs = Get-QAzManagementGroupSubscription -GroupName $sourceManagementGroupName
foreach ($subscription in $mgmtSubs) {
try {
$subscriptionID = $subscription.Id -replace '.*/' # Retrieve subscription ID (everything behind last '/')
$subscriptionObj = Get-AzSubscription -SubscriptionId $subscriptionID
$subscriptionPolicies = $subscriptionObj.SubscriptionPolicies
if ($subscriptionPolicies.QuotaId -EQ $azQuotaID) {
New-AzManagementGroupSubscription -GroupId $targetManagementGroup -SubscriptionId $subscriptionID
New-QAzManagementGroupSubscription -GroupId $targetManagementGroup -SubscriptionId $subscriptionID
}
}
catch {
Write-Error $_ -ErrorAction Continue
}

}
#endregion
12 changes: 10 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,18 @@ and this module adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.

### Added

- Feature

### Changed

### Removed

### Fixed

## [1.0.1] - 2024-01-12

### Fixed

- implemented workaround for when Microsoft.Management is not registered

## [1.0.0] - 2023-12-13

initial release

0 comments on commit 0c3bf6e

Please sign in to comment.