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

[App Service] az staticwebapp enterprise-edge: Move command group from extension to official CLI #22818

Merged
merged 4 commits into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
20 changes: 20 additions & 0 deletions src/azure-cli/azure/cli/command_modules/appservice/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -2605,6 +2605,26 @@
text: az staticwebapp functions show -n MyStaticAppName -g MyResourceGroup
"""

helps['staticwebapp enterprise-edge'] = """
type: group
short-summary: Manage the Azure Front Door CDN for static webapps. For optimal experience and availability please check our documentation https://aka.ms/swaedge
"""

helps['staticwebapp enterprise-edge enable'] = """
type: command
short-summary: Enable the Azure Front Door CDN for a static webapp. Enabling enterprise-grade edge requires re-registration for the Azure Front Door Microsoft.CDN resource provider. For optimal experience and availability please check our documentation https://aka.ms/swaedge
"""

helps['staticwebapp enterprise-edge disable'] = """
type: command
short-summary: Disable the Azure Front Door CDN for a static webapp. For optimal experience and availability please check our documentation https://aka.ms/swaedge
"""

helps['staticwebapp enterprise-edge show'] = """
type: command
short-summary: Show the status (Enabled, Disabled, Enabling, Disabling) of the Azure Front Door CDN for a webapp. For optimal experience and availability please check our documentation https://aka.ms/swaedge
"""

helps['webapp deploy'] = """
type: command
short-summary: Deploys a provided artifact to Azure Web Apps.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1061,3 +1061,6 @@ def load_arguments(self, _):
with self.argument_context('staticwebapp functions link') as c:
c.argument('function_resource_id', help="Resource ID of the functionapp to link. Can be retrieved with 'az functionapp --query id'")
c.argument('force', help="Force the function link even if the function is already linked to a static webapp. May be needed if the function was previously linked to a static webapp.")

with self.argument_context('staticwebapp enterprise-edge') as c:
c.argument("no_register", help="Don't try to register the Microsoft.CDN provider. Registration can be done manually with: az provider register --wait --namespace Microsoft.CDN. For more details, please review the documentation available at https://go.microsoft.com/fwlink/?linkid=2184995 .", default=False)
Copy link
Member

@wangzelin007 wangzelin007 Jun 13, 2022

Choose a reason for hiding this comment

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

Suggested change
c.argument("no_register", help="Don't try to register the Microsoft.CDN provider. Registration can be done manually with: az provider register --wait --namespace Microsoft.CDN. For more details, please review the documentation available at https://go.microsoft.com/fwlink/?linkid=2184995 .", default=False)
c.argument("no_register", help="Enabling enterprise-grade edge requires reregistration for the Azure Front Door Microsoft.CDN resource provider. Please register the provider with: az provider register --wait --namespace Microsoft.CDN. For more details, please review the documentation available at https://go.microsoft.com/fwlink/?linkid=2184995 .", default=False)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestion, but I think this change may confuse users. The reason I wrote the help text as "Don't try to register..." is because the --no-register argument is a flag that, if included, makes the command not register the Microsoft.CDN RP. Also, we shouldn't tell the user to "please register the RP with " -- this is just provided as an alternative if they want to do it manually. By default, the command does register the RP

Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,11 @@ def load_command_table(self, _):
g.custom_command('unlink', 'unlink_user_function', validator=validate_staticsite_sku)
g.custom_show_command('show', 'get_user_function', validator=validate_staticsite_sku)

with self.command_group('staticwebapp enterprise-edge', custom_command_type=staticsite_sdk) as g:
g.custom_command('enable', 'enable_staticwebapp_enterprise_edge')
g.custom_command('disable', 'disable_staticwebapp_enterprise_edge')
g.custom_show_command('show', 'show_staticwebapp_enterprise_edge_status')

with self.command_group('logicapp') as g:
g.custom_command('delete', 'delete_function_app', confirmation=True)
g.custom_command('stop', 'stop_webapp')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@

from azure.cli.core.commands import LongRunningOperation
from azure.cli.core.azclierror import (ResourceNotFoundError, ValidationError, RequiredArgumentMissingError,
InvalidArgumentValueError)
InvalidArgumentValueError, UnauthorizedError)
from knack.log import get_logger
from msrestazure.tools import parse_resource_id

from .utils import normalize_sku_for_staticapp, raise_missing_token_suggestion
from .custom import show_app, _build_identities_info
from ._client_factory import providers_client_factory


logger = get_logger(__name__)
Expand Down Expand Up @@ -596,3 +597,59 @@ def unlink_user_function(cmd, name, resource_group_name):
def get_user_function(cmd, name, resource_group_name):
client = _get_staticsites_client_factory(cmd.cli_ctx)
return client.get_user_provided_function_apps_for_static_site(name=name, resource_group_name=resource_group_name)


def _enterprise_edge_warning():
logger.warning("For optimal experience and availability please check our documentation https://aka.ms/swaedge")


def _update_enterprise_edge(cmd, name, resource_group_name, enable: bool):
client = _get_staticsites_client_factory(cmd.cli_ctx)
site = client.get_static_site(resource_group_name, name)
site.enterprise_grade_cdn_status = "enabled" if enable else "disabled"
return client.update_static_site(resource_group_name=resource_group_name, name=name, static_site_envelope=site)


def _register_cdn_provider(cmd):
from azure.mgmt.resource.resources.models import ProviderRegistrationRequest, ProviderConsentDefinition

namespace = "Microsoft.CDN"
properties = ProviderRegistrationRequest(third_party_provider_consent=ProviderConsentDefinition(
consent_to_authorization=True))

client = providers_client_factory(cmd.cli_ctx)
try:
client.register(namespace, properties=properties)
except Exception as e:
msg = "Server responded with error message : {} \n"\
"Enabling enterprise-grade edge requires reregistration for the Azure Front "\
"Door Microsoft.CDN resource provider. We were unable to perform that reregistration on your "\
"behalf. Please check with your admin on permissions and review the documentation available at "\
"https://go.microsoft.com/fwlink/?linkid=2185350. "\
"Or try running registration manually with: az provider register --wait --namespace Microsoft.CDN"
raise UnauthorizedError(msg.format(e.args)) from e


def enable_staticwebapp_enterprise_edge(cmd, name, resource_group_name, no_register=False):
_enterprise_edge_warning()
if not no_register:
_register_cdn_provider(cmd)
_update_enterprise_edge(cmd, name, resource_group_name, enable=True)
return _get_enterprise_edge_status(cmd, name, resource_group_name)


def disable_staticwebapp_enterprise_edge(cmd, name, resource_group_name):
_enterprise_edge_warning()
_update_enterprise_edge(cmd, name, resource_group_name, enable=False)
return _get_enterprise_edge_status(cmd, name, resource_group_name)


def _get_enterprise_edge_status(cmd, name, resource_group_name):
client = _get_staticsites_client_factory(cmd.cli_ctx)
site = client.get_static_site(resource_group_name, name)
return {"enterpriseGradeCdnStatus": site.enterprise_grade_cdn_status}


def show_staticwebapp_enterprise_edge_status(cmd, name, resource_group_name):
_enterprise_edge_warning()
return _get_enterprise_edge_status(cmd, name, resource_group_name)