File: test_cli_mgmt_appconfiguration_async.py

package info (click to toggle)
python-azure 20201208%2Bgit-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,437,920 kB
  • sloc: python: 4,287,452; javascript: 269; makefile: 198; sh: 187; xml: 106
file content (366 lines) | stat: -rw-r--r-- 13,727 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# 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.
#--------------------------------------------------------------------------


# TEST SCENARIO COVERAGE
# ----------------------
# Methods Total   : 17
# Methods Covered : 16
# Examples Total  : 19
# Examples Tested : 19
# Coverage %      : 94.11764705882352
# ----------------------

# covered ops:
#   configuration_stores: 9/9
#   operations: 2/2
#   private_endpoint_connections: 4/4
#   private_link_resources: 2/2

import time
import unittest

import azure.mgmt.appconfiguration
from devtools_testutils import AzureMgmtTestCase, RandomNameResourceGroupPreparer

from _aio_testcase import AzureMgmtAsyncTestCase

AZURE_LOCATION = 'eastus'
KEY_UUID = "test_key_a6af8952-54a6-11e9-b600-2816a84d0309"
LABEL_UUID = "1d7b2b28-549e-11e9-b51c-2816a84d0309"
KEY = "PYTHON_UNIT_" + KEY_UUID
LABEL = "test_label1_" + LABEL_UUID
TEST_CONTENT_TYPE = "test content type"
TEST_VALUE = "test value"

class MgmtAppConfigurationTest(AzureMgmtAsyncTestCase):

    def setUp(self):
        super(MgmtAppConfigurationTest, self).setUp()
        from azure.mgmt.appconfiguration.aio import AppConfigurationManagementClient
        self.mgmt_client = self.create_mgmt_aio_client(
            AppConfigurationManagementClient
        )

        if self.is_live:
            import azure.mgmt.network
            self.network_client = self.create_mgmt_client(
              azure.mgmt.network.NetworkManagementClient
            )

    def create_kv(self, connection_str):
        from azure.appconfiguration import (
            AzureAppConfigurationClient,
            ConfigurationSetting
        )
        app_config_client = AzureAppConfigurationClient.from_connection_string(connection_str)
        kv = ConfigurationSetting(
            key=KEY,
            label=LABEL,
            value=TEST_VALUE,
            content_type=TEST_CONTENT_TYPE,
            tags={"tag1": "tag1", "tag2": "tag2"}
        )
        created_kv = app_config_client.add_configuration_setting(kv)
        return created_kv
        

    # TODO: update to track 2 version later
    def create_endpoint(self, group_name, vnet_name, sub_net, endpoint_name, conf_store_id):
        # Create VNet
        async_vnet_creation = self.network_client.virtual_networks.create_or_update(
            group_name,
            vnet_name,
            {
                'location': AZURE_LOCATION,
                'address_space': {
                    'address_prefixes': ['10.0.0.0/16']
                }
            }
        )
        async_vnet_creation.wait()

        # Create Subnet
        async_subnet_creation = self.network_client.subnets.create_or_update(
            group_name,
            vnet_name,
            sub_net,
            {
              'address_prefix': '10.0.0.0/24',
               'private_link_service_network_policies': 'disabled',
               'private_endpoint_network_policies': 'disabled'
            }
        )
        subnet_info = async_subnet_creation.result()

        # Create private endpoint
        BODY = {
          "location": "eastus",
          "properties": {
            "privateLinkServiceConnections": [
              # {
              #   "name": PRIVATE_LINK_SERVICES,  # TODO: This is needed, but was not showed in swagger.
              #   "private_link_service_id": "/subscriptions/" + SUBSCRIPTION_ID + "/resourceGroups/" + RESOURCE_GROUP + "/providers/Microsoft.Network/privateLinkServices/" + PRIVATE_LINK_SERVICES,
              # },
              {
                "name": "myconnection",
                # "private_link_service_id": "/subscriptions/" + self.settings.SUBSCRIPTION_ID + "/resourceGroups/" + group_name + "/providers/Microsoft.Storage/storageAccounts/" + STORAGE_ACCOUNT_NAME + ""
                "private_link_service_id": conf_store_id,
                "group_ids": ["configurationStores"]
              }
            ],
            "subnet": {
              "id": "/subscriptions/" + self.settings.SUBSCRIPTION_ID + "/resourceGroups/" + group_name + "/providers/Microsoft.Network/virtualNetworks/" + vnet_name + "/subnets/" + sub_net
            }
          }
        }
        result = self.network_client.private_endpoints.create_or_update(group_name, endpoint_name, BODY)

        return result.result()

    @RandomNameResourceGroupPreparer(location=AZURE_LOCATION)
    def test_appconfiguration_list_key_values(self, resource_group):
        CONFIGURATION_STORE_NAME = self.get_resource_name("configuration")

        # ConfigurationStores_Create[put]
        BODY = {
          "location": "westus",
          "sku": {
            "name": "Standard"  # Free can not use private endpoint
          },
          "tags": {
            "my_tag": "myTagValue"
          }
        }
        result = self.event_loop.run_until_complete(
            self.mgmt_client.configuration_stores.begin_create(resource_group.name, CONFIGURATION_STORE_NAME, BODY)
        )
        result = self.event_loop.run_until_complete(
            result.result()
        )

        # ConfigurationStores_ListKeys[post]
        keys = self.to_list(
            self.mgmt_client.configuration_stores.list_keys(resource_group.name, CONFIGURATION_STORE_NAME)
        )

        # ConfigurationStores_RegenerateKey[post]
        BODY = {
          "id": keys[0].id
        }
        key = self.event_loop.run_until_complete(
            self.mgmt_client.configuration_stores.regenerate_key(resource_group.name, CONFIGURATION_STORE_NAME, BODY)
        )

        if self.is_live:
            # create key-value
            self.create_kv(key.connection_string)

        # ConfigurationStores_ListKeyValue[post]
        BODY = {
          "key": KEY,
          "label": LABEL
        }
        result = self.event_loop.run_until_complete(
            self.mgmt_client.configuration_stores.list_key_value(resource_group.name, CONFIGURATION_STORE_NAME, BODY)
        )

    @RandomNameResourceGroupPreparer(location=AZURE_LOCATION)
    def test_appconfiguration(self, resource_group):

        SERVICE_NAME = "myapimrndxyz"
        VNET_NAME = "vnetnamexxy"
        SUB_NET = "subnetnamexxy"
        ENDPOINT_NAME = "endpointxyz"
        CONFIGURATION_STORE_NAME = self.get_resource_name("configuration")
        PRIVATE_ENDPOINT_CONNECTION_NAME = self.get_resource_name("privateendpoint")

        # ConfigurationStores_Create[put]
        BODY = {
          "location": "westus",
          "sku": {
            "name": "Standard"  # Free can not use private endpoint
          },
          "tags": {
            "my_tag": "myTagValue"
          }
        }
        result = self.event_loop.run_until_complete(
            self.mgmt_client.configuration_stores.begin_create(resource_group.name, CONFIGURATION_STORE_NAME, BODY)
        )
        conf_store = self.event_loop.run_until_complete(
            result.result()
        )

        if self.is_live:
            # create endpoint
            endpoint = self.create_endpoint(resource_group.name, VNET_NAME, SUB_NET, ENDPOINT_NAME, conf_store.id)

        # ConfigurationStores_Create_WithIdentity[put]
        # BODY = {
        #   "location": "westus",
        #   "sku": {
        #     "name": "Free"
        #   },
        #   "tags": {
        #     "my_tag": "myTagValue"
        #   },
        #   "identity": {
        #     "type": "SystemAssigned, UserAssigned",
        #     "user_assigned_identities": {}
        #   }
        # }
        # result = self.mgmt_client.configuration_stores.begin_create(resource_group.name, CONFIGURATION_STORE_NAME, BODY)
        # result = result.result()

        # ConfigurationStores_Get[get]
        conf_store = self.event_loop.run_until_complete(
            self.mgmt_client.configuration_stores.get(resource_group.name, CONFIGURATION_STORE_NAME)
        )

        PRIVATE_ENDPOINT_CONNECTION_NAME = conf_store.private_endpoint_connections[0].name
        private_connection_id = conf_store.private_endpoint_connections[0].id

        # PrivateEndpointConnection_CreateOrUpdate[put]
        BODY = {
          # "id": "https://management.azure.com/subscriptions/" + self.settings.SUBSCRIPTION_ID + "/resourceGroups/" + resource_group.name + "/providers/Microsoft.AppConfiguration/configurationStores/" + CONFIGURATION_STORE_NAME + "/privateEndpointConnections/" + PRIVATE_ENDPOINT_CONNECTION_NAME,
          "id": private_connection_id,
          "private_endpoint": {
            "id": "/subscriptions/" + self.settings.SUBSCRIPTION_ID + "/resourceGroups/" + resource_group.name + "/providers/Microsoft.Network/privateEndpoints/" + ENDPOINT_NAME,
          },
          "private_link_service_connection_state": {
            "status": "Approved",
            "description": "Auto-Approved"
          }
        }
        result = self.event_loop.run_until_complete(
            self.mgmt_client.private_endpoint_connections.begin_create_or_update(
                resource_group.name,
                CONFIGURATION_STORE_NAME,
                PRIVATE_ENDPOINT_CONNECTION_NAME,
                BODY
            )
        )
        result = self.event_loop.run_until_complete(
            result.result()
        )
        # result = self.mgmt_client.private_endpoint_connections.begin_create_or_update(
        #     resource_group.name,
        #     CONFIGURATION_STORE_NAME,
        #     PRIVATE_ENDPOINT_CONNECTION_NAME,
        #     BODY)
        #     # id=BODY["id"],
        #     # private_endpoint=BODY["private_endpoint"],
        #     # private_link_service_connection_state=BODY["private_link_service_connection_state"])
        # result = result.result()
          
        # PrivateEndpointConnection_GetConnection[get]
        result = self.event_loop.run_until_complete(
            self.mgmt_client.private_endpoint_connections.get(resource_group.name, CONFIGURATION_STORE_NAME, PRIVATE_ENDPOINT_CONNECTION_NAME)
        )

        # PrivateLinkResources_ListGroupIds[get]
        privatelinks = self.to_list(
            self.mgmt_client.private_link_resources.list_by_configuration_store(resource_group.name, CONFIGURATION_STORE_NAME)
        )
        PRIVATE_LINK_RESOURCE_NAME = privatelinks[0].name

        # PrivateLinkResources_Get[get]
        self.event_loop.run_until_complete(
            self.mgmt_client.private_link_resources.get(resource_group.name, CONFIGURATION_STORE_NAME, PRIVATE_LINK_RESOURCE_NAME)
        )

        # PrivateEndpointConnection_List[get]
        result = self.to_list(
            self.mgmt_client.private_endpoint_connections.list_by_configuration_store(resource_group.name, CONFIGURATION_STORE_NAME)
        )

        # List the operations available
        result = self.to_list(
            self.mgmt_client.operations.list()
        )

        # ConfigurationStores_ListByResourceGroup[get]
        result = self.to_list(
            self.mgmt_client.configuration_stores.list_by_resource_group(resource_group.name)
        )

        # ConfigurationStores_List[get]
        result = self.to_list(
            self.mgmt_client.configuration_stores.list()
        )

        # ConfigurationStores_Update[patch]
        BODY = {
          "tags": {
            "category": "Marketing"
          },
          "sku": {
            "name": "Standard"
          }
        }
        result = self.event_loop.run_until_complete(
            self.mgmt_client.configuration_stores.begin_update(resource_group.name, CONFIGURATION_STORE_NAME, BODY)
        )
        result = self.event_loop.run_until_complete(
            result.result()
        )

        # ConfigurationStores_Update_WithIdentity[patch]
        # BODY = {
        #   "tags": {
        #     "category": "Marketing"
        #   },
        #   "sku": {
        #     "name": "Standard"
        #   },
        #   "identity": {
        #     "type": "SystemAssigned, UserAssigned",
        #     "user_assigned_identities": {}
        #   }
        # }
        # result = self.mgmt_client.configuration_stores.begin_update(resource_group.name, CONFIGURATION_STORE_NAME, BODY)
        # result = result.result()

        # ConfigurationStores_CheckNameNotAvailable[post]
        BODY = {
          "name": "contoso",
          "type": "Microsoft.AppConfiguration/configurationStores"
        }
        result = self.event_loop.run_until_complete(
            self.mgmt_client.operations.check_name_availability(BODY)
        )

        # ConfigurationStores_CheckNameAvailable[post]
        BODY = {
          "name": "contoso",
          "type": "Microsoft.AppConfiguration/configurationStores"
        }
        result = self.event_loop.run_until_complete(
            self.mgmt_client.operations.check_name_availability(BODY)
        )

        # PrivateEndpointConnections_Delete[delete]
        result = self.event_loop.run_until_complete(
            self.mgmt_client.private_endpoint_connections.begin_delete(resource_group.name, CONFIGURATION_STORE_NAME, PRIVATE_ENDPOINT_CONNECTION_NAME)
        )
        result = self.event_loop.run_until_complete(
            result.result()
        )

        # ConfigurationStores_Delete[delete]
        result = self.event_loop.run_until_complete(
            self.mgmt_client.configuration_stores.begin_delete(resource_group.name, CONFIGURATION_STORE_NAME)
        )
        result = self.event_loop.run_until_complete(
            result.result()
        )

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