| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
|
| 11 |
DOCUMENTATION = ''' |
| 12 |
--- |
| 13 |
module: azure_rm_sqlmidatabase_info |
| 14 |
version_added: "2.4.0" |
| 15 |
short_description: Get Azure SQL managed instance database facts |
| 16 |
description: |
| 17 |
- Get facts of Azure SQL managed instance database facts. |
| 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 |
tags: |
| 35 |
description: |
| 36 |
- Limit results by providing a list of tags. Format tags as 'key' or 'key:value'. |
| 37 |
type: list |
| 38 |
elements: str |
| 39 |
extends_documentation_fragment: |
| 40 |
- azure.azcollection.azure |
| 41 |
|
| 42 |
author: |
| 43 |
- xuzhang3 (@xuzhang3) |
| 44 |
- Fred-sun (@Fred-sun) |
| 45 |
''' |
| 46 |
|
| 47 |
EXAMPLES = ''' |
| 48 |
- name: Get SQL managed instance database by name |
| 49 |
azure_rm_sqlmidatabase_info: |
| 50 |
resource_group: testrg |
| 51 |
managed_instance_name: testinstancename |
| 52 |
database_name: newdatabase |
| 53 |
''' |
| 54 |
|
| 55 |
RETURN = ''' |
| 56 |
database: |
| 57 |
description: |
| 58 |
- A dictionary containing facts for SQL Managed Instance database info. |
| 59 |
returned: always |
| 60 |
type: complex |
| 61 |
contains: |
| 62 |
auto_complete_restore: |
| 63 |
description: |
| 64 |
- Whether to auto complete restore of this managed database. |
| 65 |
type: bool |
| 66 |
returned: always |
| 67 |
sample: null |
| 68 |
catalog_collation: |
| 69 |
description: |
| 70 |
- Collation of the metadata catalog. |
| 71 |
type: str |
| 72 |
returned: always |
| 73 |
sample: null |
| 74 |
create_mode: |
| 75 |
description: |
| 76 |
- Managed database create mode. |
| 77 |
type: str |
| 78 |
returned: always |
| 79 |
sample: null |
| 80 |
create_date: |
| 81 |
description: |
| 82 |
- Creation date of the database. |
| 83 |
type: str |
| 84 |
returned: always |
| 85 |
sample: "2024-05-06T23:59:49.770Z" |
| 86 |
database_name: |
| 87 |
description: |
| 88 |
- The sql mi databse name. |
| 89 |
type: str |
| 90 |
returned: always |
| 91 |
sample: fredtest |
| 92 |
default_secondary_location: |
| 93 |
description: |
| 94 |
- Geo paired region. |
| 95 |
type: str |
| 96 |
returned: always |
| 97 |
sample: westus |
| 98 |
id: |
| 99 |
description: |
| 100 |
- The resource ID. |
| 101 |
type: str |
| 102 |
returned: always |
| 103 |
sample: "/subscriptions/xxx-xxxx/resourceGroups/testRG/providers/Microsoft.Sql/managedInstances/fredsqlmin/databases/fredtest" |
| 104 |
last_backup_name: |
| 105 |
description: |
| 106 |
- Last backup file name for restore of this managed database. |
| 107 |
type: str |
| 108 |
returned: always |
| 109 |
sample: null |
| 110 |
location: |
| 111 |
description: |
| 112 |
- The resource's location. |
| 113 |
type: str |
| 114 |
returned: always |
| 115 |
sample: eastus |
| 116 |
long_term_retention_backup_resource_id: |
| 117 |
description: |
| 118 |
- The name of the Long Term Retention backup to be used for restore of this managed database. |
| 119 |
type: str |
| 120 |
returned: always |
| 121 |
sample: null |
| 122 |
managed_instance_name: |
| 123 |
description: |
| 124 |
- The name of the SQL managed instance. |
| 125 |
type: str |
| 126 |
returned: always |
| 127 |
sample: fredsqlmin |
| 128 |
recoverable_database_id: |
| 129 |
description: |
| 130 |
- The resource identifier of the recoverable database associated with the database. |
| 131 |
type: str |
| 132 |
returned: always |
| 133 |
sample: null |
| 134 |
resource_group: |
| 135 |
description: |
| 136 |
- The resource's resource group. |
| 137 |
type: str |
| 138 |
returned: always |
| 139 |
sample: testRG |
| 140 |
restorable_dropped_database_id: |
| 141 |
description: |
| 142 |
- The restorable dropped database resource id. |
| 143 |
type: str |
| 144 |
returned: always |
| 145 |
sample: null |
| 146 |
restore_point_in_time: |
| 147 |
description: |
| 148 |
- Specifies the point in time (ISO8601 format) of the source database. |
| 149 |
type: str |
| 150 |
returned: always |
| 151 |
sample: null |
| 152 |
source_database_id: |
| 153 |
description: |
| 154 |
- The resource identifier of the source database associated with create operation of this database. |
| 155 |
type: str |
| 156 |
returned: always |
| 157 |
sample: null |
| 158 |
status: |
| 159 |
description: |
| 160 |
- Status of the database. |
| 161 |
type: str |
| 162 |
returned: always |
| 163 |
sample: online |
| 164 |
storage_container_sas_token: |
| 165 |
description: |
| 166 |
- Specifies the storage container sas token. |
| 167 |
type: str |
| 168 |
returned: always |
| 169 |
sample: null |
| 170 |
storage_container_uri: |
| 171 |
description: |
| 172 |
- Specifies the uri of the storage container where backups for this restore are stopped. |
| 173 |
type: str |
| 174 |
returned: always |
| 175 |
sample: null |
| 176 |
tags: |
| 177 |
description: |
| 178 |
- The resource's tags |
| 179 |
type: str |
| 180 |
returned: always |
| 181 |
sample: {key1: value1} |
| 182 |
type: |
| 183 |
description: |
| 184 |
- The resource type. |
| 185 |
type: str |
| 186 |
returned: always |
| 187 |
sample: "Microsoft.Sql/managedInstances/databases" |
| 188 |
''' |
| 189 |
|
| 190 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 191 |
|
| 192 |
try: |
| 193 |
from azure.core.exceptions import HttpResponseError |
| 194 |
except ImportError: |
| 195 |
|
| 196 |
pass |
| 197 |
|
| 198 |
|
| 199 |
class AzureRMSqlMIDatabaseInfo(AzureRMModuleBase): |
| 200 |
def __init__(self): |
| 201 |
|
| 202 |
self.module_arg_spec = dict( |
| 203 |
resource_group=dict( |
| 204 |
type='str', |
| 205 |
required=True, |
| 206 |
), |
| 207 |
managed_instance_name=dict( |
| 208 |
type='str', |
| 209 |
required=True, |
| 210 |
), |
| 211 |
database_name=dict( |
| 212 |
type='str', |
| 213 |
), |
| 214 |
tags=dict( |
| 215 |
type='list', |
| 216 |
elements='str' |
| 217 |
), |
| 218 |
) |
| 219 |
|
| 220 |
self.results = dict( |
| 221 |
changed=False |
| 222 |
) |
| 223 |
self.resource_group = None |
| 224 |
self.managed_instance_name = None |
| 225 |
self.database_name = None |
| 226 |
self.tags = None |
| 227 |
|
| 228 |
super(AzureRMSqlMIDatabaseInfo, self).__init__(self.module_arg_spec, supports_check_mode=True, supports_tags=False, facts_module=True) |
| 229 |
|
| 230 |
def exec_module(self, **kwargs): |
| 231 |
for key in self.module_arg_spec: |
| 232 |
setattr(self, key, kwargs[key]) |
| 233 |
|
| 234 |
if self.database_name is not None: |
| 235 |
self.results['database'] = self.get() |
| 236 |
else: |
| 237 |
self.results['database'] = self.list_by_instance() |
| 238 |
return self.results |
| 239 |
|
| 240 |
def list_by_instance(self): |
| 241 |
response = None |
| 242 |
results = [] |
| 243 |
try: |
| 244 |
response = self.sql_client.managed_databases.list_by_instance(resource_group_name=self.resource_group, |
| 245 |
managed_instance_name=self.managed_instance_name) |
| 246 |
self.log("Response : {0}".format(response)) |
| 247 |
except HttpResponseError: |
| 248 |
self.log('Could not get facts for SQL managed instance database.') |
| 249 |
|
| 250 |
if response is not None: |
| 251 |
for item in response: |
| 252 |
if self.has_tags(item.tags, self.tags): |
| 253 |
results.append(self.format_item(item)) |
| 254 |
return results |
| 255 |
|
| 256 |
def get(self): |
| 257 |
response = None |
| 258 |
try: |
| 259 |
response = self.sql_client.managed_databases.get(resource_group_name=self.resource_group, |
| 260 |
managed_instance_name=self.managed_instance_name, |
| 261 |
database_name=self.database_name) |
| 262 |
self.log("Response : {0}".format(response)) |
| 263 |
except HttpResponseError as ec: |
| 264 |
self.log('Could not get facts for SQL managed instance database.') |
| 265 |
|
| 266 |
if response is not None and self.has_tags(response.tags, self.tags): |
| 267 |
return [self.format_item(response)] |
| 268 |
|
| 269 |
def format_item(self, item): |
| 270 |
d = item.as_dict() |
| 271 |
d = { |
| 272 |
'resource_group': self.resource_group, |
| 273 |
'managed_instance_name': self.managed_instance_name, |
| 274 |
'database_name': d.get('name'), |
| 275 |
'id': d.get('id', None), |
| 276 |
'type': d.get('type', None), |
| 277 |
'location': d.get('location'), |
| 278 |
'tags': d.get('tags'), |
| 279 |
'collation': d.get('collation'), |
| 280 |
'status': d.get('status'), |
| 281 |
'creation_date': d.get('creation_date'), |
| 282 |
'earliest_restore_point': d.get('earliest_restore_point'), |
| 283 |
'restore_point_in_time': d.get('restore_point_in_time'), |
| 284 |
'default_secondary_location': d.get('default_secondary_location'), |
| 285 |
'catalog_collation': d.get('catalog_collation'), |
| 286 |
'create_mode': d.get('create_mode'), |
| 287 |
'storage_container_uri': d.get('storage_container_uri'), |
| 288 |
'source_database_id': d.get('source_database_id'), |
| 289 |
'restorable_dropped_database_id': d.get('restorable_dropped_database_id'), |
| 290 |
'storage_container_sas_token': d.get('storage_container_sas_token'), |
| 291 |
'failover_group_id': d.get('failover_group_id'), |
| 292 |
'recoverable_database_id': d.get('recoverable_database_id'), |
| 293 |
'long_term_retention_backup_resource_id': d.get('long_term_retention_backup_resource_id'), |
| 294 |
'auto_complete_restore': d.get('auto_complete_restore'), |
| 295 |
'last_backup_name': d.get('last_backup_name') |
| 296 |
} |
| 297 |
return d |
| 298 |
|
| 299 |
|
| 300 |
def main(): |
| 301 |
AzureRMSqlMIDatabaseInfo() |
| 302 |
|
| 303 |
|
| 304 |
if __name__ == '__main__': |
| 305 |
main() |