Skip to content

Commit 633a7d6

Browse files
author
dragonfly91
committed
Fixing some issues with filtering in GetContainer API
1 parent f9585d5 commit 633a7d6

File tree

6 files changed

+49
-100
lines changed

6 files changed

+49
-100
lines changed

src/ResourceManager/AzureBackup/Commands.AzureBackup/AzureBackupClientAdapter/ContainerAdapter.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,13 @@ public void EnableMachineContainerReregistration(long containerId)
8383
}
8484

8585
/// <summary>
86-
/// Gets all containers in the vault
86+
/// Gets all IaaSVM containers in the vault by friendly name
8787
/// </summary>
88-
/// <param name="filter"></param>
88+
/// <param name="parameters"></param>
8989
/// <returns></returns>
90-
public IEnumerable<CSMContainerResponse> ListContainers(string filter)
90+
public IEnumerable<CSMContainerResponse> ListContainers(ContainerQueryParameters parameters)
9191
{
92-
var listResponse = AzureBackupClient.Container.ListAsync(filter, GetCustomRequestHeaders(), CmdletCancellationToken).Result;
92+
var listResponse = AzureBackupClient.Container.ListAsync(parameters, GetCustomRequestHeaders(), CmdletCancellationToken).Result;
9393
return listResponse.CSMContainerListResponse.Value;
9494
}
9595

src/ResourceManager/AzureBackup/Commands.AzureBackup/Cmdlets/Container/GetAzureBackupContainer.cs

Lines changed: 21 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,21 @@ namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
3131
[Cmdlet(VerbsCommon.Get, "AzureBackupContainer"), OutputType(typeof(List<AzureBackupContainer>))]
3232
public class GetAzureBackupContainer : AzureBackupVaultCmdletBase
3333
{
34-
private const string MachineContainerParamSet = "MachineContainer";
35-
private const string ManagedContainerParamSet = "ManagedContainer";
36-
37-
[Parameter(Mandatory = false, ParameterSetName = MachineContainerParamSet, HelpMessage = AzureBackupCmdletHelpMessage.ManagedResourceName)]
38-
[Parameter(Mandatory = false, ParameterSetName = ManagedContainerParamSet, HelpMessage = AzureBackupCmdletHelpMessage.ManagedResourceName)]
34+
[Parameter(Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.ManagedResourceName)]
3935
[ValidateNotNullOrEmpty]
4036
public string Name { get; set; }
4137

42-
[Parameter(Mandatory = true, ParameterSetName = MachineContainerParamSet, HelpMessage = AzureBackupCmdletHelpMessage.ContainerType)]
43-
[Parameter(Mandatory = true, ParameterSetName = ManagedContainerParamSet, HelpMessage = AzureBackupCmdletHelpMessage.ContainerType)]
38+
[Parameter(Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.ContainerType)]
4439
[ValidateNotNullOrEmpty]
4540
public AzureBackupContainerType Type { get; set; }
4641

47-
[Parameter(Mandatory = false, ParameterSetName = ManagedContainerParamSet, HelpMessage = AzureBackupCmdletHelpMessage.ManagedResourceGroupName)]
42+
[Parameter(Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.ManagedResourceGroupName)]
4843
[ValidateNotNullOrEmpty]
4944
public string ManagedResourceGroupName { get; set; }
5045

51-
[Parameter(Mandatory = false, ParameterSetName = ManagedContainerParamSet, HelpMessage = AzureBackupCmdletHelpMessage.ContainerRegistrationStatus)]
46+
[Parameter(Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.ContainerRegistrationStatus)]
5247
[ValidateNotNullOrEmpty]
53-
public AzureBackupContainerStatusInput Status { get; set; }
48+
public AzureBackupContainerRegistrationStatus Status { get; set; }
5449

5550
public override void ExecuteCmdlet()
5651
{
@@ -108,70 +103,40 @@ private List<AzureBackupContainer> GetMachineContainers()
108103

109104
private bool GetManagedContainers(List<AzureBackupContainer> managedContainers)
110105
{
111-
string queryFilterString = string.Empty;
112-
queryFilterString = ConstructQueryFilterString();
113-
var containerList = AzureBackupClient.ListContainers(queryFilterString);
114-
115-
WriteDebug(string.Format("Fetched {0} containers", containerList.Count()));
106+
ContainerQueryParameters parameters = new ContainerQueryParameters();
107+
parameters.ContainerType = ManagedContainerType.IaasVM.ToString();
108+
parameters.FriendlyName = Name;
109+
if (Status != 0)
110+
{
111+
parameters.Status = Status.ToString();
112+
}
116113

117-
List<CSMContainerResponse> containerInfos = containerList.ToList();
114+
List<CSMContainerResponse> containers = new List<CSMContainerResponse>();
115+
containers.AddRange(AzureBackupClient.ListContainers(parameters));
116+
WriteDebug(string.Format("Fetched {0} containers", containers.Count()));
118117

119118
// When resource group name is specified, remove all containers whose resource group name
120119
// doesn't match the given resource group name
121120
if (!string.IsNullOrEmpty(ManagedResourceGroupName))
122121
{
123-
containerInfos.RemoveAll(containerInfo =>
122+
containers.RemoveAll(container =>
124123
{
125-
string rgName = ContainerHelpers.GetRGNameFromId(containerInfo.Properties.ParentContainerId);
124+
string rgName = ContainerHelpers.GetRGNameFromId(container.Properties.ParentContainerId);
126125
return rgName != ManagedResourceGroupName;
127126
});
128-
WriteDebug(string.Format("Count of containers after resource group filter = {0}", containerInfos.Count));
127+
WriteDebug(string.Format("Count of containers after resource group filter = {0}", containers.Count));
129128
}
130129

131130
// TODO: Container friendly name is not captures in Container response
132131
// BUG: Friendly name was previously assigned to ResourceName (vault name)
133-
List<AzureBackupContainer> containers = containerInfos.ConvertAll(containerInfo =>
132+
managedContainers.AddRange(containers.ConvertAll(container =>
134133
{
135-
return new AzureBackupContainer(Vault, containerInfo);
136-
});
137-
managedContainers.AddRange(containers);
134+
return new AzureBackupContainer(Vault, container);
135+
}));
138136

139137
// When container resource name and container resource group name are specified, this parameter set
140138
// identifies a container uniquely. Thus, we return just one container instead of a list.
141139
return !string.IsNullOrEmpty(Name) & !string.IsNullOrEmpty(ManagedResourceGroupName);
142140
}
143-
144-
private string ConstructQueryFilterString()
145-
{
146-
var containerQueryObject = new ListContainerQueryParameter();
147-
148-
switch (Type)
149-
{
150-
case AzureBackupContainerType.AzureVM:
151-
containerQueryObject.ContainerTypeField = ManagedContainerType.IaasVMContainer.ToString();
152-
break;
153-
default:
154-
break;
155-
}
156-
157-
switch (Status)
158-
{
159-
case AzureBackupContainerStatusInput.Registered:
160-
containerQueryObject.ContainerStatusField = AzureBackupContainerRegistrationStatus.Registered.ToString();
161-
break;
162-
case AzureBackupContainerStatusInput.Registering:
163-
containerQueryObject.ContainerStatusField = AzureBackupContainerRegistrationStatus.Registering.ToString();
164-
break;
165-
default:
166-
break;
167-
}
168-
169-
if (!string.IsNullOrEmpty(Name))
170-
{
171-
containerQueryObject.ContainerFriendlyNameField = Name;
172-
}
173-
174-
return ContainerHelpers.GetQueryFilter(containerQueryObject);
175-
}
176141
}
177142
}

src/ResourceManager/AzureBackup/Commands.AzureBackup/Cmdlets/Container/RegisterAzureBackupContainer.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,14 @@ private bool WaitForDiscoveryToComplete(Guid operationId, out bool isDiscoverySu
156156
private bool IsDiscoveryNeeded(string vmName, string rgName, out CSMContainerResponse container)
157157
{
158158
bool isDiscoveryNeed = false;
159-
//First check if container is discoverd or not
160-
ListContainerQueryParameter queryParams = new ListContainerQueryParameter();
161-
queryParams.ContainerTypeField = ManagedContainerType.IaasVMContainer.ToString();
162-
queryParams.ContainerStatusField = String.Empty;
163-
queryParams.ContainerFriendlyNameField = vmName;
164-
string queryString = ContainerHelpers.GetQueryFilter(queryParams);
165-
166-
var containers = AzureBackupClient.ListContainers(queryString);
159+
ContainerQueryParameters parameters = new ContainerQueryParameters()
160+
{
161+
ContainerType = ManagedContainerType.IaasVM.ToString(),
162+
FriendlyName = vmName,
163+
};
164+
165+
//First check if container is discoverd or not
166+
var containers = AzureBackupClient.ListContainers(parameters);
167167
WriteDebug(String.Format("Container count returned from service: {0}.", containers.Count()));
168168
if (containers.Count() == 0)
169169
{

src/ResourceManager/AzureBackup/Commands.AzureBackup/Commands.AzureBackup.csproj

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@
126126
<Compile Include="AzureBackupContainerCmdletBase.cs" />
127127
<Compile Include="AzureBackupDSCmdletBase.cs" />
128128
<Compile Include="AzureBackupItemCmdletBase.cs" />
129-
<Compile Include="AzureBackupPolicyCmdletBase.cs" />
129+
<Compile Include="AzureBackupPolicyCmdletBase.cs">
130+
<SubType>Code</SubType>
131+
</Compile>
130132
<Compile Include="AzureBackUpRestoreBase.cs" />
131133
<Compile Include="AzureBackupVaultCmdletBase.cs" />
132134
<Compile Include="AzureBackupCmdletBase.cs" />
@@ -137,7 +139,9 @@
137139
<Compile Include="Cmdlets\Container\RegisterAzureBackupContainer.cs" />
138140
<Compile Include="Cmdlets\Container\UnregisterAzureBackupContainer.cs" />
139141
<Compile Include="Cmdlets\Item\Disable-AzureBackupProtection .cs" />
140-
<Compile Include="Cmdlets\Item\Enable-AzureBackupProtection .cs" />
142+
<Compile Include="Cmdlets\Item\Enable-AzureBackupProtection .cs">
143+
<SubType>Code</SubType>
144+
</Compile>
141145
<Compile Include="Cmdlets\Item\GetAzureBackupItem.cs" />
142146
<Compile Include="Cmdlets\Jobs\AzureBackupJobHelper.cs" />
143147
<Compile Include="Cmdlets\Jobs\GetAzureBackupJob.cs" />
@@ -147,7 +151,9 @@
147151
<Compile Include="Cmdlets\ProtectionPolicy\GetAzureBackupProtectionPolicy.cs" />
148152
<Compile Include="Cmdlets\ProtectionPolicy\NewAzureBackupProtectionPolicy.cs" />
149153
<Compile Include="Cmdlets\ProtectionPolicy\RemoveAzureBackupProtectionPolicy.cs" />
150-
<Compile Include="Cmdlets\ProtectionPolicy\SetAzureBackupProtectionPolicy.cs" />
154+
<Compile Include="Cmdlets\ProtectionPolicy\SetAzureBackupProtectionPolicy.cs">
155+
<SubType>Code</SubType>
156+
</Compile>
151157
<Compile Include="Cmdlets\RecoveryPoint\GetAzureBackupRecoveryPoint.cs" />
152158
<Compile Include="Cmdlets\Restore\TriggerRestore.cs" />
153159
<Compile Include="Cmdlets\VaultCredentials\GetAzureBackupVaultCredentials.cs" />
@@ -211,9 +217,6 @@
211217
<Name>Commands.Utilities</Name>
212218
</ProjectReference>
213219
</ItemGroup>
214-
<ItemGroup>
215-
<WCFMetadata Include="Service References\" />
216-
</ItemGroup>
217220
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
218221
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
219222
<Import Project="..\..\..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets" Condition="Exists('..\..\..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" />

src/ResourceManager/AzureBackup/Commands.AzureBackup/Models/AzureBackupEnums.cs

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,11 @@ public enum AzureBackupContainerType
2727
AzureVM,
2828
}
2929

30-
public enum AzureBackupContainerStatusInput
31-
{
32-
Registering = 1,
33-
Registered,
34-
}
35-
3630
public enum AzureBackupContainerRegistrationStatus
3731
{
38-
Invalid = 0,
39-
40-
Unknown,
41-
42-
NotRegistered,
43-
44-
Registered,
45-
32+
Registered = 1,
4633
Registering,
34+
NotRegistered,
4735
}
4836

4937
public enum ScheduleType
@@ -86,23 +74,16 @@ public enum AzureBackupOperationErrorCode
8674
DiscoveryInProgress = 410002,
8775
}
8876

89-
public enum ManagedContainerType
77+
public enum WorkloadType
9078
{
9179
Invalid = 0,
92-
93-
Unknown,
94-
95-
// used by fabric adapter to populate discovered VMs
96-
IaasVMContainer,
97-
98-
// used by fabric adapter to populate discovered services
99-
// VMs are child containers of services they belong to
100-
IaasVMServiceContainer
80+
VM = 1
10181
}
10282

103-
public enum WorkloadType
83+
public enum ManagedContainerType
10484
{
10585
Invalid = 0,
106-
VM = 1
86+
IaasVM,
87+
IaasVMService,
10788
}
10889
}

0 commit comments

Comments
 (0)