18
18
using System . Xml ;
19
19
using System . Linq ;
20
20
using Microsoft . Azure . Management . BackupServices . Models ;
21
+ using System . Runtime . Serialization ;
21
22
22
23
namespace Microsoft . Azure . Commands . AzureBackup . Cmdlets
23
24
{
@@ -27,9 +28,9 @@ namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
27
28
[ Cmdlet ( VerbsCommon . Get , "AzureBackupItem" ) , OutputType ( typeof ( AzureBackupItem ) , typeof ( List < AzureBackupItem > ) ) ]
28
29
public class GetAzureBackupItem : AzureBackupContainerCmdletBase
29
30
{
30
- [ Parameter ( Position = 2 , Mandatory = false , HelpMessage = AzureBackupCmdletHelpMessage . PolicyName ) ]
31
+ [ Parameter ( Position = 2 , Mandatory = false , HelpMessage = AzureBackupCmdletHelpMessage . ProtectionStatus ) ]
31
32
[ ValidateNotNullOrEmpty ]
32
- public string Id { get ; set ; }
33
+ public protectionStatus ProtectionStatus { get ; set ; }
33
34
34
35
public override void ExecuteCmdlet ( )
35
36
{
@@ -39,23 +40,38 @@ public override void ExecuteCmdlet()
39
40
{
40
41
WriteVerbose ( "Making client call" ) ;
41
42
42
- var azureBackupDatasourceListResponse = AzureBackupClient . DataSource . ListAsync ( GetCustomRequestHeaders ( ) , CmdletCancellationToken ) . Result ;
43
- var azureBackupPOListResponse = AzureBackupClient . ProtectableObject . ListAsync ( GetCustomRequestHeaders ( ) , CmdletCancellationToken ) . Result ;
43
+ DataSourceQueryParameter dsQueryParameter = new DataSourceQueryParameter ( )
44
+ {
45
+ ContainerName = AzureBackupContainer . ContainerName
46
+ } ;
47
+
48
+ POQueryParameter pOQueryParameter = new POQueryParameter ( )
49
+ {
50
+ Status = this . ProtectionStatus . ToString ( )
51
+ } ;
52
+
53
+ var azureBackupDatasourceListResponse = AzureBackupClient . DataSource . ListAsync ( dsQueryParameter , GetCustomRequestHeaders ( ) , CmdletCancellationToken ) . Result ;
54
+ var azureBackupPOListResponse = AzureBackupClient . ProtectableObject . ListAsync ( pOQueryParameter , GetCustomRequestHeaders ( ) , CmdletCancellationToken ) . Result ;
44
55
45
56
WriteVerbose ( "Received policy response" ) ;
46
57
WriteVerbose ( "Received policy response2" ) ;
47
- IEnumerable < DataSourceInfo > azureBackupDatasourceObjects = null ;
48
- IEnumerable < ProtectableObjectInfo > azureBackupPOObjects = null ;
58
+ List < DataSourceInfo > azureBackupDatasourceObjects = null ;
59
+ List < ProtectableObjectInfo > azureBackupPOObjects = null ;
49
60
50
- if ( Id != null )
51
- {
52
- azureBackupDatasourceObjects = azureBackupDatasourceListResponse . DataSources . Objects . Where ( x => x . InstanceId . Equals ( Id , System . StringComparison . InvariantCultureIgnoreCase ) ) ;
53
- azureBackupPOObjects = azureBackupPOListResponse . ProtectableObject . Objects . Where ( x => x . InstanceId . Equals ( Id , System . StringComparison . InvariantCultureIgnoreCase ) ) ;
54
- }
55
- else
61
+ azureBackupDatasourceObjects = azureBackupDatasourceListResponse . DataSources . Objects . ToList ( ) ;
62
+ azureBackupPOObjects = azureBackupPOListResponse . ProtectableObject . Objects . Where ( x => x . ContainerName . Equals ( AzureBackupContainer . ContainerName , System . StringComparison . InvariantCultureIgnoreCase ) ) . ToList ( ) ;
63
+
64
+ //If user has stopped protection for some datasoure then we will have duplicate items(po and ds).
65
+ //So in this case removing poitem.
66
+ foreach ( var DSItem in azureBackupDatasourceObjects )
56
67
{
57
- azureBackupDatasourceObjects = azureBackupDatasourceListResponse . DataSources . Objects ;
58
- azureBackupPOObjects = azureBackupPOListResponse . ProtectableObject . Objects ;
68
+ foreach ( var POItem in azureBackupPOObjects )
69
+ {
70
+ if ( DSItem . ProtectableObjectName == POItem . Name )
71
+ {
72
+ azureBackupPOObjects . Remove ( POItem ) ;
73
+ }
74
+ }
59
75
}
60
76
61
77
WriteVerbose ( "Converting response" ) ;
@@ -68,7 +84,7 @@ public void WriteAzureBackupProtectionPolicy(DataSourceInfo sourceItem, AzureBac
68
84
this . WriteObject ( new AzureBackupItem ( sourceItem , azureBackupItem ) ) ;
69
85
}
70
86
71
- public void WriteAzureBackupProtectionPolicy ( IEnumerable < DataSourceInfo > sourceDataSourceList , IEnumerable < ProtectableObjectInfo > sourcePOList , AzureBackupContainer azureBackupContainer )
87
+ public void WriteAzureBackupProtectionPolicy ( List < DataSourceInfo > sourceDataSourceList , List < ProtectableObjectInfo > sourcePOList , AzureBackupContainer azureBackupContainer )
72
88
{
73
89
List < AzureBackupItem > targetList = new List < AzureBackupItem > ( ) ;
74
90
@@ -85,5 +101,13 @@ public void WriteAzureBackupProtectionPolicy(IEnumerable<DataSourceInfo> sourceD
85
101
this . WriteObject ( targetList , true ) ;
86
102
}
87
103
104
+ public enum protectionStatus
105
+ {
106
+ [ EnumMember ]
107
+ NotProtected = 0 ,
108
+
109
+ [ EnumMember ]
110
+ Protected ,
111
+ }
88
112
}
89
113
}
0 commit comments