| 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_postgresqlflexibleconfiguration |
| 14 |
version_added: "3.18.0" |
| 15 |
short_description: Manage Azure PostgreSQL Flexible Configuration |
| 16 |
description: |
| 17 |
- Update or reset Azure PostgreSQL Flexible Server Configuration setting. |
| 18 |
|
| 19 |
options: |
| 20 |
resource_group: |
| 21 |
description: |
| 22 |
- The name of the resource group that contains the resource. |
| 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 |
- Setting name. |
| 33 |
required: true |
| 34 |
type: str |
| 35 |
value: |
| 36 |
description: |
| 37 |
- Setting value. |
| 38 |
type: str |
| 39 |
state: |
| 40 |
description: |
| 41 |
- Assert the state of the PostgreSQL Flexible setting. |
| 42 |
- Use C(present) to update setting, or C(absent) to reset to default value. |
| 43 |
default: present |
| 44 |
type: str |
| 45 |
choices: |
| 46 |
- absent |
| 47 |
- present |
| 48 |
|
| 49 |
extends_documentation_fragment: |
| 50 |
- azure.azcollection.azure |
| 51 |
|
| 52 |
author: |
| 53 |
- zunyangc (@zunyangc) |
| 54 |
|
| 55 |
''' |
| 56 |
|
| 57 |
EXAMPLES = ''' |
| 58 |
- name: Update PostgreSQL Flexible Server setting |
| 59 |
azure_rm_postgresqlflexibleconfiguration: |
| 60 |
resource_group: myResourceGroup |
| 61 |
server_name: myServer |
| 62 |
name: deadlock_timeout |
| 63 |
value: 2000 |
| 64 |
|
| 65 |
- name: Reset PostgreSQL Flexible Server setting to default |
| 66 |
azure_rm_postgresqlflexibleconfiguration: |
| 67 |
resource_group: myResourceGroup |
| 68 |
server_name: myServer |
| 69 |
name: deadlock_timeout |
| 70 |
state: absent |
| 71 |
''' |
| 72 |
|
| 73 |
RETURN = ''' |
| 74 |
id: |
| 75 |
description: |
| 76 |
- Resource ID. |
| 77 |
returned: always |
| 78 |
type: str |
| 79 |
sample: "/subscriptions/xxx-xxx/resourceGroups/myResourceGroup/providers/Microsoft.DBforPostgreSQL/flexibleServers/myServer/configurations/deadlock_timeout" |
| 80 |
''' |
| 81 |
|
| 82 |
try: |
| 83 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 84 |
from azure.mgmt.rdbms.postgresql_flexibleservers.models import Configuration |
| 85 |
from azure.core.exceptions import ResourceNotFoundError |
| 86 |
from azure.core.polling import LROPoller |
| 87 |
except ImportError: |
| 88 |
|
| 89 |
pass |
| 90 |
|
| 91 |
|
| 92 |
class Actions: |
| 93 |
NoAction, Create, Update, Delete = range(4) |
| 94 |
|
| 95 |
|
| 96 |
class AzureRMPostgreSqlFlexibleConfigurations(AzureRMModuleBase): |
| 97 |
|
| 98 |
def __init__(self): |
| 99 |
self.module_arg_spec = dict( |
| 100 |
resource_group=dict( |
| 101 |
type='str', |
| 102 |
required=True |
| 103 |
), |
| 104 |
server_name=dict( |
| 105 |
type='str', |
| 106 |
required=True |
| 107 |
), |
| 108 |
name=dict( |
| 109 |
type='str', |
| 110 |
required=True |
| 111 |
), |
| 112 |
value=dict( |
| 113 |
type='str' |
| 114 |
), |
| 115 |
state=dict( |
| 116 |
type='str', |
| 117 |
default='present', |
| 118 |
choices=['present', 'absent'] |
| 119 |
) |
| 120 |
) |
| 121 |
|
| 122 |
self.resource_group = None |
| 123 |
self.server_name = None |
| 124 |
self.name = None |
| 125 |
self.value = None |
| 126 |
|
| 127 |
self.results = dict(changed=False) |
| 128 |
self.state = None |
| 129 |
self.to_do = Actions.NoAction |
| 130 |
|
| 131 |
super(AzureRMPostgreSqlFlexibleConfigurations, self).__init__(derived_arg_spec=self.module_arg_spec, |
| 132 |
supports_check_mode=True, |
| 133 |
supports_tags=False) |
| 134 |
|
| 135 |
def exec_module(self, **kwargs): |
| 136 |
for key in list(self.module_arg_spec.keys()): |
| 137 |
if hasattr(self, key): |
| 138 |
setattr(self, key, kwargs[key]) |
| 139 |
|
| 140 |
old_response = None |
| 141 |
response = None |
| 142 |
|
| 143 |
old_response = self.get_configuration() |
| 144 |
|
| 145 |
if not old_response: |
| 146 |
self.log("Configuration instance doesn't exist") |
| 147 |
if self.state == 'absent': |
| 148 |
self.log("Old instance didn't exist") |
| 149 |
else: |
| 150 |
self.to_do = Actions.Create |
| 151 |
else: |
| 152 |
self.log("Configuration instance already exists") |
| 153 |
if self.state == 'absent': |
| 154 |
if str(old_response.get('value')) != str(old_response.get('default_value')): |
| 155 |
self.to_do = Actions.Delete |
| 156 |
elif self.state == 'present': |
| 157 |
self.log("Need to check if Configuration instance has to be updated") |
| 158 |
if self.value is not None and str(self.value) != str(old_response.get('value')): |
| 159 |
self.to_do = Actions.Update |
| 160 |
|
| 161 |
if (self.to_do == Actions.Create) or (self.to_do == Actions.Update): |
| 162 |
self.log("Need to Create / Update the Configuration instance") |
| 163 |
|
| 164 |
if self.check_mode: |
| 165 |
self.results['changed'] = True |
| 166 |
return self.results |
| 167 |
|
| 168 |
response = self.create_update_configuration() |
| 169 |
|
| 170 |
self.results['changed'] = True |
| 171 |
self.log("Creation / Update done") |
| 172 |
elif self.to_do == Actions.Delete: |
| 173 |
self.log("Configuration instance deleted") |
| 174 |
self.results['changed'] = True |
| 175 |
|
| 176 |
if self.check_mode: |
| 177 |
return self.results |
| 178 |
|
| 179 |
self.delete_configuration(old_response.get('default_value')) |
| 180 |
else: |
| 181 |
self.log("Configuration instance unchanged") |
| 182 |
self.results['changed'] = False |
| 183 |
response = old_response |
| 184 |
|
| 185 |
if response: |
| 186 |
self.results["id"] = response["id"] |
| 187 |
|
| 188 |
return self.results |
| 189 |
|
| 190 |
def create_update_configuration(self): |
| 191 |
''' |
| 192 |
Apply the requested value to the PostgreSQL Flexible Server configuration |
| 193 |
setting using ``source='user-override'`` and return the resulting resource as a dict. |
| 194 |
''' |
| 195 |
self.log("Creating / Updating the Configuration instance {0}".format(self.name)) |
| 196 |
|
| 197 |
try: |
| 198 |
response = self.postgresql_flexible_client.configurations.begin_update(resource_group_name=self.resource_group, |
| 199 |
server_name=self.server_name, |
| 200 |
configuration_name=self.name, |
| 201 |
parameters=Configuration( |
| 202 |
value=self.value, |
| 203 |
source='user-override' |
| 204 |
)) |
| 205 |
if isinstance(response, LROPoller): |
| 206 |
response = self.get_poller_result(response) |
| 207 |
|
| 208 |
except Exception as exc: |
| 209 |
self.log('Error attempting to create the Configuration instance.') |
| 210 |
self.fail("Error creating the Configuration instance: {0}".format(str(exc))) |
| 211 |
return response.as_dict() |
| 212 |
|
| 213 |
def delete_configuration(self, default_value): |
| 214 |
''' |
| 215 |
Reset the PostgreSQL Flexible Server configuration setting back to its |
| 216 |
``default_value``. |
| 217 |
''' |
| 218 |
self.log("Resetting the Configuration instance {0} to default value {1}".format(self.name, default_value)) |
| 219 |
try: |
| 220 |
response = self.postgresql_flexible_client.configurations.begin_update(resource_group_name=self.resource_group, |
| 221 |
server_name=self.server_name, |
| 222 |
configuration_name=self.name, |
| 223 |
parameters=Configuration( |
| 224 |
value=default_value, |
| 225 |
source='user-override' |
| 226 |
)) |
| 227 |
if isinstance(response, LROPoller): |
| 228 |
self.get_poller_result(response) |
| 229 |
except Exception as e: |
| 230 |
self.log('Error attempting to reset the Configuration instance.') |
| 231 |
self.fail("Error resetting the Configuration instance: {0}".format(str(e))) |
| 232 |
|
| 233 |
return True |
| 234 |
|
| 235 |
def get_configuration(self): |
| 236 |
''' |
| 237 |
Fetch the current configuration setting from Azure. |
| 238 |
|
| 239 |
:return: dict representation of the configuration if found, otherwise ``False``. |
| 240 |
''' |
| 241 |
self.log("Checking if the Configuration instance {0} is present".format(self.name)) |
| 242 |
found = False |
| 243 |
try: |
| 244 |
response = self.postgresql_flexible_client.configurations.get(resource_group_name=self.resource_group, |
| 245 |
server_name=self.server_name, |
| 246 |
configuration_name=self.name) |
| 247 |
found = True |
| 248 |
self.log("Response : {0}".format(response)) |
| 249 |
self.log("Configuration instance : {0} found".format(response.name)) |
| 250 |
except ResourceNotFoundError as e: |
| 251 |
self.log('Did not find the Configuration instance. Exception: {0}'.format(e)) |
| 252 |
if found is True: |
| 253 |
return response.as_dict() |
| 254 |
|
| 255 |
return False |
| 256 |
|
| 257 |
|
| 258 |
def main(): |
| 259 |
AzureRMPostgreSqlFlexibleConfigurations() |
| 260 |
|
| 261 |
|
| 262 |
if __name__ == '__main__': |
| 263 |
main() |