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

[Breadth Coverage] Log Analytics Solution #1545

Merged
merged 5 commits into from
Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 1 addition & 2 deletions src/log-analytics-solution/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ Manage Log Analytics Solution: [more info](https://docs.microsoft.com/en-us/azur
az monitor log-analytics solution create \
--resource-group MyResourceGroup \
--name Containers({SolutionName}) \
--location "East US" \
--plan-publisher Microsoft \
--plan-product "OMSGallery/Containers" \
--workspace-resource-id "/subscriptions/{SubID}/resourceGroups/{ResourceGroup}/providers/ \
--workspace "/subscriptions/{SubID}/resourceGroups/{ResourceGroup}/providers/ \
Microsoft.OperationalInsights/workspaces/{WorkspaceName}" \
--tags key=value
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
- name: Create a log-analytics solution for the plan product of OMSGallery/Containers
text: |-
az monitor log-analytics solution create --resource-group MyResourceGroup \\
--name Containers({SolutionName}) --location "East US" --tags key=value \\
--name Containers({SolutionName}) --tags key=value \\
--plan-publisher Microsoft --plan-product "OMSGallery/Containers" \\
--workspace-resource-id "/subscriptions/{SubID}/resourceGroups/{ResourceGroup}/providers/ \\
--workspace "/subscriptions/{SubID}/resourceGroups/{ResourceGroup}/providers/ \\
Microsoft.OperationalInsights/workspaces/{WorkspaceName}"
"""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@

from azure.cli.core.commands.parameters import (
tags_type,
resource_group_name_type,
get_location_type
resource_group_name_type
)
from azure.cli.core.commands.validators import get_default_location_from_resource_group
from ._validators import validate_workspace_resource_id
from knack.arguments import CLIArgumentType

Expand All @@ -24,18 +22,15 @@
def load_arguments(self, _):

with self.argument_context('monitor log-analytics solution create') as c:
c.ignore('location')
c.argument('solution_name', solution_name)
c.argument('location', arg_type=get_location_type(self.cli_ctx),
validator=get_default_location_from_resource_group)
c.argument('tags', tags_type)
c.argument('plan_publisher', help='Publisher name of the plan for solution. For gallery solution, it is Microsoft.')
c.argument('plan_product', help='Product name of the plan for solution. '
'For Microsoft published gallery solution it should be in the format of OMSGallery/<solutionType>. This is case sensitive.')
c.argument('workspace_resource_id', options_list=['--workspace-resource-id', '-w'],
c.argument('workspace_resource_id', options_list=['--workspace', '-w'],
validator=validate_workspace_resource_id,
help='The resource ID of the log analytics workspace with which the solution will be linked.')
c.argument('workspace_name', options_list=['--workspace-name'],
help='The name of the log analytics workspace with which the solution will be linked.')
help='The name or resource ID of the log analytics workspace with which the solution will be linked.')

with self.argument_context('monitor log-analytics solution update') as c:
c.argument('solution_name', solution_name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,33 @@
from msrestazure.tools import is_valid_resource_id, parse_resource_id
from knack.util import CLIError
from azure.mgmt.loganalytics import LogAnalyticsManagementClient
from azure.cli.core.commands.client_factory import get_mgmt_service_client
from azure.cli.core.commands.client_factory import get_mgmt_service_client, get_subscription_id
from msrestazure.tools import resource_id


def validate_workspace_resource_id(cmd, namespace):

if namespace.workspace_resource_id:
# If the workspace_resource_id is invalid, use it as a workspace name to splice the workspace_resource_id
if not is_valid_resource_id(namespace.workspace_resource_id):
namespace.workspace_resource_id = resource_id(
subscription=get_subscription_id(cmd.cli_ctx),
resource_group=namespace.resource_group_name,
namespace='microsoft.OperationalInsights',
type='workspaces',
name=namespace.workspace_resource_id
)

if not is_valid_resource_id(namespace.workspace_resource_id):
raise CLIError('usage error: --workspace-resource-id is invalid')
raise CLIError('usage error: --workspace is invalid, it must be name of resource id of workspace')
Copy link
Member

Choose a reason for hiding this comment

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

or


# Determine whether the workspace already exists
workspace_param = parse_resource_id(namespace.workspace_resource_id)
if workspace_param['resource_group'] != namespace.resource_group_name:
raise CLIError('usage error: workspace and solution must be under the same resource group')

workspaces_client = get_mgmt_service_client(cmd.cli_ctx, LogAnalyticsManagementClient).workspaces
workspaces = workspaces_client.get(workspace_param['resource_group'], workspace_param['resource_name'])

if workspaces and namespace.location:
if workspaces.location != namespace.location:
raise CLIError('usage error: workspace and solution must be under the same location')

if namespace.workspace_resource_id is None and namespace.workspace_name is None:
raise CLIError('usage error: please specify only one of --workspace-resource-id and --workspace-name')

if namespace.workspace_resource_id and namespace.workspace_name:
raise CLIError('usage error: please specify only one of --workspace-resource-id and --workspace-name, not both')
# The location of solution is the same as the location of the workspace
namespace.location = workspaces.location
35 changes: 16 additions & 19 deletions src/log-analytics-solution/azext_log_analytics_solution/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,24 @@
# pylint: disable=too-many-locals
# pylint: disable=unused-argument

from azure.cli.core.commands.client_factory import get_subscription_id
from azure.cli.core.util import sdk_no_wait


def create_monitor_log_analytics_solution(cmd,
client,
def create_monitor_log_analytics_solution(client,
resource_group_name,
solution_name,
plan_publisher,
plan_product,
location=None,
workspace_resource_id=None,
workspace_name=None,
tags=None):
if workspace_name:
reference_workspace = '/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.OperationalInsights/' \
'workspaces/{2}'.format(get_subscription_id(cmd.cli_ctx), resource_group_name,
workspace_name)
else:
reference_workspace = workspace_resource_id
workspace_resource_id,
location,
tags=None,
no_wait=False):

body = {
'location': location,
'tags': tags,
'properties': {
"workspace_resource_id": reference_workspace
"workspace_resource_id": workspace_resource_id
},
"plan": {
"name": solution_name,
Expand All @@ -42,21 +35,25 @@ def create_monitor_log_analytics_solution(cmd,
}
}

return client.create_or_update(resource_group_name=resource_group_name, solution_name=solution_name, parameters=body)
return sdk_no_wait(no_wait, client.create_or_update, resource_group_name=resource_group_name,
solution_name=solution_name, parameters=body)


def update_monitor_log_analytics_solution(client,
resource_group_name,
solution_name,
tags=None):
tags=None,
no_wait=False):

return client.update(resource_group_name=resource_group_name, solution_name=solution_name, tags=tags)
return sdk_no_wait(no_wait, client.update, resource_group_name=resource_group_name,
solution_name=solution_name, tags=tags)


def delete_monitor_log_analytics_solution(client,
resource_group_name,
solution_name):
return client.delete(resource_group_name=resource_group_name, solution_name=solution_name)
solution_name,
no_wait=False):
return sdk_no_wait(no_wait, client.delete, resource_group_name=resource_group_name, solution_name=solution_name)


def get_monitor_log_analytics_solution(client,
Expand Down
Loading