Debian Codegraph @

ansible_collections/azure/azcollection/plugins/modules/azure_rm_postgresqlflexibleadministrator.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
9 DOCUMENTATION = '''
10 ---
11 module: azure_rm_postgresqlflexibleadministrator
12 version_added: "3.6.0"
13 short_description: Manage PostgreSQL Flexible Administrator instance
14 description:
15 - Add or Delete instance of PostgreSQL Flexible Administrator.
16 - Require the postflexible server Enable Microsoft Entra authenticate.
17
18 options:
19 resource_group:
20 description:
21 - The name of the resource group that contains the resource. You can obtain this value from the Azure Resource Manager API or the portal.
22 required: True
23 type: str
24 server_name:
25 description:
26 - The name of the post gresql flexible server.
27 required: True
28 type: str
29 object_id:
30 description:
31 - The Object ID of Azure Directory Administrator.
32 required: True
33 type: str
34 principal_name:
35 description:
36 - Active Directory administrator principal name.
37 type: str
38 principal_type:
39 description:
40 - The principal type used to represent the type of Active Directory Administrator.
41 type: str
42 choices:
43 - Unknown
44 - User
45 - Group
46 - ServicePrincipal
47 tenant_id:
48 description:
49 - The tenant ID of Active Directory Administrator.
50 type: str
51 state:
52 description:
53 - Assert the state of the PostgreSQL Flexible administrator.
54 - Use C(present) to create or update a administrator and C(absent) to delete it.
55 default: present
56 type: str
57 choices:
58 - absent
59 - present
60
61 extends_documentation_fragment:
62 - azure.azcollection.azure
63
64 author:
65 - magodo (@magodo)
66 - xuzhang3 (@xuzhang3)
67 - Fred-sun (@Fred-sun)
68
69 '''
70
71 EXAMPLES = '''
72 - name: Create (or update) PostgreSQL Flexible Administrator
73 azure_rm_postgresqlflexibleadministrator:
74 resource_group: myResourceGroup
75 server_name: testserver
76 object_id: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
77 principal_type: User
78 principal_name: fred-sun
79 tenant_id: yyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy
80
81 - name: Delete PostgreSQL Flexible Administrator
82 azure_rm_postgresqlflexibleadministrator:
83 resource_group: myResourceGroup
84 server_name: testserver
85 object_id: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
86 state: absent
87 '''
88
89 RETURN = '''
90 administrator:
91 description:
92 - A list of dictionaries containing facts for PostgreSQL Flexible Administrator.
93 returned: always
94 type: complex
95 contains:
96 id:
97 description:
98 - Resource ID of the postgresql flexible admistrator.
99 returned: always
100 type: str
101 sample: "/subscriptions/xxx-xxx/resourceGroups/testRG/providers/Microsoft.DBforPostgreSQL/flexibleServers/postgresql03/administrators/xxx-xxx"
102 principal_name:
103 description:
104 - Active Directory administrator principal name.
105 returned: always
106 type: str
107 sample: fred-sun
108 principal_type:
109 description:
110 - The principal type used to represent the type of Active Directory Administrator.
111 returned: always
112 type: str
113 sample: User
114 object_id:
115 description:
116 - The Object ID of Azure Directory Administrator.
117 returned: always
118 type: str
119 sample: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
120 type:
121 description:
122 - The type of the resource.
123 returned: always
124 type: str
125 sample: Microsoft.DBforPostgreSQL/flexibleServers/administrators
126 tenant_id:
127 description:
128 - The tenant ID of Active Directory Administrator.
129 returned: always
130 type: str
131 sample: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx
132 resource_group:
133 description:
134 - The resource group name.
135 returned: always
136 type: str
137 sample: testRG
138 server_name:
139 description:
140 - The type of the resource.
141 returned: always
142 type: str
143 sample: postgresql03
144 '''
145
146
147 try:
148 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
149 from azure.core.exceptions import ResourceNotFoundError
150 from azure.core.polling import LROPoller
151 except ImportError:
152 # This is handled in azure_rm_common
153 pass
154
155
156 class AzureRMPostgreSqlFlexibleAdministrator(AzureRMModuleBase):
157 """Configuration class for an Azure RM PostgreSQL Flexible Administrator resource"""
158
159 def __init__(self):
160 self.module_arg_spec = dict(
161 resource_group=dict(
162 type='str',
163 required=True
164 ),
165 server_name=dict(
166 type='str',
167 required=True
168 ),
169 object_id=dict(
170 type='str',
171 required=True
172 ),
173 principal_type=dict(
174 type='str',
175 choices=['Unknown', 'User', 'Group', 'ServicePrincipal']
176 ),
177 principal_name=dict(
178 type='str'
179 ),
180 tenant_id=dict(
181 type='str'
182 ),
183 state=dict(
184 type='str',
185 default='present',
186 choices=['present', 'absent']
187 )
188 )
189
190 self.resource_group = None
191 self.server_name = None
192 self.object_id = None
193 self.parameters = dict()
194
195 self.results = dict(changed=False)
196 self.state = None
197
198 super(AzureRMPostgreSqlFlexibleAdministrator, self).__init__(derived_arg_spec=self.module_arg_spec,
199 supports_check_mode=True,
200 supports_tags=False)
201
202 def exec_module(self, **kwargs):
203 """Main module execution method"""
204
205 for key in list(self.module_arg_spec.keys()):
206 if hasattr(self, key):
207 setattr(self, key, kwargs[key])
208 else:
209 self.parameters[key] = kwargs[key]
210
211 old_response = None
212 response = None
213 changed = False
214
215 old_response = self.get_postgresqlflexibleadministrator()
216
217 if not old_response:
218 self.log("PostgreSQL Flexible Administrator instance doesn't exist")
219 if self.state == 'absent':
220 self.log("The PostgreSQL Flexible Administrator with object {0} no exist".format(self.object_id))
221 else:
222 changed = True
223 if not self.check_mode:
224 response = self.add_postgresqlflexibleadministrator(self.parameters)
225 else:
226 self.log("PostgreSQL Flexible Administrator instance with object {0} already exists".format(self.object_id))
227 if self.state == 'absent':
228 changed = True
229 if not self.check_mode:
230 response = self.delete_postgresqlflexibleadministrator()
231 else:
232 if self.check_mode:
233 changed = True
234 self.fail("PostgreSQL Flexible Administrator instance with object {0} already exists".format(self.object_id))
235 response = old_response
236
237 self.results['administrator'] = response
238 self.results['changed'] = changed
239 return self.results
240
241 def add_postgresqlflexibleadministrator(self, body):
242 '''
243 Add PostgreSQL Flexible Administrator with the specified configuration.
244
245 :return: deserialized PostgreSQL Flexible Administrator instance state dictionary
246 '''
247 self.log("Adding the PostgreSQL Flexible Administrator instance {0}".format(self.object_id))
248
249 try:
250 response = self.postgresql_flexible_client.administrators.begin_create(resource_group_name=self.resource_group,
251 server_name=self.server_name,
252 object_id=self.object_id,
253 parameters=body)
254 if isinstance(response, LROPoller):
255 response = self.get_poller_result(response)
256
257 except Exception as exc:
258 self.log('Error attempting to add the PostgreSQL Flexible Administrator instance.')
259 self.fail("Error add the PostgreSQL Flexible Administrator instance: {0}".format(str(exc)))
260 return self.format_item(response)
261
262 def delete_postgresqlflexibleadministrator(self):
263 '''
264 Deletes specified PostgreSQL Flexible Administrator instance in the specified server name and resource group.
265
266 :return: True
267 '''
268 self.log("Deleting the PostgreSQL Flexible Administrator instance {0}".format(self.object_id))
269 try:
270 self.postgresql_flexible_client.administrators.begin_delete(resource_group_name=self.resource_group,
271 server_name=self.server_name,
272 object_id=self.object_id)
273 except Exception as ec:
274 self.log('Error attempting to delete the PostgreSQL Flexible Administrator instance.')
275 self.fail("Error deleting the PostgreSQL Flexible Administrator instance: {0}".format(str(ec)))
276
277 def get_postgresqlflexibleadministrator(self):
278 '''
279 Gets the properties of the specified PostgreSQL Flexible Administrator.
280
281 :return: deserialized PostgreSQL Flexible Administrator instance state dictionary
282 '''
283 self.log("Checking if the PostgreSQL Flexible Administrator instance {0} is present".format(self.object_id))
284 found = False
285 try:
286 response = self.postgresql_flexible_client.administrators.get(resource_group_name=self.resource_group,
287 server_name=self.server_name,
288 object_id=self.object_id)
289 found = True
290 self.log("Response : {0}".format(response))
291 self.log("PostgreSQL Flexible Administrator instance : {0} found".format(response.name))
292 except ResourceNotFoundError as e:
293 self.log('Did not find the PostgreSQL Flexible Administrator instance. Exception as {0}'.format(e))
294 if found is True:
295 return self.format_item(response)
296
297 return None
298
299 def format_item(self, item):
300 return dict(
301 resource_group=self.resource_group,
302 server_name=self.server_name,
303 object_id=item.object_id,
304 id=item.id,
305 principal_name=item.principal_name,
306 principal_type=item.principal_type,
307 tenant_id=item.tenant_id,
308 type=item.type
309 )
310
311
312 def main():
313 """Main execution"""
314 AzureRMPostgreSqlFlexibleAdministrator()
315
316
317 if __name__ == '__main__':
318 main()