Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add audience for multi-cloud support #24237

Merged
merged 6 commits into from
Oct 5, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.azure.core.util.Configuration;
import com.azure.core.util.logging.ClientLogger;
import com.azure.monitor.query.implementation.logs.AzureLogAnalyticsImplBuilder;
import com.azure.monitor.query.models.LogsQueryClientAudience;

/**
* Fluent builder for creating instances of {@link LogsQueryClient} and {@link LogsQueryAsyncClient}.
Expand Down Expand Up @@ -116,17 +117,29 @@ public LogsQueryClientBuilder credential(TokenCredential tokenCredential) {
/**
* Set the {@link ClientOptions} used for creating the client.
* @param clientOptions The {@link ClientOptions}.
* @return the {@link LogsQueryClientBuilder}
* @return the {@link LogsQueryClientBuilder}.
*/
public LogsQueryClientBuilder clientOptions(ClientOptions clientOptions) {
this.clientOptions = clientOptions;
return this;
}

/**
* Sets the audience to use for authentication with Azure Active Directory. The Azure Public Cloud audience will be
* used if the property is null.
* @param audience audience to use for authentication with Azure Active Directory. The Azure Public Cloud audience
* will be used if the property is null.
* @return the {@link LogsQueryClientBuilder}.
*/
public LogsQueryClientBuilder audience(LogsQueryClientAudience audience) {
innerLogBuilder.audience(audience == null ? null : audience.toString());
return this;
}

/**
* The service version to use when creating the client.
* @param serviceVersion The {@link LogsQueryServiceVersion}.
* @return the {@link LogsQueryClientBuilder}
* @return the {@link LogsQueryClientBuilder}.
*/
public LogsQueryClientBuilder serviceVersion(LogsQueryServiceVersion serviceVersion) {
this.serviceVersion = serviceVersion;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.azure.monitor.query.implementation.metrics.MonitorManagementClientImplBuilder;
import com.azure.monitor.query.implementation.metricsdefinitions.MetricsDefinitionsClientImplBuilder;
import com.azure.monitor.query.implementation.metricsnamespaces.MetricsNamespacesClientImplBuilder;
import com.azure.monitor.query.models.MetricsQueryClientAudience;

/**
* Fluent builder for creating instances of {@link MetricsQueryClient} and {@link MetricsQueryAsyncClient}.
Expand Down Expand Up @@ -137,6 +138,20 @@ public MetricsQueryClientBuilder credential(TokenCredential tokenCredential) {
return this;
}

/**
* Sets the audience to use for authentication with Azure Active Directory. The Azure Public Cloud audience will be
* used if the property is null.
* @param audience audience to use for authentication with Azure Active Directory. The Azure Public Cloud audience
* will be used if the property is null.
* @return the {@link MetricsQueryClientBuilder}.
*/
public MetricsQueryClientBuilder audience(MetricsQueryClientAudience audience) {
innerMetricsBuilder.audience(audience == null ? null : audience.toString());
innerMetricsDefinitionsBuilder.audience(audience == null ? null : audience.toString());
innerMetricsNamespaceBuilder.audience(audience == null ? null : audience.toString());
return this;
}

/**
* Set the {@link ClientOptions} used for creating the client.
* @param clientOptions The {@link ClientOptions}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
import com.azure.core.util.CoreUtils;
import com.azure.core.util.serializer.JacksonAdapter;
import com.azure.core.util.serializer.SerializerAdapter;
import com.azure.monitor.query.LogsQueryClientBuilder;
import com.azure.monitor.query.models.LogsQueryClientAudience;
import com.azure.monitor.query.models.MetricsQueryClientAudience;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand All @@ -33,6 +37,7 @@ public final class AzureLogAnalyticsImplBuilder {
private static final String SDK_VERSION = "version";

private final Map<String, String> properties = CoreUtils.getProperties("azure-monitor-query.properties");
private String audience;

/** Create an instance of the AzureLogAnalyticsImplBuilder. */
public AzureLogAnalyticsImplBuilder() {
Expand Down Expand Up @@ -185,22 +190,35 @@ public AzureLogAnalyticsImplBuilder addPolicy(HttpPipelinePolicy customPolicy) {
return this;
}

/**
* Sets the audience to use for authentication with Azure Active Directory. The Azure Public Cloud audience will be
* used if the property is null.
* @param audience audience to use for authentication with Azure Active Directory. The Azure Public Cloud audience
* will be used if the property is null.
* @return the {@link AzureLogAnalyticsImplBuilder}.
*/
public AzureLogAnalyticsImplBuilder audience(String audience) {
this.audience = audience;
return this;
}
srnagar marked this conversation as resolved.
Show resolved Hide resolved

/**
* Builds an instance of AzureLogAnalyticsImpl with the provided parameters.
*
* @return an instance of AzureLogAnalyticsImpl.
*/
public AzureLogAnalyticsImpl buildClient() {
if (host == null) {
this.host = "https://api.loganalytics.io/v1";
this.host = "https://api.loganalytics.io";
}
String hostVersion = this.host + "/v1";
if (pipeline == null) {
this.pipeline = createHttpPipeline();
}
if (serializerAdapter == null) {
this.serializerAdapter = JacksonAdapter.createDefaultSerializerAdapter();
}
AzureLogAnalyticsImpl client = new AzureLogAnalyticsImpl(pipeline, serializerAdapter, host);
AzureLogAnalyticsImpl client = new AzureLogAnalyticsImpl(pipeline, serializerAdapter, hostVersion);
return client;
}

Expand All @@ -217,8 +235,14 @@ private HttpPipeline createHttpPipeline() {
new UserAgentPolicy(httpLogOptions.getApplicationId(), clientName, clientVersion, buildConfiguration));
HttpPolicyProviders.addBeforeRetryPolicies(policies);

String resolvedAudience = this.audience;
if (resolvedAudience == null) {
resolvedAudience = LogsQueryClientAudience.AZURE_PUBLIC_CLOUD.toString();
}
resolvedAudience += "/.default";
BearerTokenAuthenticationPolicy tokenPolicy = new BearerTokenAuthenticationPolicy(this.tokenCredential,
"https://api.loganalytics.io/.default");
resolvedAudience);

policies.add(tokenPolicy);
policies.add(retryPolicy == null ? new RetryPolicy() : retryPolicy);
policies.add(new CookiePolicy());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.azure.core.util.CoreUtils;
import com.azure.core.util.serializer.JacksonAdapter;
import com.azure.core.util.serializer.SerializerAdapter;
import com.azure.monitor.query.models.MetricsQueryClientAudience;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand All @@ -33,6 +35,7 @@ public final class MonitorManagementClientImplBuilder {
private static final String SDK_VERSION = "version";

private final Map<String, String> properties = CoreUtils.getProperties("azure-monitor-query.properties");
private String audience;

/** Create an instance of the MonitorManagementClientImplBuilder. */
public MonitorManagementClientImplBuilder() {
Expand Down Expand Up @@ -201,6 +204,18 @@ public MonitorManagementClientImplBuilder addPolicy(HttpPipelinePolicy customPol
return this;
}

/**
* Sets the audience to use for authentication with Azure Active Directory. The Azure Public Cloud audience will be
* used if the property is null.
* @param audience audience to use for authentication with Azure Active Directory. The Azure Public Cloud audience
* will be used if the property is null.
* @return the {@link MonitorManagementClientImplBuilder}.
*/
public MonitorManagementClientImplBuilder audience(String audience) {
this.audience = audience;
return this;
}

/**
* Builds an instance of MonitorManagementClientImpl with the provided parameters.
*
Expand All @@ -210,6 +225,7 @@ public MonitorManagementClientImpl buildClient() {
if (host == null) {
this.host = "https://management.azure.com";
}

if (apiVersion == null) {
this.apiVersion = "2018-01-01";
}
Expand Down Expand Up @@ -237,8 +253,13 @@ private HttpPipeline createHttpPipeline() {
new UserAgentPolicy(httpLogOptions.getApplicationId(), clientName, clientVersion, buildConfiguration));
HttpPolicyProviders.addBeforeRetryPolicies(policies);
policies.add(retryPolicy == null ? new RetryPolicy() : retryPolicy);
BearerTokenAuthenticationPolicy tokenPolicy = new BearerTokenAuthenticationPolicy(this.tokenCredential, " https://management.azure.com" +
"/.default");
String resolvedAudience = this.audience;
if (resolvedAudience == null) {
resolvedAudience = MetricsQueryClientAudience.AZURE_RESOURCE_MANAGER_PUBLIC_CLOUD.toString();
}
resolvedAudience += "/.default";
BearerTokenAuthenticationPolicy tokenPolicy = new BearerTokenAuthenticationPolicy(this.tokenCredential,
resolvedAudience);
policies.add(tokenPolicy);
policies.add(new CookiePolicy());
policies.addAll(this.pipelinePolicies);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.azure.core.util.CoreUtils;
import com.azure.core.util.serializer.JacksonAdapter;
import com.azure.core.util.serializer.SerializerAdapter;
import com.azure.monitor.query.models.MetricsQueryClientAudience;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand All @@ -33,6 +35,7 @@ public final class MetricsDefinitionsClientImplBuilder {
private static final String SDK_VERSION = "version";

private final Map<String, String> properties = CoreUtils.getProperties("azure-monitor-query.properties");
private String audience;

/** Create an instance of the MetricsDefinitionsClientImplBuilder. */
public MetricsDefinitionsClientImplBuilder() {
Expand Down Expand Up @@ -201,6 +204,18 @@ public MetricsDefinitionsClientImplBuilder addPolicy(HttpPipelinePolicy customPo
return this;
}

/**
* Sets the audience to use for authentication with Azure Active Directory. The Azure Public Cloud audience will be
* used if the property is null.
* @param audience audience to use for authentication with Azure Active Directory. The Azure Public Cloud audience
* will be used if the property is null.
* @return the {@link MetricsDefinitionsClientImplBuilder}.
*/
public MetricsDefinitionsClientImplBuilder audience(String audience) {
this.audience = audience;
return this;
}

/**
* Builds an instance of MetricsDefinitionsClientImpl with the provided parameters.
*
Expand Down Expand Up @@ -236,8 +251,13 @@ private HttpPipeline createHttpPipeline() {
policies.add(
new UserAgentPolicy(httpLogOptions.getApplicationId(), clientName, clientVersion, buildConfiguration));
HttpPolicyProviders.addBeforeRetryPolicies(policies);
BearerTokenAuthenticationPolicy tokenPolicy = new BearerTokenAuthenticationPolicy(this.tokenCredential, "https://management.azure.com" +
"/.default");
String resolvedAudience = this.audience;
if (resolvedAudience == null) {
resolvedAudience = MetricsQueryClientAudience.AZURE_RESOURCE_MANAGER_PUBLIC_CLOUD.toString();
}
resolvedAudience += "/.default";
BearerTokenAuthenticationPolicy tokenPolicy = new BearerTokenAuthenticationPolicy(this.tokenCredential,
resolvedAudience);
policies.add(tokenPolicy);
policies.add(retryPolicy == null ? new RetryPolicy() : retryPolicy);
policies.add(new CookiePolicy());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.azure.core.util.CoreUtils;
import com.azure.core.util.serializer.JacksonAdapter;
import com.azure.core.util.serializer.SerializerAdapter;
import com.azure.monitor.query.models.MetricsQueryClientAudience;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand All @@ -33,6 +35,7 @@ public final class MetricsNamespacesClientImplBuilder {
private static final String SDK_VERSION = "version";

private final Map<String, String> properties = CoreUtils.getProperties("azure-monitor-query.properties");
private String audience;

/** Create an instance of the MetricsNamespacesClientImplBuilder. */
public MetricsNamespacesClientImplBuilder() {
Expand Down Expand Up @@ -201,6 +204,18 @@ public MetricsNamespacesClientImplBuilder addPolicy(HttpPipelinePolicy customPol
return this;
}

/**
* Sets the audience to use for authentication with Azure Active Directory. The Azure Public Cloud audience will be
* used if the property is null.
* @param audience audience to use for authentication with Azure Active Directory. The Azure Public Cloud audience
* will be used if the property is null.
* @return the {@link MetricsNamespacesClientImplBuilder}.
*/
public MetricsNamespacesClientImplBuilder audience(String audience) {
this.audience = audience;
return this;
}

/**
* Builds an instance of MetricsNamespacesClientImpl with the provided parameters.
*
Expand Down Expand Up @@ -236,8 +251,13 @@ private HttpPipeline createHttpPipeline() {
policies.add(
new UserAgentPolicy(httpLogOptions.getApplicationId(), clientName, clientVersion, buildConfiguration));
HttpPolicyProviders.addBeforeRetryPolicies(policies);
BearerTokenAuthenticationPolicy tokenPolicy = new BearerTokenAuthenticationPolicy(this.tokenCredential, "https://management.azure.com" +
"/.default");
String resolvedAudience = this.audience;
if (resolvedAudience == null) {
resolvedAudience = MetricsQueryClientAudience.AZURE_RESOURCE_MANAGER_PUBLIC_CLOUD.toString();
}
resolvedAudience += "/.default";
BearerTokenAuthenticationPolicy tokenPolicy = new BearerTokenAuthenticationPolicy(this.tokenCredential,
resolvedAudience);
policies.add(tokenPolicy);
policies.add(retryPolicy == null ? new RetryPolicy() : retryPolicy);
policies.add(new CookiePolicy());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.monitor.query.models;

import com.azure.core.util.ExpandableStringEnum;
import com.azure.monitor.query.LogsQueryClient;
import com.fasterxml.jackson.annotation.JsonCreator;

import java.util.Collection;

/**
* Cloud audiences available for {@link LogsQueryClient}.
*/
public final class LogsQueryClientAudience extends ExpandableStringEnum<LogsQueryClientAudience> {
srnagar marked this conversation as resolved.
Show resolved Hide resolved

/** Static audience for Azure public cloud. */
public static final LogsQueryClientAudience AZURE_PUBLIC_CLOUD = fromString("https://api.monitor.azure.com");

/** Static default audience that uses Azure public cloud. */
public static final LogsQueryClientAudience DEFAULT = fromString("https://api.monitor.azure.com");

/**
* Creates or finds a LogsQueryClientAudience from its string representation.
*
* @param name a name to look for.
* @return the corresponding LogsQueryClientAudience.
*/
@JsonCreator
public static LogsQueryClientAudience fromString(String name) {
return fromString(name, LogsQueryClientAudience.class);
}

/** @return known LogsQueryClientAudience values. */
public static Collection<LogsQueryClientAudience> values() {
return values(LogsQueryClientAudience.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.monitor.query.models;

import com.azure.core.util.ExpandableStringEnum;
import com.azure.monitor.query.MetricsQueryClient;
import com.fasterxml.jackson.annotation.JsonCreator;

import java.util.Collection;

/**
* Cloud audiences available for {@link MetricsQueryClient}.
*/
public final class MetricsQueryClientAudience extends ExpandableStringEnum<MetricsQueryClientAudience> {
/** Static value AZURE_RESOURCE_MANAGER_CHINA for MetricsQueryClientAudience. */
public static final MetricsQueryClientAudience AZURE_RESOURCE_MANAGER_CHINA = fromString("https://management.chinacloudapi.cn");

/** Static value AZURE_RESOURCE_MANAGER_GERMANY for MetricsQueryClientAudience. */
public static final MetricsQueryClientAudience AZURE_RESOURCE_MANAGER_GERMANY = fromString("https://management.microsoftazure.de");

/** Static value AZURE_RESOURCE_MANAGER_GOVERNMENT for MetricsQueryClientAudience. */
public static final MetricsQueryClientAudience AZURE_RESOURCE_MANAGER_GOVERNMENT = fromString("https://management.usgovcloudapi.net");

/** Static value AZURE_RESOURCE_MANAGER_PUBLIC_CLOUD for MetricsQueryClientAudience. */
public static final MetricsQueryClientAudience AZURE_RESOURCE_MANAGER_PUBLIC_CLOUD = fromString("https://management.azure.com");

/** Static default audience that uses Azure public cloud. */
public static final MetricsQueryClientAudience DEFAULT = fromString("https://management.azure.com");

/**
* Creates or finds a MetricsQueryClientAudience from its string representation.
* @param name a name to look for.
* @return the corresponding MetricsQueryClientAudience.
*/
@JsonCreator
public static MetricsQueryClientAudience fromString(String name) {
return fromString(name, MetricsQueryClientAudience.class);
}

/** @return known MetricsQueryClientAudience values. */
public static Collection<MetricsQueryClientAudience> values() {
return values(MetricsQueryClientAudience.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public List<MetricResult> getMetrics() {
* @param metricName The name of the metric to look up the result for.
* @return The {@link MetricResult} for {@code metricName} if found, {@code null} otherwise.
*/
public MetricResult getMetrics(String metricName) {
public MetricResult getMetricByName(String metricName) {
Objects.requireNonNull(metricName, "'metricName' cannot be null");
if (CoreUtils.isNullOrEmpty(metrics)) {
return null;
Expand Down