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