1# coding=utf-8
2# --------------------------------------------------------------------------
3# Copyright (c) Microsoft Corporation. All rights reserved.
4# Licensed under the MIT License. See License.txt in the project root for
5# license information.
6#
7# Code generated by Microsoft (R) AutoRest Code Generator.
8# Changes may cause incorrect behavior and will be lost if the code is
9# regenerated.
10# --------------------------------------------------------------------------
11
12import uuid
13from msrest.pipeline import ClientRawResponse
14from msrestazure.azure_exceptions import CloudError
15from msrest.polling import LROPoller, NoPolling
16from msrestazure.polling.arm_polling import ARMPolling
17
18from .. import models
19
20
21class VirtualNetworksOperations(object):
22    """VirtualNetworksOperations operations.
23
24    You should not instantiate directly this class, but create a Client instance that will create it for you and attach it as attribute.
25
26    :param client: Client for service requests.
27    :param config: Configuration of service client.
28    :param serializer: An object model serializer.
29    :param deserializer: An object model deserializer.
30    :ivar api_version: Client API version. Constant value: "2018-09-15".
31    """
32
33    models = models
34
35    def __init__(self, client, config, serializer, deserializer):
36
37        self._client = client
38        self._serialize = serializer
39        self._deserialize = deserializer
40        self.api_version = "2018-09-15"
41
42        self.config = config
43
44    def list(
45            self, resource_group_name, lab_name, expand=None, filter=None, top=None, orderby=None, custom_headers=None, raw=False, **operation_config):
46        """List virtual networks in a given lab.
47
48        :param resource_group_name: The name of the resource group.
49        :type resource_group_name: str
50        :param lab_name: The name of the lab.
51        :type lab_name: str
52        :param expand: Specify the $expand query. Example:
53         'properties($expand=externalSubnets)'
54        :type expand: str
55        :param filter: The filter to apply to the operation. Example:
56         '$filter=contains(name,'myName')
57        :type filter: str
58        :param top: The maximum number of resources to return from the
59         operation. Example: '$top=10'
60        :type top: int
61        :param orderby: The ordering expression for the results, using OData
62         notation. Example: '$orderby=name desc'
63        :type orderby: str
64        :param dict custom_headers: headers that will be added to the request
65        :param bool raw: returns the direct response alongside the
66         deserialized response
67        :param operation_config: :ref:`Operation configuration
68         overrides<msrest:optionsforoperations>`.
69        :return: An iterator like instance of VirtualNetwork
70        :rtype:
71         ~azure.mgmt.devtestlabs.models.VirtualNetworkPaged[~azure.mgmt.devtestlabs.models.VirtualNetwork]
72        :raises: :class:`CloudError<msrestazure.azure_exceptions.CloudError>`
73        """
74        def prepare_request(next_link=None):
75            if not next_link:
76                # Construct URL
77                url = self.list.metadata['url']
78                path_format_arguments = {
79                    'subscriptionId': self._serialize.url("self.config.subscription_id", self.config.subscription_id, 'str'),
80                    'resourceGroupName': self._serialize.url("resource_group_name", resource_group_name, 'str'),
81                    'labName': self._serialize.url("lab_name", lab_name, 'str')
82                }
83                url = self._client.format_url(url, **path_format_arguments)
84
85                # Construct parameters
86                query_parameters = {}
87                if expand is not None:
88                    query_parameters['$expand'] = self._serialize.query("expand", expand, 'str')
89                if filter is not None:
90                    query_parameters['$filter'] = self._serialize.query("filter", filter, 'str')
91                if top is not None:
92                    query_parameters['$top'] = self._serialize.query("top", top, 'int')
93                if orderby is not None:
94                    query_parameters['$orderby'] = self._serialize.query("orderby", orderby, 'str')
95                query_parameters['api-version'] = self._serialize.query("self.api_version", self.api_version, 'str')
96
97            else:
98                url = next_link
99                query_parameters = {}
100
101            # Construct headers
102            header_parameters = {}
103            header_parameters['Accept'] = 'application/json'
104            if self.config.generate_client_request_id:
105                header_parameters['x-ms-client-request-id'] = str(uuid.uuid1())
106            if custom_headers:
107                header_parameters.update(custom_headers)
108            if self.config.accept_language is not None:
109                header_parameters['accept-language'] = self._serialize.header("self.config.accept_language", self.config.accept_language, 'str')
110
111            # Construct and send request
112            request = self._client.get(url, query_parameters, header_parameters)
113            return request
114
115        def internal_paging(next_link=None):
116            request = prepare_request(next_link)
117
118            response = self._client.send(request, stream=False, **operation_config)
119
120            if response.status_code not in [200]:
121                exp = CloudError(response)
122                exp.request_id = response.headers.get('x-ms-request-id')
123                raise exp
124
125            return response
126
127        # Deserialize response
128        header_dict = None
129        if raw:
130            header_dict = {}
131        deserialized = models.VirtualNetworkPaged(internal_paging, self._deserialize.dependencies, header_dict)
132
133        return deserialized
134    list.metadata = {'url': '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DevTestLab/labs/{labName}/virtualnetworks'}
135
136    def get(
137            self, resource_group_name, lab_name, name, expand=None, custom_headers=None, raw=False, **operation_config):
138        """Get virtual network.
139
140        :param resource_group_name: The name of the resource group.
141        :type resource_group_name: str
142        :param lab_name: The name of the lab.
143        :type lab_name: str
144        :param name: The name of the virtual network.
145        :type name: str
146        :param expand: Specify the $expand query. Example:
147         'properties($expand=externalSubnets)'
148        :type expand: str
149        :param dict custom_headers: headers that will be added to the request
150        :param bool raw: returns the direct response alongside the
151         deserialized response
152        :param operation_config: :ref:`Operation configuration
153         overrides<msrest:optionsforoperations>`.
154        :return: VirtualNetwork or ClientRawResponse if raw=true
155        :rtype: ~azure.mgmt.devtestlabs.models.VirtualNetwork or
156         ~msrest.pipeline.ClientRawResponse
157        :raises: :class:`CloudError<msrestazure.azure_exceptions.CloudError>`
158        """
159        # Construct URL
160        url = self.get.metadata['url']
161        path_format_arguments = {
162            'subscriptionId': self._serialize.url("self.config.subscription_id", self.config.subscription_id, 'str'),
163            'resourceGroupName': self._serialize.url("resource_group_name", resource_group_name, 'str'),
164            'labName': self._serialize.url("lab_name", lab_name, 'str'),
165            'name': self._serialize.url("name", name, 'str')
166        }
167        url = self._client.format_url(url, **path_format_arguments)
168
169        # Construct parameters
170        query_parameters = {}
171        if expand is not None:
172            query_parameters['$expand'] = self._serialize.query("expand", expand, 'str')
173        query_parameters['api-version'] = self._serialize.query("self.api_version", self.api_version, 'str')
174
175        # Construct headers
176        header_parameters = {}
177        header_parameters['Accept'] = 'application/json'
178        if self.config.generate_client_request_id:
179            header_parameters['x-ms-client-request-id'] = str(uuid.uuid1())
180        if custom_headers:
181            header_parameters.update(custom_headers)
182        if self.config.accept_language is not None:
183            header_parameters['accept-language'] = self._serialize.header("self.config.accept_language", self.config.accept_language, 'str')
184
185        # Construct and send request
186        request = self._client.get(url, query_parameters, header_parameters)
187        response = self._client.send(request, stream=False, **operation_config)
188
189        if response.status_code not in [200]:
190            exp = CloudError(response)
191            exp.request_id = response.headers.get('x-ms-request-id')
192            raise exp
193
194        deserialized = None
195        if response.status_code == 200:
196            deserialized = self._deserialize('VirtualNetwork', response)
197
198        if raw:
199            client_raw_response = ClientRawResponse(deserialized, response)
200            return client_raw_response
201
202        return deserialized
203    get.metadata = {'url': '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DevTestLab/labs/{labName}/virtualnetworks/{name}'}
204
205
206    def _create_or_update_initial(
207            self, resource_group_name, lab_name, name, virtual_network, custom_headers=None, raw=False, **operation_config):
208        # Construct URL
209        url = self.create_or_update.metadata['url']
210        path_format_arguments = {
211            'subscriptionId': self._serialize.url("self.config.subscription_id", self.config.subscription_id, 'str'),
212            'resourceGroupName': self._serialize.url("resource_group_name", resource_group_name, 'str'),
213            'labName': self._serialize.url("lab_name", lab_name, 'str'),
214            'name': self._serialize.url("name", name, 'str')
215        }
216        url = self._client.format_url(url, **path_format_arguments)
217
218        # Construct parameters
219        query_parameters = {}
220        query_parameters['api-version'] = self._serialize.query("self.api_version", self.api_version, 'str')
221
222        # Construct headers
223        header_parameters = {}
224        header_parameters['Accept'] = 'application/json'
225        header_parameters['Content-Type'] = 'application/json; charset=utf-8'
226        if self.config.generate_client_request_id:
227            header_parameters['x-ms-client-request-id'] = str(uuid.uuid1())
228        if custom_headers:
229            header_parameters.update(custom_headers)
230        if self.config.accept_language is not None:
231            header_parameters['accept-language'] = self._serialize.header("self.config.accept_language", self.config.accept_language, 'str')
232
233        # Construct body
234        body_content = self._serialize.body(virtual_network, 'VirtualNetwork')
235
236        # Construct and send request
237        request = self._client.put(url, query_parameters, header_parameters, body_content)
238        response = self._client.send(request, stream=False, **operation_config)
239
240        if response.status_code not in [200, 201]:
241            exp = CloudError(response)
242            exp.request_id = response.headers.get('x-ms-request-id')
243            raise exp
244
245        deserialized = None
246
247        if response.status_code == 200:
248            deserialized = self._deserialize('VirtualNetwork', response)
249        if response.status_code == 201:
250            deserialized = self._deserialize('VirtualNetwork', response)
251
252        if raw:
253            client_raw_response = ClientRawResponse(deserialized, response)
254            return client_raw_response
255
256        return deserialized
257
258    def create_or_update(
259            self, resource_group_name, lab_name, name, virtual_network, custom_headers=None, raw=False, polling=True, **operation_config):
260        """Create or replace an existing virtual network. This operation can take
261        a while to complete.
262
263        :param resource_group_name: The name of the resource group.
264        :type resource_group_name: str
265        :param lab_name: The name of the lab.
266        :type lab_name: str
267        :param name: The name of the virtual network.
268        :type name: str
269        :param virtual_network: A virtual network.
270        :type virtual_network: ~azure.mgmt.devtestlabs.models.VirtualNetwork
271        :param dict custom_headers: headers that will be added to the request
272        :param bool raw: The poller return type is ClientRawResponse, the
273         direct response alongside the deserialized response
274        :param polling: True for ARMPolling, False for no polling, or a
275         polling object for personal polling strategy
276        :return: An instance of LROPoller that returns VirtualNetwork or
277         ClientRawResponse<VirtualNetwork> if raw==True
278        :rtype:
279         ~msrestazure.azure_operation.AzureOperationPoller[~azure.mgmt.devtestlabs.models.VirtualNetwork]
280         or
281         ~msrestazure.azure_operation.AzureOperationPoller[~msrest.pipeline.ClientRawResponse[~azure.mgmt.devtestlabs.models.VirtualNetwork]]
282        :raises: :class:`CloudError<msrestazure.azure_exceptions.CloudError>`
283        """
284        raw_result = self._create_or_update_initial(
285            resource_group_name=resource_group_name,
286            lab_name=lab_name,
287            name=name,
288            virtual_network=virtual_network,
289            custom_headers=custom_headers,
290            raw=True,
291            **operation_config
292        )
293
294        def get_long_running_output(response):
295            deserialized = self._deserialize('VirtualNetwork', response)
296
297            if raw:
298                client_raw_response = ClientRawResponse(deserialized, response)
299                return client_raw_response
300
301            return deserialized
302
303        lro_delay = operation_config.get(
304            'long_running_operation_timeout',
305            self.config.long_running_operation_timeout)
306        if polling is True: polling_method = ARMPolling(lro_delay, **operation_config)
307        elif polling is False: polling_method = NoPolling()
308        else: polling_method = polling
309        return LROPoller(self._client, raw_result, get_long_running_output, polling_method)
310    create_or_update.metadata = {'url': '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DevTestLab/labs/{labName}/virtualnetworks/{name}'}
311
312
313    def _delete_initial(
314            self, resource_group_name, lab_name, name, custom_headers=None, raw=False, **operation_config):
315        # Construct URL
316        url = self.delete.metadata['url']
317        path_format_arguments = {
318            'subscriptionId': self._serialize.url("self.config.subscription_id", self.config.subscription_id, 'str'),
319            'resourceGroupName': self._serialize.url("resource_group_name", resource_group_name, 'str'),
320            'labName': self._serialize.url("lab_name", lab_name, 'str'),
321            'name': self._serialize.url("name", name, 'str')
322        }
323        url = self._client.format_url(url, **path_format_arguments)
324
325        # Construct parameters
326        query_parameters = {}
327        query_parameters['api-version'] = self._serialize.query("self.api_version", self.api_version, 'str')
328
329        # Construct headers
330        header_parameters = {}
331        if self.config.generate_client_request_id:
332            header_parameters['x-ms-client-request-id'] = str(uuid.uuid1())
333        if custom_headers:
334            header_parameters.update(custom_headers)
335        if self.config.accept_language is not None:
336            header_parameters['accept-language'] = self._serialize.header("self.config.accept_language", self.config.accept_language, 'str')
337
338        # Construct and send request
339        request = self._client.delete(url, query_parameters, header_parameters)
340        response = self._client.send(request, stream=False, **operation_config)
341
342        if response.status_code not in [200, 202, 204]:
343            exp = CloudError(response)
344            exp.request_id = response.headers.get('x-ms-request-id')
345            raise exp
346
347        if raw:
348            client_raw_response = ClientRawResponse(None, response)
349            return client_raw_response
350
351    def delete(
352            self, resource_group_name, lab_name, name, custom_headers=None, raw=False, polling=True, **operation_config):
353        """Delete virtual network. This operation can take a while to complete.
354
355        :param resource_group_name: The name of the resource group.
356        :type resource_group_name: str
357        :param lab_name: The name of the lab.
358        :type lab_name: str
359        :param name: The name of the virtual network.
360        :type name: str
361        :param dict custom_headers: headers that will be added to the request
362        :param bool raw: The poller return type is ClientRawResponse, the
363         direct response alongside the deserialized response
364        :param polling: True for ARMPolling, False for no polling, or a
365         polling object for personal polling strategy
366        :return: An instance of LROPoller that returns None or
367         ClientRawResponse<None> if raw==True
368        :rtype: ~msrestazure.azure_operation.AzureOperationPoller[None] or
369         ~msrestazure.azure_operation.AzureOperationPoller[~msrest.pipeline.ClientRawResponse[None]]
370        :raises: :class:`CloudError<msrestazure.azure_exceptions.CloudError>`
371        """
372        raw_result = self._delete_initial(
373            resource_group_name=resource_group_name,
374            lab_name=lab_name,
375            name=name,
376            custom_headers=custom_headers,
377            raw=True,
378            **operation_config
379        )
380
381        def get_long_running_output(response):
382            if raw:
383                client_raw_response = ClientRawResponse(None, response)
384                return client_raw_response
385
386        lro_delay = operation_config.get(
387            'long_running_operation_timeout',
388            self.config.long_running_operation_timeout)
389        if polling is True: polling_method = ARMPolling(lro_delay, **operation_config)
390        elif polling is False: polling_method = NoPolling()
391        else: polling_method = polling
392        return LROPoller(self._client, raw_result, get_long_running_output, polling_method)
393    delete.metadata = {'url': '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DevTestLab/labs/{labName}/virtualnetworks/{name}'}
394
395    def update(
396            self, resource_group_name, lab_name, name, virtual_network, custom_headers=None, raw=False, **operation_config):
397        """Allows modifying tags of virtual networks. All other properties will be
398        ignored.
399
400        :param resource_group_name: The name of the resource group.
401        :type resource_group_name: str
402        :param lab_name: The name of the lab.
403        :type lab_name: str
404        :param name: The name of the virtual network.
405        :type name: str
406        :param virtual_network: A virtual network.
407        :type virtual_network:
408         ~azure.mgmt.devtestlabs.models.VirtualNetworkFragment
409        :param dict custom_headers: headers that will be added to the request
410        :param bool raw: returns the direct response alongside the
411         deserialized response
412        :param operation_config: :ref:`Operation configuration
413         overrides<msrest:optionsforoperations>`.
414        :return: VirtualNetwork or ClientRawResponse if raw=true
415        :rtype: ~azure.mgmt.devtestlabs.models.VirtualNetwork or
416         ~msrest.pipeline.ClientRawResponse
417        :raises: :class:`CloudError<msrestazure.azure_exceptions.CloudError>`
418        """
419        # Construct URL
420        url = self.update.metadata['url']
421        path_format_arguments = {
422            'subscriptionId': self._serialize.url("self.config.subscription_id", self.config.subscription_id, 'str'),
423            'resourceGroupName': self._serialize.url("resource_group_name", resource_group_name, 'str'),
424            'labName': self._serialize.url("lab_name", lab_name, 'str'),
425            'name': self._serialize.url("name", name, 'str')
426        }
427        url = self._client.format_url(url, **path_format_arguments)
428
429        # Construct parameters
430        query_parameters = {}
431        query_parameters['api-version'] = self._serialize.query("self.api_version", self.api_version, 'str')
432
433        # Construct headers
434        header_parameters = {}
435        header_parameters['Accept'] = 'application/json'
436        header_parameters['Content-Type'] = 'application/json; charset=utf-8'
437        if self.config.generate_client_request_id:
438            header_parameters['x-ms-client-request-id'] = str(uuid.uuid1())
439        if custom_headers:
440            header_parameters.update(custom_headers)
441        if self.config.accept_language is not None:
442            header_parameters['accept-language'] = self._serialize.header("self.config.accept_language", self.config.accept_language, 'str')
443
444        # Construct body
445        body_content = self._serialize.body(virtual_network, 'VirtualNetworkFragment')
446
447        # Construct and send request
448        request = self._client.patch(url, query_parameters, header_parameters, body_content)
449        response = self._client.send(request, stream=False, **operation_config)
450
451        if response.status_code not in [200]:
452            exp = CloudError(response)
453            exp.request_id = response.headers.get('x-ms-request-id')
454            raise exp
455
456        deserialized = None
457        if response.status_code == 200:
458            deserialized = self._deserialize('VirtualNetwork', response)
459
460        if raw:
461            client_raw_response = ClientRawResponse(deserialized, response)
462            return client_raw_response
463
464        return deserialized
465    update.metadata = {'url': '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DevTestLab/labs/{labName}/virtualnetworks/{name}'}
466