24
24
using System . Threading ;
25
25
using System . Threading . Tasks ;
26
26
using ResourcesNS = Microsoft . Azure . Management . Resources ;
27
+ using Newtonsoft . Json . Linq ;
28
+ using Microsoft . Azure . Commands . RecoveryServices . Backup . Properties ;
27
29
28
30
namespace Microsoft . Azure . Commands . RecoveryServices . Backup . Cmdlets
29
31
{
30
32
[ Cmdlet ( VerbsData . Restore , "AzureRMRecoveryServicesBackupItem" ) , OutputType ( typeof ( AzureRmRecoveryServicesJobBase ) ) ]
31
- class RestoreAzureRMRecoveryServicesBackupItem : RecoveryServicesBackupCmdletBase
33
+ public class RestoreAzureRMRecoveryServicesBackupItem : RecoveryServicesBackupCmdletBase
32
34
{
33
35
[ Parameter ( Mandatory = true , ValueFromPipeline = true , Position = 0 , HelpMessage = ParamHelpMsg . RestoreDisk . RecoveryPoint ) ]
34
36
[ ValidateNotNullOrEmpty ]
@@ -38,56 +40,119 @@ class RestoreAzureRMRecoveryServicesBackupItem : RecoveryServicesBackupCmdletBas
38
40
[ ValidateNotNullOrEmpty ]
39
41
public string StorageAccountName { get ; set ; }
40
42
43
+ [ Parameter ( Mandatory = true , Position = 2 , HelpMessage = ParamHelpMsg . RestoreDisk . StorageAccountResourceGroupName ) ]
44
+ [ ValidateNotNullOrEmpty ]
45
+ public string StorageAccountResourceGroupName { get ; set ; }
46
+
41
47
public override void ExecuteCmdlet ( )
42
48
{
43
49
ExecutionBlock ( ( ) =>
44
50
{
45
51
base . ExecuteCmdlet ( ) ;
46
-
52
+ StorageAccountName = StorageAccountName . ToLower ( ) ;
53
+ WriteDebug ( "InsideRestore. going to create ResourceManager Client" ) ;
47
54
ResourcesNS . ResourceManagementClient rmClient = AzureSession . ClientFactory . CreateClient < ResourcesNS . ResourceManagementClient > ( DefaultContext , AzureEnvironment . Endpoint . ResourceManager ) ;
55
+ WriteDebug ( "Client Created successfully" ) ;
48
56
ResourceIdentity identity = new ResourceIdentity ( ) ;
49
57
identity . ResourceName = StorageAccountName ;
50
- identity . ResourceType = "Microsoft.ClassicStorage" ;
58
+ identity . ResourceProviderNamespace = "Microsoft.ClassicStorage/storageAccounts" ;
59
+ identity . ResourceProviderApiVersion = "2015-12-01" ;
60
+ identity . ResourceType = string . Empty ;
51
61
52
- ResourcesNS . Models . ResourceGetResult resource = rmClient . Resources . GetAsync ( StorageAccountName , identity , CancellationToken . None ) . Result ;
53
- if ( resource == null )
62
+ ResourcesNS . Models . ResourceGetResult resource = null ;
63
+ try
54
64
{
55
- identity . ResourceType = "Microsoft.Storage" ;
56
- resource = rmClient . Resources . GetAsync ( StorageAccountName , identity , CancellationToken . None ) . Result ;
65
+ WriteDebug ( String . Format ( "Query Microsoft.ClassicStorage with name = {0}" , StorageAccountName ) ) ;
66
+ resource = rmClient . Resources . GetAsync ( StorageAccountResourceGroupName , identity , CancellationToken . None ) . Result ;
57
67
}
58
- if ( resource == null )
68
+ catch ( Exception )
59
69
{
60
- throw new ArgumentException ( "Storage account doesnt exists" ) ;
70
+ identity . ResourceProviderNamespace = "Microsoft.Storage/storageAccounts" ;
71
+ identity . ResourceProviderApiVersion = "2016-01-01" ;
72
+ resource = rmClient . Resources . GetAsync ( StorageAccountResourceGroupName , identity , CancellationToken . None ) . Result ;
61
73
}
74
+
75
+ string storageAccountId = resource . Resource . Id ;
76
+ string storageAccountlocation = resource . Resource . Location ;
77
+ string storageAccountType = resource . Resource . Type ;
62
78
63
- string storageId = resource . Resource . Id ;
79
+ //GetStorageResource(StorageAccountName, out storageAccountId, out storageAccountlocation, out storageAccountType) ;
64
80
65
- storageId = StorageAccountName ; //TBD: once service will migrate to storageID we will remove this line;
81
+ WriteDebug ( String . Format ( "StorageId = {0}" , storageAccountId ) ) ;
82
+
83
+ storageAccountId = StorageAccountName ; //TBD: once service will migrate to storageID we will remove this line;
66
84
67
85
PsBackupProviderManager providerManager = new PsBackupProviderManager ( new Dictionary < System . Enum , object > ( )
68
86
{
69
87
{ RestoreBackupItemParams . RecoveryPoint , RecoveryPoint } ,
70
- { RestoreBackupItemParams . StorageAccountId , storageId }
88
+ { RestoreBackupItemParams . StorageAccountId , storageAccountId } ,
89
+ { RestoreBackupItemParams . StorageAccountLocation , storageAccountlocation } ,
90
+ { RestoreBackupItemParams . StorageAccountType , storageAccountType }
71
91
} , HydraAdapter ) ;
72
92
73
93
IPsBackupProvider psBackupProvider = providerManager . GetProviderInstance ( RecoveryPoint . WorkloadType , RecoveryPoint . BackupManagementType ) ;
74
94
var jobResponse = psBackupProvider . TriggerRestore ( ) ;
75
95
96
+ WriteDebug ( String . Format ( "Restore submitted" ) ) ;
76
97
var response = HydraAdapter . GetProtectedItemOperationStatusByURL ( jobResponse . AzureAsyncOperation ) ;
77
98
while ( response . OperationStatus . Status == "InProgress" )
78
99
{
100
+ WriteDebug ( String . Format ( "Restore inProgress" ) ) ;
79
101
response = HydraAdapter . GetProtectedItemOperationStatusByURL ( jobResponse . AzureAsyncOperation ) ;
80
102
System . Threading . Thread . Sleep ( TimeSpan . FromSeconds ( 5 ) ) ;
81
103
}
82
104
83
105
if ( response . OperationStatus . Status == "Completed" )
84
106
{
85
107
// TBD -- Hydra change to add jobId in OperationStatusExtendedInfo
108
+ WriteDebug ( String . Format ( "Restore Completed" ) ) ;
86
109
string jobId = "" ; //response.OperationStatus.Properties.jobId;
87
110
var job = HydraAdapter . GetJob ( jobId ) ;
88
111
//WriteObject(ConversionHelpers.GetJobModel(job));
89
112
}
90
113
} ) ;
91
114
}
115
+
116
+ /// <summary>
117
+ /// This code is not getting used. Will delete it once things will be closed.
118
+ /// </summary>
119
+ /// <param name="storageAccountName"></param>
120
+ /// <param name="id"></param>
121
+ /// <param name="location"></param>
122
+ /// <param name="resourceType"></param>
123
+ internal void GetStorageResource ( string storageAccountName , out string id , out string location , out string resourceType )
124
+ {
125
+ using ( System . Management . Automation . PowerShell ps = System . Management . Automation . PowerShell . Create ( ) )
126
+ {
127
+ //Get-AzureRmResource | Where-Object { ($_.Name -eq "<StorageAccName>") -and ($_.ResourceType -eq "Microsoft.Storage/storageAccounts" -or $_.ResourceType -eq "Microsoft.ClassicStorage/storageAccounts")}
128
+ ps . AddCommand ( "Get-AzureRmResource" ) ;
129
+ ps . AddCommand ( "where-object" ) ;
130
+ string filterString = String . Format ( @"($_.Name -eq ""{0}"") -and ($_.ResourceType -eq ""Microsoft.Storage/storageAccounts"" -or $_.ResourceType -eq ""Microsoft.ClassicStorage/storageAccounts"")" ) ;
131
+ ScriptBlock filter = ScriptBlock . Create ( filterString ) ;
132
+
133
+ ps . AddParameter ( "FilterScript" , filter ) ;
134
+ var result = ps . Invoke ( ) ;
135
+
136
+ if ( ps . HadErrors )
137
+ {
138
+ WriteVerbose ( string . Format ( "Error in Get-AzureRmResource" ) ) ;
139
+ throw new Exception ( ps . HadErrors . ToString ( ) ) ;
140
+ }
141
+
142
+ if ( result . Count == 0 )
143
+ {
144
+ WriteVerbose ( string . Format ( "Storage Account not fount" ) ) ;
145
+ throw new ArgumentException ( Resources . RestoreAzureStorageNotFound ) ;
146
+ }
147
+ else if ( result . Count > 1 )
148
+ {
149
+ WriteVerbose ( string . Format ( "Found more than one StorageAccount with same name. Some thing went wrong" ) ) ;
150
+ throw new Exception ( Resources . RestoreDiskMoreThanOneAccFound ) ;
151
+ }
152
+ id = result [ 0 ] . Members [ "ResourceId" ] . Value . ToString ( ) ;
153
+ location = result [ 0 ] . Members [ "Location" ] . Value . ToString ( ) ;
154
+ resourceType = result [ 0 ] . Members [ "ResourceType" ] . Value . ToString ( ) ;
155
+ }
156
+ }
92
157
}
93
158
}
0 commit comments