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 license information.
5# Code generated by Microsoft (R) AutoRest Code Generator.
6# Changes may cause incorrect behavior and will be lost if the code is regenerated.
7# --------------------------------------------------------------------------
8from typing import TYPE_CHECKING
9import warnings
10
11from azure.core.exceptions import ClientAuthenticationError, HttpResponseError, ResourceExistsError, ResourceNotFoundError, map_error
12from azure.core.paging import ItemPaged
13from azure.core.pipeline import PipelineResponse
14from azure.core.pipeline.transport import HttpRequest, HttpResponse
15from azure.mgmt.core.exceptions import ARMErrorFormat
16
17from .. import models as _models
18
19if TYPE_CHECKING:
20    # pylint: disable=unused-import,ungrouped-imports
21    from typing import Any, Callable, Dict, Generic, Iterable, Optional, TypeVar
22
23    T = TypeVar('T')
24    ClsType = Optional[Callable[[PipelineResponse[HttpRequest, HttpResponse], T, Dict[str, Any]], Any]]
25
26class DefaultSecurityRulesOperations(object):
27    """DefaultSecurityRulesOperations operations.
28
29    You should not instantiate this class directly. Instead, you should create a Client instance that
30    instantiates it for you and attaches it as an attribute.
31
32    :ivar models: Alias to model classes used in this operation group.
33    :type models: ~azure.mgmt.network.v2019_11_01.models
34    :param client: Client for service requests.
35    :param config: Configuration of service client.
36    :param serializer: An object model serializer.
37    :param deserializer: An object model deserializer.
38    """
39
40    models = _models
41
42    def __init__(self, client, config, serializer, deserializer):
43        self._client = client
44        self._serialize = serializer
45        self._deserialize = deserializer
46        self._config = config
47
48    def list(
49        self,
50        resource_group_name,  # type: str
51        network_security_group_name,  # type: str
52        **kwargs  # type: Any
53    ):
54        # type: (...) -> Iterable["_models.SecurityRuleListResult"]
55        """Gets all default security rules in a network security group.
56
57        :param resource_group_name: The name of the resource group.
58        :type resource_group_name: str
59        :param network_security_group_name: The name of the network security group.
60        :type network_security_group_name: str
61        :keyword callable cls: A custom type or function that will be passed the direct response
62        :return: An iterator like instance of either SecurityRuleListResult or the result of cls(response)
63        :rtype: ~azure.core.paging.ItemPaged[~azure.mgmt.network.v2019_11_01.models.SecurityRuleListResult]
64        :raises: ~azure.core.exceptions.HttpResponseError
65        """
66        cls = kwargs.pop('cls', None)  # type: ClsType["_models.SecurityRuleListResult"]
67        error_map = {
68            401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError
69        }
70        error_map.update(kwargs.pop('error_map', {}))
71        api_version = "2019-11-01"
72        accept = "application/json"
73
74        def prepare_request(next_link=None):
75            # Construct headers
76            header_parameters = {}  # type: Dict[str, Any]
77            header_parameters['Accept'] = self._serialize.header("accept", accept, 'str')
78
79            if not next_link:
80                # Construct URL
81                url = self.list.metadata['url']  # type: ignore
82                path_format_arguments = {
83                    'resourceGroupName': self._serialize.url("resource_group_name", resource_group_name, 'str'),
84                    'networkSecurityGroupName': self._serialize.url("network_security_group_name", network_security_group_name, 'str'),
85                    'subscriptionId': self._serialize.url("self._config.subscription_id", self._config.subscription_id, 'str'),
86                }
87                url = self._client.format_url(url, **path_format_arguments)
88                # Construct parameters
89                query_parameters = {}  # type: Dict[str, Any]
90                query_parameters['api-version'] = self._serialize.query("api_version", api_version, 'str')
91
92                request = self._client.get(url, query_parameters, header_parameters)
93            else:
94                url = next_link
95                query_parameters = {}  # type: Dict[str, Any]
96                request = self._client.get(url, query_parameters, header_parameters)
97            return request
98
99        def extract_data(pipeline_response):
100            deserialized = self._deserialize('SecurityRuleListResult', pipeline_response)
101            list_of_elem = deserialized.value
102            if cls:
103                list_of_elem = cls(list_of_elem)
104            return deserialized.next_link or None, iter(list_of_elem)
105
106        def get_next(next_link=None):
107            request = prepare_request(next_link)
108
109            pipeline_response = self._client._pipeline.run(request, stream=False, **kwargs)
110            response = pipeline_response.http_response
111
112            if response.status_code not in [200]:
113                map_error(status_code=response.status_code, response=response, error_map=error_map)
114                raise HttpResponseError(response=response, error_format=ARMErrorFormat)
115
116            return pipeline_response
117
118        return ItemPaged(
119            get_next, extract_data
120        )
121    list.metadata = {'url': '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkSecurityGroups/{networkSecurityGroupName}/defaultSecurityRules'}  # type: ignore
122
123    def get(
124        self,
125        resource_group_name,  # type: str
126        network_security_group_name,  # type: str
127        default_security_rule_name,  # type: str
128        **kwargs  # type: Any
129    ):
130        # type: (...) -> "_models.SecurityRule"
131        """Get the specified default network security rule.
132
133        :param resource_group_name: The name of the resource group.
134        :type resource_group_name: str
135        :param network_security_group_name: The name of the network security group.
136        :type network_security_group_name: str
137        :param default_security_rule_name: The name of the default security rule.
138        :type default_security_rule_name: str
139        :keyword callable cls: A custom type or function that will be passed the direct response
140        :return: SecurityRule, or the result of cls(response)
141        :rtype: ~azure.mgmt.network.v2019_11_01.models.SecurityRule
142        :raises: ~azure.core.exceptions.HttpResponseError
143        """
144        cls = kwargs.pop('cls', None)  # type: ClsType["_models.SecurityRule"]
145        error_map = {
146            401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError
147        }
148        error_map.update(kwargs.pop('error_map', {}))
149        api_version = "2019-11-01"
150        accept = "application/json"
151
152        # Construct URL
153        url = self.get.metadata['url']  # type: ignore
154        path_format_arguments = {
155            'resourceGroupName': self._serialize.url("resource_group_name", resource_group_name, 'str'),
156            'networkSecurityGroupName': self._serialize.url("network_security_group_name", network_security_group_name, 'str'),
157            'defaultSecurityRuleName': self._serialize.url("default_security_rule_name", default_security_rule_name, 'str'),
158            'subscriptionId': self._serialize.url("self._config.subscription_id", self._config.subscription_id, 'str'),
159        }
160        url = self._client.format_url(url, **path_format_arguments)
161
162        # Construct parameters
163        query_parameters = {}  # type: Dict[str, Any]
164        query_parameters['api-version'] = self._serialize.query("api_version", api_version, 'str')
165
166        # Construct headers
167        header_parameters = {}  # type: Dict[str, Any]
168        header_parameters['Accept'] = self._serialize.header("accept", accept, 'str')
169
170        request = self._client.get(url, query_parameters, header_parameters)
171        pipeline_response = self._client._pipeline.run(request, stream=False, **kwargs)
172        response = pipeline_response.http_response
173
174        if response.status_code not in [200]:
175            map_error(status_code=response.status_code, response=response, error_map=error_map)
176            raise HttpResponseError(response=response, error_format=ARMErrorFormat)
177
178        deserialized = self._deserialize('SecurityRule', pipeline_response)
179
180        if cls:
181            return cls(pipeline_response, deserialized, {})
182
183        return deserialized
184    get.metadata = {'url': '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/networkSecurityGroups/{networkSecurityGroupName}/defaultSecurityRules/{defaultSecurityRuleName}'}  # type: ignore
185