Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2024 xuzhang3 (@xuzhang3), Fred-sun (@Fred-sun)
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 __metaclass__ = type
9
10
11 DOCUMENTATION = '''
12 ---
13 module: azure_rm_sqlmidatabase
14 version_added: "2.4.0"
15 short_description: Manage SQL Managed Instance databases
16 description:
17 - Manage SQL Managed Instance databases.
18
19 options:
20 resource_group:
21 description:
22 - The name of the resource group that contains the resource.
23 type: str
24 required: true
25 managed_instance_name:
26 description:
27 - The name of the SQL managed instance.
28 type: str
29 required: true
30 database_name:
31 description:
32 - The name of the SQL managed instance database.
33 type: str
34 required: true
35 collation:
36 description:
37 - The collation of the Azure SQL Managed Database collation to use.
38 - For example C(SQL_Latin1_General_CP1_CI_AS) or C(Latin1_General_100_CS_AS_SC).
39 type: str
40 location:
41 description:
42 - The resource location.
43 type: str
44 state:
45 description:
46 - State of the SQL Managed Database.
47 - Use C(present) to create or update a automation runbook and use C(absent) to delete.
48 type: str
49 default: present
50 choices:
51 - present
52 - absent
53 extends_documentation_fragment:
54 - azure.azcollection.azure
55 - azure.azcollection.azure_tags
56
57 author:
58 - xuzhang3 (@xuzhang3)
59 - Fred-sun (@Fred-sun)
60 '''
61
62 EXAMPLES = '''
63 - name: Create a SQL managed instance database
64 azure_rm_sqlmidatabase:
65 resource_group: testrg
66 managed_instance_name: testinstancename
67 database_name: newdatabase
68 collation: SQL_Latin1_General_CP1_CI_AS
69 location: eastus
70 tags:
71 key2: value2
72
73 - name: Delete the SQL managed instance database
74 azure_rm_sqlmidatabase:
75 resource_group: testrg
76 managed_instance_name: testinstancename
77 database_name: newdatabase
78 state: absent
79 '''
80
81 RETURN = '''
82 database:
83 description:
84 - A dictionary containing facts for SQL Managed Instance database info.
85 returned: always
86 type: complex
87 contains:
88 auto_complete_restore:
89 description:
90 - Whether to auto complete restore of this managed database.
91 type: bool
92 returned: always
93 sample: null
94 catalog_collation:
95 description:
96 - Collation of the metadata catalog.
97 type: str
98 returned: always
99 sample: null
100 create_mode:
101 description:
102 - Managed database create mode.
103 type: str
104 returned: always
105 sample: null
106 create_date:
107 description:
108 - Creation date of the database.
109 type: str
110 returned: always
111 sample: "2024-05-06T23:59:49.770Z"
112 database_name:
113 description:
114 - The sql mi databse name.
115 type: str
116 returned: always
117 sample: fredtest
118 default_secondary_location:
119 description:
120 - Geo paired region.
121 type: str
122 returned: always
123 sample: westus
124 id:
125 description:
126 - The resource ID.
127 type: str
128 returned: always
129 sample: "/subscriptions/xxx-xxxx/resourceGroups/testRG/providers/Microsoft.Sql/managedInstances/fredsqlmin/databases/fredtest"
130 last_backup_name:
131 description:
132 - Last backup file name for restore of this managed database.
133 type: str
134 returned: always
135 sample: null
136 location:
137 description:
138 - The resource's location.
139 type: str
140 returned: always
141 sample: eastus
142 long_term_retention_backup_resource_id:
143 description:
144 - The name of the Long Term Retention backup to be used for restore of this managed database.
145 type: str
146 returned: always
147 sample: null
148 managed_instance_name:
149 description:
150 - The name of the SQL managed instance.
151 type: str
152 returned: always
153 sample: fredsqlmin
154 recoverable_database_id:
155 description:
156 - The resource identifier of the recoverable database associated with the database.
157 type: str
158 returned: always
159 sample: null
160 resource_group:
161 description:
162 - The resource's resource group.
163 type: str
164 returned: always
165 sample: testRG
166 restorable_dropped_database_id:
167 description:
168 - The restorable dropped database resource id.
169 type: str
170 returned: always
171 sample: null
172 restore_point_in_time:
173 description:
174 - Specifies the point in time (ISO8601 format) of the source database.
175 type: str
176 returned: always
177 sample: null
178 source_database_id:
179 description:
180 - The resource identifier of the source database associated with create operation of this database.
181 type: str
182 returned: always
183 sample: null
184 status:
185 description:
186 - Status of the database.
187 type: str
188 returned: always
189 sample: online
190 storage_container_sas_token:
191 description:
192 - Specifies the storage container sas token.
193 type: str
194 returned: always
195 sample: null
196 storage_container_uri:
197 description:
198 - Specifies the uri of the storage container where backups for this restore are stopped.
199 type: str
200 returned: always
201 sample: null
202 tags:
203 description:
204 - The resource's tags
205 type: str
206 returned: always
207 sample: {key1: value1}
208 type:
209 description:
210 - The resource type.
211 type: str
212 returned: always
213 sample: "Microsoft.Sql/managedInstances/databases"
214
215 '''
216
217 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
218
219 try:
220 from azure.core.exceptions import HttpResponseError
221 except ImportError:
222 # This is handled in azure_rm_common
223 pass
224
225
226 class AzureRMSqlMIDatabase(AzureRMModuleBase):
227 def __init__(self):
228 # define user inputs into argument
229 self.module_arg_spec = dict(
230 resource_group=dict(
231 type='str',
232 required=True,
233 ),
234 managed_instance_name=dict(
235 type='str',
236 required=True,
237 ),
238 database_name=dict(
239 type='str',
240 required=True,
241 ),
242 collation=dict(
243 type='str'
244 ),
245 location=dict(
246 type='str'
247 ),
248 state=dict(
249 type='str',
250 choices=['present', 'absent'],
251 default='present'
252 ),
253 )
254 # store the results of the module operation
255 self.results = dict(
256 changed=False
257 )
258 self.resource_group = None
259 self.managed_instance_name = None
260 self.database_name = None
261 self.state = None
262 self.parameters = dict()
263
264 super(AzureRMSqlMIDatabase, self).__init__(self.module_arg_spec, supports_check_mode=True, supports_tags=True, facts_module=False)
265
266 def exec_module(self, **kwargs):
267 for key in list(self.module_arg_spec.keys()) + ['tags']:
268 if hasattr(self, key):
269 setattr(self, key, kwargs[key])
270 elif kwargs.get(key) is not None:
271 self.parameters[key] = kwargs.get(key)
272
273 changed = False
274 resource_group = self.get_resource_group(self.resource_group)
275 if self.parameters.get('location') is None:
276 # Set default location
277 self.parameters['location'] = resource_group.location
278
279 old_response = self.get()
280 if old_response is None:
281 if self.state == 'present':
282 changed = True
283 if not self.check_mode:
284 self.results['database'] = self.create_database()
285 else:
286 update_tags, tags = self.update_tags(old_response.get('tags'))
287 if update_tags:
288 changed = True
289 self.parameters['tags'] = tags
290 for key in self.parameters.keys():
291 if key != 'tags' and self.parameters[key] != old_response.get(key):
292 self.fail("The collection and location not support to update")
293 if self.state == 'present':
294 if changed and not self.check_mode:
295 self.results['database'] = self.update_database()
296 else:
297 self.results['database'] = old_response
298 else:
299 changed = True
300 if not self.check_mode:
301 self.results['database'] = self.delete_database()
302
303 self.results['changed'] = changed
304 return self.results
305
306 def create_database(self):
307 response = None
308 try:
309 response = self.sql_client.managed_databases.begin_create_or_update(resource_group_name=self.resource_group,
310 managed_instance_name=self.managed_instance_name,
311 database_name=self.database_name,
312 parameters=self.parameters)
313 self.log("Response : {0}".format(response))
314 except HttpResponseError as ec:
315 self.fail('Create the SQL managed instance database failed, exception as {0}'.format(ec))
316
317 return self.format_item(self.get_poller_result(response))
318
319 def update_database(self):
320 response = None
321 try:
322 response = self.sql_client.managed_databases.begin_update(resource_group_name=self.resource_group,
323 managed_instance_name=self.managed_instance_name,
324 database_name=self.database_name,
325 parameters=self.parameters)
326 self.log("Response : {0}".format(response))
327 except HttpResponseError as ec:
328 self.fail('Update the SQL managed instance database failed, exception as {0}'.format(ec))
329
330 return self.format_item(self.get_poller_result(response))
331
332 def get(self):
333 response = None
334 try:
335 response = self.sql_client.managed_databases.get(resource_group_name=self.resource_group,
336 managed_instance_name=self.managed_instance_name,
337 database_name=self.database_name)
338 self.log("Response : {0}".format(response))
339 except HttpResponseError as ec:
340 self.log('Could not get facts for SQL managed instance database. Exception as {0}'.format(ec))
341
342 return self.format_item(response)
343
344 def delete_database(self):
345 response = None
346 try:
347 response = self.sql_client.managed_databases.begin_delete(resource_group_name=self.resource_group,
348 managed_instance_name=self.managed_instance_name,
349 database_name=self.database_name)
350 self.log("Response : {0}".format(response))
351 except HttpResponseError as ec:
352 self.fail('Could not get facts for SQL managed instance database. Exception as {0}'.format(ec))
353
354 return self.format_item(self.get_poller_result(response))
355
356 def format_item(self, item):
357 if item is None:
358 return
359 d = item.as_dict()
360 d = {
361 'resource_group': self.resource_group,
362 'managed_instance_name': self.managed_instance_name,
363 'database_name': d.get('name'),
364 'id': d.get('id', None),
365 'type': d.get('type', None),
366 'location': d.get('location'),
367 'tags': d.get('tags'),
368 'collation': d.get('collation'),
369 'status': d.get('status'),
370 'creation_date': d.get('creation_date'),
371 'restore_point_in_time': d.get('restore_point_in_time'),
372 'default_secondary_location': d.get('default_secondary_location'),
373 'catalog_collation': d.get('catalog_collation'),
374 'create_mode': d.get('create_mode'),
375 'storage_container_uri': d.get('storage_container_uri'),
376 'source_database_id': d.get('source_database_id'),
377 'restorable_dropped_database_id': d.get('restorable_dropped_database_id'),
378 'storage_container_sas_token': d.get('storage_container_sas_token'),
379 'recoverable_database_id': d.get('recoverable_database_id'),
380 'long_term_retention_backup_resource_id': d.get('long_term_retention_backup_resource_id'),
381 'auto_complete_restore': d.get('auto_complete_restore'),
382 'last_backup_name': d.get('last_backup_name')
383 }
384 return d
385
386
387 def main():
388 AzureRMSqlMIDatabase()
389
390
391 if __name__ == '__main__':
392 main()