1212// limitations under the License.
1313// ----------------------------------------------------------------------------------
1414
15+ using BCI = Microsoft . BackupManagementService . CommonInterface ;
16+ using BMI = Microsoft . BackupManagementService . ManagementInterface ;
17+ using Microsoft . Azure . Management . BackupServices . Models ;
1518using System ;
1619using System . Collections . Generic ;
1720using System . Linq ;
@@ -27,19 +30,19 @@ namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
2730 [ Cmdlet ( VerbsCommon . Get , "AzureBackupContainer" ) , OutputType ( typeof ( AzureBackupContainer ) , typeof ( List < AzureBackupContainer > ) ) ]
2831 public class GetAzureBackupContainer : AzureBackupVaultCmdletBase
2932 {
30- [ Parameter ( Position = 2 , Mandatory = false , HelpMessage = AzureBackupCmdletHelpMessage . ContainerName ) ]
33+ [ Parameter ( Mandatory = false , HelpMessage = AzureBackupCmdletHelpMessage . ContainerResourceGroupName ) ]
3134 [ ValidateNotNullOrEmpty ]
32- public string Name { get ; set ; }
35+ public string ContainerResourceGroupName { get ; set ; }
3336
34- [ Parameter ( Position = 2 , Mandatory = false , HelpMessage = AzureBackupCmdletHelpMessage . ContainerId ) ]
37+ [ Parameter ( Mandatory = false , HelpMessage = AzureBackupCmdletHelpMessage . ContainerResourceName ) ]
3538 [ ValidateNotNullOrEmpty ]
36- public string Id { get ; set ; }
39+ public string ContainerResourceName { get ; set ; }
3740
38- [ Parameter ( Position = 2 , Mandatory = false , HelpMessage = AzureBackupCmdletHelpMessage . ContainerRegistrationStatus ) ]
41+ [ Parameter ( Mandatory = false , HelpMessage = AzureBackupCmdletHelpMessage . ContainerRegistrationStatus ) ]
3942 [ ValidateNotNullOrEmpty ]
4043 public AzureBackupContainerStatus Status { get ; set ; }
4144
42- [ Parameter ( Position = 2 , Mandatory = false , HelpMessage = AzureBackupCmdletHelpMessage . ContainerType ) ]
45+ [ Parameter ( Mandatory = false , HelpMessage = AzureBackupCmdletHelpMessage . ContainerType ) ]
4346 [ ValidateNotNullOrEmpty ]
4447 public AzureBackupContainerType Type { get ; set ; }
4548
@@ -49,12 +52,64 @@ public override void ExecuteCmdlet()
4952
5053 ExecutionBlock ( ( ) =>
5154 {
52- IEnumerable < AzureBackupContainer > containers = new List < AzureBackupContainer > ( ) ;
55+ string queryFilterString = string . Empty ;
56+ queryFilterString = ConstructQueryFilterString ( ) ;
57+ ListContainerResponse listContainerResponse = AzureBackupClient . Container . ListAsync ( queryFilterString ,
58+ GetCustomRequestHeaders ( ) , CmdletCancellationToken ) . Result ;
5359
54- // TODO: Call Hydra
60+ List < ContainerInfo > containerInfos = listContainerResponse . Objects . ToList ( ) ;
5561
56- WriteObject ( containers ) ;
62+ // When resource group name is specified, remove all containers whose resource group name
63+ // doesn't match the given resource group name
64+ if ( ! string . IsNullOrEmpty ( ContainerResourceGroupName ) )
65+ {
66+ containerInfos . RemoveAll ( containerInfo =>
67+ {
68+ return containerInfo . ParentContainerName != ContainerResourceGroupName ;
69+ } ) ;
70+ }
71+
72+ WriteObject ( containerInfos . ConvertAll ( containerInfo =>
73+ {
74+ return new AzureBackupContainer ( containerInfo ) ;
75+ } ) ) ;
5776 } ) ;
5877 }
78+
79+ private string ConstructQueryFilterString ( )
80+ {
81+ BMI . ContainerQueryObject containerQueryObject = new BMI . ContainerQueryObject ( ) ;
82+
83+ switch ( Type )
84+ {
85+ case AzureBackupContainerType . AzureVirtualMachine :
86+ containerQueryObject . Type = BCI . ContainerType . IaasVMContainer . ToString ( ) ;
87+ break ;
88+ default :
89+ break ;
90+ }
91+
92+ switch ( Status )
93+ {
94+ case AzureBackupContainerStatus . Registered :
95+ containerQueryObject . Status = BCI . RegistrationStatus . Registered . ToString ( ) ;
96+ break ;
97+ case AzureBackupContainerStatus . Registering :
98+ containerQueryObject . Status = BCI . RegistrationStatus . Registering . ToString ( ) ;
99+ break ;
100+ case AzureBackupContainerStatus . NotRegistered :
101+ containerQueryObject . Status = BCI . RegistrationStatus . NotRegistered . ToString ( ) ;
102+ break ;
103+ default :
104+ break ;
105+ }
106+
107+ if ( ! string . IsNullOrEmpty ( ContainerResourceName ) )
108+ {
109+ containerQueryObject . FriendlyName = ContainerResourceName ;
110+ }
111+
112+ return BMI . BackupManagementAPIHelper . GetQueryString ( containerQueryObject . GetNameValueCollection ( ) ) ;
113+ }
59114 }
60115}
0 commit comments