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

az webpubsub update for Azure Web PubSub Service GA #4001

Merged
merged 7 commits into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
5 changes: 5 additions & 0 deletions src/webpubsub/HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
Release History
===============

1.0.0
++++++
* Add command group `az webpubsub hub`
* Remove command group `az webpubsub event-handlers`
zackliu marked this conversation as resolved.
Show resolved Hide resolved

0.2.1
++++++
* Add argument `--user-id` to command: `az webpubsub client start`
Expand Down
23 changes: 21 additions & 2 deletions src/webpubsub/azext_webpubsub/_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,39 @@
import argparse
from azure.cli.core.azclierror import InvalidArgumentValueError
from knack.log import get_logger
from .vendored_sdks.azure_mgmt_webpubsub.models import EventHandlerTemplate
from .vendored_sdks.azure_mgmt_webpubsub.models import EventHandler, UpstreamAuthSettings, ManagedIdentitySettings

logger = get_logger(__name__)


# pylint: disable=protected-access, too-few-public-methods
class EventHandlerTemplateUpdateAction(argparse._AppendAction):
# --event-handler urlTemplate="" user_event_pattern="" system_event="" system_event="" auth_type="" auth_resource=""
def __call__(self, parser, namespace, values, option_string=None):
kwargs = {}
auth_type = None
auth_resource = None
system_events = []
for item in values:
try:
key, value = item.split('=', 1)

if key == 'system-event':
system_events.append(value)
continue
elif key == 'auth-type':
auth_type = value
continue
elif key == 'auth-resource':
auth_resource = value
continue

kwargs[key.replace('-', '_')] = value
except ValueError:
raise InvalidArgumentValueError('usage error: {} KEY=VALUE [KEY=VALUE ...]'.format(option_string))
value = EventHandlerTemplate(**kwargs)
if auth_type is not None:
kwargs['auth'] = UpstreamAuthSettings(type=auth_type, managed_identity=ManagedIdentitySettings(resource=auth_resource))
if system_events:
kwargs['system_events'] = system_events
value = EventHandler(**kwargs)
super().__call__(parser, namespace, value, option_string)
4 changes: 4 additions & 0 deletions src/webpubsub/azext_webpubsub/_client_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ def _webpubsub_client_factory(cli_ctx, *_):

def cf_webpubsub(cli_ctx, *_):
return _webpubsub_client_factory(cli_ctx).web_pub_sub


def cf_webpubsubhub(cli_ctx, *_):
return _webpubsub_client_factory(cli_ctx).web_pub_sub_hubs
52 changes: 20 additions & 32 deletions src/webpubsub/azext_webpubsub/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@
short-summary: Commands to manage Webpubsub keys.
"""

helps['webpubsub event-handler'] = """
type: group
short-summary: Commands to manage Webpubsub event handler settings.
"""

helps['webpubsub event-handler hub'] = """
helps['webpubsub hub'] = """
type: group
short-summary: Commands to manage Webpubsub hub settings.
"""
Expand Down Expand Up @@ -138,50 +133,43 @@
az webpubsub network-rule update --public-network --connection-name MyPrivateEndpointConnection1 MyPrivateEndpointConnection2 -n MyWebPubSub -g MyResourceGroup --deny ClientConnection
"""

helps['webpubsub event-handler show'] = """
helps['webpubsub hub show'] = """
type: command
short-summary: Show event handler settings for WebPubSub Service.
short-summary: Show hub settings for WebPubSub Service.
"""

helps['webpubsub event-handler clear'] = """
helps['webpubsub hub list'] = """
type: command
short-summary: Clear event handler settings for WebPubSub Service.
short-summary: List all hub settings for WebPubSub Service.
"""

helps['webpubsub event-handler update'] = """
helps['webpubsub hub delete'] = """
type: command
short-summary: Update event handler settings for WebPubSub Service with json. For updating settings per hub, see help in hub subgroup.
examples:
- name: Update event handler to handler connect event.
text: >
az webpubsub event-handler update -n MyWebPubSub -g MyResourceGroup --items '{\"myHub\": [{\"urlTemplate\": \"http://host.com\", \"systemEventPattern\": \"connect\"}]}'
short-summary: Delete hub settings for WebPubSub Service.
"""

helps['webpubsub event-handler update'] = """
helps['webpubsub hub create'] = """
type: command
short-summary: Update event handler settings for WebPubSub Service.
short-summary: Create hub settings for WebPubSub Service.
examples:
- name: Update event handler to handler connect event.
- name: Create a hub setting with two event handler settings
text: >
az webpubsub event-handler update -n MyWebPubSub -g MyResourceGroup --items '{\"myHub\": [{\"urlTemplate\": \"http://host.com\", \"systemEventPattern\": \"connect\"}]}'
"""

helps['webpubsub event-handler hub remove'] = """
type: command
short-summary: Remove a hub's event handler settings
examples:
- name: Remove all event handler settings in a hub
az webpubsub hub create -n MyWebPubSub -g MyResourceGroup --hub-name MyHub --event-handler url-template="http://host.com" user-event-pattern="MyEvent" --event-handler url-template="http://host2.com" system-event="connected" system-event="disconnected" auth-type="ManagedIdentity" auth-resource="uri://myUri"
- name: Create a hub setting with anonymous connection allowed
text: >
az webpubsub event-handler hub remove -n MyWebPubSub -g MyResourceGroup --hub-name MyHub
az webpubsub hub create -n MyWebPubSub -g MyResourceGroup --hub-name MyHub --allow-anonymous true
"""

helps['webpubsub event-handler hub update'] = """
helps['webpubsub hub update'] = """
type: command
short-summary: Update a hub's event handler settings
short-summary: Update hub settings for WebPubSub Service.
examples:
- name: Update two event handler settings in a hub
- name: Update event handler settings of a hub
text: >
az webpubsub hub update -n MyWebPubSub -g MyResourceGroup --hub-name MyHub --event-handler url-template="http://host.com" user-event-pattern="MyEvent" --event-handler url-template="http://host2.com" system-event="connected" system-event="disconnected" auth-type="ManagedIdentity" auth-resource="uri://myUri"
- name: Update to allow anonymous connection
text: >
az webpubsub event-handler hub update -n MyWebPubSub -g MyResourceGroup --hub-name MyHub --template url-template='http://host.com user-event-pattern="MyEvent" --template url-template="http://host2.com" system-event-pattern="connect"'
az webpubsub hub update -n MyWebPubSub -g MyResourceGroup --hub-name MyHub --allow-anonymous true
"""

helps['webpubsub client start'] = """
Expand Down
21 changes: 14 additions & 7 deletions src/webpubsub/azext_webpubsub/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
EventHandlerTemplateUpdateAction
)

WEBPUBSUB_KEY_TYPE = ['primary', 'secondary']
WEBPUBSUB_KEY_TYPE = ['primary', 'secondary', 'salt']
SKU_TYPE = ['Standard_S1', 'Free_F1']
PERMISSION_TYPE = ['joinLeaveGroup', 'sendToGroup']

Expand All @@ -24,6 +24,7 @@ def load_arguments(self, _):
from azure.cli.core.commands.validators import get_default_location_from_resource_group

webpubsub_name_type = CLIArgumentType(options_list='--webpubsub-name-name', help='Name of the Webpubsub.', id_part='name')
webpubsubhub_name_type = CLIArgumentType(help='Name of the hub.', id_part='child_name_1')

with self.argument_context('webpubsub') as c:
c.argument('tags', tags_type)
Expand All @@ -48,14 +49,20 @@ def load_arguments(self, _):
c.argument('allow', arg_type=get_enum_type(WebPubSubRequestType), nargs='*', help='The allowed virtual network rule. Space-separeted list of scope to assign.', type=WebPubSubRequestType, required=False)
c.argument('deny', arg_type=get_enum_type(WebPubSubRequestType), nargs='*', help='The denied virtual network rule. Space-separeted list of scope to assign.', type=WebPubSubRequestType, required=False)

with self.argument_context('webpubsub event-handler update') as c:
c.argument('items', help='A JSON-formatted string containing event handler items')
for scope in ['webpubsub hub delete',
'webpubsub hub show']:
with self.argument_context(scope) as c:
c.argument('hub_name', webpubsubhub_name_type)

with self.argument_context('webpubsub event-handler hub') as c:
c.argument('hub_name', help='The hub whose event handler settings need to delete.')
for scope in ['webpubsub hub update',
'webpubsub hub create']:
with self.argument_context(scope) as c:
c.argument('hub_name', help='The hub to manage')
c.argument('event_handler', action=EventHandlerTemplateUpdateAction, nargs='*', help='Template item for event handler settings. Use key=value pattern to set properties. Supported keys are "url-template", "user-event-pattern", "system-event", "auth-type" and "auth-resource".')
c.argument('allow_anonymous', arg_type=get_three_state_flag(), help='Set if anonymous connections are allowed for this hub')

with self.argument_context('webpubsub event-handler hub update') as c:
c.argument('template', action=EventHandlerTemplateUpdateAction, nargs='+', help='Template item for event handler settings. Use key=value pattern to set properties. Supported keys are "url-template", "user-event-pattern", "system-event-pattern".')
with self.argument_context('webpubsub hub list') as c:
c.argument('webpubsub_name', webpubsub_name_type, options_list=['--name', '-n'], id_part=None)

with self.argument_context('webpubsub client') as c:
c.argument('hub_name', help='The hub which client connects to')
Expand Down
2 changes: 1 addition & 1 deletion src/webpubsub/azext_webpubsub/azext_metadata.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"azext.isPreview": true,
"azext.isPreview": false,
"azext.minCliCoreVersion": "2.22.0"
}
21 changes: 10 additions & 11 deletions src/webpubsub/azext_webpubsub/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from azure.cli.core.commands import CliCommandType
from azure.cli.core.util import empty_on_404
from ._client_factory import cf_webpubsub
from ._client_factory import cf_webpubsubhub


def load_command_table(self, _):
Expand All @@ -26,9 +27,9 @@ def load_command_table(self, _):
client_factory=cf_webpubsub
)

webpubsub_eventhandler_utils = CliCommandType(
webpubsub_hub_utils = CliCommandType(
operations_tmpl='azext_webpubsub.eventhandler#{}',
client_factory=cf_webpubsub
client_factory=cf_webpubsubhub
)

webpubsub_client_utils = CliCommandType(
Expand All @@ -41,7 +42,7 @@ def load_command_table(self, _):
client_factory=cf_webpubsub
)

with self.command_group('webpubsub', webpubsub_general_utils, is_preview=True) as g:
with self.command_group('webpubsub', webpubsub_general_utils) as g:
g.command('create', 'webpubsub_create')
g.command('delete', 'webpubsub_delete')
g.command('list', 'webpubsub_list')
Expand All @@ -59,14 +60,12 @@ def load_command_table(self, _):
g.show_command('show', 'list_network_rules')
g.command('update', 'update_network_rules')

with self.command_group('webpubsub event-handler', webpubsub_eventhandler_utils) as g:
g.show_command('show', 'event_handler_list')
g.command('update', 'event_handler_update')
g.command('clear', 'event_handler_clear')

with self.command_group('webpubsub event-handler hub', webpubsub_eventhandler_utils) as g:
g.command('remove', 'event_handler_hub_remove')
g.command('update', 'event_handler_hub_update')
with self.command_group('webpubsub hub', webpubsub_hub_utils) as g:
g.command('delete', 'hub_delete')
g.generic_update_command('update', getter_name='get_hub', setter_name='set_hub', custom_func_name='update', custom_func_type=webpubsub_hub_utils)
g.command('create', 'hub_create')
g.show_command('show', 'hub_show', exception_handler=empty_on_404)
g.command('list', 'hub_list')

with self.command_group('webpubsub client', webpubsub_client_utils) as g:
g.command('start', 'start_client')
Expand Down
61 changes: 29 additions & 32 deletions src/webpubsub/azext_webpubsub/eventhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,45 @@
# --------------------------------------------------------------------------------------------
# pylint: disable=line-too-long

import json
from .vendored_sdks.azure_mgmt_webpubsub.models import (
WebPubSubResource,
EventHandlerSettings
WebPubSubHubProperties,
WebPubSubHub,
)
from .vendored_sdks.azure_mgmt_webpubsub.operations import (
WebPubSubHubsOperations
)


def hub_create(client: WebPubSubHubsOperations, resource_group_name, webpubsub_name, hub_name, event_handler=None, allow_anonymous=False):
anonymous_connect_policy = 'allow' if allow_anonymous else 'deny'
properties = WebPubSubHubProperties(anonymous_connect_policy=anonymous_connect_policy, event_handlers=event_handler)
parameters = WebPubSubHub(properties=properties)
return client.begin_create_or_update(hub_name, resource_group_name, webpubsub_name, parameters)


def hub_delete(client: WebPubSubHubsOperations, resource_group_name, webpubsub_name, hub_name):
return client.begin_delete(hub_name, resource_group_name, webpubsub_name)

def event_handler_list(client, resource_group_name, webpubsub_name):
resource = client.get(resource_group_name, webpubsub_name)
return resource.event_handler

def hub_show(client: WebPubSubHubsOperations, resource_group_name, webpubsub_name, hub_name):
return client.get(hub_name, resource_group_name, webpubsub_name)

def event_handler_update(client, resource_group_name, webpubsub_name, items):
parsedItem = json.loads(items)
event_handler = EventHandlerSettings(items=parsedItem)
parameters = WebPubSubResource(event_handler=event_handler)
return client.begin_update(resource_group_name, webpubsub_name, parameters)

def hub_list(client: WebPubSubHubsOperations, resource_group_name, webpubsub_name):
return client.list(resource_group_name, webpubsub_name)

def event_handler_clear(client, resource_group_name, webpubsub_name):
event_handler = EventHandlerSettings(items={})
parameters = WebPubSubResource(event_handler=event_handler)
return client.begin_update(resource_group_name, webpubsub_name, parameters)

def get_hub(client: WebPubSubHubsOperations, resource_group_name, webpubsub_name, hub_name):
return client.get(hub_name, resource_group_name, webpubsub_name)

def event_handler_hub_update(client, resource_group_name, webpubsub_name, hub_name, template):
event_handler = client.get(resource_group_name, webpubsub_name).event_handler
if event_handler is None or event_handler.items is None:
event_handler = EventHandlerSettings(items={})
items = event_handler.items

items[hub_name] = template
parameters = WebPubSubResource(event_handler=event_handler)
print(parameters.event_handler)
return client.begin_update(resource_group_name, webpubsub_name, parameters)
def set_hub(client: WebPubSubHubsOperations, resource_group_name, webpubsub_name, hub_name, parameters):
return client.begin_create_or_update(hub_name, resource_group_name, webpubsub_name, parameters)


def event_handler_hub_remove(client, resource_group_name, webpubsub_name, hub_name):
event_handler = client.get(resource_group_name, webpubsub_name).event_handler
if event_handler is None or event_handler.items is None:
event_handler = EventHandlerSettings(items={})
items = event_handler.items
items.pop(hub_name, None)
parameters = WebPubSubResource(event_handler=event_handler)
return client.begin_update(resource_group_name, webpubsub_name, parameters)
def update(instance: WebPubSubHub, event_handler=None, allow_anonymous=None):
if allow_anonymous is not None:
instance.properties.anonymous_connect_policy = 'allow' if allow_anonymous else 'deny'
if event_handler is not None:
instance.properties.event_handlers = event_handler
return instance
Loading