Skip to content

Commit 0789be9

Browse files
authored
Merge pull request Azure#6495 from MabOneSdk/get-status-2
Adding Get-AzureRmRecoveryServicesBackupStatus cmdlet
2 parents 69374cc + 5c80e4e commit 0789be9

File tree

24 files changed

+44875
-18
lines changed

24 files changed

+44875
-18
lines changed

src/ResourceManager/RecoveryServices/Commands.RecoveryServices.Backup.Models/CmdletParamEnums.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,10 @@ public enum ItemParams
8484
BackupManagementType,
8585
ExpiryDateTimeUTC,
8686
}
87+
88+
public enum ProtectionCheckParams
89+
{
90+
Name,
91+
ResourceGroupName,
92+
}
8793
}

src/ResourceManager/RecoveryServices/Commands.RecoveryServices.Backup.Models/Commands.RecoveryServices.Backup.Models.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
<Compile Include="AzureVmModels\AzureVmRecoveryPoint.cs" />
5858
<Compile Include="BaseObjects.cs" />
5959
<Compile Include="CmdletParamEnums.cs" />
60+
<Compile Include="CommonModels\ResourceBackupStatus.cs" />
6061
<Compile Include="CommonModels\Utils.cs" />
6162
<Compile Include="CommonModels\Enums.cs" />
6263
<Compile Include="CommonModels\PolicyRetentionObjects.cs" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
16+
17+
namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.Models
18+
{
19+
/// <summary>
20+
/// Backup status of a resource.
21+
/// </summary>
22+
public class ResourceBackupStatus
23+
{
24+
/// <summary>
25+
/// The Resource Name.
26+
/// </summary>
27+
public string Name { get; set; }
28+
29+
/// <summary>
30+
/// The Resource Group Name.
31+
/// </summary>
32+
public string ResourceGroupName { get; set; }
33+
34+
/// <summary>
35+
/// If the resource is protected by some vault in the subscription, this contains the resource ID of that vault.
36+
/// </summary>
37+
public string VaultId { get; set; }
38+
39+
/// <summary>
40+
/// Specifies whether the resource is protected by some vault in the subscription.
41+
/// </summary>
42+
public bool BackedUp { get; set; }
43+
44+
public ResourceBackupStatus(string name, string resourceGroupName, string vaultId, bool backedUp)
45+
{
46+
if (backedUp && string.IsNullOrEmpty(vaultId) ||
47+
!backedUp && !string.IsNullOrEmpty(vaultId))
48+
{
49+
throw new ArgumentException($"Inconsistent parameters specified. backedUp: {backedUp} and vaultId: {vaultId}.");
50+
}
51+
52+
Name = name;
53+
ResourceGroupName = resourceGroupName;
54+
VaultId = vaultId;
55+
BackedUp = backedUp;
56+
}
57+
}
58+
}

src/ResourceManager/RecoveryServices/Commands.RecoveryServices.Backup.Models/Properties/Resources.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ResourceManager/RecoveryServices/Commands.RecoveryServices.Backup.Models/Properties/Resources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,4 +501,7 @@ Path of the file along with filename: {0}
501501

502502
Password to run the file: {1}</value>
503503
</data>
504+
<data name="UnsupportedResourceTypeException" xml:space="preserve">
505+
<value>The resource type {0} is currently not supported.</value>
506+
</data>
504507
</root>

src/ResourceManager/RecoveryServices/Commands.RecoveryServices.Backup.Providers/IPsBackupProvider.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,7 @@ public interface IPsBackupProvider
6161
RPMountScriptDetails ProvisionItemLevelRecoveryAccess();
6262

6363
void RevokeItemLevelRecoveryAccess();
64+
65+
ResourceBackupStatus CheckBackupStatus();
6466
}
6567
}

src/ResourceManager/RecoveryServices/Commands.RecoveryServices.Backup.Providers/Providers/AzureSqlPsBackupProvider.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,11 @@ public List<ItemBase> ListProtectedItems()
479479
return itemModels;
480480
}
481481

482+
public ResourceBackupStatus CheckBackupStatus()
483+
{
484+
throw new NotImplementedException();
485+
}
486+
482487
#region private
483488
private void ValidateAzureSqlWorkloadType(CmdletModel.WorkloadType type)
484489
{

src/ResourceManager/RecoveryServices/Commands.RecoveryServices.Backup.Providers/Providers/DpmPsBackupProvider.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,5 +153,10 @@ public List<ItemBase> ListProtectedItems()
153153
{
154154
throw new NotImplementedException();
155155
}
156+
157+
public ResourceBackupStatus CheckBackupStatus()
158+
{
159+
throw new NotImplementedException();
160+
}
156161
}
157162
}

src/ResourceManager/RecoveryServices/Commands.RecoveryServices.Backup.Providers/Providers/IaasVmPsBackupProvider.cs

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
using System.Text;
3232
using System.Text.RegularExpressions;
3333
using Microsoft.Azure.Commands.Common.Authentication;
34+
using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
3435

3536
namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.ProviderModel
3637
{
@@ -923,6 +924,55 @@ public RetentionPolicyBase GetDefaultRetentionPolicyObject()
923924

924925
}
925926

927+
public ResourceBackupStatus CheckBackupStatus()
928+
{
929+
string azureVmName = (string)ProviderData[ProtectionCheckParams.Name];
930+
string azureVmResourceGroupName =
931+
(string)ProviderData[ProtectionCheckParams.ResourceGroupName];
932+
933+
ODataQuery<ProtectedItemQueryObject> queryParams =
934+
new ODataQuery<ProtectedItemQueryObject>(
935+
q => q.BackupManagementType
936+
== ServiceClientModel.BackupManagementType.AzureIaasVM &&
937+
q.ItemType == DataSourceType.VM);
938+
939+
var vaultIds = ServiceClientAdapter.ListVaults();
940+
foreach (var vaultId in vaultIds)
941+
{
942+
ResourceIdentifier vaultIdentifier = new ResourceIdentifier(vaultId);
943+
944+
var items = ServiceClientAdapter.ListProtectedItem(
945+
queryParams,
946+
vaultName: vaultIdentifier.ResourceName,
947+
resourceGroupName: vaultIdentifier.ResourceGroupName);
948+
949+
if (items.Any(
950+
item =>
951+
{
952+
ResourceIdentifier vmIdentifier =
953+
new ResourceIdentifier(item.Properties.SourceResourceId);
954+
var itemVmName = vmIdentifier.ResourceName;
955+
var itemVmRgName = vmIdentifier.ResourceGroupName;
956+
957+
return itemVmName.ToLower() == azureVmName.ToLower() &&
958+
itemVmRgName.ToLower() == azureVmResourceGroupName.ToLower();
959+
}))
960+
{
961+
return new ResourceBackupStatus(
962+
azureVmName,
963+
azureVmResourceGroupName,
964+
vaultId,
965+
true);
966+
}
967+
}
968+
969+
return new ResourceBackupStatus(
970+
azureVmName,
971+
azureVmResourceGroupName,
972+
null,
973+
false);
974+
}
975+
926976
#region private
927977

928978
private static CmdletModel.DailyRetentionFormat GetDailyRetentionFormat()
@@ -1286,8 +1336,6 @@ private void CopyScheduleTimeToRetentionTimes(CmdletModel.LongTermRetentionPolic
12861336
}
12871337
}
12881338

1289-
#endregion
1290-
12911339
/// <summary>
12921340
/// Generates ILR Response object for Windows VMs
12931341
/// </summary>
@@ -1443,5 +1491,7 @@ private string ReplacePasswordInScriptContentAndReturn(ref string content)
14431491

14441492
return password;
14451493
}
1494+
1495+
#endregion
14461496
}
14471497
}

src/ResourceManager/RecoveryServices/Commands.RecoveryServices.Backup.Providers/Providers/MabPsBackupProvider.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,5 +156,10 @@ public List<ItemBase> ListProtectedItems()
156156
{
157157
throw new NotImplementedException();
158158
}
159+
160+
public ResourceBackupStatus CheckBackupStatus()
161+
{
162+
throw new NotImplementedException();
163+
}
159164
}
160165
}

src/ResourceManager/RecoveryServices/Commands.RecoveryServices.Backup.Providers/PsBackupProviderManager.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,5 +226,21 @@ public IPsBackupProvider GetProviderInstance(PsBackupProviderTypes providerType)
226226

227227
return psBackupProvider;
228228
}
229+
230+
public IPsBackupProvider GetProviderInstance(string resourceType)
231+
{
232+
if (resourceType == "Microsoft.Compute/virtualMachines" ||
233+
resourceType == "Microsoft.ClassicCompute/virtualMachines" ||
234+
resourceType == "AzureVM")
235+
{
236+
return GetProviderInstance(PsBackupProviderTypes.IaasVm);
237+
}
238+
else
239+
{
240+
throw new ArgumentException(
241+
string.Format(Resources.UnsupportedResourceTypeException,
242+
resourceType));
243+
}
244+
}
229245
}
230246
}

src/ResourceManager/RecoveryServices/Commands.RecoveryServices.Backup.ServiceClientAdapter/BMSAPIs/VaultAPIs.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,16 @@
1414

1515
using System.Collections.Generic;
1616
using System.Linq;
17-
using Microsoft.Azure.Management.RecoveryServices.Models;
1817

1918
namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.ServiceClientAdapterNS
2019
{
2120
public partial class ServiceClientAdapter
2221
{
23-
public List<Vault> ListVaults()
22+
public List<string> ListVaults()
2423
{
2524
var response = RSAdapter.Client.Vaults.ListBySubscriptionIdWithHttpMessagesAsync(
2625
cancellationToken: RSAdapter.CmdletCancellationToken).Result;
27-
return response.Body.ToList();
26+
return response.Body.Select(vault => vault.Id).ToList();
2827
}
2928
}
3029
}

src/ResourceManager/RecoveryServices/Commands.RecoveryServices.Backup.Test/Commands.RecoveryServices.Backup.Test.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@
146146
</ItemGroup>
147147
<ItemGroup>
148148
<Compile Include="Properties\AssemblyInfo.cs" />
149+
<Compile Include="ScenarioTests\IaasVm\ProtectionCheckTests.cs" />
149150
<Compile Include="TestConstants.cs" />
150151
<None Include="packages.config">
151152
<SubType>Designer</SubType>
@@ -187,6 +188,9 @@
187188
<None Include="ScenarioTests\IaasVm\PolicyTests.ps1">
188189
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
189190
</None>
191+
<None Include="ScenarioTests\IaasVm\ProtectionCheckTests.ps1">
192+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
193+
</None>
190194
<None Include="ScenarioTests\Mab\ContainerTests.ps1">
191195
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
192196
</None>
@@ -259,6 +263,9 @@
259263
<None Include="SessionRecords\Microsoft.Azure.Commands.RecoveryServices.Backup.Test.ScenarioTests.PolicyTests\TestAzureVMPolicy.json">
260264
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
261265
</None>
266+
<None Include="SessionRecords\Microsoft.Azure.Commands.RecoveryServices.Backup.Test.ScenarioTests.ProtectionCheckTests\TestAzureVMProtectionCheck.json">
267+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
268+
</None>
262269
</ItemGroup>
263270
<ItemGroup />
264271
<ItemGroup>

src/ResourceManager/RecoveryServices/Commands.RecoveryServices.Backup.Test/ScenarioTests/IaasVm/Common.ps1

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,12 @@
1414

1515
function Get-ResourceGroupLocation
1616
{
17-
$namespace = "Microsoft.RecoveryServices"
18-
$type = "vaults"
19-
$resourceProvider = Get-AzureRmResourceProvider -ProviderNamespace $namespace | where {$_.ResourceTypes[0].ResourceTypeName -eq $type}
20-
21-
if ($resourceProvider -eq $null)
22-
{
23-
return "westus";
24-
}
25-
else
26-
{
27-
return $resourceProvider.Locations[0]
28-
}
17+
$location = Get-Location "Microsoft.RecoveryServices" "vaults" "West US";
18+
$outputLocation = $location.ToLower()
19+
$outputLocation = $outputLocation -replace '\s', ''
20+
$outputLocation = $outputLocation -replace '-', ''
21+
$outputLocation = $outputLocation -replace '_', ''
22+
return $outputLocation;
2923
}
3024

3125
function Get-RandomSuffix(
@@ -189,6 +183,44 @@ function Create-VM(
189183
return $vm
190184
}
191185

186+
function Create-GalleryVM(
187+
[string] $resourceGroupName,
188+
[string] $location,
189+
[int] $nick = 0)
190+
{
191+
$suffix = $(Get-RandomSuffix 5) + $nick
192+
$vmName = "PSTestGVM" + $suffix
193+
194+
$vm = Get-AzureRmVM -ResourceGroupName $resourceGroupName -Name $vmName -ErrorAction Ignore
195+
196+
if ($vm -eq $null)
197+
{
198+
$subnetConfigName = "PSTestSNC" + $suffix
199+
$vnetName = "PSTestVNET" + $suffix
200+
$pipName = "pstestpublicdns" + $suffix
201+
$nsgName = "PSTestNSG" + $suffix
202+
$dnsLabel = "pstestdnslabel" + "-" + $suffix
203+
204+
$UserName='demouser'
205+
$PasswordString = $(Get-RandomSuffix 12)
206+
$Password=$PasswordString| ConvertTo-SecureString -Force -AsPlainText
207+
$Credential=New-Object PSCredential($UserName,$Password)
208+
209+
$vm = New-AzureRmVm `
210+
-ResourceGroupName $resourceGroupName `
211+
-Name $vmName `
212+
-Location $location `
213+
-SubnetName $subnetConfigName `
214+
-SecurityGroupName $nsgName `
215+
-PublicIpAddressName $pipName `
216+
-ImageName "MicrosoftWindowsServer:WindowsServer:2012-R2-Datacenter:latest" `
217+
-Credential $Credential `
218+
-DomainNameLabel $dnsLabel
219+
}
220+
221+
return $vm
222+
}
223+
192224
function Create-SA(
193225
[string] $resourceGroupName,
194226
[string] $location)

0 commit comments

Comments
 (0)