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

Metrics advisor aad #15922

Merged
merged 10 commits into from
Jan 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
3 changes: 3 additions & 0 deletions sdk/metricsadvisor/azure-ai-metricsadvisor/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## 1.0.0b3 (Unreleased)

**New Features**

- AAD support authentication #15922

## 1.0.0b2 (2020-11-10)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import datetime
import six
from azure.core.tracing.decorator import distributed_trace
from azure.core.pipeline.policies import BearerTokenCredentialPolicy
from ._generated._azure_cognitive_service_metrics_advisor_restapi_open_ap_iv2 \
import AzureCognitiveServiceMetricsAdvisorRESTAPIOpenAPIV2 as _Client
from ._generated.models import (
Expand Down Expand Up @@ -52,6 +53,7 @@
IngestionStatusQueryOptions as _IngestionStatusQueryOptions,
)
from ._version import SDK_MONIKER
from ._metrics_advisor_key_credential import MetricsAdvisorKeyCredential
from ._metrics_advisor_key_credential_policy import MetricsAdvisorKeyCredentialPolicy
from ._helpers import (
convert_to_generated_data_feed_type,
Expand Down Expand Up @@ -85,7 +87,6 @@

if TYPE_CHECKING:
from azure.core.paging import ItemPaged
from ._metrics_advisor_key_credential import MetricsAdvisorKeyCredential
from ._generated.models import IngestionStatus as DataFeedIngestionStatus
from .models._models import (
MetricAlertConfiguration,
Expand Down Expand Up @@ -153,8 +154,9 @@ class MetricsAdvisorAdministrationClient(object): # pylint:disable=too-many-pub
:param str endpoint: Supported Cognitive Services endpoints (protocol and hostname,
for example: https://:code:`<resource-name>`.cognitiveservices.azure.com).
:param credential: An instance of ~azure.ai.metricsadvisor.MetricsAdvisorKeyCredential.
Requires both subscription key and API key.
:type credential: ~azure.ai.metricsadvisor.MetricsAdvisorKeyCredential
which requires both subscription key and API key. Or an object which can provide an access
token for the Metrics Advisor service, such as a credential from :mod:`azure.identity`
:type credential: ~azure.ai.metricsadvisor.MetricsAdvisorKeyCredential or ~azure.core.credentials.TokenCredential

.. admonition:: Example:

Expand All @@ -179,12 +181,27 @@ def __init__(self, endpoint, credential, **kwargs):

self._endpoint = endpoint

self._client = _Client(
endpoint=endpoint,
sdk_moniker=SDK_MONIKER,
authentication_policy=MetricsAdvisorKeyCredentialPolicy(credential),
**kwargs
)
if isinstance(credential, MetricsAdvisorKeyCredential):
self._client = _Client(
endpoint=endpoint,
sdk_moniker=SDK_MONIKER,
authentication_policy=MetricsAdvisorKeyCredentialPolicy(credential),
**kwargs
)
else:
if hasattr(credential, "get_token"):
credential_scopes = kwargs.pop('credential_scopes',
['https://cognitiveservices.azure.com/.default'])
credential_policy = BearerTokenCredentialPolicy(credential, *credential_scopes)
else:
raise TypeError("Please provide an instance from azure-identity "
"or a class that implement the 'get_token protocol")
self._client = _Client(
endpoint=endpoint,
sdk_moniker=SDK_MONIKER,
authentication_policy=credential_policy,
**kwargs
)

def __repr__(self):
# type: () -> str
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from typing import List, Union, Dict, Any, cast, TYPE_CHECKING, overload

from azure.core.tracing.decorator import distributed_trace
from azure.core.pipeline.policies import BearerTokenCredentialPolicy
from ._metrics_advisor_key_credential import MetricsAdvisorKeyCredential
from ._metrics_advisor_key_credential_policy import MetricsAdvisorKeyCredentialPolicy
from ._generated.models import (
Expand Down Expand Up @@ -59,8 +60,9 @@ class MetricsAdvisorClient(object):
:param str endpoint: Supported Cognitive Services endpoints (protocol and hostname,
for example: https://:code:`<resource-name>`.cognitiveservices.azure.com).
:param credential: An instance of ~azure.ai.metricsadvisor.MetricsAdvisorKeyCredential.
Requires both subscription key and API key.
:type credential: ~azure.ai.metricsadvisor.MetricsAdvisorKeyCredential
which requires both subscription key and API key. Or an object which can provide an access
token for the vault, such as a credential from :mod:`azure.identity`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we update the credential docstring for the other clients as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! 👍

:type credential: ~azure.ai.metricsadvisor.MetricsAdvisorKeyCredential or ~azure.core.credentials.TokenCredential
:keyword Pipeline pipeline: If omitted, the standard pipeline is used.
:keyword HttpTransport transport: If omitted, the standard pipeline is used.
:keyword list[HTTPPolicy] policies: If omitted, the standard pipeline is used.
Expand All @@ -79,12 +81,27 @@ def __init__(self, endpoint, credential, **kwargs):

self._endpoint = endpoint

self._client = AzureCognitiveServiceMetricsAdvisorRESTAPIOpenAPIV2(
endpoint=endpoint,
sdk_moniker=SDK_MONIKER,
authentication_policy=MetricsAdvisorKeyCredentialPolicy(credential),
**kwargs
)
if isinstance(credential, MetricsAdvisorKeyCredential):
self._client = AzureCognitiveServiceMetricsAdvisorRESTAPIOpenAPIV2(
endpoint=endpoint,
sdk_moniker=SDK_MONIKER,
authentication_policy=MetricsAdvisorKeyCredentialPolicy(credential),
**kwargs
)
else:
if hasattr(credential, "get_token"):
credential_scopes = kwargs.pop('credential_scopes',
['https://cognitiveservices.azure.com/.default'])
credential_policy = BearerTokenCredentialPolicy(credential, *credential_scopes)
else:
raise TypeError("Please provide an instance from azure-identity "
"or a class that implement the 'get_token protocol")
self._client = AzureCognitiveServiceMetricsAdvisorRESTAPIOpenAPIV2(
endpoint=endpoint,
sdk_moniker=SDK_MONIKER,
authentication_policy=credential_policy,
**kwargs
)

def __repr__(self):
# type: () -> str
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from azure.core.tracing.decorator import distributed_trace
from azure.core.tracing.decorator_async import distributed_trace_async
from azure.core.async_paging import AsyncItemPaged
from azure.core.pipeline.policies import AsyncBearerTokenCredentialPolicy
from .._generated.aio import AzureCognitiveServiceMetricsAdvisorRESTAPIOpenAPIV2 as _ClientAsync
from .._generated.models import (
AnomalyAlertingConfiguration as _AnomalyAlertingConfiguration,
Expand Down Expand Up @@ -65,8 +66,9 @@ class MetricsAdvisorAdministrationClient(object): # pylint:disable=too-many-pub
:param str endpoint: Supported Cognitive Services endpoints (protocol and hostname,
for example: https://:code:`<resource-name>`.cognitiveservices.azure.com).
:param credential: An instance of ~azure.ai.metricsadvisor.MetricsAdvisorKeyCredential.
Requires both subscription key and API key.
:type credential: ~azure.ai.metricsadvisor.MetricsAdvisorKeyCredential
which requires both subscription key and API key. Or an object which can provide an access
token for the vault, such as a credential from :mod:`azure.identity`
:type credential: ~azure.ai.metricsadvisor.MetricsAdvisorKeyCredential or ~azure.core.credentials.TokenCredential

.. admonition:: Example:

Expand All @@ -89,12 +91,27 @@ def __init__(self, endpoint: str, credential: MetricsAdvisorKeyCredential, **kwa

self._endpoint = endpoint

self._client = _ClientAsync(
endpoint=endpoint,
sdk_moniker=SDK_MONIKER,
authentication_policy=MetricsAdvisorKeyCredentialPolicy(credential),
**kwargs
)
if isinstance(credential, MetricsAdvisorKeyCredential):
self._client = _ClientAsync(
endpoint=endpoint,
sdk_moniker=SDK_MONIKER,
authentication_policy=MetricsAdvisorKeyCredentialPolicy(credential),
**kwargs
)
else:
if hasattr(credential, "get_token"):
credential_scopes = kwargs.pop('credential_scopes',
['https://cognitiveservices.azure.com/.default'])
credential_policy = AsyncBearerTokenCredentialPolicy(credential, *credential_scopes)
else:
raise TypeError("Please provide an instance from azure-identity "
"or a class that implement the 'get_token protocol")
self._client = _ClientAsync(
endpoint=endpoint,
sdk_moniker=SDK_MONIKER,
authentication_policy=credential_policy,
**kwargs
)

def __repr__(self):
# type: () -> str
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from azure.core.tracing.decorator import distributed_trace
from azure.core.tracing.decorator_async import distributed_trace_async
from azure.core.async_paging import AsyncItemPaged
from azure.core.pipeline.policies import AsyncBearerTokenCredentialPolicy
from .._metrics_advisor_key_credential import MetricsAdvisorKeyCredential
from .._metrics_advisor_key_credential_policy import MetricsAdvisorKeyCredentialPolicy
from .._generated.models import (
Expand Down Expand Up @@ -59,8 +60,9 @@ class MetricsAdvisorClient(object):
:param str endpoint: Supported Cognitive Services endpoints (protocol and hostname,
for example: https://:code:`<resource-name>`.cognitiveservices.azure.com).
:param credential: An instance of ~azure.ai.metricsadvisor.MetricsAdvisorKeyCredential.
Requires both subscription key and API key.
:type credential: ~azure.ai.metricsadvisor.MetricsAdvisorKeyCredential
which requires both subscription key and API key. Or an object which can provide an access
token for the vault, such as a credential from :mod:`azure.identity`
:type credential: ~azure.ai.metricsadvisor.MetricsAdvisorKeyCredential or ~azure.core.credentials.TokenCredential
:keyword Pipeline pipeline: If omitted, the standard pipeline is used.
:keyword HttpTransport transport: If omitted, the standard pipeline is used.
:keyword list[HTTPPolicy] policies: If omitted, the standard pipeline is used.
Expand All @@ -79,12 +81,27 @@ def __init__(self, endpoint, credential, **kwargs):

self._endpoint = endpoint

self._client = AzureCognitiveServiceMetricsAdvisorRESTAPIOpenAPIV2(
endpoint=endpoint,
sdk_moniker=SDK_MONIKER,
authentication_policy=MetricsAdvisorKeyCredentialPolicy(credential),
**kwargs
)
if isinstance(credential, MetricsAdvisorKeyCredential):
self._client = AzureCognitiveServiceMetricsAdvisorRESTAPIOpenAPIV2(
endpoint=endpoint,
sdk_moniker=SDK_MONIKER,
authentication_policy=MetricsAdvisorKeyCredentialPolicy(credential),
**kwargs
)
else:
if hasattr(credential, "get_token"):
credential_scopes = kwargs.pop('credential_scopes',
['https://cognitiveservices.azure.com/.default'])
credential_policy = AsyncBearerTokenCredentialPolicy(credential, *credential_scopes)
else:
raise TypeError("Please provide an instance from azure-identity "
"or a class that implement the 'get_token protocol")
self._client = AzureCognitiveServiceMetricsAdvisorRESTAPIOpenAPIV2(
endpoint=endpoint,
sdk_moniker=SDK_MONIKER,
authentication_policy=credential_policy,
**kwargs
)

def __repr__(self):
# type: () -> str
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
-e ../../../tools/azure-sdk-tools
-e ../../../tools/azure-devtools
-e ../../identity/azure-identity
../../core/azure-core
aiohttp>=3.0; python_version >= '3.5'
msrest>=0.6.12
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,33 @@ async def authentication_administration_client_with_metrics_advisor_credential_a
# [END administration_client_with_metrics_advisor_credential_async]


async def authentication_client_with_aad_async():
# [START authentication_client_with_aad_async]
kristapratico marked this conversation as resolved.
Show resolved Hide resolved
from azure.identity.aio import DefaultAzureCredential
from azure.ai.metricsadvisor.aio import MetricsAdvisorClient

service_endpoint = os.getenv("METRICS_ADVISOR_ENDPOINT")
credential = DefaultAzureCredential()
client = MetricsAdvisorClient(service_endpoint, credential)
# [END authentication_client_with_aad_async]


async def authentication_administration_client_with_aad_async():
# [START authentication_administration_client_with_aad_async]
from azure.identity.aio import DefaultAzureCredential
from azure.ai.metricsadvisor.aio import MetricsAdvisorAdministrationClient

service_endpoint = os.getenv("METRICS_ADVISOR_ENDPOINT")
credential = DefaultAzureCredential()
client = MetricsAdvisorAdministrationClient(service_endpoint, credential)
# [END authentication_administration_client_with_aad_async]


async def main():
await authentication_client_with_metrics_advisor_credential_async()
await authentication_administration_client_with_metrics_advisor_credential_async()
xiangyan99 marked this conversation as resolved.
Show resolved Hide resolved
await authentication_client_with_aad_async()
await authentication_administration_client_with_aad_async()


if __name__ == '__main__':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,30 @@ def authentication_administration_client_with_metrics_advisor_credential():
# [END administration_client_with_metrics_advisor_credential]


def authentication_client_with_aad():
# [START authentication_client_with_aad]
from azure.ai.metricsadvisor import MetricsAdvisorClient
from azure.identity import DefaultAzureCredential

service_endpoint = os.getenv("METRICS_ADVISOR_ENDPOINT")
credential = DefaultAzureCredential()
client = MetricsAdvisorClient(service_endpoint, credential)
# [END authentication_client_with_aad]


def authentication_administration_client_with_aad():
# [START administration_client_with_aad]
from azure.ai.metricsadvisor import MetricsAdvisorAdministrationClient
from azure.identity import DefaultAzureCredential

service_endpoint = os.getenv("METRICS_ADVISOR_ENDPOINT")
credential = DefaultAzureCredential()
client = MetricsAdvisorAdministrationClient(service_endpoint, credential)
# [END administration_client_with_aad]


if __name__ == '__main__':
authentication_client_with_metrics_advisor_credential()
authentication_administration_client_with_metrics_advisor_credential()
authentication_client_with_aad()
authentication_administration_client_with_aad()
Loading