Skip to content

Commit a776264

Browse files
authored
Add default endpoint and custom endpoint support to AMC (Azure#21851)
1 parent 762af05 commit a776264

File tree

4 files changed

+80
-1
lines changed

4 files changed

+80
-1
lines changed

sdk/monitor/Azure.Monitor.Query/api/Azure.Monitor.Query.netstandard2.0.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ public LogsBatchQuery() { }
88
public partial class LogsQueryClient
99
{
1010
protected LogsQueryClient() { }
11+
public LogsQueryClient(Azure.Core.TokenCredential credential) { }
12+
public LogsQueryClient(Azure.Core.TokenCredential credential, Azure.Monitor.Query.LogsQueryClientOptions options) { }
1113
public LogsQueryClient(System.Uri endpoint, Azure.Core.TokenCredential credential) { }
1214
public LogsQueryClient(System.Uri endpoint, Azure.Core.TokenCredential credential, Azure.Monitor.Query.LogsQueryClientOptions options) { }
1315
public virtual Azure.Response<Azure.Monitor.Query.Models.LogsQueryResult> Query(string workspace, string query, Azure.Core.DateTimeRange timeRange, Azure.Monitor.Query.LogsQueryOptions options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
@@ -20,6 +22,7 @@ public LogsQueryClient(System.Uri endpoint, Azure.Core.TokenCredential credentia
2022
public partial class LogsQueryClientOptions : Azure.Core.ClientOptions
2123
{
2224
public LogsQueryClientOptions(Azure.Monitor.Query.LogsQueryClientOptions.ServiceVersion version = Azure.Monitor.Query.LogsQueryClientOptions.ServiceVersion.V1) { }
25+
public string AuthenticationScope { get { throw null; } set { } }
2326
public enum ServiceVersion
2427
{
2528
V1 = 0,

sdk/monitor/Azure.Monitor.Query/src/LogsQueryClient.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,30 @@ namespace Azure.Monitor.Query
1717
/// </summary>
1818
public class LogsQueryClient
1919
{
20+
private static readonly Uri _defaultEndpoint = new Uri("https://api.loganalytics.io");
2021
private static readonly TimeSpan _networkTimeoutOffset = TimeSpan.FromSeconds(15);
2122
private readonly QueryRestClient _queryClient;
2223
private readonly ClientDiagnostics _clientDiagnostics;
2324
private readonly HttpPipeline _pipeline;
2425
private readonly RowBinder _rowBinder;
2526

27+
/// <summary>
28+
/// Initializes a new instance of <see cref="LogsQueryClient"/>. Uses the default 'https://api.loganalytics.io' endpoint.
29+
/// </summary>
30+
/// <param name="credential">The <see cref="TokenCredential"/> instance to use for authentication.</param>
31+
public LogsQueryClient(TokenCredential credential) : this(credential, null)
32+
{
33+
}
34+
35+
/// <summary>
36+
/// Initializes a new instance of <see cref="LogsQueryClient"/>. Uses the default 'https://api.loganalytics.io' endpoint.
37+
/// </summary>
38+
/// <param name="credential">The <see cref="TokenCredential"/> instance to use for authentication.</param>
39+
/// <param name="options">The <see cref="LogsQueryClientOptions"/> instance to use as client configuration.</param>
40+
public LogsQueryClient(TokenCredential credential, LogsQueryClientOptions options) : this(_defaultEndpoint, credential, options)
41+
{
42+
}
43+
2644
/// <summary>
2745
/// Initializes a new instance of <see cref="LogsQueryClient"/>.
2846
/// </summary>
@@ -46,7 +64,9 @@ public LogsQueryClient(Uri endpoint, TokenCredential credential, LogsQueryClient
4664
options ??= new LogsQueryClientOptions();
4765
endpoint = new Uri(endpoint, options.GetVersionString());
4866
_clientDiagnostics = new ClientDiagnostics(options);
49-
_pipeline = HttpPipelineBuilder.Build(options, new BearerTokenAuthenticationPolicy(credential, "https://api.loganalytics.io//.default"));
67+
_pipeline = HttpPipelineBuilder.Build(options, new BearerTokenAuthenticationPolicy(
68+
credential,
69+
options.AuthenticationScope ?? "https://api.loganalytics.io//.default"));
5070
_queryClient = new QueryRestClient(_clientDiagnostics, _pipeline, endpoint);
5171
_rowBinder = new RowBinder();
5272
}

sdk/monitor/Azure.Monitor.Query/src/LogsQueryClientOptions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ public enum ServiceVersion
4444
V1
4545
}
4646

47+
/// <summary>
48+
/// Gets or sets the authentication scope to use for authentication with Azure Active Directory. The default scope would be used if the property is null.
49+
/// </summary>
50+
public string AuthenticationScope { get; set; }
51+
4752
internal string GetVersionString()
4853
{
4954
return _version switch

sdk/monitor/Azure.Monitor.Query/tests/LogsQueryClientClientTests.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
// Licensed under the MIT License.
33

44
using System;
5+
using System.Threading;
56
using System.Threading.Tasks;
67
using Azure.Core;
78
using Azure.Core.TestFramework;
89
using Azure.Monitor.Query.Models;
10+
using Moq;
911
using NUnit.Framework;
1012

1113
namespace Azure.Monitor.Query.Tests
@@ -76,5 +78,54 @@ public async Task QueryBatchHandledInvalidResponse()
7678
LogsBatchQueryResult batchResult = await client.QueryBatchAsync(batch);
7779
Assert.NotNull(batchResult.GetResult("0"));
7880
}
81+
82+
[Test]
83+
public async Task UsesDefaultEndpoint()
84+
{
85+
string uri = null;
86+
var mockTransport = MockTransport.FromMessageCallback(message =>
87+
{
88+
uri = message.Request.Uri.ToString();
89+
var mockResponse = new MockResponse(200);
90+
mockResponse.SetContent("{\"tables\":[]}");
91+
return mockResponse;
92+
});
93+
94+
var client = new LogsQueryClient(new MockCredential(), new LogsQueryClientOptions()
95+
{
96+
Transport = mockTransport
97+
});
98+
99+
await client.QueryAsync("", "", DateTimeRange.All);
100+
StringAssert.StartsWith("https://api.loganalytics.io", uri);
101+
}
102+
103+
[TestCase(null, "https://api.loganalytics.io//.default")]
104+
[TestCase("https://api.loganalytics.gov//.default", "https://api.loganalytics.gov//.default")]
105+
public async Task UsesDefaultAuthScope(string scope, string expectedScope)
106+
{
107+
var mockTransport = MockTransport.FromMessageCallback(message =>
108+
{
109+
var mockResponse = new MockResponse(200);
110+
mockResponse.SetContent("{\"tables\":[]}");
111+
return mockResponse;
112+
});
113+
114+
Mock<MockCredential> mock = new() { CallBase = true };
115+
116+
string[] scopes = null;
117+
mock.Setup(m => m.GetTokenAsync(It.IsAny<TokenRequestContext>(), It.IsAny<CancellationToken>()))
118+
.Callback<TokenRequestContext, CancellationToken>((c, _) => scopes = c.Scopes)
119+
.CallBase();
120+
121+
var client = new LogsQueryClient(mock.Object, new LogsQueryClientOptions()
122+
{
123+
Transport = mockTransport,
124+
AuthenticationScope = scope
125+
});
126+
127+
await client.QueryAsync("", "", DateTimeRange.All);
128+
Assert.AreEqual(new[] { expectedScope }, scopes);
129+
}
79130
}
80131
}

0 commit comments

Comments
 (0)