File: test_mgmt_resource_locks_test.py

package info (click to toggle)
python-azure 20250603%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 851,724 kB
  • sloc: python: 7,362,925; ansic: 804; javascript: 287; makefile: 195; sh: 145; xml: 109
file content (172 lines) | stat: -rw-r--r-- 6,761 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
# coding: utf-8

# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------

# coverd ops:
#   management_locks: 16/16
#   authorization_operations: 1/1

import unittest
import pytest

import azure.mgmt.resource
from devtools_testutils import AzureMgmtRecordedTestCase, RandomNameResourceGroupPreparer, recorded_by_proxy
import pytest


@pytest.mark.live_test_only
class TestMgmtResourceLocks(AzureMgmtRecordedTestCase):

    def setup_method(self, method):
        self.locks_client = self.create_mgmt_client(azure.mgmt.resource.ManagementLockClient)

        self.resource_client = self.create_mgmt_client(azure.mgmt.resource.ResourceManagementClient)

    @pytest.mark.skip(reason="authorization failed, need to add white_list")
    @recorded_by_proxy
    def test_locks_at_subscription_level(self):
        lock_name = "pylockrg"

        lock = self.locks_client.management_locks.create_or_update_at_subscription_level(
            lock_name, {"level": "CanNotDelete"}
        )
        assert lock is not None

        self.locks_client.management_locks.get_at_subscription_level(lock_name)

        locks = list(self.locks_client.management_locks.list_at_subscription_level())

        lock = self.locks_client.management_locks.delete_at_subscription_level(lock_name)

    @pytest.mark.skip(reason="authorization failed, need to add white_list")
    @RandomNameResourceGroupPreparer()
    @recorded_by_proxy
    def test_locks_by_scope(self, resource_group, location):
        lock_name = "pylockrg"
        SUBSCRIPTION_ID = self.get_settings_value("SUBSCRIPTION_ID")
        resource_name = self.get_resource_name("pytestavset")

        resource_id = "/subscriptions/{guid}/resourceGroups/{resourcegroupname}/providers/{resourceprovidernamespace}/{resourcetype}/{resourcename}".format(
            guid=SUBSCRIPTION_ID,
            resourcegroupname=resource_group.name,
            resourceprovidernamespace="Microsoft.Compute",
            resourcetype="availabilitySets",
            resourcename=resource_name,
        )

        create_result = self.resource_client.resources.begin_create_or_update_by_id(
            resource_id, parameters={"location": location}, api_version="2019-07-01"
        )

        lock = self.locks_client.management_locks.create_or_update_by_scope(
            resource_id, lock_name, {"level": "CanNotDelete"}
        )

        self.locks_client.management_locks.get_by_scope(resource_id, lock_name)

        self.locks_client.management_locks.list_by_scope(resource_id)

        self.locks_client.management_locks.delete_by_scope(resource_id, lock_name)

        result = self.resource_client.resources.begin_delete_by_id(resource_id, api_version="2019-07-01")
        result = result.result()

    @pytest.mark.skip(reason="authorization failed, need to add white_list")
    @RandomNameResourceGroupPreparer()
    @recorded_by_proxy
    def test_locks_at_resource_level(self, resource_group, location):
        lock_name = "pylockrg"
        resource_name = self.get_resource_name("pytestavset")

        # create resource
        create_result = self.resource_client.resources.begin_create_or_update(
            resource_group_name=resource_group.name,
            resource_provider_namespace="Microsoft.Compute",
            parent_resource_path="",
            resource_type="availabilitySets",
            resource_name=resource_name,
            parameters={"location": location},
            api_version="2019-07-01",
        )

        lock = self.locks_client.management_locks.create_or_update_at_resource_level(
            resource_group_name=resource_group.name,
            resource_provider_namespace="Microsoft.Compute",
            parent_resource_path="",
            resource_type="availabilitySets",
            resource_name=resource_name,
            lock_name=lock_name,
            parameters={"level": "CanNotDelete"},
        )
        assert lock is not None

        self.locks_client.management_locks.get_at_resource_level(
            resource_group_name=resource_group.name,
            resource_provider_namespace="Microsoft.Compute",
            parent_resource_path="",
            resource_type="availabilitySets",
            resource_name=resource_name,
            lock_name=lock_name,
        )

        locks = list(
            self.locks_client.management_locks.list_at_resource_level(
                resource_group_name=resource_group.name,
                resource_provider_namespace="Microsoft.Compute",
                parent_resource_path="",
                resource_type="availabilitySets",
                resource_name=resource_name,
            )
        )
        assert len(locks) == 1

        lock = self.locks_client.management_locks.delete_at_resource_level(
            resource_group_name=resource_group.name,
            resource_provider_namespace="Microsoft.Compute",
            parent_resource_path="",
            resource_type="availabilitySets",
            resource_name=resource_name,
            lock_name=lock_name,
        )

        # delete resource
        delete_result = self.resource_client.resources.begin_delete(
            resource_group_name=resource_group.name,
            resource_provider_namespace="Microsoft.Compute",
            parent_resource_path="",
            resource_type="availabilitySets",
            resource_name=resource_name,
            api_version="2019-07-01",
        )
        delete_result.wait()

    @pytest.mark.skip(reason="authorization failed, need to add white_list")
    @RandomNameResourceGroupPreparer()
    @recorded_by_proxy
    def test_locks_at_resource_group_level(self, resource_group, location):
        lock_name = "pylockrg"

        lock = self.locks_client.management_locks.create_or_update_at_resource_group_level(
            resource_group.name, lock_name, {"level": "CanNotDelete"}
        )
        assert lock is not None

        self.locks_client.management_locks.get_at_resource_group_level(resource_group.name, lock_name)

        locks = list(self.locks_client.management_locks.list_at_resource_group_level(resource_group.name))
        assert len(locks) == 1

        lock = self.locks_client.management_locks.delete_at_resource_group_level(resource_group.name, lock_name)

    @recorded_by_proxy
    def test_operations(self):
        self.locks_client.authorization_operations.list()


# ------------------------------------------------------------------------------
if __name__ == "__main__":
    unittest.main()