File: __init__.py

package info (click to toggle)
python-azure 20181112%2Bgit-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 407,300 kB
  • sloc: python: 717,190; makefile: 201; sh: 76
file content (178 lines) | stat: -rw-r--r-- 6,079 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#-------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
#--------------------------------------------------------------------------
from enum import Enum

class ProfileDefinition(object):
    """Allow to define a custom Profile definition.

    Note::

    The dict format taken as input is yet to be confirmed and should
    *not* be considered as stable in the current implementation.

    :param dict profile_dict: A profile dictionnary
    :param str label: A label for pretty printing
    """
    def __init__(self, profile_dict, label=None):
        self._profile_dict = profile_dict
        self._label = label

    @property
    def label(self):
        """The label associated to this profile definition.
        """
        return self._label

    def __repr__(self):
        return self._label if self._label else self._profile_dict.__repr__()

    def get_profile_dict(self):
        """Return the current profile dict.

        This is internal information, and content should not be considered stable.
        """
        return self._profile_dict


class DefaultProfile(object):
    """Store a default profile.

    :var ProfileDefinition profile: The default profile as class attribute
    """
    profile = None

    def use(self, profile):
        """Define a new default profile."""
        if not isinstance(profile, (KnownProfiles, ProfileDefinition)):
            raise ValueError("Can only set as default a ProfileDefinition or a KnownProfiles")
        type(self).profile = profile

    def definition(self):
        return type(self).profile

class KnownProfiles(Enum):
    """This defines known Azure Profiles.

    There is two meta-profiles:

    - latest : will always use latest available api-version on each package
    - default : mutable, will define profile automatically for all packages

    If you change default, this changes all created packages on the fly to
    this profile. This can be used to switch a complete set of API Version
    without re-creating all clients.
    """

    # default - This is a meta-profile and point to another profile
    default = DefaultProfile()
    # latest - This is a meta-profile and does not contain definitions
    latest = ProfileDefinition(None, "latest")
    v2017_03_09_profile = ProfileDefinition(
        {
            "azure.keyvault.KeyVaultClient":{
                None: "2016-10-01"
            },
            "azure.mgmt.authorization.AuthorizationManagementClient": {
                None: "2015-07-01"
            },
            "azure.mgmt.compute.ComputeManagementClient": {
                None: "2016-03-30"
            },
            "azure.mgmt.keyvault.KeyVaultManagementClient":{
                None: "2016-10-01"
            },
            "azure.mgmt.network.NetworkManagementClient": {
                None: "2015-06-15"
            },
            "azure.mgmt.storage.StorageManagementClient": {
                None: "2016-01-01"
            },
            "azure.mgmt.resource.policy.PolicyClient": {
                None: "2015-10-01-preview"
            },
            "azure.mgmt.resource.locks.ManagementLockClient": {
                None: "2015-01-01"
            },
            "azure.mgmt.resource.links.ManagementLinkClient": {
                None: "2016-09-01"
            },
            "azure.mgmt.resource.resources.ResourceManagementClient": {
                None: "2016-02-01"
            },
            "azure.mgmt.resource.subscriptions.SubscriptionClient": {
                None: "2016-06-01"
            }
        },
        "2017-03-09-profile"
    )
    v2018_03_01_hybrid = ProfileDefinition(
        {
            "azure.keyvault.KeyVaultClient":{
                None: "2016-10-01"
            },
            "azure.mgmt.authorization.AuthorizationManagementClient": {
                None: "2015-07-01"
            },
            "azure.mgmt.compute.ComputeManagementClient": {
                None: "2017-03-30"
            },
            "azure.mgmt.keyvault.KeyVaultManagementClient":{
                None: "2016-10-01"
            },
            "azure.mgmt.network.NetworkManagementClient": {
                None: "2017-10-01"
            },
            "azure.mgmt.storage.StorageManagementClient": {
                None: "2016-01-01"
            },
            "azure.mgmt.resource.policy.PolicyClient": {
                None: "2016-12-01"
            },
            "azure.mgmt.resource.locks.ManagementLockClient": {
                None: "2016-09-01"
            },
            "azure.mgmt.resource.links.ManagementLinkClient": {
                None: "2016-09-01"
            },
            "azure.mgmt.resource.resources.ResourceManagementClient": {
                None: "2018-02-01"
            },
            "azure.mgmt.resource.subscriptions.SubscriptionClient": {
                None: "2016-06-01"
            },
            "azure.mgmt.dns": {
                None: "2016-04-01"
            }
        },
        "2018-03-01-hybrid"
    )


    def __init__(self, profile_definition):
        self._profile_definition = profile_definition

    def use(self, profile):
        if self is not type(self).default:
            raise ValueError("use can only be used for `default` profile")
        self.value.use(profile)

    def definition(self):
        if self is not type(self).default:
            raise ValueError("use can only be used for `default` profile")
        return self.value.definition()

    @classmethod
    def from_name(cls, profile_name):
        if profile_name == "default":
            return cls.default
        for profile in cls:
            if isinstance(profile.value, ProfileDefinition) and profile.value.label == profile_name:
                return profile
        raise ValueError("No profile called {}".format(profile_name))


# Default profile is floating "latest"
KnownProfiles.default.use(KnownProfiles.latest)