Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2021 Praveen Ghuge (@praveenghuge), Karl Dasan (@ikarldasan)
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
9 __metaclass__ = type
10
11
12 DOCUMENTATION = '''
13 ---
14 module: azure_rm_ddosprotectionplan_info
15 version_added: "1.7.0"
16 short_description: Get Azure DDoS protection plan
17 description:
18 - Get facts of Azure DDoS protection plan.
19 options:
20 resource_group:
21 description:
22 - The name of the resource group.
23 type: str
24 name:
25 description:
26 - The name of the DDoS protection plan.
27 type: str
28 extends_documentation_fragment:
29 - azure.azcollection.azure
30 author:
31 - Praveen Ghuge (@praveenghuge)
32 - Karl Dasan (@ikarldasan)
33 '''
34
35
36 EXAMPLES = '''
37 - name: Get facts of specific DDoS protection plan
38 azure_rm_ddosprotectionplan_info:
39 resource_group: myResourceGroup
40 name: myDDoSProtectionPlan
41 '''
42
43 RETURN = '''
44 '''
45
46 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
47
48 try:
49 from azure.core.exceptions import ResourceNotFoundError
50 except ImportError:
51 # This is handled in azure_rm_common
52 pass
53
54
55 class AzureDDoSProtectionPlanInfo(AzureRMModuleBase):
56 def __init__(self):
57 self.module_arg_spec = dict(
58 resource_group=dict(
59 type='str'
60 ),
61 name=dict(
62 type='str'
63 )
64 )
65 # store the results of the module operation
66 self.results = dict(
67 changed=False)
68 self.resource_group = None
69 self.name = None
70 self.tags = None
71
72 super(AzureDDoSProtectionPlanInfo, self).__init__(
73 self.module_arg_spec, supports_check_mode=True, supports_tags=False)
74
75 def exec_module(self, **kwargs):
76
77 for key in self.module_arg_spec:
78 setattr(self, key, kwargs[key])
79
80 if self.name is not None:
81 results = self.get()
82 elif self.resource_group:
83 # all the DDoS protection plan listed in that specific resource group
84 results = self.list_resource_group()
85 else:
86 # all the DDoS protection plan listed in the subscription
87 results = self.list_subscription()
88
89 self.results['ddosprotectionplan'] = [
90 self.ddos_protection_plan_to_dict(x) for x in results]
91 return self.results
92
93 def get(self):
94 response = None
95 results = []
96 try:
97 response = self.network_client.ddos_protection_plans.get(
98 self.resource_group, self.name)
99 self.log("Response : {0}".format(response))
100 except ResourceNotFoundError as e:
101 self.fail('Could not get info for DDoS protection plan. {0}'.format(str(e)))
102
103 if response and self.has_tags(response.tags, self.tags):
104 results = [response]
105 return results
106
107 def list_resource_group(self):
108 self.log('List items for resource group')
109 try:
110 response = self.network_client.ddos_protection_plans.list_by_resource_group(
111 self.resource_group)
112
113 except ResourceNotFoundError as exc:
114 self.fail(
115 "Failed to list for resource group {0} - {1}".format(self.resource_group, str(exc)))
116
117 results = []
118 for item in response:
119 if self.has_tags(item.tags, self.tags):
120 results.append(item)
121 return results
122
123 def list_subscription(self):
124 self.log('List items for subscription')
125 try:
126 response = self.network_client.ddos_protection_plans.list()
127
128 except ResourceNotFoundError as exc:
129 self.fail(
130 "Failed to list DDoS protection plan in the subscription - {0}".format(str(exc)))
131
132 results = []
133 for item in response:
134 if self.has_tags(item.tags, self.tags):
135 results.append(item)
136 return results
137
138 def ddos_protection_plan_to_dict(self, item):
139 # turn DDoS protection plan object into a dictionary (serialization)
140 ddos_protection_plan = item.as_dict()
141
142 result = dict(
143 additional_properties=ddos_protection_plan.get('additional_properties', None),
144 id=ddos_protection_plan.get('id', None),
145 name=ddos_protection_plan.get('name', None),
146 type=ddos_protection_plan.get('type', None),
147 location=ddos_protection_plan.get('location', None),
148 tags=ddos_protection_plan.get('tags', None),
149 etag=ddos_protection_plan.get('etag', None),
150 resource_guid=ddos_protection_plan.get('resource_guid', None),
151 provisioning_state=ddos_protection_plan.get('provisioning_state', None),
152 virtual_networks=ddos_protection_plan.get('virtual_networks', None)
153 )
154 return result
155
156
157 def main():
158 AzureDDoSProtectionPlanInfo()
159
160
161 if __name__ == '__main__':
162 main()