Debian Codegraph @

ansible_collections/azure/azcollection/plugins/modules/azure_rm_networkflowlog_info.py

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2024 xuzhang3 (@xuzhang3), Fred-sun (@Fred-sun)
4 #
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8 __metaclass__ = type
9
10
11 DOCUMENTATION = '''
12 ---
13 module: azure_rm_networkflowlog_info
14
15 version_added: "2.5.0"
16
17 short_description: Get or list the network flow logs
18
19 description:
20 - Get or list the network flow logs facts.
21
22 options:
23 resource_group:
24 description:
25 - The name of the resource group.
26 type: str
27 required: true
28 network_watcher_name:
29 description:
30 - Name of the network watcher.
31 type: str
32 required: true
33 name:
34 description:
35 - Name of the network flow logs.
36 type: str
37 tags:
38 description:
39 - Limit results by providing a list of tags. Format tags as 'key' or 'key:value'.
40 type: list
41 elements: str
42
43 extends_documentation_fragment:
44 - azure.azcollection.azure
45
46 author:
47 - xuzhang3 (@xuzhang3)
48 - Fred-sun (@Fred-sun)
49
50 '''
51
52 EXAMPLES = '''
53 - name: Get the network watcher facts
54 azure_rm_networkflowlog_info:
55 resource_group: myResourceGroup
56 network_watcher_name: mywatcher01
57 name: flowlogname
58
59 - name: list the network flow logs and filter by tags
60 azure_rm_networkflowlog_info:
61 resource_group: myResourceGroup
62 network_watcher_name: mywatcher01
63 tags:
64 - key1
65 '''
66
67 RETURN = '''
68 flow_logs:
69 description:
70 - The facts of the network flow logs.
71 returned: always
72 type: complex
73 contains:
74 resource_group:
75 description:
76 - The resource group.
77 type: str
78 returned: always
79 sample: NetworkWatcherRG
80 id:
81 description:
82 - Resource ID.
83 returned: always
84 type: str
85 sample: /subscriptions/xxx-xxx/resourceGroups/NetWatcherRG/providers/Microsoft.Network/networkWatchers/NetWatcher_eastus/flowLogs/xz-flowlog"
86 location:
87 description:
88 - Resource location.
89 returned: always
90 type: str
91 sample: eastus
92 name:
93 description:
94 - Resource name.
95 returned: always
96 type: str
97 sample: xz-flowlog
98 network_watcher_name:
99 description:
100 - The name of the network watcher.
101 type: str
102 returned: always
103 sample: NetWatcher_eastus
104 target_resource_id:
105 description:
106 - ID of network security group to which flow log will be applied.
107 type: str
108 returned: always
109 sample: /subscriptions/xxx-xxx/resourceGroups/xz3mlwaiserv/providers/Microsoft.Network/virtualNetworks/xz3mlwvnet"
110 storage_id:
111 description:
112 - ID of the storage account which is used to store the flow log.
113 type: str
114 returned: always
115 sample: "/subscriptions/xxx-xxx/resourceGroups/AutoTagFunctionAppRG/providers/Microsoft.Storage/storageAccounts/autotagfunc01"
116 enanbled:
117 description:
118 - Flag to enable/disable flow logging.
119 type: str
120 returned: always
121 sample: true
122 retention_policy:
123 description:
124 - Parameters that define the retention policy for flow log.
125 type: complex
126 returned: always
127 contains:
128 day:
129 description:
130 - Number of days to retain flow log records.
131 type: int
132 returned: always
133 sample: 0
134 enabled:
135 description:
136 - Flag to enable/disable retention.
137 type: bool
138 returned: always
139 sample: false
140 flow_analytics_configuration:
141 description:
142 - Parameters that define the configuration of traffic analytics.
143 type: complex
144 returned: always
145 contains:
146 network_watcher_flow_analytics_configuration:
147 description:
148 - Parameters that define the configuration of traffic analytics.
149 type: complex
150 returned: always
151 contains:
152 enabled:
153 description:
154 - Flag to enable/disable traffic analytics.
155 type: bool
156 returned: always
157 sample: true
158 workspace_id:
159 description:
160 - The resource guid of the attached workspace.
161 type: str
162 returned: always
163 sample: 7c16a8dd-b983-4f75-b78b-a804c169306c
164 workspace_region:
165 description:
166 - The location of the attached workspace.
167 type: str
168 returned: always
169 sample: eastus
170 workspace_resource_id:
171 description:
172 - Resource Id of the attached workspace.
173 type: str
174 returned: always
175 sample: /subscriptions/xxx-xxx/resourceGroups/DefaulUS/providers/Microsoft.OperationalInsights/workspaces/DefaultWorkspace-0-EUS"
176 traffic_analytics_interval:
177 description:
178 - The interval in minutes which would decide how frequently TA service should do flow analytics.
179 type: str
180 returned: always
181 sample: 60
182 tags:
183 description:
184 - Resource tags.
185 returned: always
186 type: dict
187 sample: { 'key1':'value1' }
188 type:
189 description:
190 - Resource type.
191 returned: always
192 type: str
193 sample: "Microsoft.Network/networkWatchers/flowLogs"
194 provisioning_state:
195 description:
196 - The provisioning state of the network flow logs resource.
197 type: str
198 returned: always
199 sample: Succeeded
200 '''
201
202 try:
203 from azure.core.exceptions import ResourceNotFoundError
204 except Exception:
205 # This is handled in azure_rm_common
206 pass
207
208 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
209
210
211 class AzureRMNetworkFlowLogInfo(AzureRMModuleBase):
212
213 def __init__(self):
214
215 self.module_arg_spec = dict(
216 resource_group=dict(type='str', required=True),
217 network_watcher_name=dict(type='str', required=True),
218 name=dict(type='str'),
219 tags=dict(type='list', elements='str')
220 )
221
222 self.results = dict(
223 changed=False,
224 flow_logs=[]
225 )
226
227 self.resource_group = None
228 self.network_watcher_name = None
229 self.name = None
230 self.tags = None
231
232 super(AzureRMNetworkFlowLogInfo, self).__init__(self.module_arg_spec,
233 supports_check_mode=True,
234 supports_tags=False,
235 facts_module=True)
236
237 def exec_module(self, **kwargs):
238 for key in self.module_arg_spec:
239 setattr(self, key, kwargs[key])
240
241 if self.name:
242 response = [self.get_by_name()]
243 else:
244 response = self.list_by_network_watcher()
245
246 self.results['flow_logs'] = [self.to_dict(item) for item in response if response is not None]
247
248 return self.results
249
250 def get_by_name(self):
251 response = None
252 try:
253 response = self.network_client.flow_logs.get(self.resource_group, self.network_watcher_name, self.name)
254
255 except ResourceNotFoundError as exec:
256 self.log("Failed to get network flow logs, Exception as {0}".format(exec))
257
258 return response
259
260 def list_by_network_watcher(self):
261 response = []
262 try:
263 response = self.network_client.flow_logs.list(self.resource_group, self.network_watcher_name)
264 except Exception as exec:
265 self.log("Faild to list network flow logs by network watcher, exception as {0}".format(exec))
266 return response
267
268 def to_dict(self, body):
269 results = dict()
270 if body is not None and self.has_tags(body.tags, self.tags):
271 results = dict(
272 resource_group=self.resource_group,
273 network_watcher_name=self.network_watcher_name,
274 id=body.id,
275 name=body.name,
276 location=body.location,
277 tags=body.tags,
278 type=body.type,
279 provisioning_state=body.provisioning_state,
280 target_resource_id=body.target_resource_id,
281 storage_id=body.storage_id,
282 enabled=body.enabled,
283 retention_policy=dict(),
284 flow_analytics_configuration=dict()
285 )
286 if body.retention_policy is not None:
287 results['retention_policy']['days'] = body.retention_policy.days
288 results['retention_policy']['enabled'] = body.retention_policy.enabled
289 if body.flow_analytics_configuration is not None:
290 results['flow_analytics_configuration']['network_watcher_flow_analytics_configuration'] = dict()
291 if body.flow_analytics_configuration.network_watcher_flow_analytics_configuration is not None:
292 new_config = body.flow_analytics_configuration.network_watcher_flow_analytics_configuration
293 results['flow_analytics_configuration']['network_watcher_flow_analytics_configuration']['enabled'] = new_config.enabled
294 results['flow_analytics_configuration']['network_watcher_flow_analytics_configuration']['workspace_id'] = new_config.workspace_id
295 results['flow_analytics_configuration']['network_watcher_flow_analytics_configuration']['workspace_region'] = new_config.workspace_region
296 results['flow_analytics_configuration']['network_watcher_flow_analytics_configuration']['workspace_resource_id'] = \
297 new_config.workspace_resource_id
298 results['flow_analytics_configuration']['network_watcher_flow_analytics_configuration']['traffic_analytics_interval'] = \
299 new_config.traffic_analytics_interval
300
301 return results
302
303
304 def main():
305 AzureRMNetworkFlowLogInfo()
306
307
308 if __name__ == '__main__':
309 main()