Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
4 #
5 # Python SDK Reference: https://learn.microsoft.com/en-us/python/api/azure-mgmt-cdn/azure.mgmt.cdn.operations.rulesetsoperations?view=azure-python
6
7 from __future__ import absolute_import, division, print_function
8 __metaclass__ = type
9
10 DOCUMENTATION = '''
11 ---
12 module: azure_rm_afdruleset
13 version_added: "3.4.0"
14 short_description: Manage an Azure Front Door Rule Set
15 description:
16 - Create, update and delete an Azure Front Door Rule Set to be used by a Front Door Service Profile created using azure_rm_cdnprofile.
17
18 options:
19 name:
20 description:
21 - Name of the Front Door Rule Set.
22 required: true
23 type: str
24 profile_name:
25 description:
26 - Name of the Front Door Profile.
27 required: true
28 type: str
29 resource_group:
30 description:
31 - Name of a resource group where the CDN front door ruleset exists or will be created.
32 required: true
33 type: str
34 state:
35 description:
36 - Assert the state of the CDN profile. Use C(present) to create or update a CDN profile and C(absent) to delete it.
37 default: present
38 type: str
39 choices:
40 - absent
41 - present
42
43 extends_documentation_fragment:
44 - azure.azcollection.azure
45
46 author:
47 - Jarret Tooley (@jartoo)
48 '''
49
50 EXAMPLES = '''
51 - name: Create a Ruleset
52 azure_rm_afdruleset:
53 name: myRuleset
54 profile_name: myProfile
55 resource_group: myResourceGroup
56 state: present
57
58 - name: Delete a Ruleset
59 azure_rm_afdruleset:
60 name: myRuleset
61 profile_name: myProfile
62 resource_group: myResourceGroup
63 state: absent
64 '''
65 RETURN = '''
66 id:
67 description: Resource ID
68 returned: always
69 type: str
70 example: "/subscriptions/xxxxxx-xxxx-xxxx-xxxx-xxxxxxx/resourcegroups/myRG/providers/Microsoft.Cdn/profiles/myProf/rulesets/myRS"
71 '''
72 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
73
74
75 class AzureRMRuleSet(AzureRMModuleBase):
76 """Class representing a Ruleset for Azure Frontdoor Standard or Premium"""
77
78 def __init__(self):
79 self.module_arg_spec = dict(
80 name=dict(
81 type='str',
82 required=True
83 ),
84 profile_name=dict(
85 type='str',
86 required=True
87 ),
88 resource_group=dict(
89 type='str',
90 required=True
91 ),
92 state=dict(
93 type='str',
94 default='present',
95 choices=['present', 'absent'],
96 required=False
97 )
98 )
99
100 self.name = None
101 self.profile_name = None
102 self.resource_group = None
103 self.state = None
104
105 self.results = dict(changed=False)
106
107 super(AzureRMRuleSet, self).__init__(
108 derived_arg_spec=self.module_arg_spec,
109 supports_check_mode=True,
110 supports_tags=False)
111
112 def exec_module(self, **kwargs):
113 """Main module execution method"""
114
115 for key in list(self.module_arg_spec.keys()):
116 setattr(self, key, kwargs[key])
117
118 response = self.get_ruleset()
119
120 self.results['changed'] = False
121 self.results['id'] = None
122
123 if self.state == 'present':
124
125 if not response:
126 self.log("Need to create the Rule Set")
127 if not self.check_mode:
128 new_response = self.create_ruleset()
129 self.results['id'] = new_response['id']
130
131 self.results['changed'] = True
132
133 else:
134 self.results['id'] = response['id']
135 self.log('Results : {0}'.format(response))
136
137 elif self.state == 'absent':
138 if not response:
139 self.log("Rule Set {0} does not exist.".format(self.name))
140 else:
141 self.log("Need to delete the Rule Set")
142 self.results['changed'] = True
143
144 self.results['id'] = response['id']
145 if not self.check_mode:
146 self.delete_ruleset()
147
148 return self.results
149
150 def create_ruleset(self):
151 '''
152 Creates an Azure Rule Set.
153
154 :return: deserialized Azure Rule Set instance state dictionary
155 '''
156 self.log("Creating the Azure Rule Set instance {0}".format(self.name))
157
158 try:
159 response = self.cdn_client.rule_sets.create(
160 resource_group_name=self.resource_group,
161 profile_name=self.profile_name,
162 rule_set_name=self.name
163 )
164 return ruleset_to_dict(response)
165 except AttributeError as exc:
166 self.fail("Please ensure azure-mgmt-cdn is upgraded to version 13.1.0 or greater: {0}".format(str(exc)))
167 except Exception as exc:
168 self.log('Error attempting to create Azure Rule Set instance.')
169 self.fail("Error Creating Azure Rule Set instance: {0}".format(str(exc)))
170
171 def delete_ruleset(self):
172 '''
173 Deletes the specified Azure Rule Set in the specified subscription and resource group.
174
175 :return: bool
176 '''
177 self.log("Deleting the Rule Set {0}".format(self.name))
178 try:
179 poller = self.cdn_client.rule_sets.begin_delete(
180 resource_group_name=self.resource_group,
181 profile_name=self.profile_name,
182 rule_set_name=self.name)
183 self.get_poller_result(poller)
184 return True
185 except Exception as exc:
186 self.log('Error attempting to delete the Rule Set.')
187 self.fail("Error deleting the Rule Set: {0}".format(str(exc)))
188 return False
189
190 def get_ruleset(self):
191 '''
192 Gets the properties of the specified Rule Set.
193
194 :return: deserialized Rule Set state dictionary
195 '''
196 self.log(
197 "Checking if the Rule Set {0} is present".format(self.name))
198 try:
199 response = self.cdn_client.rule_sets.get(
200 resource_group_name=self.resource_group,
201 profile_name=self.profile_name,
202 rule_set_name=self.name,
203 )
204 self.log("Response : {0}".format(response))
205 self.log("Rule Set : {0} found".format(response.name))
206 return ruleset_to_dict(response)
207 except Exception as err:
208 self.log('Did not find the Rule Set.' + err.args[0])
209 return False
210
211
212 def ruleset_to_dict(ruleset):
213 '''
214 Sets the properties of the specified Rule Set.
215
216 :return: deserialized Rule Set state dictionary
217 '''
218 return dict(
219 deployment_status=ruleset.deployment_status,
220 id=ruleset.id,
221 name=ruleset.name,
222 provisioning_state=ruleset.provisioning_state,
223 type=ruleset.type
224 )
225
226
227 def main():
228 """Main execution"""
229 AzureRMRuleSet()
230
231
232 if __name__ == '__main__':
233 main()