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

[EventHub] az eventhubs namespace application-group: New command group to support application group management #23045

Merged
merged 13 commits into from
Jun 30, 2022
6 changes: 3 additions & 3 deletions src/azure-cli-core/azure/cli/core/profiles/_shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def default_api_version(self):
ResourceType.DATA_STORAGE_QUEUE: '2018-03-28',
ResourceType.DATA_COSMOS_TABLE: '2017-04-17',
ResourceType.MGMT_SERVICEBUS: '2021-06-01-preview',
ResourceType.MGMT_EVENTHUB: '2021-11-01',
ResourceType.MGMT_EVENTHUB: '2022-01-01-preview',
ResourceType.MGMT_MONITOR: SDKProfile('2019-06-01', {
'action_groups': '2021-09-01',
'activity_log_alerts': '2017-04-01',
Expand Down Expand Up @@ -288,7 +288,7 @@ def default_api_version(self):
ResourceType.DATA_STORAGE_QUEUE: '2019-07-07',
ResourceType.DATA_COSMOS_TABLE: '2017-04-17',
ResourceType.MGMT_APPSERVICE: '2018-02-01',
ResourceType.MGMT_EVENTHUB: '2021-11-01',
ResourceType.MGMT_EVENTHUB: '2022-01-01-preview',
ResourceType.MGMT_SERVICEBUS: '2021-06-01-preview',
ResourceType.MGMT_IOTHUB: '2019-07-01-preview',
ResourceType.MGMT_DATABOXEDGE: '2019-08-01',
Expand Down Expand Up @@ -335,7 +335,7 @@ def default_api_version(self):
# to have commands show up in the hybrid profile which happens to have the latest
# API versions
ResourceType.MGMT_APPSERVICE: '2018-02-01',
ResourceType.MGMT_EVENTHUB: '2021-11-01',
ResourceType.MGMT_EVENTHUB: '2022-01-01-preview',
ResourceType.MGMT_SERVICEBUS: '2021-06-01-preview',
ResourceType.MGMT_IOTHUB: '2019-03-22',
ResourceType.MGMT_DATABOXEDGE: '2019-08-01'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@ def private_link_mgmt_client_factory(cli_ctx, _):

def schema_registry_mgmt_client_factory(cli_ctx, _):
return cf_eventhub(cli_ctx).schema_registry


def application_group_mgmt_client_factory(cli_ctx, _):
return cf_eventhub(cli_ctx).application_group
72 changes: 72 additions & 0 deletions src/azure-cli/azure/cli/command_modules/eventhubs/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,14 @@
text: az eventhubs namespace network-rule list --resource-group myresourcegroup --namespace-name mynamespace
"""

helps['eventhubs namespace network-rule update'] = """
type: command
short-summary: Updates network rule properties of the given Namespace.
examples:
- name: Updates network rule properties of the given Namespace, can be used to update public network access, trusted service and default action.
text: az eventhubs namespace network-rule update --resource-group myresourcegroup --namespace-name mynamespace --public-network-access Enabled
"""

helps['eventhubs namespace network-rule remove'] = """
type: command
short-summary: Remove network rule for a namespace
Expand Down Expand Up @@ -684,3 +692,67 @@
text: |
az eventhubs namespace schema-registry delete --namespace-name mynamespace -g MyResourceGroup --name mySchemaGroup
"""

helps['eventhubs namespace application-group'] = """
type: group
short-summary: Manages Application Groups for premium eventhubs namespace.
"""

helps['eventhubs namespace application-group policy'] = """
type: group
short-summary: Append or Remove application group policies.
"""

helps['eventhubs namespace application-group create'] = """
type: command
short-summary: Creates an application group for an EventHub namespace
examples:
- name: Create an application group myAppGroup for eventhub namespace mynamespace with 2 throttling policies. Maximum allowed throttling policies is 4.
text: |
az eventhubs namespace application-group create --namespace-name mynamespace -g MyResourceGroup --name myAppGroup --client-app-group-identifier SASKeyName=keyname --throttling-policy-config name=policy1 metric-id=IncomingMessages rate-limit-threshold=10000 --throttling-policy-config name=policy2 metric-id=IncomingBytes rate-limit-threshold=20000
"""

helps['eventhubs namespace application-group update'] = """
type: command
short-summary: Updates an application group in an EventHub namespace
examples:
- name: Updates an application group myAppGroup for eventhub namespace mynamespace.
text: |
az eventhubs namespace application-group update --namespace-name mynamespace -g MyResourceGroup --name myAppGroup --is-enabled false
"""

helps['eventhubs namespace application-group show'] = """
type: command
short-summary: Gets an application group in an EventHub namespace
examples:
- name: Gets an application group myAppGroup for eventhub namespace mynamespace.
text: |
az eventhubs namespace application-group show --namespace-name mynamespace -g MyResourceGroup --name myAppGroup
"""

helps['eventhubs namespace application-group list'] = """
type: command
short-summary: Lists all application groups in an EventHub namespace
examples:
- name: Lists all application groups in eventhub namespace mynamespace.
text: |
az eventhubs namespace application-group list --namespace-name mynamespace -g MyResourceGroup
"""

helps['eventhubs namespace application-group policy add'] = """
type: command
short-summary: Appends an application group policy to the existing policy. This cmdlet can be used to append one or more throttling policies.
examples:
- name: Append 2 throttling policies to an application group. Maximum allowed throttling policies is 4.
text: |
az eventhubs namespace application-group policy add --namespace-name mynamespace -g MyResourceGroup --name myAppGroup --throttling-policy-config name=policy1 metric-id=OutgoingMessages rate-limit-threshold=10500 --throttling-policy-config name=policy2 metric-id=IncomingBytes rate-limit-threshold=20000
"""

helps['eventhubs namespace application-group policy remove'] = """
type: command
short-summary: Removes an application group policy from the existing policies. This cmdlet can be used to remove one or more throttling policies.
examples:
- name: Removes a throttling policy from an applicatin group myAppGroup.
text: |
az eventhubs namespace application-group policy remove --namespace-name mynamespace -g MyResourceGroup --name myAppGroup --throttling-policy-config name=policy1 metric-id=OutgoingMessages rate-limit-threshold=10500
"""
36 changes: 34 additions & 2 deletions src/azure-cli/azure/cli/command_modules/eventhubs/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def load_arguments_eh(self, _):
from knack.arguments import CLIArgumentType
from azure.cli.core.profiles import ResourceType
(KeyType, AccessRights, SkuName, KeySource) = self.get_models('KeyType', 'AccessRights', 'SkuName', 'KeySource', resource_type=ResourceType.MGMT_EVENTHUB)
from azure.cli.command_modules.eventhubs.action import AlertAddEncryption
from azure.cli.command_modules.eventhubs.action import AlertAddEncryption, ConstructPolicy

rights_arg_type = CLIArgumentType(options_list=['--rights'], nargs='+', arg_type=get_enum_type(AccessRights), validator=validate_rights, help='Space-separated list of Authorization rule rights')
key_arg_type = CLIArgumentType(options_list=['--key'], arg_type=get_enum_type(KeyType), help='specifies Primary or Secondary key needs to be reset')
Expand Down Expand Up @@ -204,6 +204,15 @@ def load_arguments_eh(self, _):
c.argument('namespace_name', options_list=['--namespace-name'], id_part=None, help='Name of the Namespace')
c.extra('vnet_name', arg_group='Virtual Network Rule', options_list=['--vnet-name'], help='Name of the Virtual Network')

with self.argument_context('eventhubs namespace network-rule update', resource_type=ResourceType.MGMT_EVENTHUB, min_api='2017-04-01') as c:
c.argument('public_network_access', options_list=['--public-network-access', '--public-network'], arg_type=get_enum_type(['Enabled', 'Disabled']), help='This determines if traffic is allowed over public network. By default it is enabled. If value is SecuredByPerimeter then Inbound and Outbound communication is controlled by the network security perimeter and profile\' access rules.')
c.argument('trusted_service_access_enabled', options_list=['--enable-trusted-service-access', '-t'],
arg_type=get_three_state_flag(),
help='A boolean value that indicates whether Trusted Service Access is enabled for Network Rule Set.')
c.argument('default_action', arg_group='networkrule', options_list=['--default-action'],
arg_type=get_enum_type(['Allow', 'Deny']),
help='Default Action for Network Rule Set.')

with self.argument_context('eventhubs namespace network-rule add', resource_type=ResourceType.MGMT_EVENTHUB, min_api='2017-04-01') as c:
c.argument('ignore_missing_vnet_service_endpoint', arg_group='Virtual Network Rule', options_list=['--ignore-missing-endpoint'], arg_type=get_three_state_flag(), help='A boolean value that indicates whether to ignore missing vnet Service Endpoint')
c.argument('action', arg_group='IP Address Rule', options_list=['--action'], arg_type=get_enum_type(['Allow']), help='Action of the IP rule')
Expand Down Expand Up @@ -247,7 +256,7 @@ def load_arguments_eh(self, _):
with self.argument_context('eventhubs namespace encryption', resource_type=ResourceType.MGMT_EVENTHUB) as c:
c.argument('namespace_name', options_list=['--namespace-name'], id_part=None, help='Name of the Namespace')

for scope in ['eventhubs namespace encryption add', 'eventhubs namespace identity remove']:
for scope in ['eventhubs namespace encryption add', 'eventhubs namespace encryption remove']:
with self.argument_context(scope, resource_type=ResourceType.MGMT_EVENTHUB) as c:
c.argument('encryption_config', action=AlertAddEncryption, nargs='+', help='List of KeyVaultProperties objects.')

Expand All @@ -265,3 +274,26 @@ def load_arguments_eh(self, _):
c.argument('schema_type', options_list=['--schema-type'], arg_type=get_enum_type(['Avro']), help='Type of Schema')
c.argument('tags', options_list=['--group-properties'], arg_type=tags_type,
help='Type of Schema')

# Application Group
with self.argument_context('eventhubs namespace application-group') as c:
c.argument('namespace_name', options_list=['--namespace-name'], arg_type=namespace_name_arg_type, help='Name of Namespace')
c.argument('application_group_name', arg_type=name_type, id_part='child_name_1', help='Name of Application Group')

for scope in ['eventhubs namespace application-group create', 'eventhubs namespace application-group list']:
with self.argument_context(scope) as c:
c.argument('namespace_name', options_list=['--namespace-name'], id_part=None, help='Name of Namespace')
c.argument('application_group_name', arg_type=name_type, id_part=None, help='Name of Application Group')

for scope in ['eventhubs namespace application-group create', 'eventhubs namespace application-group update']:
with self.argument_context(scope) as c:
c.argument('is_enabled', arg_type=get_three_state_flag(),
help='Determines if Application Group is allowed to create connection with namespace or not. '
'Once the isEnabled is set to false, all the existing connections of application group gets dropped and no new connections will be allowed')

with self.argument_context('eventhubs namespace application-group create') as c:
c.argument('client_app_group_identifier', options_list=['--client-app-group-identifier', '--client-app-group-id'], help='The Unique identifier for application group.Supports SAS(SASKeyName=KeyName) or AAD(AADAppID=Guid)')

for scope in ['eventhubs namespace application-group policy add', 'eventhubs namespace application-group policy remove', 'eventhubs namespace application-group create']:
with self.argument_context(scope) as c:
c.argument('throttling_policy_config', action=ConstructPolicy, options_list=['--throttling-policy-config', '--throttling-policy', '--policy-config'], nargs='+', help='List of Throttling Policy Objects')
48 changes: 45 additions & 3 deletions src/azure-cli/azure/cli/command_modules/eventhubs/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ def __call__(self, parser, namespace, values, option_string=None):
super(AlertAddEncryption, self).__call__(parser, namespace, action, option_string)

def get_action(self, values, option_string): # pylint: disable=no-self-use
from azure.mgmt.eventhub.v2021_11_01.models import KeyVaultProperties
from azure.mgmt.eventhub.v2021_11_01.models import UserAssignedIdentityProperties
from azure.mgmt.eventhub.v2022_01_01_preview.models import KeyVaultProperties
from azure.mgmt.eventhub.v2022_01_01_preview.models import UserAssignedIdentityProperties
from azure.cli.core.azclierror import InvalidArgumentValueError
from azure.cli.core import CLIError

keyVaultObject = KeyVaultProperties()

for (k, v) in (x.split('=', 1) for x in values):
Expand All @@ -49,3 +48,46 @@ def get_action(self, values, option_string): # pylint: disable=no-self-use
keyVaultObject.key_version = ''

return keyVaultObject


class ConstructPolicy(argparse._AppendAction):
def __call__(self, parser, namespace, values, option_string=None):
action = self.get_action(values, option_string)
super(ConstructPolicy, self).__call__(parser, namespace, action, option_string)

def get_action(self, values, option_string): # pylint: disable=no-self-use
from azure.mgmt.eventhub.v2022_01_01_preview.models import ThrottlingPolicy
from azure.cli.core import CLIError
from azure.cli.core.azclierror import InvalidArgumentValueError
from azure.cli.command_modules.eventhubs.constants import INCOMING_BYTES, INCOMING_MESSAGES, OUTGOING_MESSAGES, OUTGOING_BYTES

for (k, v) in (x.split('=', 1) for x in values):
if k == 'name':
name = v

elif k == 'rate-limit-threshold':
if v.isdigit() is False:
raise CLIError('rate-limit-threshold should be an integer')
rate_limit_threshold = int(v)

elif k == 'metric-id':
if v.lower() == INCOMING_MESSAGES.lower():
metric_id = INCOMING_MESSAGES
elif v.lower() == INCOMING_BYTES.lower():
metric_id = INCOMING_BYTES
elif v.lower() == OUTGOING_MESSAGES.lower():
metric_id = OUTGOING_MESSAGES
elif v.lower() == OUTGOING_BYTES.lower():
metric_id = OUTGOING_BYTES
else:
raise CLIError('Only allowed values for metric_id are: {0}, {1}, {2}, {3}'.format(INCOMING_MESSAGES, INCOMING_BYTES, OUTGOING_MESSAGES, OUTGOING_BYTES))

else:
raise InvalidArgumentValueError("Invalid Argument for:'{}' Only allowed arguments are 'name, rate-limit-threshold and metric-id'".format(option_string))

if (name is None) or (metric_id is None) or (rate_limit_threshold is None):
raise CLIError('One of the throttling policies is missing one of these parameters: name, metric-id, rate-limit-threshold')

throttlingPolicy = ThrottlingPolicy(name=name, rate_limit_threshold=rate_limit_threshold, metric_id=metric_id)

return throttlingPolicy
22 changes: 21 additions & 1 deletion src/azure-cli/azure/cli/command_modules/eventhubs/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ def load_command_table(self, _):
cluster_mgmt_client_factory,
private_endpoint_connections_mgmt_client_factory,
private_link_mgmt_client_factory,
schema_registry_mgmt_client_factory)
schema_registry_mgmt_client_factory,
application_group_mgmt_client_factory)

eh_namespace_util = CliCommandType(
operations_tmpl='azure.mgmt.eventhub.operations#NamespacesOperations.{}',
Expand Down Expand Up @@ -61,6 +62,12 @@ def load_command_table(self, _):
resource_type=ResourceType.MGMT_EVENTHUB
)

eh_application_group_util = CliCommandType(
operations_tmpl='azure.mgmt.eventhub.operations#ApplicationGroupOperations.{}',
client_factory=schema_registry_mgmt_client_factory,
resource_type=ResourceType.MGMT_EVENTHUB
)

from ._validator import validate_subnet


Expand Down Expand Up @@ -160,6 +167,7 @@ def load_command_table(self, _):
g.custom_command('add', 'cli_networkrule_createupdate', validator=validate_subnet)
g.show_command('list', 'get_network_rule_set')
g.custom_command('remove', 'cli_networkrule_delete', validator=validate_subnet)
g.custom_command('update', 'cli_networkrule_update')

# Identity Region
with self.command_group('eventhubs namespace identity', eh_namespace_util, min_api='2021-06-01-preview', resource_type=ResourceType.MGMT_EVENTHUB, client_factory=namespaces_mgmt_client_factory) as g:
Expand All @@ -177,3 +185,15 @@ def load_command_table(self, _):
g.command('list', 'list_by_namespace')
g.show_command('show', 'get')
g.command('delete', 'delete')

# ApplicationGroup Region
with self.command_group('eventhubs namespace application-group', eh_application_group_util, resource_type=ResourceType.MGMT_EVENTHUB, client_factory=application_group_mgmt_client_factory) as g:
g.custom_command('create', 'cli_appgroup_create')
g.custom_command('update', 'cli_appgroup_update')
g.show_command('show', 'get')
g.command('delete', 'delete')
g.command('list', 'list_by_namespace')

with self.command_group('eventhubs namespace application-group policy', eh_application_group_util, resource_type=ResourceType.MGMT_EVENTHUB, client_factory=application_group_mgmt_client_factory) as g:
g.custom_command('add', 'cli_add_appgroup_policy')
g.custom_command('remove', 'cli_remove_appgroup_policy')
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

INCOMING_MESSAGES = 'IncomingMessages'
INCOMING_BYTES = 'IncomingBytes'
OUTGOING_MESSAGES = 'OutgoingMessages'
OUTGOING_BYTES = 'OutgoingBytes'
Loading