File: test_mgmt_compute_galleries.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 (321 lines) | stat: -rw-r--r-- 13,386 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
# 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.
# --------------------------------------------------------------------------

# covered ops:
#   galleries: 6/6
#   gallery_applications: 5/5
#   gallery_application_versions: 0/5
#   gallery_images: 5/5
#   gallery_image_versions: 5/5

import os
import unittest

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

AZURE_LOCATION = "eastus2"


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

    def setup_method(self, method):
        self.mgmt_client = self.create_mgmt_client(azure.mgmt.compute.ComputeManagementClient)

        if self.is_live:
            from azure.mgmt.network import NetworkManagementClient

            self.network_client = self.create_mgmt_client(NetworkManagementClient)

    def create_snapshot(self, group_name, disk_name, snapshot_name):
        # Create an empty managed disk.[put]
        BODY = {"location": AZURE_LOCATION, "creation_data": {"create_option": "Empty"}, "disk_size_gb": "200"}
        result = self.mgmt_client.disks.begin_create_or_update(group_name, disk_name, BODY)
        disk = result.result()

        # Create a snapshot by copying a disk.
        BODY = {
            "location": AZURE_LOCATION,
            "creation_data": {
                "create_option": "Copy",
                # "source_uri": "/subscriptions/" + SUBSCRIPTION_ID + "/resourceGroups/" + RESOURCE_GROUP + "/providers/Microsoft.Compute/disks/" + DISK_NAME
                "source_uri": disk.id,
            },
        }
        result = self.mgmt_client.snapshots.begin_create_or_update(group_name, snapshot_name, BODY)
        result = result.result()

    def delete_snapshot(self, group_name, snapshot_name):

        # Revoke access snapshot (TODO: need swagger file)
        result = self.mgmt_client.snapshots.begin_revoke_access(group_name, snapshot_name)
        result = result.result()

        # Delete snapshot (TODO: need swagger file)
        result = self.mgmt_client.snapshots.begin_delete(group_name, snapshot_name)
        result = result.result()

    @RandomNameResourceGroupPreparer(location=AZURE_LOCATION)
    @recorded_by_proxy
    def test_compute_galleries(self, resource_group):
        SUBSCRIPTION_ID = self.get_settings_value("SUBSCRIPTION_ID")
        RESOURCE_GROUP = resource_group.name
        GALLERY_NAME = self.get_resource_name("galleryname")
        APPLICATION_NAME = self.get_resource_name("applicationname")
        IMAGE_NAME = self.get_resource_name("imagex")
        DISK_NAME = self.get_resource_name("diskname")
        SNAPSHOT_NAME = self.get_resource_name("snapshotname")
        VERSION_NAME = "1.0.0"

        if self.is_live:
            self.create_snapshot(RESOURCE_GROUP, DISK_NAME, SNAPSHOT_NAME)

        # Create or update a simple gallery.[put]
        BODY = {"location": AZURE_LOCATION, "description": "This is the gallery description."}
        result = self.mgmt_client.galleries.begin_create_or_update(resource_group.name, GALLERY_NAME, BODY)
        result = result.result()

        # Create or update a simple gallery Application.[put]
        BODY = {
            "location": AZURE_LOCATION,
            "description": "This is the gallery application description.",
            "eula": "This is the gallery application EULA.",
            # "privacy_statement_uri": "myPrivacyStatementUri}",
            # "release_note_uri": "myReleaseNoteUri",
            "supported_os_type": "Windows",
        }
        result = self.mgmt_client.gallery_applications.begin_create_or_update(
            resource_group.name, GALLERY_NAME, APPLICATION_NAME, BODY
        )
        result = result.result()

        # TODO: NEED CREATE BLOB
        # # Create or update a simple gallery Application Version.[put]
        # BODY = {
        #   "location": "eastus",
        #   "publishing_profile": {
        #     "source": {
        #       "file_name": "package.zip",
        #       "media_link": "https://mystorageaccount.blob.core.windows.net/mycontainer/package.zip?{sasKey}"
        #     },
        #     "target_regions": [
        #       {
        #         "name": "eastus",
        #         "regional_replica_count": "1",
        #         "storage_account_type": "Standard_LRS"
        #       }
        #     ],
        #     "replica_count": "1",
        #     "end_of_life_date": "2019-07-01T07:00:00Z",
        #     "storage_account_type": "Standard_LRS"
        #   }
        # }
        # result = self.mgmt_client.gallery_application_versions.create_or_update(resource_group.name, GALLERY_NAME, APPLICATION_NAME, VERSION_NAME, BODY)
        # result = result.result()

        # Create or update a simple gallery image.[put]
        BODY = {
            "location": AZURE_LOCATION,
            "os_type": "Windows",
            "os_state": "Generalized",
            "hyper_v_generation": "V1",
            "identifier": {"publisher": "myPublisherName", "offer": "myOfferName", "sku": "mySkuName"},
        }
        result = self.mgmt_client.gallery_images.begin_create_or_update(
            resource_group.name, GALLERY_NAME, IMAGE_NAME, BODY
        )
        result = result.result()

        # Create or update a simple Gallery Image Version using snapshots as a source.[put]
        BODY = {
            "location": AZURE_LOCATION,
            "publishing_profile": {
                "target_regions": [
                    {"name": AZURE_LOCATION, "regional_replica_count": "2", "storage_account_type": "Standard_ZRS"}
                ]
            },
            "storage_profile": {
                "os_disk_image": {
                    "source": {
                        "id": "/subscriptions/"
                        + SUBSCRIPTION_ID
                        + "/resourceGroups/"
                        + RESOURCE_GROUP
                        + "/providers/Microsoft.Compute/snapshots/"
                        + SNAPSHOT_NAME
                        + ""
                    },
                    "host_caching": "ReadOnly",
                }
            },
        }
        result = self.mgmt_client.gallery_image_versions.begin_create_or_update(
            resource_group.name, GALLERY_NAME, IMAGE_NAME, VERSION_NAME, BODY
        )
        result = result.result()

        # # TODO:need finish
        # # Get a gallery Application Version with replication status.[get]
        # result = self.mgmt_client.gallery_application_versions.get(resource_group.name, GALLERY_NAME, APPLICATION_NAME, VERSION_NAME)

        # Get a gallery Image Version.[get]
        result = self.mgmt_client.gallery_image_versions.get(
            resource_group.name, GALLERY_NAME, IMAGE_NAME, VERSION_NAME
        )

        # Get a gallery image.[get]
        result = self.mgmt_client.gallery_images.get(resource_group.name, GALLERY_NAME, IMAGE_NAME)

        # Get a gallery Application.[get]
        result = self.mgmt_client.gallery_applications.get(resource_group.name, GALLERY_NAME, APPLICATION_NAME)

        # Get a gallery.[get]
        result = self.mgmt_client.galleries.get(resource_group.name, GALLERY_NAME)

        # List gallery Image Versions in a gallery Image Definition.[get]
        result = self.mgmt_client.gallery_image_versions.list_by_gallery_image(
            resource_group.name, GALLERY_NAME, IMAGE_NAME
        )

        # List gallery images in a gallery.[get]
        result = self.mgmt_client.gallery_images.list_by_gallery(resource_group.name, GALLERY_NAME)

        # TODO:need finish
        # # List gallery Application Versions in a gallery Application Definition.[get]
        # result = self.mgmt_client.gallery_application_versions.list_by_gallery_application(resource_group.name, GALLERY_NAME, APPLICATION_NAME)

        # List gallery Applications in a gallery.[get]
        result = self.mgmt_client.gallery_applications.list_by_gallery(resource_group.name, GALLERY_NAME)

        # List galleries in a resource group.[get]
        result = self.mgmt_client.galleries.list_by_resource_group(resource_group.name)

        # List galleries in a subscription.[get]
        result = self.mgmt_client.galleries.list()

        # Update a simple Gallery Image Version (Managed Image as source).[patch]
        BODY = {
            "publishing_profile": {
                "target_regions": [
                    # {
                    #   "name": "eastus",
                    #   "regional_replica_count": "1"
                    # },
                    {"name": AZURE_LOCATION, "regional_replica_count": "2", "storage_account_type": "Standard_ZRS"}
                ]
            },
            "storage_profile": {
                "os_disk_image": {
                    "source": {
                        "id": "/subscriptions/"
                        + SUBSCRIPTION_ID
                        + "/resourceGroups/"
                        + RESOURCE_GROUP
                        + "/providers/Microsoft.Compute/snapshots/"
                        + SNAPSHOT_NAME
                        + ""
                    },
                    "host_caching": "ReadOnly",
                },
                # TODO: NEED A IMAGE
                # "source": {
                #   "id": "/subscriptions/" + SUBSCRIPTION_ID + "/resourceGroups/" + RESOURCE_GROUP + "/providers/Microsoft.Compute/images/" + IMAGE_NAME + ""
                # }
            },
        }
        result = self.mgmt_client.gallery_image_versions.begin_update(
            resource_group.name, GALLERY_NAME, IMAGE_NAME, VERSION_NAME, BODY
        )
        result = result.result()

        # Update a simple gallery image.[patch]
        BODY = {
            "os_type": "Windows",
            "os_state": "Generalized",
            "hyper_v_generation": "V1",
            "identifier": {"publisher": "myPublisherName", "offer": "myOfferName", "sku": "mySkuName"},
        }
        result = self.mgmt_client.gallery_images.begin_update(resource_group.name, GALLERY_NAME, IMAGE_NAME, BODY)
        result = result.result()

        # TODO: dont finish
        # Update a simple gallery Application Version.[patch]
        # BODY = {
        #   "properties": {
        #     "publishing_profile": {
        #       "source": {
        #         "file_name": "package.zip",
        #         "media_link": "https://mystorageaccount.blob.core.windows.net/mycontainer/package.zip?{sasKey}"
        #       },
        #       "target_regions": [
        #         {
        #           "name": "eastus",
        #           "regional_replica_count": "1",
        #           "storage_account_type": "Standard_LRS"
        #         }
        #       ],
        #       "replica_count": "1",
        #       "end_of_life_date": "2019-07-01T07:00:00Z",
        #       "storage_account_type": "Standard_LRS"
        #     }
        #   }
        # }
        # result = self.mgmt_client.gallery_application_versions.update(resource_group.name, GALLERY_NAME, APPLICATION_NAME, VERSION_NAME, BODY)
        # result = result.result()

        # Update a simple gallery Application.[patch]
        BODY = {
            "description": "This is the gallery application description.",
            "eula": "This is the gallery application EULA.",
            # "privacy_statement_uri": "myPrivacyStatementUri}",
            # "release_note_uri": "myReleaseNoteUri",
            "supported_os_type": "Windows",
            "tags": {"tag1": "tag1"},
        }
        result = self.mgmt_client.gallery_applications.begin_update(
            resource_group.name, GALLERY_NAME, APPLICATION_NAME, BODY
        )
        result = result.result()

        # Update a simple gallery.[patch]
        BODY = {"description": "This is the gallery description."}
        result = self.mgmt_client.galleries.begin_update(resource_group.name, GALLERY_NAME, BODY)
        result = result.result()

        # Delete a gallery Image Version.[delete]
        result = self.mgmt_client.gallery_image_versions.begin_delete(
            resource_group.name, GALLERY_NAME, IMAGE_NAME, VERSION_NAME
        )
        result = result.result()

        # Delete a gallery Application.[delete]
        result = self.mgmt_client.gallery_applications.begin_delete(resource_group.name, GALLERY_NAME, APPLICATION_NAME)
        result = result.result()

        if self.is_live:
            self.delete_snapshot(RESOURCE_GROUP, SNAPSHOT_NAME)

        # Delete a gallery image.[delete]
        result = self.mgmt_client.gallery_images.begin_delete(resource_group.name, GALLERY_NAME, IMAGE_NAME)
        result = result.result()

        if self.is_live:
            import time

            time.sleep(180)

        # TODO: need finish
        # # Delete a gallery Application Version.[delete]
        # result = self.mgmt_client.gallery_application_versions.delete(resource_group.name, GALLERY_NAME, APPLICATION_NAME, VERSION_NAME)
        # result = result.result()

        # Delete a gallery.[delete]
        result = self.mgmt_client.galleries.begin_delete(resource_group.name, GALLERY_NAME)
        result = result.result()