File: base_testcase.py

package info (click to toggle)
python-azure 20250829%2Bgit-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 756,824 kB
  • sloc: python: 6,224,989; ansic: 804; javascript: 287; makefile: 198; sh: 195; xml: 109
file content (40 lines) | stat: -rw-r--r-- 1,483 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See LICENSE.txt in the project root for
# license information.
# -------------------------------------------------------------------------
import os

from devtools_testutils import AzureRecordedTestCase


ENV_MONITOR_ENVIRONMENT = "MONITOR_ENVIRONMENT"
ENV_MONITOR_RESOURCE_MANAGER_URL = "MONITOR_RESOURCE_MANAGER_URL"
ENV_MONITOR_LOCATION = "MONITOR_LOCATION"


METRICS_CLIENT_ENVIRONMENT_AUDIENCE_MAP = {
    "AzureCloud": "https://metrics.monitor.azure.com",
    "AzureChinaCloud": "https://metrics.monitor.azure.cn",
    "AzureUSGovernment": "https://metrics.monitor.azure.us",
}

TLD_MAP = {"AzureCloud": "com", "AzureChinaCloud": "cn", "AzureUSGovernment": "us"}


class MetricsClientTestCase(AzureRecordedTestCase):

    def get_client(self, client_class, credential, endpoint=None):

        environment = os.getenv(ENV_MONITOR_ENVIRONMENT)
        kwargs = {}
        tld = "com"
        if environment:
            kwargs["audience"] = METRICS_CLIENT_ENVIRONMENT_AUDIENCE_MAP.get(environment)
            tld = TLD_MAP.get(environment, "com")

        if not endpoint:
            region = os.getenv(ENV_MONITOR_LOCATION) or "westus2"
            kwargs["endpoint"] = f"https://{region}.metrics.monitor.azure.{tld}"

        return self.create_client_from_credential(client_class, credential, **kwargs)