@@ -30,15 +30,19 @@ namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
30
30
[ Cmdlet ( VerbsCommon . Get , "AzureBackupContainer" ) , OutputType ( typeof ( AzureBackupContainer ) , typeof ( List < AzureBackupContainer > ) ) ]
31
31
public class GetAzureBackupContainer : AzureBackupVaultCmdletBase
32
32
{
33
- [ Parameter ( Position = 2 , Mandatory = false , HelpMessage = AzureBackupCmdletHelpMessage . ContainerName ) ]
33
+ [ Parameter ( Mandatory = false , HelpMessage = AzureBackupCmdletHelpMessage . ContainerResourceGroupName ) ]
34
34
[ ValidateNotNullOrEmpty ]
35
- public string VirtualMachine { get ; set ; }
35
+ public string ContainerResourceGroupName { get ; set ; }
36
36
37
- [ Parameter ( Position = 2 , Mandatory = false , HelpMessage = AzureBackupCmdletHelpMessage . ContainerRegistrationStatus ) ]
37
+ [ Parameter ( Mandatory = false , HelpMessage = AzureBackupCmdletHelpMessage . ContainerResourceName ) ]
38
+ [ ValidateNotNullOrEmpty ]
39
+ public string ContainerResourceName { get ; set ; }
40
+
41
+ [ Parameter ( Mandatory = false , HelpMessage = AzureBackupCmdletHelpMessage . ContainerRegistrationStatus ) ]
38
42
[ ValidateNotNullOrEmpty ]
39
43
public AzureBackupContainerStatus Status { get ; set ; }
40
44
41
- [ Parameter ( Position = 2 , Mandatory = false , HelpMessage = AzureBackupCmdletHelpMessage . ContainerType ) ]
45
+ [ Parameter ( Mandatory = false , HelpMessage = AzureBackupCmdletHelpMessage . ContainerType ) ]
42
46
[ ValidateNotNullOrEmpty ]
43
47
public AzureBackupContainerType Type { get ; set ; }
44
48
@@ -49,74 +53,79 @@ public override void ExecuteCmdlet()
49
53
ExecutionBlock ( ( ) =>
50
54
{
51
55
string queryFilterString = string . Empty ;
52
- // TODO: Construct query filter string based on input filters.
53
- //queryFilterString = ConstructQueryFilterString();
54
-
56
+ queryFilterString = ConstructQueryFilterString ( ) ;
55
57
ListContainerResponse listContainerResponse = AzureBackupClient . Container . ListAsync ( queryFilterString ,
56
58
GetCustomRequestHeaders ( ) , CmdletCancellationToken ) . Result ;
57
59
58
- IEnumerable < AzureBackupContainer > containers = listContainerResponse . Objects . ToList ( ) . ConvertAll ( containerInfo =>
60
+ WriteVerbose ( string . Format ( "# of fetched containers = {0}" , listContainerResponse . Objects . Count ) ) ;
61
+
62
+ List < ContainerInfo > containerInfos = listContainerResponse . Objects . ToList ( ) ;
63
+
64
+ // When resource group name is specified, remove all containers whose resource group name
65
+ // doesn't match the given resource group name
66
+ if ( ! string . IsNullOrEmpty ( ContainerResourceGroupName ) )
59
67
{
60
- return new AzureBackupContainer ( )
68
+ containerInfos . RemoveAll ( containerInfo =>
61
69
{
62
- ContainerType = containerInfo . ContainerType ,
63
- FriendlyName = containerInfo . FriendlyName ,
64
- HealthStatus = containerInfo . HealthStatus ,
65
- InstanceId = containerInfo . InstanceId ,
66
- Name = containerInfo . Name ,
67
- ParentContainerFriendlyName = containerInfo . ParentContainerFriendlyName ,
68
- ParentContainerName = containerInfo . ParentContainerName ,
69
- RegistrationStatus = containerInfo . RegistrationStatus ,
70
- ResourceGroupName = ResourceGroupName ,
71
- ResourceName = ResourceName ,
72
- } ;
70
+ return containerInfo . ParentContainerName != ContainerResourceGroupName ;
71
+ } ) ;
72
+ }
73
+
74
+ WriteVerbose ( string . Format ( "# of containers after resource group filter = {0}" , listContainerResponse . Objects . Count ) ) ;
75
+
76
+ List < AzureBackupContainer > containers = containerInfos . ConvertAll ( containerInfo =>
77
+ {
78
+ return new AzureBackupContainer ( containerInfo , ResourceGroupName , ResourceName , Location ) ;
73
79
} ) ;
74
80
75
- WriteObject ( containers ) ;
81
+ if ( ! string . IsNullOrEmpty ( ResourceName ) & ! string . IsNullOrEmpty ( ResourceGroupName ) )
82
+ {
83
+ if ( containers . Any ( ) )
84
+ {
85
+ WriteObject ( containers . First ( ) ) ;
86
+ }
87
+ }
88
+ else
89
+ {
90
+ WriteObject ( containers ) ;
91
+ }
76
92
} ) ;
77
93
}
78
94
79
95
private string ConstructQueryFilterString ( )
80
96
{
81
- string queryFilterString = string . Empty ;
82
97
BMI . ContainerQueryObject containerQueryObject = new BMI . ContainerQueryObject ( ) ;
83
98
84
- if ( Type != null )
99
+ switch ( Type )
85
100
{
86
- switch ( Type )
87
- {
88
- case AzureBackupContainerType . AzureVirtualMachine :
89
- containerQueryObject . Type = BCI . ContainerType . IaasVMContainer . ToString ( ) ;
90
- break ;
91
- default :
92
- break ;
93
- }
101
+ case AzureBackupContainerType . AzureVirtualMachine :
102
+ containerQueryObject . Type = BCI . ContainerType . IaasVMContainer . ToString ( ) ;
103
+ break ;
104
+ default :
105
+ break ;
94
106
}
95
107
96
- if ( Status != null )
108
+ switch ( Status )
97
109
{
98
- switch ( Status )
99
- {
100
- case AzureBackupContainerStatus . Registered :
101
- containerQueryObject . Status = BCI . RegistrationStatus . Registered . ToString ( ) ;
102
- break ;
103
- case AzureBackupContainerStatus . Registering :
104
- containerQueryObject . Status = BCI . RegistrationStatus . Registering . ToString ( ) ;
105
- break ;
106
- case AzureBackupContainerStatus . NotRegistered :
107
- containerQueryObject . Status = BCI . RegistrationStatus . NotRegistered . ToString ( ) ;
108
- break ;
109
- default :
110
- break ;
111
- }
110
+ case AzureBackupContainerStatus . Registered :
111
+ containerQueryObject . Status = BCI . RegistrationStatus . Registered . ToString ( ) ;
112
+ break ;
113
+ case AzureBackupContainerStatus . Registering :
114
+ containerQueryObject . Status = BCI . RegistrationStatus . Registering . ToString ( ) ;
115
+ break ;
116
+ case AzureBackupContainerStatus . NotRegistered :
117
+ containerQueryObject . Status = BCI . RegistrationStatus . NotRegistered . ToString ( ) ;
118
+ break ;
119
+ default :
120
+ break ;
112
121
}
113
122
114
- if ( VirtualMachine != null )
123
+ if ( ! string . IsNullOrEmpty ( ContainerResourceName ) )
115
124
{
116
-
125
+ containerQueryObject . FriendlyName = ContainerResourceName ;
117
126
}
118
127
119
- return queryFilterString ;
128
+ return BMI . BackupManagementAPIHelper . GetQueryString ( containerQueryObject . GetNameValueCollection ( ) ) ;
120
129
}
121
130
}
122
131
}
0 commit comments