Debian Codegraph @

ansible_collections/azure/azcollection/plugins/modules/azure_rm_ddosprotectionplan.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 from __future__ import absolute_import, division, print_function
7 __metaclass__ = type
8 DOCUMENTATION = '''
9 ---
10 module: azure_rm_ddosprotectionplan
11 version_added: "1.7.0"
12 short_description: Manage DDoS protection plan
13 description:
14 - Create, update and delete instance of DDoS protection plan.
15 options:
16 resource_group:
17 description:
18 - Name of the resource group to which the resource belongs.
19 required: true
20 type: str
21 name:
22 description:
23 - Unique name of the app service plan to create or update.
24 required: true
25 type: str
26 location:
27 description:
28 - Resource location. If not set, location from the resource group will be used as default.
29 type: str
30 state:
31 description:
32 - Assert the state of the DDoS protection plan.
33 - Use C(present) to create or update an DDoS protection plan and C(absent) to delete it.
34 type: str
35 default: present
36 choices:
37 - absent
38 - present
39 log_path:
40 description:
41 - parent argument.
42 type: str
43 log_mode:
44 description:
45 - parent argument.
46 type: str
47 extends_documentation_fragment:
48 - azure.azcollection.azure
49 - azure.azcollection.azure_tags
50 author:
51 - Praveen Ghuge (@praveenghuge)
52 - Karl Dasan (@ikarldasan)
53 '''
54 EXAMPLES = '''
55 - name: "Create DDoS protection plan"
56 azure_rm_ddosprotectionplan:
57 resource_group: rg
58 location: eastus
59 name: ddosplan
60 - name: Delete DDoS protection plan
61 azure_rm_ddosprotectionplan:
62 resource_group: rg
63 name: ddosplan
64 state: absent
65 '''
66
67 RETURN = '''
68 state:
69 description:
70 - Current state of the DDoS protection plan.
71 returned: always
72 type: dict
73 sample: {
74 "id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroup/myResourceGroup/providers/Microsoft.Network/ddosProtectionPlans/ddosplan",
75 "location": "eastus",
76 "name": "ddosplan",
77 "etag": "W/60ac0480-44dd-4881-a2ed-680d20b3978e",
78 "provisioning_state": "Succeeded",
79 "resource_guid": null,
80 "type": "Microsoft.Network/ddosProtectionPlans",
81 "tags": {"a": "b"},
82 "virtual_networks": []
83 }
84 '''
85
86 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
87
88 try:
89 from azure.core.exceptions import ResourceNotFoundError
90 except ImportError:
91 # This is handled in azure_rm_common
92 pass
93
94
95 class AzureDDoSProtectionPlan(AzureRMModuleBase):
96
97 def __init__(self):
98 # define user inputs from playbook
99
100 self.module_arg_spec = dict(
101 resource_group=dict(type='str', required=True),
102 name=dict(type='str', required=True),
103 location=dict(type='str'),
104 state=dict(choices=['present', 'absent'],
105 default='present', type='str'),
106 )
107
108 self.resource_group = None
109 self.name = None
110 self.location = None
111 self.state = None
112 self.tags = None
113 self.log_path = None
114 self.results = dict(
115 changed=False,
116 state=dict()
117 )
118
119 super(AzureDDoSProtectionPlan, self).__init__(self.module_arg_spec,
120 supports_check_mode=True,
121 supports_tags=True)
122
123 def exec_module(self, **kwargs):
124
125 for key in list(self.module_arg_spec.keys()) + ['tags']:
126 setattr(self, key, kwargs[key])
127
128 self.results['check_mode'] = self.check_mode
129
130 # retrieve resource group to make sure it exists
131 self.get_resource_group(self.resource_group)
132
133 results = dict()
134 changed = False
135
136 try:
137 self.log('Fetching DDoS protection plan {0}'.format(self.name))
138 ddos_protection_plan = self.network_client.ddos_protection_plans.get(
139 self.resource_group, self.name)
140
141 results = ddos_protection_plan_to_dict(ddos_protection_plan)
142
143 # don't change anything if creating an existing zone, but change if deleting it
144 if self.state == 'present':
145 changed = False
146
147 update_tags, results['tags'] = self.update_tags(
148 results['tags'])
149 if update_tags:
150 changed = True
151
152 elif self.state == 'absent':
153 changed = True
154
155 except ResourceNotFoundError:
156 # the DDoS protection plan does not exist so create it
157 if self.state == 'present':
158 changed = True
159 else:
160 # you can't delete what is not there
161 changed = False
162
163 self.results['changed'] = changed
164 self.results['state'] = results
165
166 # return the results if you are only gathering information
167 if self.check_mode:
168 return self.results
169
170 if changed:
171 if self.state == "present":
172 self.results['state'] = self.create_or_update_ddos_protection_plan(
173 self.module.params)
174 elif self.state == "absent":
175 # delete DDoS protection plan
176 self.delete_ddos_protection_plan()
177 self.results['state']['status'] = 'Deleted'
178
179 return self.results
180
181 def create_or_update_ddos_protection_plan(self, params):
182 '''
183 Create or update DDoS protection plan.
184 :return: create or update DDoS protection plan instance state dictionary
185 '''
186 self.log("create or update DDoS protection plan {0}".format(self.name))
187 try:
188 poller = self.network_client.ddos_protection_plans.begin_create_or_update(
189 resource_group_name=params.get("resource_group"),
190 ddos_protection_plan_name=params.get("name"),
191 parameters=params)
192 result = self.get_poller_result(poller)
193 self.log("Response : {0}".format(result))
194 except Exception as ex:
195 self.fail("Failed to create DDoS protection plan {0} in resource group {1}: {2}".format(
196 self.name, self.resource_group, str(ex)))
197 return ddos_protection_plan_to_dict(result)
198
199 def delete_ddos_protection_plan(self):
200 '''
201 Deletes specified DDoS protection plan
202 :return True
203 '''
204 self.log("Deleting the DDoS protection plan {0}".format(self.name))
205 try:
206 poller = self.network_client.ddos_protection_plans.begin_delete(
207 self.resource_group, self.name)
208 result = self.get_poller_result(poller)
209 except ResourceNotFoundError as e:
210 self.log('Error attempting to delete DDoS protection plan.')
211 self.fail(
212 "Error deleting the DDoS protection plan : {0}".format(str(e)))
213 return result
214
215
216 def ddos_protection_plan_to_dict(item):
217 # turn DDoS protection plan object into a dictionary (serialization)
218 ddos_protection_plan = item.as_dict()
219
220 result = dict(
221 additional_properties=ddos_protection_plan.get('additional_properties', None),
222 id=ddos_protection_plan.get('id', None),
223 name=ddos_protection_plan.get('name', None),
224 type=ddos_protection_plan.get('type', None),
225 location=ddos_protection_plan.get('location', None),
226 tags=ddos_protection_plan.get('tags', None),
227 etag=ddos_protection_plan.get('etag', None),
228 resource_guid=ddos_protection_plan.get('resource_guid', None),
229 provisioning_state=ddos_protection_plan.get('provisioning_state', None),
230 virtual_networks=ddos_protection_plan.get('virtual_networks', None)
231 )
232 return result
233
234
235 def main():
236 AzureDDoSProtectionPlan()
237
238
239 if __name__ == '__main__':
240 main()