18
18
using System . Text ;
19
19
using System . Threading . Tasks ;
20
20
using Microsoft . Azure . Management . RecoveryServices . Backup . Models ;
21
- using Microsoft . Azure . Commands . RecoveryServices . Backup . Cmdlets . Models ;
22
21
using Microsoft . Azure . Commands . RecoveryServices . Backup . Helpers ;
22
+ using Microsoft . Azure . Commands . RecoveryServices . Backup . Cmdlets . Models ;
23
23
24
24
namespace Microsoft . Azure . Commands . RecoveryServices . Backup . Cmdlets . ProviderModel
25
25
{
26
26
public class IaasVmPsBackupProvider : IPsBackupProvider
27
27
{
28
- ProviderData providerData ;
29
- HydraAdapter . HydraAdapter hydraAdapter ;
28
+ ProviderData ProviderData { get ; set ; }
29
+ HydraAdapter . HydraAdapter HydraAdapter { get ; set ; }
30
30
31
31
public void Initialize ( ProviderData providerData , HydraAdapter . HydraAdapter hydraAdapter )
32
32
{
33
- this . providerData = providerData ;
34
- this . hydraAdapter = hydraAdapter ;
33
+ this . ProviderData = providerData ;
34
+ this . HydraAdapter = hydraAdapter ;
35
35
}
36
36
37
37
public BaseRecoveryServicesJobResponse EnableProtection ( )
@@ -66,16 +66,16 @@ public RecoveryPointResponse GetRecoveryPoint()
66
66
67
67
public ProtectionPolicyResponse CreatePolicy ( )
68
68
{
69
- string policyName = ( string ) providerData . ProviderParameters [ PolicyParams . PolicyName ] ;
70
- WorkloadType workloadType = ( WorkloadType ) providerData . ProviderParameters [ PolicyParams . WorkloadType ] ;
71
- BackupManagementType backupManagementType = ( BackupManagementType ) providerData . ProviderParameters [
69
+ string policyName = ( string ) ProviderData . ProviderParameters [ PolicyParams . PolicyName ] ;
70
+ WorkloadType workloadType = ( WorkloadType ) ProviderData . ProviderParameters [ PolicyParams . WorkloadType ] ;
71
+ BackupManagementType backupManagementType = ( BackupManagementType ) ProviderData . ProviderParameters [
72
72
PolicyParams . BackupManagementType ] ;
73
73
AzureRmRecoveryServicesRetentionPolicyBase retentionPolicy = ( AzureRmRecoveryServicesRetentionPolicyBase )
74
- providerData . ProviderParameters [ PolicyParams . RetentionPolicy ] ;
74
+ ProviderData . ProviderParameters [ PolicyParams . RetentionPolicy ] ;
75
75
AzureRmRecoveryServicesSchedulePolicyBase schedulePolicy = ( AzureRmRecoveryServicesSchedulePolicyBase )
76
- providerData . ProviderParameters [ PolicyParams . SchedulePolicy ] ;
77
- string resourceName = ( string ) providerData . ProviderParameters [ PolicyParams . ResourceName ] ;
78
- string resourceGroupName = ( string ) providerData . ProviderParameters [ PolicyParams . ResourceGroupName ] ;
76
+ ProviderData . ProviderParameters [ PolicyParams . SchedulePolicy ] ;
77
+ string resourceName = ( string ) ProviderData . ProviderParameters [ PolicyParams . ResourceName ] ;
78
+ string resourceGroupName = ( string ) ProviderData . ProviderParameters [ PolicyParams . ResourceGroupName ] ;
79
79
80
80
81
81
// do validations
@@ -84,7 +84,7 @@ public ProtectionPolicyResponse CreatePolicy()
84
84
// validate both RetentionPolicy and SchedulePolicy
85
85
ValidateAzureVMRetentionPolicy ( retentionPolicy ) ;
86
86
ValidateAzureVMSchedulePolicy ( schedulePolicy ) ;
87
-
87
+
88
88
// construct Hydra policy request
89
89
ProtectionPolicyRequest hydraRequest = new ProtectionPolicyRequest ( )
90
90
{
@@ -100,29 +100,29 @@ public ProtectionPolicyResponse CreatePolicy()
100
100
}
101
101
} ;
102
102
103
- return hydraAdapter . CreateOrUpdateProtectionPolicy (
103
+ return HydraAdapter . CreateOrUpdateProtectionPolicy (
104
104
resourceGroupName ,
105
105
resourceName ,
106
106
policyName ,
107
107
hydraRequest ) ;
108
108
}
109
109
110
110
public List < AzureRmRecoveryServicesJobBase > ModifyPolicy ( )
111
- {
111
+ {
112
112
AzureRmRecoveryServicesRetentionPolicyBase retentionPolicy = ( AzureRmRecoveryServicesRetentionPolicyBase )
113
- providerData . ProviderParameters [ PolicyParams . RetentionPolicy ] ;
113
+ ProviderData . ProviderParameters [ PolicyParams . RetentionPolicy ] ;
114
114
AzureRmRecoveryServicesSchedulePolicyBase schedulePolicy = ( AzureRmRecoveryServicesSchedulePolicyBase )
115
- providerData . ProviderParameters [ PolicyParams . SchedulePolicy ] ;
115
+ ProviderData . ProviderParameters [ PolicyParams . SchedulePolicy ] ;
116
116
AzureRmRecoveryServicesPolicyBase policy = ( AzureRmRecoveryServicesPolicyBase )
117
- providerData . ProviderParameters [ PolicyParams . ProtectionPolicy ] ;
118
- string resourceName = ( string ) providerData . ProviderParameters [ PolicyParams . ResourceName ] ;
119
- string resourceGroupName = ( string ) providerData . ProviderParameters [ PolicyParams . ResourceGroupName ] ;
120
-
117
+ ProviderData . ProviderParameters [ PolicyParams . ProtectionPolicy ] ;
118
+ string resourceName = ( string ) ProviderData . ProviderParameters [ PolicyParams . ResourceName ] ;
119
+ string resourceGroupName = ( string ) ProviderData . ProviderParameters [ PolicyParams . ResourceGroupName ] ;
120
+
121
121
// do validations
122
122
ValidateAzureVMProtectionPolicy ( policy ) ;
123
-
123
+
124
124
// RetentionPolicy and SchedulePolicy both should not be empty
125
- if ( retentionPolicy == null && schedulePolicy == null )
125
+ if ( retentionPolicy == null && schedulePolicy == null )
126
126
{
127
127
throw new ArgumentException ( "Both RetentionPolicy and SchedulePolicy are Empty .. nothing to update" ) ;
128
128
}
@@ -132,12 +132,12 @@ public List<AzureRmRecoveryServicesJobBase> ModifyPolicy()
132
132
{
133
133
ValidateAzureVMSchedulePolicy ( schedulePolicy ) ;
134
134
( ( AzureRmRecoveryServicesIaasVmPolicy ) policy ) . SchedulePolicy = schedulePolicy ;
135
- }
135
+ }
136
136
if ( retentionPolicy != null )
137
137
{
138
138
ValidateAzureVMRetentionPolicy ( retentionPolicy ) ;
139
- ( ( AzureRmRecoveryServicesIaasVmPolicy ) policy ) . RetentionPolicy = retentionPolicy ;
140
- }
139
+ ( ( AzureRmRecoveryServicesIaasVmPolicy ) policy ) . RetentionPolicy = retentionPolicy ;
140
+ }
141
141
142
142
// Now validate both RetentionPolicy and SchedulePolicy matches or not
143
143
PolicyHelpers . ValidateLongTermRetentionPolicyWithSimpleRetentionPolicy (
@@ -151,15 +151,15 @@ public List<AzureRmRecoveryServicesJobBase> ModifyPolicy()
151
151
{
152
152
Properties = new AzureIaaSVMProtectionPolicy ( )
153
153
{
154
- RetentionPolicy = PolicyHelpers . GetHydraLongTermRetentionPolicy (
154
+ RetentionPolicy = PolicyHelpers . GetHydraLongTermRetentionPolicy (
155
155
( AzureRmRecoveryServicesLongTermRetentionPolicy ) ( ( AzureRmRecoveryServicesIaasVmPolicy ) policy ) . RetentionPolicy ) ,
156
- SchedulePolicy = PolicyHelpers . GetHydraSimpleSchedulePolicy (
156
+ SchedulePolicy = PolicyHelpers . GetHydraSimpleSchedulePolicy (
157
157
( AzureRmRecoveryServicesSimpleSchedulePolicy ) ( ( AzureRmRecoveryServicesIaasVmPolicy ) policy ) . SchedulePolicy )
158
- }
158
+ }
159
159
}
160
160
} ;
161
-
162
- ProtectionPolicyResponse response = hydraAdapter . CreateOrUpdateProtectionPolicy (
161
+
162
+ ProtectionPolicyResponse response = HydraAdapter . CreateOrUpdateProtectionPolicy (
163
163
resourceGroupName ,
164
164
resourceName ,
165
165
policy . Name ,
@@ -181,10 +181,52 @@ public List<AzureRmRecoveryServicesJobBase> ModifyPolicy()
181
181
return jobsList ;
182
182
}
183
183
184
+ public List < AzureRmRecoveryServicesContainerBase > ListProtectionContainers ( )
185
+ {
186
+ string name = ( string ) this . ProviderData . ProviderParameters [ ContainerParams . Name ] ;
187
+ ContainerRegistrationStatus status = ( ContainerRegistrationStatus ) this . ProviderData . ProviderParameters [ ContainerParams . Status ] ;
188
+ ARSVault vault = ( ARSVault ) this . ProviderData . ProviderParameters [ ContainerParams . Vault ] ;
189
+ string resourceGroupName = ( string ) this . ProviderData . ProviderParameters [ ContainerParams . ResourceGroupName ] ;
190
+
191
+ ProtectionContainerListQueryParams queryParams = new ProtectionContainerListQueryParams ( ) ;
192
+
193
+ // 1. Filter by Name
194
+ queryParams . FriendlyName = name ;
195
+
196
+ // 2. Filter by ContainerType
197
+ queryParams . ProviderType = ProviderType . AzureIaasVM . ToString ( ) ;
198
+
199
+ // 3. Filter by Status
200
+ queryParams . RegistrationStatus = status . ToString ( ) ;
201
+
202
+ var listResponse = HydraAdapter . ListContainers ( vault . ResouceGroupName , vault . Name , queryParams ) ;
203
+
204
+ List < AzureRmRecoveryServicesContainerBase > containerModels = ConversionHelpers . GetContainerModelList ( listResponse ) ;
205
+
206
+ // 4. Filter by RG Name
207
+ if ( ! string . IsNullOrEmpty ( resourceGroupName ) )
208
+ {
209
+ containerModels = containerModels . Where ( containerModel =>
210
+ ( containerModel as AzureRmRecoveryServicesIaasVmContainer ) . ResourceGroupName == resourceGroupName ) . ToList ( ) ;
211
+ }
212
+
213
+ return containerModels ;
214
+ }
215
+
216
+ public ProtectionPolicyResponse GetPolicy ( )
217
+ {
218
+ throw new NotImplementedException ( ) ;
219
+ }
220
+
221
+ public void DeletePolicy ( )
222
+ {
223
+ throw new NotImplementedException ( ) ;
224
+ }
225
+
184
226
#region private
185
227
private void ValidateAzureVMWorkloadType ( WorkloadType type )
186
228
{
187
- if ( type != WorkloadType . AzureVM )
229
+ if ( type != WorkloadType . AzureVM )
188
230
{
189
231
throw new ArgumentException ( "ExpectedWorkloadType = " + type . ToString ( ) ) ;
190
232
}
@@ -220,11 +262,10 @@ private void ValidateAzureVMRetentionPolicy(AzureRmRecoveryServicesRetentionPoli
220
262
{
221
263
throw new ArgumentException ( "RetentionPolicy is NULL or not of type AzureRmRecoveryServicesLongTermRetentionPolicy" ) ;
222
264
}
223
-
265
+
224
266
// call validation
225
267
policy . Validate ( ) ;
226
268
}
227
-
228
269
#endregion
229
270
}
230
271
}
0 commit comments