1# --------------------------------------------------------------------------------------------
2# Copyright (c) Microsoft Corporation. All rights reserved.
3# Licensed under the MIT License. See License.txt in the project root for license information.
4# --------------------------------------------------------------------------------------------
5
6
7# pylint: disable=inconsistent-return-statements
8def ex_handler_factory(creating_plan=False, no_throw=False):
9    def _polish_bad_errors(ex):
10        import json
11        from knack.util import CLIError
12        try:
13            detail = json.loads(ex.response.text)['Message']
14            if creating_plan:
15                if 'Requested features are not supported in region' in detail:
16                    detail = ("Plan with linux worker is not supported in current region. For " +
17                              "supported regions, please refer to https://docs.microsoft.com/"
18                              "azure/app-service-web/app-service-linux-intro")
19                elif 'Not enough available reserved instance servers to satisfy' in detail:
20                    detail = ("Plan with Linux worker can only be created in a group " +
21                              "which has never contained a Windows worker, and vice versa. " +
22                              "Please use a new resource group. Original error:" + detail)
23            ex = CLIError(detail)
24        except Exception:  # pylint: disable=broad-except
25            pass
26        if no_throw:
27            return ex
28        raise ex
29    return _polish_bad_errors
30
31
32def web_client_factory(cli_ctx, api_version=None, **_):
33    from azure.cli.core.profiles import ResourceType
34    from azure.cli.core.commands.client_factory import get_mgmt_service_client
35    return get_mgmt_service_client(cli_ctx, ResourceType.MGMT_APPSERVICE, api_version=api_version)
36
37
38def dns_client_factory(cli_ctx, api_version=None, **_):
39    from azure.cli.core.profiles import ResourceType
40    from azure.cli.core.commands.client_factory import get_mgmt_service_client
41    return get_mgmt_service_client(cli_ctx, ResourceType.MGMT_NETWORK_DNS, api_version=api_version)
42
43
44def providers_client_factory(cli_ctx):
45    from azure.cli.core.profiles import ResourceType
46    from azure.cli.core.commands.client_factory import get_mgmt_service_client
47    return get_mgmt_service_client(cli_ctx, ResourceType.MGMT_RESOURCE_RESOURCES).providers
48
49
50def customlocation_client_factory(cli_ctx, api_version=None, **_):
51    from azure.cli.core.profiles import ResourceType
52    from azure.cli.core.commands.client_factory import get_mgmt_service_client
53    return get_mgmt_service_client(cli_ctx, ResourceType.MGMT_CUSTOMLOCATION, api_version=api_version)
54
55
56def cf_plans(cli_ctx, _):
57    return web_client_factory(cli_ctx).app_service_plans
58
59
60def cf_webapps(cli_ctx, _):
61    return web_client_factory(cli_ctx).web_apps
62
63
64def cf_providers(cli_ctx, _):
65    return web_client_factory(cli_ctx).provider  # pylint: disable=no-member
66
67
68def cf_web_client(cli_ctx, _):
69    return web_client_factory(cli_ctx)
70