12
12
// limitations under the License.
13
13
// ----------------------------------------------------------------------------------
14
14
15
+ using BCI = Microsoft . BackupManagementService . CommonInterface ;
16
+ using BMI = Microsoft . BackupManagementService . ManagementInterface ;
17
+ using Microsoft . Azure . Management . BackupServices . Models ;
15
18
using System ;
16
19
using System . Collections . Generic ;
17
20
using System . Linq ;
@@ -27,19 +30,19 @@ namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
27
30
[ Cmdlet ( VerbsCommon . Get , "AzureBackupContainer" ) , OutputType ( typeof ( AzureBackupContainer ) , typeof ( List < AzureBackupContainer > ) ) ]
28
31
public class GetAzureBackupContainer : AzureBackupVaultCmdletBase
29
32
{
30
- [ Parameter ( Position = 2 , Mandatory = false , HelpMessage = AzureBackupCmdletHelpMessage . ContainerName ) ]
33
+ [ Parameter ( Mandatory = false , HelpMessage = AzureBackupCmdletHelpMessage . ContainerResourceGroupName ) ]
31
34
[ ValidateNotNullOrEmpty ]
32
- public string Name { get ; set ; }
35
+ public string ContainerResourceGroupName { get ; set ; }
33
36
34
- [ Parameter ( Position = 2 , Mandatory = false , HelpMessage = AzureBackupCmdletHelpMessage . ContainerId ) ]
37
+ [ Parameter ( Mandatory = false , HelpMessage = AzureBackupCmdletHelpMessage . ContainerResourceName ) ]
35
38
[ ValidateNotNullOrEmpty ]
36
- public string Id { get ; set ; }
39
+ public string ContainerResourceName { get ; set ; }
37
40
38
- [ Parameter ( Position = 2 , Mandatory = false , HelpMessage = AzureBackupCmdletHelpMessage . ContainerRegistrationStatus ) ]
41
+ [ Parameter ( Mandatory = false , HelpMessage = AzureBackupCmdletHelpMessage . ContainerRegistrationStatus ) ]
39
42
[ ValidateNotNullOrEmpty ]
40
43
public AzureBackupContainerStatus Status { get ; set ; }
41
44
42
- [ Parameter ( Position = 2 , Mandatory = false , HelpMessage = AzureBackupCmdletHelpMessage . ContainerType ) ]
45
+ [ Parameter ( Mandatory = false , HelpMessage = AzureBackupCmdletHelpMessage . ContainerType ) ]
43
46
[ ValidateNotNullOrEmpty ]
44
47
public AzureBackupContainerType Type { get ; set ; }
45
48
@@ -49,12 +52,64 @@ public override void ExecuteCmdlet()
49
52
50
53
ExecutionBlock ( ( ) =>
51
54
{
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 ;
53
59
54
- // TODO: Call Hydra
60
+ List < ContainerInfo > containerInfos = listContainerResponse . Objects . ToList ( ) ;
55
61
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
+ } ) ) ;
57
76
} ) ;
58
77
}
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
+ }
59
114
}
60
115
}
0 commit comments