@@ -1036,6 +1036,255 @@ function Test-CRUDVnetFlowLog
1036
1036
}
1037
1037
}
1038
1038
1039
+ <#
1040
+ . SYNOPSIS
1041
+ Test Flow log CRUD API With Managed Identity.
1042
+ #>
1043
+ function Test-CRUDVnetFlowLogWithManagedIdentity
1044
+ {
1045
+ # Setup
1046
+ $resourceGroupName = Get-NrpResourceGroupName
1047
+ $nwName = Get-NrpResourceName
1048
+ $nwRgName = Get-NrpResourceGroupName
1049
+ $flowLogName = Get-NrpResourceName
1050
+ $domainNameLabel = Get-NrpResourceName
1051
+ $vnetName = Get-NrpResourceName
1052
+ $stoname = Get-NrpResourceName
1053
+ $location = Get-ProviderLocation " Microsoft.Network/networkWatchers" " Central US EUAP"
1054
+ $identityName = Get-NrpResourceName
1055
+
1056
+ try
1057
+ {
1058
+ # Create Resource group
1059
+ New-AzResourceGroup - Name $resourceGroupName - Location " $location "
1060
+
1061
+ # Create the Virtual Network
1062
+ $subnet = New-AzVirtualNetworkSubnetConfig - Name " FlowLogSubnet" - AddressPrefix 10.0 .0.0 / 24
1063
+ $vnet = New-AzVirtualNetwork - Name $vnetName - ResourceGroupName $resourceGroupName - Location $location - AddressPrefix 10.0 .0.0 / 16 - Subnet $subnet
1064
+ $vnet = Get-AzVirtualNetwork - Name $vnetName - ResourceGroupName $resourceGroupName
1065
+
1066
+ # Create Resource group for Network Watcher
1067
+ New-AzResourceGroup - Name $nwRgName - Location " $location "
1068
+
1069
+ # Get Network Watcher
1070
+ $nw = Get-CreateTestNetworkWatcher - location $location - nwName $nwName - nwRgName $nwRgName
1071
+
1072
+ # Create storage
1073
+ $stoname = ' sto' + $stoname
1074
+ $stotype = ' Standard_GRS'
1075
+
1076
+ New-AzStorageAccount - ResourceGroupName $resourceGroupName - Name $stoname - Location $location - Type $stotype ;
1077
+ $sto = Get-AzStorageAccount - ResourceGroupName $resourceGroupName - Name $stoname ;
1078
+
1079
+ # Create Managed Identity
1080
+ $identity = New-AzUserAssignedIdentity - Name $identityName - Location $location - ResourceGroup $resourceGroupName
1081
+
1082
+ # Create flow log
1083
+ $config = New-AzNetworkWatcherFlowLog - NetworkWatcher $nw - Name $flowLogName - TargetResourceId $vnet.Id - StorageId $sto.Id - Enabled $true - UserAssignedIdentity $identity.Id
1084
+
1085
+ # Validation set operation
1086
+ Assert-AreEqual $config.TargetResourceId $vnet.Id
1087
+ Assert-AreEqual $config.StorageId $sto.Id
1088
+ Assert-AreEqual $config.Enabled $true
1089
+ Assert-AreEqual $config.Format.Version 2
1090
+
1091
+ # Get flow log
1092
+ $flowLog = Get-AzNetworkWatcherFlowLog - NetworkWatcher $nw - Name $flowLogName
1093
+
1094
+ # Get flow log Identity
1095
+ $identity01 = $flowLog.Identity
1096
+
1097
+ # Validation get operation
1098
+ Assert-AreEqual $flowLog.TargetResourceId $vnet.Id
1099
+ Assert-AreEqual $flowLog.StorageId $sto.Id
1100
+ Assert-AreEqual $flowLog.Enabled $true
1101
+ Assert-AreEqual $identity01.UserAssignedIdentities.Count 1
1102
+ Assert-NotNull $identity01.UserAssignedIdentities.Values [0 ].PrincipalId
1103
+ Assert-NotNull $identity01.UserAssignedIdentities.Values [0 ].ClientId
1104
+
1105
+ # Delete flow log
1106
+ Remove-AzNetworkWatcherFlowLog - NetworkWatcher $nw - Name $flowLogName
1107
+ }
1108
+ finally
1109
+ {
1110
+ # Cleanup
1111
+ Clean - ResourceGroup $resourceGroupName
1112
+ Clean - ResourceGroup $nwRgName
1113
+ }
1114
+ }
1115
+
1116
+ <#
1117
+ . SYNOPSIS
1118
+ Test Flow log CRUD API With Managed Identity.
1119
+ #>
1120
+ function Test-CRUDVnetFlowLogWithNoneManagedIdentity
1121
+ {
1122
+ # Setup
1123
+ $resourceGroupName = Get-NrpResourceGroupName
1124
+ $nwName = Get-NrpResourceName
1125
+ $nwRgName = Get-NrpResourceGroupName
1126
+ $flowLogName = Get-NrpResourceName
1127
+ $domainNameLabel = Get-NrpResourceName
1128
+ $vnetName = Get-NrpResourceName
1129
+ $stoname = Get-NrpResourceName
1130
+ $location = Get-ProviderLocation " Microsoft.Network/networkWatchers" " Central US EUAP"
1131
+ $identityName = Get-NrpResourceName
1132
+
1133
+ try
1134
+ {
1135
+ # Create Resource group
1136
+ New-AzResourceGroup - Name $resourceGroupName - Location " $location "
1137
+
1138
+ # Create the Virtual Network
1139
+ $subnet = New-AzVirtualNetworkSubnetConfig - Name " FlowLogSubnet" - AddressPrefix 10.0 .0.0 / 24
1140
+ $vnet = New-AzVirtualNetwork - Name $vnetName - ResourceGroupName $resourceGroupName - Location $location - AddressPrefix 10.0 .0.0 / 16 - Subnet $subnet
1141
+ $vnet = Get-AzVirtualNetwork - Name $vnetName - ResourceGroupName $resourceGroupName
1142
+
1143
+ # Create Resource group for Network Watcher
1144
+ New-AzResourceGroup - Name $nwRgName - Location " $location "
1145
+
1146
+ # Get Network Watcher
1147
+ $nw = Get-CreateTestNetworkWatcher - location $location - nwName $nwName - nwRgName $nwRgName
1148
+
1149
+ # Create storage
1150
+ $stoname = ' sto' + $stoname
1151
+ $stotype = ' Standard_GRS'
1152
+
1153
+ New-AzStorageAccount - ResourceGroupName $resourceGroupName - Name $stoname - Location $location - Type $stotype ;
1154
+ $sto = Get-AzStorageAccount - ResourceGroupName $resourceGroupName - Name $stoname ;
1155
+
1156
+ # Create Managed Identity
1157
+ $identity = New-AzUserAssignedIdentity - Name $identityName - Location $location - ResourceGroup $resourceGroupName
1158
+
1159
+ # Create flow log
1160
+ $config = New-AzNetworkWatcherFlowLog - NetworkWatcher $nw - Name $flowLogName - TargetResourceId $vnet.Id - StorageId $sto.Id - Enabled $true - UserAssignedIdentity $identity.Id
1161
+
1162
+ # Validation set operation
1163
+ Assert-AreEqual $config.TargetResourceId $vnet.Id
1164
+ Assert-AreEqual $config.StorageId $sto.Id
1165
+ Assert-AreEqual $config.Enabled $true
1166
+ Assert-AreEqual $config.Format.Version 2
1167
+
1168
+ # Get flow log
1169
+ $flowLog = Get-AzNetworkWatcherFlowLog - NetworkWatcher $nw - Name $flowLogName
1170
+
1171
+ # Get flow log Identity
1172
+ $identity01 = $flowLog.Identity
1173
+
1174
+ # Validation get operation
1175
+ Assert-AreEqual $flowLog.TargetResourceId $vnet.Id
1176
+ Assert-AreEqual $flowLog.StorageId $sto.Id
1177
+ Assert-AreEqual $flowLog.Enabled $true
1178
+ Assert-AreEqual $identity01.UserAssignedIdentities.Count 1
1179
+ Assert-NotNull $identity01.UserAssignedIdentities.Values [0 ].PrincipalId
1180
+ Assert-NotNull $identity01.UserAssignedIdentities.Values [0 ].ClientId
1181
+
1182
+ Set-AzNetworkWatcherFlowLog - InputObject $flowLog - UserAssignedIdentity " None" - Force
1183
+
1184
+ # Get flow log
1185
+ $updatedFlowLog = Get-AzNetworkWatcherFlowLog - NetworkWatcher $nw - Name $flowLogName
1186
+
1187
+ # Get flow log Identity
1188
+ $identity01 = $updatedFlowLog.Identity
1189
+
1190
+ Assert-AreEqual $identity01.UserAssignedIdentities.Count 0
1191
+ Assert-Null $identity01.UserAssignedIdentities.Values [0 ].PrincipalId
1192
+ Assert-Null $identity01.UserAssignedIdentities.Values [0 ].ClientId
1193
+
1194
+ # Delete flow log
1195
+ Remove-AzNetworkWatcherFlowLog - NetworkWatcher $nw - Name $flowLogName
1196
+ }
1197
+ finally
1198
+ {
1199
+ # Cleanup
1200
+ Clean - ResourceGroup $resourceGroupName
1201
+ Clean - ResourceGroup $nwRgName
1202
+ }
1203
+ }
1204
+
1205
+ <#
1206
+ . SYNOPSIS
1207
+ Test Flow log CRUD API With Managed Identity.
1208
+ #>
1209
+ function Test-SetVnetFlowLogWithManagedIdentity
1210
+ {
1211
+ # Setup
1212
+ $resourceGroupName = Get-NrpResourceGroupName
1213
+ $nwName = Get-NrpResourceName
1214
+ $nwRgName = Get-NrpResourceGroupName
1215
+ $flowLogName = Get-NrpResourceName
1216
+ $domainNameLabel = Get-NrpResourceName
1217
+ $vnetName = Get-NrpResourceName
1218
+ $stoname = Get-NrpResourceName
1219
+ $location = Get-ProviderLocation " Microsoft.Network/networkWatchers" " Central US EUAP"
1220
+ $identityName = Get-NrpResourceName
1221
+
1222
+ try
1223
+ {
1224
+ # Create Resource group
1225
+ New-AzResourceGroup - Name $resourceGroupName - Location " $location "
1226
+
1227
+ # Create the Virtual Network
1228
+ $subnet = New-AzVirtualNetworkSubnetConfig - Name " FlowLogSubnet" - AddressPrefix 10.0 .0.0 / 24
1229
+ $vnet = New-AzVirtualNetwork - Name $vnetName - ResourceGroupName $resourceGroupName - Location $location - AddressPrefix 10.0 .0.0 / 16 - Subnet $subnet
1230
+ $vnet = Get-AzVirtualNetwork - Name $vnetName - ResourceGroupName $resourceGroupName
1231
+
1232
+ # Create Resource group for Network Watcher
1233
+ New-AzResourceGroup - Name $nwRgName - Location " $location "
1234
+
1235
+ # Get Network Watcher
1236
+ $nw = Get-CreateTestNetworkWatcher - location $location - nwName $nwName - nwRgName $nwRgName
1237
+
1238
+ # Create storage
1239
+ $stoname = ' sto' + $stoname
1240
+ $stotype = ' Standard_GRS'
1241
+
1242
+ New-AzStorageAccount - ResourceGroupName $resourceGroupName - Name $stoname - Location $location - Type $stotype ;
1243
+ $sto = Get-AzStorageAccount - ResourceGroupName $resourceGroupName - Name $stoname ;
1244
+
1245
+ # Create flow log
1246
+ $config = New-AzNetworkWatcherFlowLog - NetworkWatcher $nw - Name $flowLogName - TargetResourceId $vnet.Id - StorageId $sto.Id - Enabled $true
1247
+
1248
+ # Validation set operation
1249
+ Assert-AreEqual $config.TargetResourceId $vnet.Id
1250
+ Assert-AreEqual $config.StorageId $sto.Id
1251
+ Assert-AreEqual $config.Enabled $true
1252
+ Assert-AreEqual $config.Format.Version 2
1253
+
1254
+ # Get flow log
1255
+ $flowLog = Get-AzNetworkWatcherFlowLog - NetworkWatcher $nw - Name $flowLogName
1256
+
1257
+ # Create Managed Identity
1258
+ $identity = New-AzUserAssignedIdentity - Name $identityName - Location $location - ResourceGroup $resourceGroupName
1259
+
1260
+ # Set flow log with Managed Identity
1261
+ Set-AzNetworkWatcherFlowLog - InputObject $flowLog - UserAssignedIdentity $identity.id - Force
1262
+
1263
+ # Get updated flowLog
1264
+ $updatedFlowLog = Get-AzNetworkWatcherFlowLog - NetworkWatcher $nw - Name $flowLogName
1265
+
1266
+ # Get updated flow log Identity
1267
+ $identity01 = $updatedFlowLog.Identity
1268
+
1269
+ # Validation get operation
1270
+ Assert-AreEqual $updatedFlowLog.TargetResourceId $vnet.Id
1271
+ Assert-AreEqual $updatedFlowLog.StorageId $sto.Id
1272
+ Assert-AreEqual $updatedFlowLog.Enabled $true
1273
+ Assert-AreEqual $identity01.UserAssignedIdentities.Count 1
1274
+ Assert-NotNull $identity01.UserAssignedIdentities.Values [0 ].PrincipalId
1275
+ Assert-NotNull $identity01.UserAssignedIdentities.Values [0 ].ClientId
1276
+
1277
+ # Delete flow log
1278
+ Remove-AzNetworkWatcherFlowLog - NetworkWatcher $nw - Name $flowLogName
1279
+ }
1280
+ finally
1281
+ {
1282
+ # Cleanup
1283
+ Clean - ResourceGroup $resourceGroupName
1284
+ Clean - ResourceGroup $nwRgName
1285
+ }
1286
+ }
1287
+
1039
1288
<#
1040
1289
. SYNOPSIS
1041
1290
Test Flow log CRUD API.
0 commit comments