Skip to content

Commit d863ec7

Browse files
author
dragonfly91
committed
Fixes to make Get Container working
1 parent 756a80f commit d863ec7

File tree

8 files changed

+131
-31
lines changed

8 files changed

+131
-31
lines changed

src/ResourceManager/RecoveryServices.Backup/Cmdlets/Container/GetAzureRmRecoveryServicesContainer.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,8 @@ namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets
2929
/// Get list of containers
3030
/// </summary>
3131
[Cmdlet(VerbsCommon.Get, "AzureRmRecoveryServicesContainer"), OutputType(typeof(List<AzureRmRecoveryServicesContainerBase>), typeof(AzureRmRecoveryServicesContainerBase))]
32-
public class GetAzureRmRecoveryServicesContainer : RecoveryServicesBackupCmdletBase
32+
public class GetAzureRmRecoveryServicesContainer : RecoveryServicesBackupVaultCmdletBase
3333
{
34-
[Parameter(Mandatory = false, HelpMessage = ParamHelpMsg.Common.Vault, ValueFromPipeline = true)]
35-
[ValidateNotNullOrEmpty]
36-
public ARSVault Vault { get; set; }
37-
3834
[Parameter(Mandatory = true, HelpMessage = ParamHelpMsg.Container.ContainerType)]
3935
[ValidateNotNullOrEmpty]
4036
public ContainerType ContainerType { get; set; }
@@ -68,19 +64,27 @@ public override void ExecuteCmdlet()
6864
// 3. Filter by Status
6965
queryParams.RegistrationStatus = Status.ToString();
7066

71-
var listResponse = HydraAdapter.ListContainers(Vault.Name, Vault.ResouceGroupName, queryParams);
67+
var listResponse = HydraAdapter.ListContainers(Vault.ResouceGroupName, Vault.Name, queryParams);
7268

7369
List<AzureRmRecoveryServicesContainerBase> containerModels = ConversionHelpers.GetContainerModelList(listResponse);
7470

7571
// NOTE: Should move this to provider?
7672
// 4. Filter by RG Name
77-
if (ContainerType == Models.ContainerType.AzureVM)
73+
if (ContainerType == Models.ContainerType.AzureVM &&
74+
!string.IsNullOrEmpty(ResourceGroupName))
7875
{
7976
containerModels = containerModels.Where(containerModel =>
8077
(containerModel as AzureRmRecoveryServicesIaasVmContainer).ResourceGroupName == ResourceGroupName).ToList();
8178
}
8279

83-
WriteObject(containerModels);
80+
if (containerModels.Count == 1)
81+
{
82+
WriteObject(containerModels.First());
83+
}
84+
else
85+
{
86+
WriteObject(containerModels);
87+
}
8488
});
8589
}
8690
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
<DesignTime>True</DesignTime>
8686
<DependentUpon>Resources.resx</DependentUpon>
8787
</Compile>
88+
<Compile Include="RecoveryServicesBackupVaultCmdletBase.cs" />
8889
<Compile Include="RecoveryServicesBackupCmdletBase.cs" />
8990
<Compile Include="CmdletParameterHelpMessages.cs" />
9091
</ItemGroup>

src/ResourceManager/RecoveryServices.Backup/Commands.RecoveryServices.Backup.Helpers/Conversions/ConversionHelpers.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ public static AzureRmRecoveryServicesContainerBase GetContainerModel(ProtectionC
3232
if (protectionContainer != null &&
3333
protectionContainer.Properties != null)
3434
{
35-
if (protectionContainer.Properties.GetType() == typeof(AzureIaaSVMProtectionContainer))
35+
if (protectionContainer.Properties.GetType().IsSubclassOf(typeof(AzureIaaSVMProtectionContainer)))
3636
{
37-
new AzureRmRecoveryServicesIaasVmContainer(protectionContainer);
37+
containerModel = new AzureRmRecoveryServicesIaasVmContainer(protectionContainer);
3838
}
3939
}
4040

src/ResourceManager/RecoveryServices.Backup/Commands.RecoveryServices.Backup.Models/CommonModels/Utils.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
using System.Text.RegularExpressions;
2121

2222
namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.Models
23-
{
23+
{
2424
public class PolicyConstants
2525
{
2626
public const int MaxAllowedRetentionDurationCount = 9999;
@@ -59,17 +59,30 @@ public static string GetString(IEnumerable<DayOfWeek> objList)
5959

6060
public class IdUtils
6161
{
62-
private static readonly Regex ResourceGroupRegex = new Regex(@"/subscriptions/(?<subscriptionsId>.+)/resourceGroups/(?<resourceGroupName>.+)/providers/(?<providersName>.+)/BackupVault/(?<BackupVaultName>.+)/containers/(?<containersName>.+)", RegexOptions.Compiled);
62+
static readonly Regex ResourceGroupRegex = new Regex(@"/Subscriptions/(?<subscriptionsId>.+)/resourceGroups/(?<resourceGroupName>.+)/providers/(?<providersName>.+)/vaults/(?<BackupVaultName>.+)/backupFabrics/(?<BackupFabricName>.+)/protectionContainers/(?<containersName>.+)", RegexOptions.Compiled);
63+
const string NameDelimiter = ";";
6364

6465
public static string GetResourceGroupName(string id)
6566
{
6667
var match = ResourceGroupRegex.Match(id);
6768
if (match.Success)
6869
{
69-
var vmRGName = match.Groups["containersName"];
70-
if (vmRGName != null && vmRGName.Success)
70+
var vmUniqueName = match.Groups["containersName"];
71+
if (vmUniqueName != null && vmUniqueName.Success)
7172
{
72-
return vmRGName.Value;
73+
var vmNameInfo = vmUniqueName.Value.Split(NameDelimiter.ToCharArray());
74+
if (vmNameInfo.Length == 3)
75+
{
76+
return vmNameInfo[1];
77+
}
78+
else if (vmNameInfo.Length == 4)
79+
{
80+
return vmNameInfo[2];
81+
}
82+
else
83+
{
84+
throw new Exception("Container name not in the expected format");
85+
}
7386
}
7487
}
7588

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

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

1515
function Test-GetContainerScenario
1616
{
17-
$vaults = Get-AzureRmRecoveryServicesVault -ResourceGroupName "phaniktRSV" -Name "phaniktRs1";
18-
echo $vaults;
17+
$vault = Get-AzureRmRecoveryServicesVault -ResourceGroupName "phaniktRSV" -Name "phaniktRs1";
18+
$containers = Get-AzureRmRecoveryServicesContainer -Vault $vault -ContainerType "AzureVM" -Status "Registered";
19+
foreach ($container in $containers)
20+
{
21+
echo $container.Name $container.ResourceGroupName;
22+
}
23+
Assert-AreEqual $containers[0].Name "mylinux1";
24+
25+
$namedContainer = Get-AzureRmRecoveryServicesContainer -Vault $vault -ContainerType "AzureVM" -Status "Registered" -Name "mylinux1";
26+
Assert-AreEqual $namedContainer.Name "mylinux1";
27+
28+
$rgFilteredContainer = Get-AzureRmRecoveryServicesContainer -Vault $vault -ContainerType "AzureVM" -Status "Registered" -Name "mylinux1" -ResourceGroupName "00prjai12";
29+
echo $rgFilteredContainer.Name $rgFilteredContainer.ResourceGroupName;
1930
}

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

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

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,24 +118,30 @@
118118
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
119119
</resheader>
120120
<data name="AggregateException" xml:space="preserve">
121-
<value>Handling aggregate exception.</value>
121+
<value>Handling aggregate exception</value>
122122
</data>
123123
<data name="ArgumentException" xml:space="preserve">
124-
<value>Received ArgumentException.</value>
124+
<value>Received ArgumentException</value>
125+
</data>
126+
<data name="RsVaultResNameNullOrEmpty" xml:space="preserve">
127+
<value>RecoveryServicesBackupVault.Name</value>
128+
</data>
129+
<data name="RsVaultRGNameNullOrEmpty" xml:space="preserve">
130+
<value>RecoveryServicesBackupVault.ResourceGroupName</value>
125131
</data>
126132
<data name="CloudException" xml:space="preserve">
127-
<value>Received CloudException, ErrorCode: {0}, Message: {1}.</value>
133+
<value>Received CloudException, ErrorCode: {0}, Message: {1}</value>
128134
</data>
129135
<data name="CloudExceptionCodeNotFound" xml:space="preserve">
130-
<value>Received CloudException, StatusCode: {0}.</value>
136+
<value>Received CloudException, StatusCode: {0}</value>
131137
</data>
132138
<data name="ExceptionInExecution" xml:space="preserve">
133-
<value>Caught exception, type: {0}.</value>
139+
<value>Caught exception, type: {0}</value>
134140
</data>
135141
<data name="ResourceNotFoundMessage" xml:space="preserve">
136-
<value>The specified resource does not exist.</value>
142+
<value>The specified resource does not exist</value>
137143
</data>
138144
<data name="WebException" xml:space="preserve">
139-
<value>Received WebException, Response: {0}, Status: {1}.</value>
145+
<value>Received WebException, Response: {0}, Status: {1}</value>
140146
</data>
141147
</root>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 Microsoft.Azure.Commands.RecoveryServices.Backup.Properties;
16+
using System;
17+
using System.Management.Automation;
18+
19+
namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets
20+
{
21+
public abstract class RecoveryServicesBackupVaultCmdletBase : RecoveryServicesBackupCmdletBase
22+
{
23+
[Parameter(Mandatory = false, HelpMessage = ParamHelpMsg.Common.Vault, ValueFromPipeline = true)]
24+
[ValidateNotNullOrEmpty]
25+
public ARSVault Vault { get; set; }
26+
27+
public override void ExecuteCmdlet()
28+
{
29+
base.ExecuteCmdlet();
30+
31+
if (Vault != null)
32+
{
33+
if (string.IsNullOrEmpty(Vault.ResouceGroupName))
34+
{
35+
throw new ArgumentException(Resources.RsVaultRGNameNullOrEmpty);
36+
}
37+
38+
if (string.IsNullOrEmpty(Vault.Name))
39+
{
40+
throw new ArgumentException(Resources.RsVaultResNameNullOrEmpty);
41+
}
42+
}
43+
44+
InitializeAzureBackupCmdlet(Vault.ResouceGroupName, Vault.Name);
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)