| 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_postgresqlflexibledatabase |
| 14 |
version_added: "2.2.0" |
| 15 |
short_description: Manage PostgreSQL Flexible Database instance |
| 16 |
description: |
| 17 |
- Create, update and delete instance of PostgreSQL Flexible Database. |
| 18 |
|
| 19 |
options: |
| 20 |
resource_group: |
| 21 |
description: |
| 22 |
- The name of the resource group that contains the resource. You can obtain this value from the Azure Resource Manager API or the portal. |
| 23 |
required: True |
| 24 |
type: str |
| 25 |
server_name: |
| 26 |
description: |
| 27 |
- The name of the server. |
| 28 |
required: True |
| 29 |
type: str |
| 30 |
name: |
| 31 |
description: |
| 32 |
- The name of the database. |
| 33 |
required: True |
| 34 |
type: str |
| 35 |
charset: |
| 36 |
description: |
| 37 |
- The charset of the database. |
| 38 |
type: str |
| 39 |
collation: |
| 40 |
description: |
| 41 |
- The collation of the database. |
| 42 |
type: str |
| 43 |
state: |
| 44 |
description: |
| 45 |
- Assert the state of the PostgreSQL Flexible database. Use C(present) to create or update a database and C(absent) to delete it. |
| 46 |
default: present |
| 47 |
type: str |
| 48 |
choices: |
| 49 |
- absent |
| 50 |
- present |
| 51 |
|
| 52 |
extends_documentation_fragment: |
| 53 |
- azure.azcollection.azure |
| 54 |
|
| 55 |
author: |
| 56 |
- xuzhang3 (@xuzhang3) |
| 57 |
- Fred-sun (@Fred-sun) |
| 58 |
|
| 59 |
''' |
| 60 |
|
| 61 |
EXAMPLES = ''' |
| 62 |
- name: Create (or update) PostgreSQL Flexible Database |
| 63 |
azure_rm_postgresqlflexibledatabase: |
| 64 |
resource_group: myResourceGroup |
| 65 |
server_name: testserver |
| 66 |
name: db1 |
| 67 |
charset: UTF8 |
| 68 |
collation: en_US.utf8 |
| 69 |
|
| 70 |
- name: Delete PostgreSQL Flexible Database |
| 71 |
azure_rm_postgresqlflexibledatabase: |
| 72 |
resource_group: myResourceGroup |
| 73 |
server_name: testserver |
| 74 |
name: db1 |
| 75 |
''' |
| 76 |
|
| 77 |
RETURN = ''' |
| 78 |
database: |
| 79 |
description: |
| 80 |
- A list of dictionaries containing facts for PostgreSQL Flexible Database. |
| 81 |
returned: always |
| 82 |
type: complex |
| 83 |
contains: |
| 84 |
id: |
| 85 |
description: |
| 86 |
- Resource ID of the postgresql flexible database. |
| 87 |
returned: always |
| 88 |
type: str |
| 89 |
sample: "/subscriptions/xxx-xxx/resourceGroups/testRG/providers/Microsoft.DBforPostgreSQL/flexibleServers/postfle9/databases/freddatabase" |
| 90 |
name: |
| 91 |
description: |
| 92 |
- Resource name. |
| 93 |
returned: always |
| 94 |
type: str |
| 95 |
sample: freddatabase |
| 96 |
charset: |
| 97 |
description: |
| 98 |
- The charset of the database. |
| 99 |
returned: always |
| 100 |
type: str |
| 101 |
sample: UTF-8 |
| 102 |
collation: |
| 103 |
description: |
| 104 |
- The collation of the database. |
| 105 |
returned: always |
| 106 |
type: str |
| 107 |
sample: en_US.utf8 |
| 108 |
type: |
| 109 |
description: |
| 110 |
- The type of the resource. |
| 111 |
returned: always |
| 112 |
type: str |
| 113 |
sample: Microsoft.DBforPostgreSQL/flexibleServers/databases |
| 114 |
''' |
| 115 |
|
| 116 |
|
| 117 |
try: |
| 118 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 119 |
from azure.core.exceptions import ResourceNotFoundError |
| 120 |
from azure.core.polling import LROPoller |
| 121 |
except ImportError: |
| 122 |
|
| 123 |
pass |
| 124 |
|
| 125 |
|
| 126 |
class AzureRMPostgreSqlFlexibleDatabases(AzureRMModuleBase): |
| 127 |
"""Configuration class for an Azure RM PostgreSQL Flexible Database resource""" |
| 128 |
|
| 129 |
def __init__(self): |
| 130 |
self.module_arg_spec = dict( |
| 131 |
resource_group=dict( |
| 132 |
type='str', |
| 133 |
required=True |
| 134 |
), |
| 135 |
server_name=dict( |
| 136 |
type='str', |
| 137 |
required=True |
| 138 |
), |
| 139 |
name=dict( |
| 140 |
type='str', |
| 141 |
required=True |
| 142 |
), |
| 143 |
charset=dict( |
| 144 |
type='str' |
| 145 |
), |
| 146 |
collation=dict( |
| 147 |
type='str' |
| 148 |
), |
| 149 |
state=dict( |
| 150 |
type='str', |
| 151 |
default='present', |
| 152 |
choices=['present', 'absent'] |
| 153 |
) |
| 154 |
) |
| 155 |
|
| 156 |
self.resource_group = None |
| 157 |
self.server_name = None |
| 158 |
self.name = None |
| 159 |
self.parameters = dict() |
| 160 |
|
| 161 |
self.results = dict(changed=False) |
| 162 |
self.state = None |
| 163 |
|
| 164 |
super(AzureRMPostgreSqlFlexibleDatabases, self).__init__(derived_arg_spec=self.module_arg_spec, |
| 165 |
supports_check_mode=True, |
| 166 |
supports_tags=False) |
| 167 |
|
| 168 |
def exec_module(self, **kwargs): |
| 169 |
"""Main module execution method""" |
| 170 |
|
| 171 |
for key in list(self.module_arg_spec.keys()): |
| 172 |
if hasattr(self, key): |
| 173 |
setattr(self, key, kwargs[key]) |
| 174 |
elif kwargs[key] is not None: |
| 175 |
if key == "charset": |
| 176 |
self.parameters["charset"] = kwargs[key] |
| 177 |
elif key == "collation": |
| 178 |
self.parameters["collation"] = kwargs[key] |
| 179 |
|
| 180 |
old_response = None |
| 181 |
response = None |
| 182 |
changed = False |
| 183 |
|
| 184 |
old_response = self.get_postgresqlflexibledatabase() |
| 185 |
|
| 186 |
if not old_response: |
| 187 |
self.log("PostgreSQL Flexible Database instance doesn't exist") |
| 188 |
if self.state == 'absent': |
| 189 |
self.log("Old instance didn't exist") |
| 190 |
else: |
| 191 |
changed = True |
| 192 |
if not self.check_mode: |
| 193 |
response = self.create_update_postgresqlflexibledatabase(self.parameters) |
| 194 |
else: |
| 195 |
self.log("PostgreSQL Flexible Database instance already exists") |
| 196 |
if self.state == 'absent': |
| 197 |
changed = True |
| 198 |
if not self.check_mode: |
| 199 |
response = self.delete_postgresqlflexibledatabase() |
| 200 |
else: |
| 201 |
if (self.parameters.get('charset') is not None and self.parameters['charset'] != old_response['charset']) or\ |
| 202 |
(self.parameters.get('collation') is not None and self.parameters['collation'] != old_response['collation']): |
| 203 |
changed = True |
| 204 |
if not self.check_mode: |
| 205 |
self.fail("The Post Gresql Flexible database not support to update") |
| 206 |
else: |
| 207 |
response = old_response |
| 208 |
|
| 209 |
self.results['database'] = response |
| 210 |
self.results['changed'] = changed |
| 211 |
return self.results |
| 212 |
|
| 213 |
def create_update_postgresqlflexibledatabase(self, body): |
| 214 |
''' |
| 215 |
Creates or updates PostgreSQL Flexible Database with the specified configuration. |
| 216 |
|
| 217 |
:return: deserialized PostgreSQL Flexible Database instance state dictionary |
| 218 |
''' |
| 219 |
self.log("Creating / Updating the PostgreSQL Flexible Database instance {0}".format(self.name)) |
| 220 |
|
| 221 |
try: |
| 222 |
response = self.postgresql_flexible_client.databases.begin_create(resource_group_name=self.resource_group, |
| 223 |
server_name=self.server_name, |
| 224 |
database_name=self.name, |
| 225 |
parameters=body) |
| 226 |
if isinstance(response, LROPoller): |
| 227 |
response = self.get_poller_result(response) |
| 228 |
|
| 229 |
except Exception as exc: |
| 230 |
self.log('Error attempting to create the PostgreSQL Flexible Database instance.') |
| 231 |
self.fail("Error creating the PostgreSQL Flexible Database instance: {0}".format(str(exc))) |
| 232 |
return self.format_item(response) |
| 233 |
|
| 234 |
def delete_postgresqlflexibledatabase(self): |
| 235 |
''' |
| 236 |
Deletes specified PostgreSQL Flexible Database instance in the specified subscription and resource group. |
| 237 |
|
| 238 |
:return: True |
| 239 |
''' |
| 240 |
self.log("Deleting the PostgreSQL Flexible Database instance {0}".format(self.name)) |
| 241 |
try: |
| 242 |
self.postgresql_flexible_client.databases.begin_delete(resource_group_name=self.resource_group, |
| 243 |
server_name=self.server_name, |
| 244 |
database_name=self.name) |
| 245 |
except Exception as ec: |
| 246 |
self.log('Error attempting to delete the PostgreSQL Flexible Database instance.') |
| 247 |
self.fail("Error deleting the PostgreSQL Flexible Database instance: {0}".format(str(ec))) |
| 248 |
|
| 249 |
def get_postgresqlflexibledatabase(self): |
| 250 |
''' |
| 251 |
Gets the properties of the specified PostgreSQL Flexible Database. |
| 252 |
|
| 253 |
:return: deserialized PostgreSQL Flexible Database instance state dictionary |
| 254 |
''' |
| 255 |
self.log("Checking if the PostgreSQL Flexible Database instance {0} is present".format(self.name)) |
| 256 |
found = False |
| 257 |
try: |
| 258 |
response = self.postgresql_flexible_client.databases.get(resource_group_name=self.resource_group, |
| 259 |
server_name=self.server_name, |
| 260 |
database_name=self.name) |
| 261 |
found = True |
| 262 |
self.log("Response : {0}".format(response)) |
| 263 |
self.log("PostgreSQL Flexible Database instance : {0} found".format(response.name)) |
| 264 |
except ResourceNotFoundError as e: |
| 265 |
self.log('Did not find the PostgreSQL Flexible Database instance. Exception as {0}'.format(e)) |
| 266 |
if found is True: |
| 267 |
return self.format_item(response) |
| 268 |
|
| 269 |
return None |
| 270 |
|
| 271 |
def format_item(self, item): |
| 272 |
result = dict( |
| 273 |
id=item.id, |
| 274 |
name=item.name, |
| 275 |
type=item.type, |
| 276 |
charset=item.charset, |
| 277 |
collation=item.collation |
| 278 |
) |
| 279 |
return result |
| 280 |
|
| 281 |
|
| 282 |
def main(): |
| 283 |
"""Main execution""" |
| 284 |
AzureRMPostgreSqlFlexibleDatabases() |
| 285 |
|
| 286 |
|
| 287 |
if __name__ == '__main__': |
| 288 |
main() |