Repository: Azure/azure-cli-extensions

Issue:
app-insights az cli extension seems not working properly in AzureChinaCloud (China East 2)
- If the issue is to do with Azure CLI 2.0 in-particular, create an issue here at [Azure/azure-cli](https://github.com/Azure/azure-cli/issues)

### Extension name (the extension in question)
application-insights

### Description of issue (in as much detail as possible)
I have properly logged in with a certain subscription in AzureChinaCloud (China East2)
```
az account show
{
  "environmentName": "AzureChinaCloud",
  "id": "5b5d5f37-8aca-49f8-8682-7ed6d2f00424",
  "isDefault": true,
  "name": "Project Vienna PROD - ChinaEast2",
  "state": "Enabled",
  "tenantId": "a55a4d5b-9241-49b1-b4ff-befa8db00269",
  "user": {
    "name": "oe-wanglongjiang@cme.gbl",
    "type": "user"
  }
}
```
However run any az monitor app-insights command always failed with error "The subscription '5b5d5f37-8aca-49f8-8682-7ed6d2f00424' could not be found" 

for example, api-key show
```
az monitor app-insights api-key show --app model-monitoring-chinaeast2 --resource-group model-mgmt-chinaeast2 --api-key MMSModelMonitoringApiKey
The subscription '5b5d5f37-8aca-49f8-8682-7ed6d2f00424' could not be found.
```

I doubted application-insights is not working in AzureChinaCloud, any insight of it? Appreciate


Discussion:
Is that possible reason with hard code "**https://api.applicationinsights.io**" in [azext_applicationinsights._client_factory](https://github.com/Azure/azure-cli-extensions/blob/f1f8f52dfe3700845212441912989e9c0d58183c/src/application-insights/azext_applicationinsights/_client_factory.py#L13). 

In AzureChinaCloud, application insights endpoint suffix is **applicationinsights.azure.cn**
@MyronFanQiu please take a look.
@alexeldeib Hello. I'm not sure. Does app-insights support AzureChinaCloud?
@evanlwj Thanks for reporting this. That's maybe the root cause. Currently, for some commands like app-insights or log-analytics, the endpoint is hard coded. I have asked @alexeldeib to confirm this issue and tried to figure out a solution.
I can confirm application insights is GA'ed in Mooncake, where is able to allow to create application insights instance.

We have a deployment tool depends on app insights extension to do roll out whole AML stack to Mooncake AKS clusters, now it's being blocked by this issue, if it can be conquered or workaround from your suggestion, it will be appreciated
Sorry for the slow reply, I've been traveling. The URL for App Insights in AzureChinaCloud is `api.applicationinsights.azure.cn`. We also have `api.loganalytics.azure.cn` for log analytics.

@MyronFanQiu for PowerShell we added the endpoints to the equivalent of this file: https://github.com/Azure/azure-cli/blob/dev/src/azure-cli-core/azure/cli/core/cloud.py#L211-L233 should we do the same here?
I think it would be something like this? https://github.com/Azure/azure-cli/pull/11385
@alexeldeib Thanks. I will take a look in this sprint.
@evanlwj Hi. We have merged the corresponding PR to add endpoint in ChinaCloud. It will be released in the next week. Could you please install new azure-cli version and have a try?
I have verified this with the latest Edge builds and the issue still reproes. Evan suggested that this might be because of the hardcoded endpoint in https://github.com/Azure/azure-cli-extensions/blob/f1f8f52dfe3700845212441912989e9c0d58183c/src/application-insights/azext_applicationinsights/_client_factory.py#L13

 that is not leveraging the changes in https://github.com/Azure/azure-cli/pull/11385

Resolving patch:
diff --git a/src/application-insights/azext_applicationinsights/_client_factory.py b/src/application-insights/azext_applicationinsights/_client_factory.py
--- a/src/application-insights/azext_applicationinsights/_client_factory.py
+++ b/src/application-insights/azext_applicationinsights/_client_factory.py
@@ -10,7 +10,7 @@ def applicationinsights_data_plane_client(cli_ctx, _, subscription=None):
     from azure.cli.core._profile import Profile
     profile = Profile(cli_ctx=cli_ctx)
     cred, _, _ = profile.get_login_credentials(
-        resource="https://api.applicationinsights.io",
+        resource=cli_ctx.cloud.endpoints.app_insights_resource_id,
         subscription_id=subscription
     )
     return ApplicationInsightsDataClient(cred)
@@ -19,20 +19,8 @@ def applicationinsights_data_plane_client(cli_ctx, _, subscription=None):
 def applicationinsights_mgmt_plane_client(cli_ctx, _, subscription=None):
     """Initialize Log Analytics mgmt client for use with CLI."""
     from .vendored_sdks.mgmt_applicationinsights import ApplicationInsightsManagementClient
-    from azure.cli.core._profile import Profile
-    profile = Profile(cli_ctx=cli_ctx)
-    # Use subscription from resource_id where possible, otherwise use login.
-    if subscription:
-        cred, _, _ = profile.get_login_credentials(subscription_id=subscription)
-        return ApplicationInsightsManagementClient(
-            cred,
-            subscription
-        )
-    cred, sub_id, _ = profile.get_login_credentials()
-    return ApplicationInsightsManagementClient(
-        cred,
-        sub_id
-    )
+    from azure.cli.core.commands.client_factory import get_mgmt_service_client
+    return get_mgmt_service_client(cli_ctx, ApplicationInsightsManagementClient, subscription_id=subscription)
 
 
 def cf_query(cli_ctx, _, subscription=None):
diff --git a/src/application-insights/setup.py b/src/application-insights/setup.py
--- a/src/application-insights/setup.py
+++ b/src/application-insights/setup.py
@@ -8,7 +8,7 @@
 from codecs import open
 from setuptools import setup, find_packages
 
-VERSION = "0.1.1"
+VERSION = "0.1.2"
 
 CLASSIFIERS = [
     'Development Status :: 4 - Beta',


Test patch: