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#  pylint: disable=unused-import
7from azure.cli.core.profiles._shared import AZURE_API_PROFILES, ResourceType, CustomResourceType, PROFILE_TYPE,\
8    SDKProfile, AD_HOC_API_VERSIONS
9
10
11def get_api_version(cli_ctx, resource_type, as_sdk_profile=False):
12    """ Get the current API version for a given resource_type.
13
14    :param resource_type: The resource type.
15    :type resource_type: ResourceType.
16    :param bool as_sdk_profile: Return SDKProfile instance.
17    :returns: The API version
18     Can return a tuple<operation_group, str> if the resource_type supports SDKProfile.
19    :rtype: str or tuple[str]
20    """
21    from azure.cli.core.profiles._shared import get_api_version as _sdk_get_api_version
22    return _sdk_get_api_version(cli_ctx.cloud.profile, resource_type, as_sdk_profile)
23
24
25def supported_api_version(cli_ctx, resource_type, min_api=None, max_api=None, operation_group=None):
26    """ Method to check if the current API version for a given resource_type is supported.
27        If resource_type is set to None, the current profile version will be used as the basis of
28        the comparison.
29
30    :param resource_type: The resource type.
31    :type resource_type: ResourceType.
32    :param min_api: The minimum API that is supported (inclusive). Omit for no minimum constraint.
33    "type min_api: str
34    :param max_api: The maximum API that is supported (inclusive). Omit for no maximum constraint.
35    :type max_api: str
36    :returns: True if the current API version of resource_type satisfies the min/max constraints. False otherwise.
37     Can return a tuple<operation_group, bool> if the resource_type supports SDKProfile.
38    :rtype: bool or tuple[bool]
39    """
40    from azure.cli.core.profiles._shared import supported_api_version as _sdk_supported_api_version
41    return _sdk_supported_api_version(cli_ctx.cloud.profile,
42                                      resource_type=resource_type,
43                                      min_api=min_api,
44                                      max_api=max_api,
45                                      operation_group=operation_group)
46
47
48def supported_resource_type(cli_ctx, resource_type):
49    from azure.cli.core.profiles._shared import supported_resource_type as _supported_resource_type
50    return _supported_resource_type(cli_ctx.cloud.profile,
51                                    resource_type=resource_type)
52
53
54def get_sdk(cli_ctx, resource_type, *attr_args, **kwargs):
55    """ Get any SDK object that's versioned using the current API version for resource_type.
56        Supported keyword arguments:
57            checked - A boolean specifying if this method should suppress/check import exceptions
58                        or not. By default, None is returned.
59            mod - A string specifying the submodule that all attr_args should be prefixed with.
60            operation_group - A string specifying the operation group name we want models.
61
62        Example usage:
63            Get a single SDK model.
64            TableService = get_sdk(resource_type, 'table#TableService')
65
66            File, Directory = get_sdk(resource_type,
67                                        'file.models#File',
68                                        'file.models#Directory')
69
70            Same as above but get multiple models where File and Directory are both part of
71            'file.models' and we don't want to specify each full path.
72            File, Directory = get_sdk(resource_type,
73                                      'File',
74                                      'Directory',
75                                      mod='file.models')
76            VirtualMachine = get_sdk(resource_type,
77                                     'VirtualMachine',
78                                     mod='models',
79                                     operation_group='virtual_machines')
80
81    :param resource_type: The resource type.
82    :type resource_type: ResourceType.
83    :param attr_args: Positional arguments for paths to objects to get.
84    :type attr_args: str
85    :param kwargs: Keyword arguments.
86    :type kwargs: str
87    :returns: object -- e.g. an SDK module, model, enum, attribute. The number of objects returned
88                        depends on len(attr_args).
89    """
90    from azure.cli.core.profiles._shared import get_versioned_sdk as _sdk_get_versioned_sdk
91    return _sdk_get_versioned_sdk(cli_ctx.cloud.profile, resource_type, *attr_args, **kwargs)
92
93
94# API Profiles currently supported in the CLI.
95API_PROFILES = {
96    'latest': AZURE_API_PROFILES['latest'],
97    '2017-03-09-profile': AZURE_API_PROFILES['2017-03-09-profile'],
98    '2018-03-01-hybrid': AZURE_API_PROFILES['2018-03-01-hybrid'],
99    '2019-03-01-hybrid': AZURE_API_PROFILES['2019-03-01-hybrid'],
100    '2020-09-01-hybrid': AZURE_API_PROFILES['2020-09-01-hybrid']
101}
102
103
104def register_resource_type(profile_name, resource_type, api_version):
105    err_msg = "Failed to add resource type to profile '{p}': "
106    if not isinstance(resource_type, CustomResourceType):
107        raise TypeError((err_msg + "resource_type should be of type {c}, got {r}.").format(p=profile_name,
108                                                                                           c=CustomResourceType,
109                                                                                           r=type(resource_type)))
110    try:
111        API_PROFILES[profile_name].update({resource_type: api_version})
112    except KeyError:
113        raise ValueError((err_msg + "Profile '{p}' not found.").format(p=profile_name))
114