|
| 1 | +# -------------------------------------------------------------------------- |
| 2 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +# Licensed under the MIT License. See License.txt in the project root for |
| 4 | +# license information. |
| 5 | +# -------------------------------------------------------------------------- |
| 6 | + |
| 7 | +import os |
| 8 | + |
| 9 | +from azure.identity import DefaultAzureCredential |
| 10 | +from azure.mgmt.compute import ComputeManagementClient |
| 11 | +from azure.mgmt.resource import ResourceManagementClient |
| 12 | + |
| 13 | + |
| 14 | +def main(): |
| 15 | + |
| 16 | + SUBSCRIPTION_ID = os.environ.get("SUBSCRIPTION_ID", None) |
| 17 | + GROUP_NAME = "testgroupx" |
| 18 | + DISK = "diskxxyyzz" |
| 19 | + |
| 20 | + # Create client |
| 21 | + # # For other authentication approaches, please see: https://pypi.org/project/azure-identity/ |
| 22 | + resource_client = ResourceManagementClient( |
| 23 | + credential=DefaultAzureCredential(), |
| 24 | + subscription_id=SUBSCRIPTION_ID |
| 25 | + ) |
| 26 | + compute_client = ComputeManagementClient( |
| 27 | + credential=DefaultAzureCredential(), |
| 28 | + subscription_id=SUBSCRIPTION_ID |
| 29 | + ) |
| 30 | + |
| 31 | + # Create resource group |
| 32 | + resource_client.resource_groups.create_or_update( |
| 33 | + GROUP_NAME, |
| 34 | + {"location": "eastus"} |
| 35 | + ) |
| 36 | + |
| 37 | + # Create disk |
| 38 | + disk = compute_client.disks.begin_create_or_update( |
| 39 | + GROUP_NAME, |
| 40 | + DISK, |
| 41 | + { |
| 42 | + "location": "eastus", |
| 43 | + "creation_data": { |
| 44 | + "create_option": "Empty" |
| 45 | + }, |
| 46 | + "disk_size_gb": "200" |
| 47 | + } |
| 48 | + ).result() |
| 49 | + print("Create disk:\n{}".format(disk)) |
| 50 | + |
| 51 | + # Get disk |
| 52 | + disk = compute_client.disks.get( |
| 53 | + GROUP_NAME, |
| 54 | + DISK |
| 55 | + ) |
| 56 | + print("Get disk:\n{}".format(disk)) |
| 57 | + |
| 58 | + # Update disk |
| 59 | + disk = compute_client.disks.begin_update( |
| 60 | + GROUP_NAME, |
| 61 | + DISK, |
| 62 | + { |
| 63 | + "disk_size_gb": "200" |
| 64 | + } |
| 65 | + ).result() |
| 66 | + print("Update disk:\n{}".format(disk)) |
| 67 | + |
| 68 | + # Delete disk |
| 69 | + disk = compute_client.disks.begin_delete( |
| 70 | + GROUP_NAME, |
| 71 | + DISK |
| 72 | + ).result() |
| 73 | + print("Delete disk.\n") |
| 74 | + |
| 75 | + # Delete Group |
| 76 | + resource_client.resource_groups.begin_delete( |
| 77 | + GROUP_NAME |
| 78 | + ).result() |
| 79 | + |
| 80 | + |
| 81 | +if __name__ == "__main__": |
| 82 | + main() |
0 commit comments