File: resourcemanagementcomputenetwork.rst

package info (click to toggle)
python-azure 2.0.0~rc6%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 269,052 kB
  • ctags: 9,428
  • sloc: python: 81,857; makefile: 149
file content (295 lines) | stat: -rw-r--r-- 10,052 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
Compute and Network Resource Management
=======================================

For general information on resource management, see :doc:`Resource Management<resourcemanagement>`.

Create the management client
----------------------------

The following code creates an instance of the management client.

You will need to provide your ``subscription_id`` which can be retrieved
from `your subscription list <https://manage.windowsazure.com/#Workspaces/AdminTasks/SubscriptionMapping>`__.

See :doc:`Resource Management Authentication <resourcemanagementauthentication>`
for details on getting a ``Credentials`` instance.

.. code:: python

    from azure.mgmt.compute import ComputeManagementClient
    from azure.mgmt.network import NetworkManagementClient

    # TODO: Replace this with your subscription id
    subscription_id = '33333333-3333-3333-3333-333333333333'
    # TODO: See above how to get a Credentials instance
    credentials = ...

    compute_client = ComputeManagementClient(
        credentials,
        subscription_id
    )

    network_client = NetworkManagementClient(
        credentials,
        subscription_id
    )


Registration
------------

Some operations in the compute/network ARM APIs require a one-time
registration of the storage provider with your subscription.

Use the following code to do the registration. You can use the same
credentials you created in the previous section.

.. code:: python

    from azure.mgmt.resource.resources import ResourceManagementClient

    resource_client = ResourceManagementClient(
        credentials,
        subscription_id
    )
    resource_client.providers.register('Microsoft.Compute')
    resource_client.providers.register('Microsoft.Network')

List images
-----------

Use the following code to print all of the available images to use for
creating virtual machines, including all skus and versions.

.. code:: python

    region = 'eastus2'

    result_list_pub = compute_client.virtual_machine_images.list_publishers(
        region,
    )

    for publisher in result_list_pub:
        result_list_offers = compute_client.virtual_machine_images.list_offers(
            region,
            publisher.name,
        )

        for offer in result_list_offers:
            result_list_skus = compute_client.virtual_machine_images.list_skus(
                region,
                publisher.name,
                offer.name,
            )

            for sku in result_list_skus:
                result_list = compute_client.virtual_machine_images.list(
                    region,
                    publisher.name,
                    offer.name,
                    sku.name,
                )

                for version in result_list:
                    result_get = compute_client.virtual_machine_images.get(
                        region,
                        publisher.name,
                        offer.name,
                        sku.name,
                        version.name,
                    )

                    print('PUBLISHER: {0}, OFFER: {1}, SKU: {2}, VERSION: {3}'.format(
                        publisher.name,
                        offer.name,
                        sku.name,
                        version.name,
                    ))

Create virtual machine
----------------------

The following code creates a new virtual machine. Creating a virtual
machine involves creating a resource group, storage accounts, virtual
network resources, and finally the virtual machine.

To create or manage resource groups, see :doc:`Resource Management<resourcemanagement>`.

To create or manage storage accounts, see :doc:`Storage Resource Management<resourcemanagementstorage>`.

.. code:: python

    import azure.mgmt.compute
    import azure.mgmt.network
    import azure.mgmt.resource
    import azure.mgmt.storage

    resource_client = azure.mgmt.resource.ResourceManagementClient(res_config)
    storage_client = azure.mgmt.storage.StorageManagementClient(storage_config)
    compute_client = azure.mgmt.compute.ComputeManagementClient(compute_config)
    network_client = azure.mgmt.network.NetworkManagementClient(network_config)

    BASE_NAME = 'pythonexample'

    GROUP_NAME = BASE_NAME
    STORAGE_NAME = BASE_NAME
    VIRTUAL_NETWORK_NAME = BASE_NAME
    SUBNET_NAME = BASE_NAME
    NETWORK_INTERFACE_NAME = BASE_NAME
    VM_NAME = BASE_NAME
    OS_DISK_NAME = BASE_NAME
    PUBLIC_IP_NAME = BASE_NAME
    COMPUTER_NAME = BASE_NAME
    ADMIN_USERNAME='azureadminuser'
    ADMIN_PASSWORD='<censored>'
    REGION = 'eastus2'
    IMAGE_PUBLISHER = 'Canonical'
    IMAGE_OFFER = 'UbuntuServer'
    IMAGE_SKU = '16.04.0-LTS'
    IMAGE_VERSION = 'latest'

    # 1. Create a resource group
    result = resource_client.resource_groups.create_or_update(
        GROUP_NAME,
        azure.mgmt.resource.models.ResourceGroup(
            location=REGION,
        ),
    )

    # 2. Create a storage account
    result = storage_client.storage_accounts.create(
        GROUP_NAME,
        STORAGE_NAME,
        azure.mgmt.storage.models.StorageAccountCreateParameters(
            location=REGION,
            account_type=azure.mgmt.storage.models.AccountType.standard_lrs,
        ),
    )
    result.wait() # async operation

    # 3. Create the network interface using a helper function (defined below)
    nic_id = create_network_interface(
        network_client,
        REGION,
        GROUP_NAME,
        NETWORK_INTERFACE_NAME,
        VIRTUAL_NETWORK_NAME,
        SUBNET_NAME,
        PUBLIC_IP_NAME,
    )

    # 4. Create the virtual machine
    result = compute_client.virtual_machines.create_or_update(
        GROUP_NAME,
        VM_NAME,
        azure.mgmt.compute.models.VirtualMachine(
            location=REGION,
            os_profile=azure.mgmt.compute.models.OSProfile(
                admin_username=ADMIN_USERNAME,
                admin_password=ADMIN_PASSWORD,
                computer_name=COMPUTER_NAME,
            ),
            hardware_profile=azure.mgmt.compute.models.HardwareProfile(
                virtual_machine_size=azure.mgmt.compute.models.VirtualMachineSizeTypes.standard_a0
            ),
            network_profile=azure.mgmt.compute.models.NetworkProfile(
                network_interfaces=[
                    azure.mgmt.compute.models.NetworkInterfaceReference(
                        reference_uri=nic_id,
                    ),
                ],
            ),
            storage_profile=azure.mgmt.compute.models.StorageProfile(
                os_disk=azure.mgmt.compute.models.OSDisk(
                    caching=azure.mgmt.compute.models.CachingTypes.none,
                    create_option=azure.mgmt.compute.models.DiskCreateOptionTypes.from_image,
                    name=OS_DISK_NAME,
                    vhd=azure.mgmt.compute.models.VirtualHardDisk(
                        uri='https://{0}.blob.core.windows.net/vhds/{1}.vhd'.format(
                            STORAGE_NAME,
                            OS_DISK_NAME,
                        ),
                    ),
                ),
                image_reference = azure.mgmt.compute.models.ImageReference(
                    publisher=IMAGE_PUBLISHER,
                    offer=IMAGE_OFFER,
                    sku=IMAGE_SKU,
                    version=IMAGE_VERSION,
                ),
            ),
        ),
    )

    # Display the public ip address
    # You can now connect to the machine using SSH
    public_ip_address = network_client.public_ip_addresses.get(GROUP_NAME, PUBLIC_IP_NAME)
    print('VM available at {}'.format(public_ip_address.ip_address))


This is the helper function that creates the network resources, such as
virtual network, public ip and network interface.

.. code:: python

    def create_network_interface(network_client, region, group_name, interface_name,
                                 network_name, subnet_name, ip_name):

        result = network_client.virtual_networks.create_or_update(
            group_name,
            network_name,
            azure.mgmt.network.models.VirtualNetwork(
                location=region,
                address_space=azure.mgmt.network.models.AddressSpace(
                    address_prefixes=[
                        '10.1.0.0/16',
                    ],
                ),
                subnets=[
                    azure.mgmt.network.models.Subnet(
                        name=subnet_name,
                        address_prefix='10.1.0.0/24',
                    ),
                ],
            ),
        )

        subnet = network_client.subnets.get(group_name, network_name, subnet_name)

        result = network_client.public_ip_addresses.create_or_update(
            group_name,
            ip_name,
            azure.mgmt.network.models.PublicIPAddress(
                location=region,
                public_ip_allocation_method=azure.mgmt.network.models.IPAllocationMethod.dynamic,
                idle_timeout_in_minutes=4,
            ),
        )

        public_ip_address = network_client.public_ip_addresses.get(group_name, ip_name)
        public_ip_id = public_ip_address.id

        result = network_client.network_interfaces.create_or_update(
            group_name,
            interface_name,
            azure.mgmt.network.models.NetworkInterface(
                location=region,
                ip_configurations=[
                    azure.mgmt.network.models.NetworkInterfaceIPConfiguration(
                        name='default',
                        private_ip_allocation_method=azure.mgmt.network.models.IPAllocationMethod.dynamic,
                        subnet=subnet,
                        public_ip_address=azure.mgmt.network.models.PublicIPAddress(
                            id=public_ip_id,
                        ),
                    ),
                ],
            ),
        )

        network_interface = network_client.network_interfaces.get(
            group_name,
            interface_name,
        )

        return network_interface.id