diff --git a/src/enterprise-edge/HISTORY.rst b/src/enterprise-edge/HISTORY.rst index 8c34bccfff8..8afa59dba2d 100644 --- a/src/enterprise-edge/HISTORY.rst +++ b/src/enterprise-edge/HISTORY.rst @@ -3,6 +3,10 @@ Release History =============== +0.1.1 +++++++ +* Automatically register Microsoft.CDN resource provider upon running enable command + 0.1.0 ++++++ * Initial release. \ No newline at end of file diff --git a/src/enterprise-edge/azext_enterprise_edge/_help.py b/src/enterprise-edge/azext_enterprise_edge/_help.py index 94a0236e534..e0a4ba534ea 100644 --- a/src/enterprise-edge/azext_enterprise_edge/_help.py +++ b/src/enterprise-edge/azext_enterprise_edge/_help.py @@ -14,7 +14,7 @@ helps['staticwebapp enterprise-edge enable'] = """ type: command - short-summary: Enable the Azure Front Door CDN for a static webapp. For optimal experience and availability please check our documentation https://aka.ms/swaedge + 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'] = """ diff --git a/src/enterprise-edge/azext_enterprise_edge/_params.py b/src/enterprise-edge/azext_enterprise_edge/_params.py index 4d11e3dc0b9..97bf9153519 100644 --- a/src/enterprise-edge/azext_enterprise_edge/_params.py +++ b/src/enterprise-edge/azext_enterprise_edge/_params.py @@ -17,3 +17,6 @@ def load_arguments(self, _): with self.argument_context('staticwebapp enterprise-edge') as c: c.argument("name", arg_type=staticsite_name_arg_type) c.argument("resource_group_name", arg_type=resource_group_name_type) + + 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) diff --git a/src/enterprise-edge/azext_enterprise_edge/custom.py b/src/enterprise-edge/azext_enterprise_edge/custom.py index 35c2ec02f51..388bcbe579c 100644 --- a/src/enterprise-edge/azext_enterprise_edge/custom.py +++ b/src/enterprise-edge/azext_enterprise_edge/custom.py @@ -10,6 +10,7 @@ from azure.cli.core.util import send_raw_request from azure.cli.core.commands.client_factory import get_subscription_id +from azure.cli.command_modules.appservice._client_factory import providers_client_factory logger = get_logger(__name__) @@ -37,11 +38,11 @@ def _request(cls, cmd, resource_group, name, http_method="GET", body=None): return r @classmethod - def set(cls, cmd, resource_group, name, enable): + def set(cls, cmd, resource_group, name, enable, no_register=False): params = cls.get(cmd, resource_group, name).json() - if enable: - cls._validate_cdn_provider_registered(cmd) + if enable and not no_register: + cls._register_cdn_provider(cmd) cls._validate_sku(params["sku"].get("name")) params["properties"]["enterpriseGradeCdnStatus"] = "enabled" if enable else "disabled" @@ -52,18 +53,24 @@ def get(cls, cmd, resource_group, name): return cls._request(cmd, resource_group, name) @classmethod - def _validate_cdn_provider_registered(cls, cmd): - management_hostname = cmd.cli_ctx.cloud.endpoints.resource_manager - api_version = "2021-04-01" - sub_id = get_subscription_id(cmd.cli_ctx) - url_fmt = "{}/subscriptions/{}/providers/Microsoft.CDN?api-version={}" - - request_url = url_fmt.format(management_hostname.strip('/'), sub_id, api_version) - - registration = send_raw_request(cmd.cli_ctx, "GET", request_url).json().get("registrationState").lower() - if registration != "registered": - raise CLIError("Provider Microsoft.CDN is not registered. " - "Please run 'az provider register --wait --namespace Microsoft.CDN'") + def _register_cdn_provider(cls, 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 CLIError(msg.format(e.args)) from e @classmethod def _validate_sku(cls, sku_name): @@ -77,18 +84,19 @@ def _format_show_response(cmd, name, resource_group_name): return {"enterpriseGradeCdnStatus": staticsite_data["properties"]["enterpriseGradeCdnStatus"]} -def enable_staticwebapp_enterprise_edge(cmd, name, resource_group_name): - logger.warn("For optimal experience and availability please check our documentation https://aka.ms/swaedge") - StaticWebAppFrontDoorClient.set(cmd, name=name, resource_group=resource_group_name, enable=True) +def enable_staticwebapp_enterprise_edge(cmd, name, resource_group_name, no_register=False): + logger.warning("For optimal experience and availability please check our documentation https://aka.ms/swaedge") + StaticWebAppFrontDoorClient.set(cmd, name=name, resource_group=resource_group_name, enable=True, + no_register=no_register) return _format_show_response(cmd, name, resource_group_name) def disable_staticwebapp_enterprise_edge(cmd, name, resource_group_name): - logger.warn("For optimal experience and availability please check our documentation https://aka.ms/swaedge") + logger.warning("For optimal experience and availability please check our documentation https://aka.ms/swaedge") StaticWebAppFrontDoorClient.set(cmd, name=name, resource_group=resource_group_name, enable=False) return _format_show_response(cmd, name, resource_group_name) def show_staticwebapp_enterprise_edge_status(cmd, name, resource_group_name): - logger.warn("For optimal experience and availability please check our documentation https://aka.ms/swaedge") + logger.warning("For optimal experience and availability please check our documentation https://aka.ms/swaedge") return _format_show_response(cmd, name, resource_group_name) diff --git a/src/enterprise-edge/setup.py b/src/enterprise-edge/setup.py index 866c200ebc1..e645d07ecef 100644 --- a/src/enterprise-edge/setup.py +++ b/src/enterprise-edge/setup.py @@ -16,7 +16,7 @@ # TODO: Confirm this is the right version number you want and it matches your # HISTORY.rst entry. -VERSION = '0.1.0' +VERSION = '0.1.1' # The full list of classifiers is available at # https://pypi.python.org/pypi?%3Aaction=list_classifiers