Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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
4 changes: 4 additions & 0 deletions src/application-insights/HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

Release History
===============
1.3.0
++++++++++++++++++
* `az monitor app-insights events/metrics/query`: Migrate data-plane using codegen tool

1.2.3
++++++++++++++++++
* `az monitor app-insights events/metrics/query`: Fix error: Profile.get_login_credentials() got an unexpected keyword argument 'resource'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ class ApplicationInsightsCommandsLoader(AzCommandsLoader):

def __init__(self, cli_ctx=None):
from azure.cli.core.commands import CliCommandType
from azext_applicationinsights._client_factory import applicationinsights_data_plane_client
applicationinsights_custom = CliCommandType(
operations_tmpl='azext_applicationinsights.custom#{}',
client_factory=applicationinsights_data_plane_client
)

super().__init__(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,13 @@
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

class _Track1Credential: # pylint: disable=too-few-public-methods

def __init__(self, credential, resource):
"""Track 1 credential that can be fed into Track 1 SDK clients. Exposes signed_session protocol.
:param credential: Track 2 credential that exposes get_token protocol
:param resource: AAD resource
"""
self._credential = credential
self._resource = resource

def signed_session(self, session=None):
import requests
from azure.cli.core.auth.util import resource_to_scopes
session = session or requests.Session()
token = self._credential.get_token(*resource_to_scopes(self._resource))
header = "{} {}".format('Bearer', token.token)
session.headers['Authorization'] = header
return session


def applicationinsights_data_plane_client(cli_ctx, _, subscription=None):
"""Initialize Log Analytics data client for use with CLI."""
from .vendored_sdks.applicationinsights import ApplicationInsightsDataClient
from azure.cli.core._profile import Profile
profile = Profile(cli_ctx=cli_ctx)
# Note: temporarily adapt track2 auth to track1 by the guidance:
# https://github.com/Azure/azure-cli/pull/29631#issuecomment-2716799520
# need to be removed after migrated by codegen
cred, _, _ = profile.get_login_credentials(subscription_id=subscription)
return ApplicationInsightsDataClient(
_Track1Credential(cred, cli_ctx.cloud.endpoints.app_insights_resource_id),
base_url=f'{cli_ctx.cloud.endpoints.app_insights_resource_id}/v1'
)


def applicationinsights_mgmt_plane_client(cli_ctx, **kwargs):
"""Initialize Log Analytics mgmt client for use with CLI."""
from .vendored_sdks.mgmt_applicationinsights import ApplicationInsightsManagementClient
from azure.cli.core.commands.client_factory import get_mgmt_service_client
return get_mgmt_service_client(cli_ctx, ApplicationInsightsManagementClient, **kwargs)


def cf_query(cli_ctx, _):
return applicationinsights_data_plane_client(cli_ctx, _).query


def cf_metrics(cli_ctx, _):
return applicationinsights_data_plane_client(cli_ctx, _).metrics


def cf_events(cli_ctx, _):
return applicationinsights_data_plane_client(cli_ctx, _).events


def cf_components(cli_ctx, _):
return applicationinsights_mgmt_plane_client(cli_ctx, api_version='2018-05-01-preview').components

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
# pylint: skip-file
# flake8: noqa

from ._clients import *
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
#
# Code generated by aaz-dev-tools
# --------------------------------------------------------------------------------------------

# pylint: skip-file
# flake8: noqa

from azure.cli.core.aaz import *


@register_client("AAZMicrosoftInsightsDataPlaneClient_application_insights")
class AAZMicrosoftInsightsDataPlaneClient(AAZBaseClient):
_CLOUD_HOST_TEMPLATES = {
CloudNameEnum.AzureChinaCloud: "https://api.applicationinsights.azure.cn",
CloudNameEnum.AzureCloud: "https://api.applicationinsights.io",
CloudNameEnum.AzureUSGovernment: "https://api.applicationinsights.us",
}
_CLOUD_HOST_METADATA_INDEX = "appInsightsResourceId"

_AAD_CREDENTIAL_SCOPES = [
"https://api.applicationinsights.io/.default",
]

@classmethod
def _build_base_url(cls, ctx, **kwargs):
endpoint = cls.get_cloud_endpoint(ctx, cls._CLOUD_HOST_METADATA_INDEX)
if not endpoint:
endpoint = cls._CLOUD_HOST_TEMPLATES.get(ctx.cli_ctx.cloud.name, None)
return endpoint

@classmethod
def _build_configuration(cls, ctx, credential, **kwargs):
return AAZClientConfiguration(
credential=credential,
credential_scopes=cls._AAD_CREDENTIAL_SCOPES,
**kwargs
)


class _AAZMicrosoftInsightsDataPlaneClientHelper:
"""Helper class for AAZMicrosoftInsightsDataPlaneClient"""


__all__ = [
"AAZMicrosoftInsightsDataPlaneClient",
]
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@

from .__cmd_group import *
from ._migrate_to_new_pricing_model import *
from ._query_execute import *
from ._query_show import *
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
#
# Code generated by aaz-dev-tools
# --------------------------------------------------------------------------------------------

# pylint: skip-file
# flake8: noqa

from azure.cli.core.aaz import *


class QueryExecute(AAZCommand):
"""Executes an Analytics query for data. [Here](https://dev.applicationinsights.io/documentation/Using-the-API/Query) is an example for using POST with an Analytics query.
"""

_aaz_info = {
"version": "v1",
"resources": [
["data-plane:microsoft.insights", "/apps/{}/query", "v1"],
]
}

def _handler(self, command_args):
super()._handler(command_args)
self._execute_operations()
return self._output()

_args_schema = None

@classmethod
def _build_arguments_schema(cls, *args, **kwargs):
if cls._args_schema is not None:
return cls._args_schema
cls._args_schema = super()._build_arguments_schema(*args, **kwargs)

# define Arg Group ""

_args_schema = cls._args_schema
_args_schema.app_id = AAZStrArg(
options=["--app-id"],
help="ID of the application. This is Application ID from the API Access settings blade in the Azure portal.",
required=True,
)

# define Arg Group "Body"

_args_schema = cls._args_schema
_args_schema.applications = AAZListArg(
options=["--applications"],
arg_group="Body",
help="A list of Application IDs for cross-application queries.",
)
_args_schema.query = AAZStrArg(
options=["--query"],
arg_group="Body",
help="The query to execute.",
required=True,
)
_args_schema.timespan = AAZStrArg(
options=["--timespan"],
arg_group="Body",
help="Optional. The timespan over which to query data. This is an ISO8601 time period value. This timespan is applied in addition to any that are specified in the query expression.",
)

applications = cls._args_schema.applications
applications.Element = AAZStrArg()
return cls._args_schema

def _execute_operations(self):
self.pre_operations()
self.QueryExecute(ctx=self.ctx)()
self.post_operations()

@register_callback
def pre_operations(self):
pass

@register_callback
def post_operations(self):
pass

def _output(self, *args, **kwargs):
result = self.deserialize_output(self.ctx.vars.instance, client_flatten=True)
return result

class QueryExecute(AAZHttpOperation):
CLIENT_TYPE = "AAZMicrosoftInsightsDataPlaneClient_application_insights"

def __call__(self, *args, **kwargs):
request = self.make_request()
session = self.client.send_request(request=request, stream=False, **kwargs)
if session.http_response.status_code in [200]:
return self.on_200(session)

return self.on_error(session.http_response)

@property
def url(self):
return self.client.format_url(
"/v1/apps/{appId}/query",
**self.url_parameters
)

@property
def method(self):
return "POST"

@property
def error_format(self):
return "ODataV4Format"

@property
def url_parameters(self):
parameters = {
**self.serialize_url_param(
"appId", self.ctx.args.app_id,
required=True,
),
}
return parameters

@property
def header_parameters(self):
parameters = {
**self.serialize_header_param(
"Content-Type", "application/json",
),
**self.serialize_header_param(
"Accept", "application/json",
),
}
return parameters

@property
def content(self):
_content_value, _builder = self.new_content_builder(
self.ctx.args,
typ=AAZObjectType,
typ_kwargs={"flags": {"required": True, "client_flatten": True}}
)
_builder.set_prop("applications", AAZListType, ".applications")
_builder.set_prop("query", AAZStrType, ".query", typ_kwargs={"flags": {"required": True}})
_builder.set_prop("timespan", AAZStrType, ".timespan")

applications = _builder.get(".applications")
if applications is not None:
applications.set_elements(AAZStrType, ".")

return self.serialize_content(_content_value)

def on_200(self, session):
data = self.deserialize_http_content(session)
self.ctx.set_var(
"instance",
data,
schema_builder=self._build_schema_on_200
)

_schema_on_200 = None

@classmethod
def _build_schema_on_200(cls):
if cls._schema_on_200 is not None:
return cls._schema_on_200

cls._schema_on_200 = AAZObjectType()

_schema_on_200 = cls._schema_on_200
_schema_on_200.tables = AAZListType(
flags={"required": True},
)

tables = cls._schema_on_200.tables
tables.Element = AAZObjectType()

_element = cls._schema_on_200.tables.Element
_element.columns = AAZListType(
flags={"required": True},
)
_element.name = AAZStrType(
flags={"required": True},
)
_element.rows = AAZListType(
flags={"required": True},
)

columns = cls._schema_on_200.tables.Element.columns
columns.Element = AAZObjectType()

_element = cls._schema_on_200.tables.Element.columns.Element
_element.name = AAZStrType()
_element.type = AAZStrType()

rows = cls._schema_on_200.tables.Element.rows
rows.Element = AAZListType()

_element = cls._schema_on_200.tables.Element.rows.Element
_element.Element = AAZAnyType()

return cls._schema_on_200


class _QueryExecuteHelper:
"""Helper class for QueryExecute"""


__all__ = ["QueryExecute"]
Loading
Loading