Skip to content

Commit 50e7d8b

Browse files
author
Samuel Anudeep
committed
Infra part 1
1 parent b70b56c commit 50e7d8b

File tree

11 files changed

+157
-47
lines changed

11 files changed

+157
-47
lines changed

src/ResourceManager/RecoveryServices.Backup/Commands.RecoveryServices.Backup.Helpers/TrackingHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class TrackingHelpers
3838
/// <returns></returns>
3939
public static BackUpOperationStatusResponse WaitForOperationCompletionUsingStatusLink(
4040
string statusUrlLink,
41-
Func<string, BackUpOperationStatusResponse> serviceClientMethod)
41+
Func<string, > serviceClientMethod)
4242
{
4343
// using this directly because it doesn't matter which function we use.
4444
// return type is same and currently we are using it in only two places.

src/ResourceManager/RecoveryServices.Backup/Commands.RecoveryServices.Backup.ServiceClientAdapter/BMSAPIs/ItemAPIs.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ public Microsoft.Rest.Azure.AzureOperationResponse CreateOrUpdateProtectedItem(
4444
containerName,
4545
protectedItemName,
4646
request,
47-
BmsAdapter.GetCustomRequestHeaders(),
48-
BmsAdapter.CmdletCancellationToken).Result;
47+
cancellationToken: BmsAdapter.CmdletCancellationToken).Result;
4948
}
5049

5150
/// <summary>
@@ -67,8 +66,7 @@ public Microsoft.Rest.Azure.AzureOperationResponse DeleteProtectedItem(
6766
AzureFabricName,
6867
containerName,
6968
protectedItemName,
70-
BmsAdapter.GetCustomRequestHeaders(),
71-
BmsAdapter.CmdletCancellationToken).Result;
69+
cancellationToken: BmsAdapter.CmdletCancellationToken).Result;
7270
}
7371

7472
/// <summary>
@@ -93,8 +91,7 @@ public Microsoft.Rest.Azure.AzureOperationResponse<ProtectedItemResource> GetPro
9391
containerName,
9492
protectedItemName,
9593
queryFilter,
96-
BmsAdapter.GetCustomRequestHeaders(),
97-
BmsAdapter.CmdletCancellationToken).Result;
94+
cancellationToken: BmsAdapter.CmdletCancellationToken).Result;
9895
}
9996

10097
/// <summary>

src/ResourceManager/RecoveryServices.Backup/Commands.RecoveryServices.Backup.ServiceClientAdapter/ClientProxy.cs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
using Hyak.Common;
1616
using Microsoft.Azure.Commands.Common.Authentication;
17+
using Microsoft.Azure.Commands.Common.Authentication.Models;
18+
using AutoRestNS = Microsoft.Rest;
1719
using System;
1820
using System.Collections.Generic;
1921
using System.Linq;
@@ -22,47 +24,41 @@
2224

2325
namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.ServiceClientAdapterNS
2426
{
25-
public partial class ClientProxy<TClient, THeader> : ClientProxyBase
26-
where TClient : ServiceClient<TClient>
27+
public partial class ClientProxy<TClient> : ClientProxyBase
28+
where TClient : AutoRestNS.ServiceClient<TClient>
2729
{
2830
/// <summary>
2931
/// Client to talk to backend service
3032
/// </summary>
3133
private TClient client;
3234

33-
/// <summary>
34-
/// Delegate action to generate custom request headers
35-
/// </summary>
36-
private Func<string, THeader> CustomRequestHeaderGenerator;
37-
3835
/// <summary>
3936
/// Get Recovery Services Backup service client.
4037
/// </summary>
4138
public TClient Client
4239
{
4340
get
4441
{
45-
if (this.client == null)
42+
if (client == null)
4643
{
47-
this.client = AzureSession.ClientFactory.CreateCustomClient<TClient>(Parameters);
44+
client = AzureSession.ClientFactory.CreateArmClient<TClient>(Context, AzureEnvironment.Endpoint.ResourceManager);
4845
}
4946

50-
return this.client;
47+
return client;
5148
}
5249
}
5350

54-
public ClientProxy(Func<string, THeader> headerGenerator, params object[] parameters)
55-
: base(parameters)
51+
public ClientProxy(AzureContext context)
52+
: base(context)
5653
{
57-
CustomRequestHeaderGenerator = headerGenerator;
5854
}
5955

60-
/// <summary>
61-
/// Gets customer request headers
62-
/// </summary>
63-
public THeader GetCustomRequestHeaders()
56+
internal Dictionary<string, List<string>> GetCustomRequestHeaders()
6457
{
65-
return CustomRequestHeaderGenerator(this.ClientRequestId);
58+
Dictionary<string, List<string>> dict = new Dictionary<string, List<string>>()
59+
{
60+
"x-ms-client-request-id"
61+
};
6662
}
6763
}
6864
}

src/ResourceManager/RecoveryServices.Backup/Commands.RecoveryServices.Backup.ServiceClientAdapter/ClientProxyBase.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@
2121
using System.Threading.Tasks;
2222
using Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.Models;
2323
using Microsoft.Azure.Commands.RecoveryServices.Backup.Properties;
24+
using Microsoft.Azure.Commands.Common.Authentication.Models;
2425

2526
namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.ServiceClientAdapterNS
2627
{
2728
public class ClientProxyBase
2829
{
29-
protected object[] Parameters;
30+
protected AzureContext Context;
3031

3132
/// <summary>
3233
/// Client request id.
@@ -39,9 +40,9 @@ public class ClientProxyBase
3940
private CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
4041
public CancellationToken CmdletCancellationToken;
4142

42-
public ClientProxyBase(params object[] parameters)
43+
public ClientProxyBase(AzureContext context)
4344
{
44-
Parameters = parameters;
45+
Context = context;
4546

4647
RefreshClientRequestId();
4748

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Net.Http;
5+
using System.Text;
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
9+
namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.ServiceClientAdapterNS
10+
{
11+
public class ClientRequestIdHandler : DelegatingHandler, ICloneable
12+
{
13+
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
14+
{
15+
request.Headers.TryAddWithoutValidation("x-ms-client-request-id", Guid.NewGuid().ToString());
16+
17+
return base.SendAsync(request, cancellationToken);
18+
}
19+
20+
public object Clone()
21+
{
22+
return new ClientRequestIdHandler();
23+
}
24+
}
25+
}

src/ResourceManager/RecoveryServices.Backup/Commands.RecoveryServices.Backup.ServiceClientAdapter/Commands.RecoveryServices.Backup.ServiceClientAdapter.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@
7272
<Compile Include="BMSAPIs\RestoreDiskAPIs.cs" />
7373
<Compile Include="ClientProxy.cs" />
7474
<Compile Include="ClientProxyBase.cs" />
75+
<Compile Include="CultureHandler.cs" />
7576
<Compile Include="CommonHelpers.cs" />
77+
<Compile Include="ClientRequestIdHandler.cs" />
78+
<Compile Include="DelegatingHandler.cs" />
7679
<Compile Include="ServiceClientAdapter.cs" />
7780
<Compile Include="Properties\AssemblyInfo.cs" />
7881
</ItemGroup>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Net.Http;
5+
using System.Text;
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
9+
namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.ServiceClientAdapterNS
10+
{
11+
public class CultureHandler : DelegatingHandler, ICloneable
12+
{
13+
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
14+
{
15+
request.Headers.TryAddWithoutValidation("Accept-Language", "en-US");
16+
17+
return base.SendAsync(request, cancellationToken);
18+
}
19+
20+
public object Clone()
21+
{
22+
return new CultureHandler();
23+
}
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Net.Http;
5+
using System.Text;
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
9+
namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.ServiceClientAdapterNS
10+
{
11+
public class RpNamespaceHandler : DelegatingHandler, ICloneable
12+
{
13+
string rpNamespace;
14+
15+
public RpNamespaceHandler(string rpNamespace)
16+
{
17+
this.rpNamespace = rpNamespace;
18+
}
19+
20+
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
21+
{
22+
request.RequestUri = new Uri(request.RequestUri.AbsoluteUri.Replace(ServiceClientAdapter.ResourceProviderProductionNamespace, rpNamespace));
23+
24+
return base.SendAsync(request, cancellationToken);
25+
}
26+
27+
public object Clone()
28+
{
29+
return new RpNamespaceHandler(rpNamespace);
30+
}
31+
}
32+
}

src/ResourceManager/RecoveryServices.Backup/Commands.RecoveryServices.Backup.ServiceClientAdapter/ServiceClientAdapter.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
using Microsoft.Azure.Commands.Common.Authentication.Models;
1516
using System;
1617
using System.Collections.Generic;
1718
using System.Linq;
19+
using System.Net.Http;
1820
using System.Text;
1921
using System.Threading.Tasks;
2022
using RecoveryServicesModelsNS = Microsoft.Azure.Management.RecoveryServices.Backup.Models;
@@ -25,25 +27,27 @@ namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.ServiceClient
2527
public partial class ServiceClientAdapter
2628
{
2729
const string AppSettingsSectionName = "appSettings";
28-
const string RecoveryServicesResourceNamespace = "Microsoft.RecoveryServices";
2930
const string ProviderNamespaceKey = "ProviderNamespace";
3031
const string AzureFabricName = "Azure";
32+
public const string ResourceProviderProductionNamespace = "Microsoft.RecoveryServices";
3133

32-
ClientProxy<RecoveryServicesNS.RecoveryServicesBackupManagementClient, RecoveryServicesModelsNS.CustomRequestHeaders> BmsAdapter;
34+
ClientProxy<RecoveryServicesNS.RecoveryServicesBackupClient> BmsAdapter;
3335

34-
public ServiceClientAdapter(SubscriptionCloudCredentials creds, Uri baseUri)
36+
public string ResourceProviderNamespace { get; private set; }
37+
38+
public ServiceClientAdapter(AzureContext context)
3539
{
40+
BmsAdapter = new ClientProxy<RecoveryServicesNS.RecoveryServicesBackupClient>(context);
41+
3642
System.Configuration.Configuration exeConfiguration = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Reflection.Assembly.GetExecutingAssembly().Location);
3743
System.Configuration.AppSettingsSection appSettings = (System.Configuration.AppSettingsSection)exeConfiguration.GetSection(AppSettingsSectionName);
38-
string recoveryServicesResourceNamespace = RecoveryServicesResourceNamespace;
44+
string resourceProviderNamespace = ResourceProviderProductionNamespace;
3945
if (appSettings.Settings[ProviderNamespaceKey] != null)
4046
{
41-
recoveryServicesResourceNamespace = appSettings.Settings[ProviderNamespaceKey].Value;
47+
resourceProviderNamespace = appSettings.Settings[ProviderNamespaceKey].Value;
4248
}
43-
BmsAdapter = new ClientProxy<RecoveryServicesNS.RecoveryServicesBackupManagementClient, RecoveryServicesModelsNS.CustomRequestHeaders>(
44-
clientRequestId => new RecoveryServicesModelsNS.CustomRequestHeaders() { ClientRequestId = clientRequestId },
45-
creds, baseUri);
46-
BmsAdapter.Client.ResourceNamespace = recoveryServicesResourceNamespace;
49+
50+
ResourceProviderNamespace = resourceProviderNamespace;
4751
}
4852
}
4953
}

src/ResourceManager/RecoveryServices.Backup/Commands.RecoveryServices.Backup/Commands.RecoveryServices.Backup.Cmdlets.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@
5959
<SpecificVersion>False</SpecificVersion>
6060
<HintPath>..\..\..\tempDll\Debug-Net45\net45\Microsoft.Rest.ClientRuntime.dll</HintPath>
6161
</Reference>
62+
<Reference Include="Microsoft.Rest.ClientRuntime.Azure">
63+
<HintPath>..\..\..\tempDll\Debug-Net45\net45\Microsoft.Rest.ClientRuntime.Azure.dll</HintPath>
64+
</Reference>
6265
<Reference Include="Microsoft.WindowsAzure.Management, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
6366
<SpecificVersion>False</SpecificVersion>
6467
<HintPath>..\..\..\packages\Microsoft.WindowsAzure.Management.4.1.1\lib\net40\Microsoft.WindowsAzure.Management.dll</HintPath>

src/ResourceManager/RecoveryServices.Backup/Commands.RecoveryServices.Backup/RecoveryServicesBackupCmdletBase.cs

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
using System;
1616
using System.Collections.Generic;
1717
using System.Management.Automation;
18+
using System.Linq;
1819
using System.Net;
19-
using Hyak.Common;
2020
using Microsoft.Azure.Commands.Common.Authentication;
2121
using Microsoft.Azure.Commands.Common.Authentication.Models;
2222
using Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.ServiceClientAdapterNS;
@@ -27,6 +27,7 @@
2727
using Microsoft.WindowsAzure.Management.Scheduler;
2828
using CmdletModel = Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.Models;
2929
using ResourcesNS = Microsoft.Azure.Management.Resources;
30+
using Microsoft.Rest.Azure;
3031

3132
namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets
3233
{
@@ -50,17 +51,24 @@ public abstract class RecoveryServicesBackupCmdletBase : AzureRMCmdlet
5051
/// </summary>
5152
protected void InitializeAzureBackupCmdlet()
5253
{
53-
var cloudServicesClient = AzureSession.ClientFactory.CreateClient<CloudServiceManagementClient>(DefaultContext, AzureEnvironment.Endpoint.ResourceManager);
54-
ServiceClientAdapter = new ServiceClientAdapter(cloudServicesClient.Credentials, cloudServicesClient.BaseUri);
54+
ServiceClientAdapter = new ServiceClientAdapter(DefaultProfile.Context);
5555

5656
WriteDebug("InsideRestore. going to create ResourceManager Client");
57-
RmClient = AzureSession.ClientFactory.CreateClient<ResourcesNS.ResourceManagementClient>(DefaultContext, AzureEnvironment.Endpoint.ResourceManager);
57+
RmClient = AzureSession.ClientFactory.CreateClient<ResourcesNS.ResourceManagementClient>(DefaultProfile.Context, AzureEnvironment.Endpoint.ResourceManager);
5858

5959
WriteDebug("Client Created successfully");
6060

6161
Logger.Instance = new Logger(WriteWarning, WriteDebug, WriteVerbose, ThrowTerminatingError);
6262
}
6363

64+
protected override void SetupHttpClientPipeline()
65+
{
66+
base.SetupHttpClientPipeline();
67+
AzureSession.ClientFactory.AddHandler(new RpNamespaceHandler(ServiceClientAdapter.ResourceProviderNamespace));
68+
AzureSession.ClientFactory.AddHandler(new ClientRequestIdHandler());
69+
//AzureSession.ClientFactory.AddHandler(new CultureHandler());
70+
}
71+
6472
/// <summary>
6573
/// Wrapper method which executes the cmdlet processing blocks.
6674
/// Catches and logs any exception occuring during the execution.
@@ -138,9 +146,11 @@ private void HandleException(Exception exception)
138146

139147
protected override void BeginProcessing()
140148
{
141-
base.BeginProcessing();
149+
// TOOD: This order might NOT work. If that's the case, need to parse the resource namespace in base cmdlet itself.
142150

143151
InitializeAzureBackupCmdlet();
152+
153+
base.BeginProcessing();
144154
}
145155

146156
/// <summary>
@@ -171,15 +181,15 @@ public CmdletModel.JobBase GetJobObject(string jobId)
171181
/// <summary>
172182
/// Based on the response from the service, handles the job created in the service appropriately.
173183
/// </summary>
174-
/// <param name="itemResponse">Response from service</param>
184+
/// <param name="jobResponse">Response from service</param>
175185
/// <param name="operationName">Name of the operation</param>
176-
protected void HandleCreatedJob(BaseRecoveryServicesJobResponse itemResponse, string operationName)
186+
protected void HandleCreatedJob(JobResponse jobResponse, string operationName)
177187
{
178188
WriteDebug(Resources.TrackingOperationStatusURLForCompletion +
179-
itemResponse.AzureAsyncOperation);
189+
jobResponse.AzureAsyncOperation);
180190

181191
var response = TrackingHelpers.WaitForOperationCompletionUsingStatusLink(
182-
itemResponse.AzureAsyncOperation,
192+
jobResponse.AzureAsyncOperation,
183193
ServiceClientAdapter.GetProtectedItemOperationStatusByURL);
184194

185195
if (response != null && response.OperationStatus != null)
@@ -193,7 +203,7 @@ protected void HandleCreatedJob(BaseRecoveryServicesJobResponse itemResponse, st
193203

194204
if (jobExtendedInfo.JobId != null)
195205
{
196-
var jobStatusResponse =
206+
var jobStatusResponse =
197207
(OperationStatusJobExtendedInfo)response.OperationStatus.Properties;
198208
WriteObject(GetJobObject(jobStatusResponse.JobId));
199209
}
@@ -212,4 +222,18 @@ protected void HandleCreatedJob(BaseRecoveryServicesJobResponse itemResponse, st
212222
}
213223
}
214224
}
225+
226+
public class JobResponse
227+
{
228+
public string AzureAsyncOperation { get; private set; }
229+
230+
public string Location { get; private set; }
231+
232+
public JobResponse(AzureOperationResponse response)
233+
{
234+
Location = response.Response.Headers.Location.ToString();
235+
236+
AzureAsyncOperation = response.Response.Headers.GetValues("Azure-AsyncOperation").FirstOrDefault();
237+
}
238+
}
215239
}

0 commit comments

Comments
 (0)