@@ -37,6 +37,15 @@ function Get-NotificationKey
37
37
return " NotificationKey-" + (getAssetName)
38
38
}
39
39
40
+ <#
41
+ . SYNOPSIS
42
+ Get Resource Group name
43
+ #>
44
+ function Get-ResourceGroupName
45
+ {
46
+ return " RG-" + (getAssetName)
47
+ }
48
+
40
49
<#
41
50
. SYNOPSIS
42
51
Test budget at subscription level
@@ -94,47 +103,58 @@ function Test-BudgetAtResourceGroupLevel
94
103
$notificationKey1 = Get-NotificationKey
95
104
$notificationKey2 = Get-NotificationKey
96
105
# This resource group is created at the scope of test subscription
97
- $resourceGroupName = ' RGBudgets '
106
+ $resourceGroupName = Get-ResourceGroupName
98
107
$startDate = Get-Date - Day 1
99
108
$endDate = ($startDate ).AddMonths(3 ).AddDays(-1 )
100
109
101
- # Create budget
102
- Write-Debug " Create a new budget $budgetName at resource group level"
103
- $budgetNew = New-AzureRmConsumptionBudget - Amount 6000 - Name $budgetName - ResourceGroupName $resourceGroupName - Category Cost - StartDate $startDate - EndDate $endDate - TimeGrain Monthly
104
- Assert-NotNull $budgetNew
105
- Assert-AreEqual 6000 $budgetNew.Amount
106
- Assert-AreEqual $budgetName $budgetNew.Name
107
- Assert-AreEqual Cost $budgetNew.Category
108
- Assert-AreEqual Monthly $budgetNew.TimeGrain
109
-
110
- # Get budget
111
- Write-Debug " Get the budget $budgetName "
112
- $budgetGet = Get-AzureRmConsumptionBudget - Name $budgetName - ResourceGroupName $resourceGroupName
113
- Assert-NotNull $budgetGet
114
- Assert-AreEqual 6000 $budgetGet.Amount
115
- Assert-AreEqual $budgetName $budgetGet.Name
116
- Assert-AreEqual Cost $budgetGet.Category
117
- Assert-AreEqual Monthly $budgetGet.TimeGrain
118
-
119
- # Set budget
120
- Write-Debug " Update the budget $budgetName with a notification $notificationKey when cost reaches a threshold of 90 percent of amount"
121
- $budgetSet1 = Set-AzureRmConsumptionBudget - Name $budgetName - ResourceGroupName $resourceGroupName - NotificationKey $notificationKey1 - NotificationEnabled - NotificationThreshold 90 - ContactEmail johndoe@contoso.com , janesmith@contoso.com - ContactRole Owner, Reader, Contributor
122
- Assert-NotNull $budgetSet1
123
- Assert-AreEqual $budgetName $budgetSet1.Name
124
- Assert-AreEqual 1 $budgetSet1.Notification.Count
125
-
126
- Write-Debug " Update the budget $budgetName with a second notificaiton $notificationKey when cost reaches a threshold of 150 percent of amount"
127
- $budgetSet2 = Set-AzureRmConsumptionBudget - Name $budgetName - ResourceGroupName $resourceGroupName - NotificationKey $notificationKey2 - NotificationEnabled - NotificationThreshold 150 - ContactEmail johndoe@contoso.com , janesmith@contoso.com - ContactRole Owner, Reader, Contributor
128
- Assert-NotNull $budgetSet2
129
- Assert-AreEqual $budgetName $budgetSet2.Name
130
- Assert-AreEqual 2 $budgetSet2.Notification.Count
131
-
132
- # Remove budget
133
- Write-Debug " Remove the budget $budgetName "
134
- $response = Remove-AzureRmConsumptionBudget - Name $budgetName - ResourceGroupName $resourceGroupName - PassThru
135
- Assert-AreEqual True $response
136
-
137
- Assert-Throws {Get-AzureRmConsumptionBudget - Name $budgetName - ResourceGroupName $resourceGroupName }
110
+ # Create Resource Group
111
+ New-AzureRmResourceGroup - Name $resourceGroupName - Location ' West US' - Force
112
+
113
+ try
114
+ {
115
+ # Create budget
116
+ Write-Debug " Create a new budget $budgetName at resource group level"
117
+ $budgetNew = New-AzureRmConsumptionBudget - Amount 6000 - Name $budgetName - ResourceGroupName $resourceGroupName - Category Cost - StartDate $startDate - EndDate $endDate - TimeGrain Monthly
118
+ Assert-NotNull $budgetNew
119
+ Assert-AreEqual 6000 $budgetNew.Amount
120
+ Assert-AreEqual $budgetName $budgetNew.Name
121
+ Assert-AreEqual Cost $budgetNew.Category
122
+ Assert-AreEqual Monthly $budgetNew.TimeGrain
123
+
124
+ # Get budget
125
+ Write-Debug " Get the budget $budgetName "
126
+ $budgetGet = Get-AzureRmConsumptionBudget - Name $budgetName - ResourceGroupName $resourceGroupName
127
+ Assert-NotNull $budgetGet
128
+ Assert-AreEqual 6000 $budgetGet.Amount
129
+ Assert-AreEqual $budgetName $budgetGet.Name
130
+ Assert-AreEqual Cost $budgetGet.Category
131
+ Assert-AreEqual Monthly $budgetGet.TimeGrain
132
+
133
+ # Set budget
134
+ Write-Debug " Update the budget $budgetName with a notification $notificationKey when cost reaches a threshold of 90 percent of amount"
135
+ $budgetSet1 = Set-AzureRmConsumptionBudget - Name $budgetName - ResourceGroupName $resourceGroupName - NotificationKey $notificationKey1 - NotificationEnabled - NotificationThreshold 90 - ContactEmail johndoe@contoso.com , janesmith@contoso.com - ContactRole Owner, Reader, Contributor
136
+ Assert-NotNull $budgetSet1
137
+ Assert-AreEqual $budgetName $budgetSet1.Name
138
+ Assert-AreEqual 1 $budgetSet1.Notification.Count
139
+
140
+ Write-Debug " Update the budget $budgetName with a second notificaiton $notificationKey when cost reaches a threshold of 150 percent of amount"
141
+ $budgetSet2 = Set-AzureRmConsumptionBudget - Name $budgetName - ResourceGroupName $resourceGroupName - NotificationKey $notificationKey2 - NotificationEnabled - NotificationThreshold 150 - ContactEmail johndoe@contoso.com , janesmith@contoso.com - ContactRole Owner, Reader, Contributor
142
+ Assert-NotNull $budgetSet2
143
+ Assert-AreEqual $budgetName $budgetSet2.Name
144
+ Assert-AreEqual 2 $budgetSet2.Notification.Count
145
+
146
+ # Remove budget
147
+ Write-Debug " Remove the budget $budgetName "
148
+ $response = Remove-AzureRmConsumptionBudget - Name $budgetName - ResourceGroupName $resourceGroupName - PassThru
149
+ Assert-AreEqual True $response
150
+
151
+ Assert-Throws {Get-AzureRmConsumptionBudget - Name $budgetName - ResourceGroupName $resourceGroupName }
152
+ }
153
+ finally
154
+ {
155
+ # Remove Resource Group
156
+ Remove-AzureRmResourceGroup - Name $resourceGroupName - Force
157
+ }
138
158
}
139
159
140
160
<#
0 commit comments