Debian Codegraph @

ansible_collections/azure/azcollection/plugins/modules/azure_rm_serviceendpointpolicy.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 from __future__ import absolute_import, division, print_function
6 __metaclass__ = type
7
8 DOCUMENTATION = '''
9 ---
10 module: azure_rm_serviceendpointpolicy
11 version_added: "3.9.0"
12 short_description: Managed the service endpoint policy
13 description:
14 - Create, update or delete the service endpoint policy.
15
16 options:
17 resource_group:
18 description:
19 - The name of the resource group.
20 type: str
21 required: true
22 name:
23 description:
24 - The name of the service endpoint policy name.
25 type: str
26 required: true
27 location:
28 description:
29 - THe resource's location.
30 type: str
31 state:
32 description:
33 - Set to C(present) to create or update the service endpoint policy.
34 - Set to C(absent) to remove the service endpoint policy.
35 default: present
36 type: str
37 choices:
38 - absent
39 - present
40 extends_documentation_fragment:
41 - azure.azcollection.azure
42 - azure.azcollection.azure_tags
43
44 author:
45 - magodo (@magodo)
46 - Fred Sun (@Fred-sun)
47 '''
48
49 EXAMPLES = '''
50 - name: Create a new service endpoint policy
51 azure.azcollection.azure_rm_serviceendpointpolicy:
52 resource_group: testRG
53 name: policyname
54 locaiton: eastus
55 tags:
56 key: value
57
58 - name: Delete the service endpoint policy
59 azure.azcollection.azure_rm_serviceendpointpolicy:
60 resource_group: testRG
61 name: policyname
62 state: absent
63 '''
64
65 RETURN = '''
66 serviceendpointpolicypolicy:
67 description:
68 - The fact of the service endpoint policys in a service end point policy.
69 type: complex
70 returned: always
71 contains:
72 id:
73 description:
74 - Fully qualified ID of the resource.
75 type: str
76 returned: always
77 sample: "/subscriptions/xxx-xxx/resourceGroups/TestRG/providers/Microsoft.Network/serviceEndpointPolicies/testpolicy"
78 name:
79 description:
80 - The name of the resource that is unique within a resource group.
81 type: str
82 returned: always
83 sample: testpolicy
84 etag:
85 description:
86 - A unique read-only string that changes whenever the resource is updated.
87 returned: always
88 type: str
89 sample: "49e15a7b-1bff-46ba-b962-7a11e75cb729"
90 resource_group:
91 description:
92 - The name of the resource group.
93 type: str
94 returned: always
95 sample: TestRG
96 location:
97 description:
98 - The resource's location.
99 type: str
100 returned: always
101 sample: eastus
102 type:
103 description:
104 - The type of the resource.
105 type: str
106 returned: always
107 sample: Microsoft.Network/serviceEndpointPolicies
108 service_endpoint_policy_definitions:
109 description:
110 - A collection of service endpoint policy definitions of the service endpoint policy.
111 type: list
112 returned: always
113 sample: []
114 provisioning_state:
115 description:
116 - The provisioning state of the service endpoint policy resource.
117 type: str
118 returned: always
119 sample: Successded
120 resource_guid:
121 description:
122 - The resource GUID property of the service endpoint policy resource.
123 type: str
124 returned: always
125 sample: 77ee69ee-4152-4271-bfc0-1d431b7b7d28
126 '''
127
128 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
129 try:
130 from azure.core.polling import LROPoller
131 except ImportError:
132 # This is handled in azure_rm_common
133 pass
134
135
136 class AzureRMServiceEndpointPolicy(AzureRMModuleBase):
137 """Information class for an Azure RM Data Collection Rules"""
138
139 def __init__(self):
140 self.module_arg_spec = dict(
141 resource_group=dict(type='str', required=True),
142 name=dict(type='str', required=True),
143 location=dict(type='str'),
144 state=dict(type='str', default='present', choices=['present', 'absent'])
145 )
146
147 self.resource_group = None
148 self.name = None
149 self.state = None
150 self.location = None
151 self.tags = None
152 self.log_path = None
153 self.log_mode = None
154
155 self.results = dict(
156 changed=False,
157 serviceendpointpolicypolicy=dict()
158 )
159
160 super(AzureRMServiceEndpointPolicy, self).__init__(derived_arg_spec=self.module_arg_spec,
161 supports_check_mode=True,
162 supports_tags=True,
163 facts_module=True)
164
165 def exec_module(self, **kwargs):
166 """Main module execution method"""
167
168 for key in list(self.module_arg_spec.keys()) + ['tags']:
169 setattr(self, key, kwargs[key])
170
171 response = self.get_policy(self.name)
172 resource_group = self.get_resource_group(self.resource_group)
173 if not self.location:
174 # Set default location
175 self.location = resource_group.location
176
177 changed = False
178 if self.state == 'present':
179 if response:
180 update_tags, self.tags = self.update_tags(response.get('tags', dict()))
181 if update_tags:
182 changed = True
183 if not self.check_mode:
184 response = self.update_tag()
185 else:
186 changed = True
187 if not self.check_mode:
188 response = self.create_or_update()
189 else:
190 if response:
191 changed = True
192 if self.check_mode:
193 self.log("The service endpoint policy already exist, will be delete")
194 else:
195 response = self.delete()
196 else:
197 if self.check_mode:
198 self.log("There is no service endpoint policy.")
199
200 self.results['changed'] = changed
201 self.results['serviceendpointpolicypolicy'] = response
202 return self.results
203
204 def get_policy(self, name):
205 '''
206 Gets the specified service endpoint policy
207 '''
208 response = None
209 try:
210 response = self.network_client.service_endpoint_policies.get(self.resource_group, self.name)
211 except Exception:
212 self.log("Could not get the specified service endpoint policy {0}".format(self.name))
213 return self.format_item(response) if response else None
214
215 def create_or_update(self):
216 '''
217 Create or update the service endpoint policy.
218 '''
219 response = None
220 try:
221 response = self.network_client.service_endpoint_policies.begin_create_or_update(resource_group_name=self.resource_group,
222 service_endpoint_policy_name=self.name,
223 parameters=dict(location=self.location,
224 tags=self.tags))
225 if isinstance(response, LROPoller):
226 response = self.get_poller_result(response)
227 except Exception as ex:
228 self.fail("Could not create or update the service endpoint policy, Exception as {0}".format(ex))
229 return self.format_item(response) if response else None
230
231 def update_tag(self):
232 '''
233 Update the service endpoint policy tags.
234 '''
235 response = None
236 try:
237 response = self.network_client.service_endpoint_policies.update_tags(resource_group_name=self.resource_group,
238 service_endpoint_policy_name=self.name,
239 parameters=dict(tags=self.tags))
240 if isinstance(response, LROPoller):
241 response = self.get_poller_result(response)
242 except Exception as ex:
243 self.fail("Could not create or update the service endpoint policy, Exception as {0}".format(ex))
244 return self.format_item(response) if response else None
245
246 def delete(self):
247 '''
248 Delete the service endpoint policy.
249 '''
250 try:
251 response = self.network_client.service_endpoint_policies.begin_delete(self.resource_group, self.name)
252 if isinstance(response, LROPoller):
253 self.get_poller_result(response)
254 except Exception as ex:
255 self.fail("Could not delete the service endpoint policy, Exception as {0}".format(ex))
256
257 def format_item(self, item):
258 response = item.as_dict()
259 response['resource_group'] = self.resource_group
260 return response
261
262
263 def main():
264 """Main execution"""
265 AzureRMServiceEndpointPolicy()
266
267
268 if __name__ == '__main__':
269 main()