| 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_mysqlflexibleconfiguration |
| 14 |
version_added: "2.7.0" |
| 15 |
short_description: Manage Configuration instance |
| 16 |
description: |
| 17 |
- Update or delete instance of Flexible Configuration. |
| 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 |
- The name of the server configuration. |
| 33 |
required: True |
| 34 |
type: str |
| 35 |
value: |
| 36 |
description: |
| 37 |
- The value server configurations. |
| 38 |
type: str |
| 39 |
source: |
| 40 |
description: |
| 41 |
- Source of the configuration. |
| 42 |
type: str |
| 43 |
choices: |
| 44 |
- system-default |
| 45 |
- user-override |
| 46 |
extends_documentation_fragment: |
| 47 |
- azure.azcollection.azure |
| 48 |
|
| 49 |
author: |
| 50 |
- xuzhang3 (@xuzhang3) |
| 51 |
- Fred-sun (@Fred-sun) |
| 52 |
|
| 53 |
''' |
| 54 |
|
| 55 |
EXAMPLES = ''' |
| 56 |
- name: Update SQL Server setting |
| 57 |
azure_rm_mysqlflexibleconfiguration: |
| 58 |
resource_group: myResourceGroup |
| 59 |
server_name: myServer |
| 60 |
name: event_scheduler |
| 61 |
value: |
| 62 |
- name: testvalue |
| 63 |
value: "ON" |
| 64 |
source: system-default |
| 65 |
''' |
| 66 |
|
| 67 |
RETURN = ''' |
| 68 |
id: |
| 69 |
description: |
| 70 |
- Resource ID. |
| 71 |
returned: always |
| 72 |
type: str |
| 73 |
sample: "/subscriptions/xx-xx/resourceGroups/myRG/providers/Microsoft.DBforMySQL/flexibleservers/myServer/configurations/event_scheduler" |
| 74 |
server_name: |
| 75 |
description: |
| 76 |
- The MySQL flexible server name. |
| 77 |
type: str |
| 78 |
returned: always |
| 79 |
sample: testmysqlserver |
| 80 |
name: |
| 81 |
description: |
| 82 |
- Setting name. |
| 83 |
returned: always |
| 84 |
type: str |
| 85 |
sample: deadlock_timeout |
| 86 |
value: |
| 87 |
description: |
| 88 |
- Setting value. |
| 89 |
returned: always |
| 90 |
type: raw |
| 91 |
sample: 1000 |
| 92 |
source: |
| 93 |
description: |
| 94 |
- Source of the configuration. |
| 95 |
returned: always |
| 96 |
type: str |
| 97 |
sample: system-default |
| 98 |
''' |
| 99 |
|
| 100 |
try: |
| 101 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 102 |
from azure.core.exceptions import ResourceNotFoundError |
| 103 |
from azure.core.polling import LROPoller |
| 104 |
except ImportError: |
| 105 |
|
| 106 |
pass |
| 107 |
|
| 108 |
|
| 109 |
class Actions: |
| 110 |
NoAction, Update = range(2) |
| 111 |
|
| 112 |
|
| 113 |
class AzureRMMySqlFlexibleConfiguration(AzureRMModuleBase): |
| 114 |
|
| 115 |
def __init__(self): |
| 116 |
self.module_arg_spec = dict( |
| 117 |
resource_group=dict( |
| 118 |
type='str', |
| 119 |
required=True |
| 120 |
), |
| 121 |
server_name=dict( |
| 122 |
type='str', |
| 123 |
required=True |
| 124 |
), |
| 125 |
name=dict( |
| 126 |
type='str', |
| 127 |
required=True |
| 128 |
), |
| 129 |
value=dict(type='str'), |
| 130 |
source=dict(type='str', choices=['system-default', 'user-override']), |
| 131 |
) |
| 132 |
|
| 133 |
self.resource_group = None |
| 134 |
self.server_name = None |
| 135 |
self.name = None |
| 136 |
self.value = None |
| 137 |
self.source = None |
| 138 |
|
| 139 |
self.results = dict(changed=False) |
| 140 |
self.to_do = Actions.NoAction |
| 141 |
|
| 142 |
super(AzureRMMySqlFlexibleConfiguration, self).__init__(derived_arg_spec=self.module_arg_spec, |
| 143 |
supports_check_mode=True, |
| 144 |
supports_tags=False) |
| 145 |
|
| 146 |
def exec_module(self, **kwargs): |
| 147 |
|
| 148 |
for key in list(self.module_arg_spec.keys()): |
| 149 |
if hasattr(self, key): |
| 150 |
setattr(self, key, kwargs[key]) |
| 151 |
|
| 152 |
old_response = None |
| 153 |
response = None |
| 154 |
|
| 155 |
old_response = self.get_configuration() |
| 156 |
|
| 157 |
if not old_response: |
| 158 |
self.fail("The MySQL configuration not exist, We can't make any changes") |
| 159 |
else: |
| 160 |
self.log("Need to check if Configuration instance has to be deleted or may be updated") |
| 161 |
if self.value is not None and self.value != old_response['value'] or self.source is not None and self.source != old_response['source']: |
| 162 |
self.to_do = Actions.Update |
| 163 |
|
| 164 |
if self.to_do == Actions.Update: |
| 165 |
self.log("Need to Update the Configuration instance") |
| 166 |
if not self.check_mode: |
| 167 |
response = self.update_configuration() |
| 168 |
self.results['changed'] = True |
| 169 |
self.log("Update done") |
| 170 |
response = self.get_configuration() |
| 171 |
|
| 172 |
if response: |
| 173 |
self.results["id"] = response["id"] |
| 174 |
self.results['resource_group'] = self.resource_group |
| 175 |
self.results['server_name'] = self.server_name |
| 176 |
self.results['name'] = self.name |
| 177 |
self.results['source'] = response['source'] |
| 178 |
self.results['value'] = response['value'] |
| 179 |
|
| 180 |
return self.results |
| 181 |
|
| 182 |
def update_configuration(self): |
| 183 |
self.log("Updating the Configuration instance {0}".format(self.name)) |
| 184 |
|
| 185 |
try: |
| 186 |
response = self.mysql_flexible_client.configurations.begin_update(resource_group_name=self.resource_group, |
| 187 |
server_name=self.server_name, |
| 188 |
configuration_name=self.name, |
| 189 |
parameters={'value': self.value, 'source': self.source}) |
| 190 |
if isinstance(response, LROPoller): |
| 191 |
response = self.get_poller_result(response) |
| 192 |
|
| 193 |
except Exception as exc: |
| 194 |
self.log('Error attempting to create the Configuration instance.') |
| 195 |
self.fail("Error creating the Configuration instance: {0}".format(str(exc))) |
| 196 |
return response.as_dict() |
| 197 |
|
| 198 |
def get_configuration(self): |
| 199 |
self.log("Checking if the Configuration instance {0} is present".format(self.name)) |
| 200 |
found = False |
| 201 |
try: |
| 202 |
response = self.mysql_flexible_client.configurations.get(resource_group_name=self.resource_group, |
| 203 |
server_name=self.server_name, |
| 204 |
configuration_name=self.name) |
| 205 |
found = True |
| 206 |
self.log("Response : {0}".format(response)) |
| 207 |
self.log("Configuration instance : {0} found".format(response.name)) |
| 208 |
except ResourceNotFoundError as e: |
| 209 |
self.log('Did not find the Configuration instance.') |
| 210 |
if found is True: |
| 211 |
return response.as_dict() |
| 212 |
|
| 213 |
return False |
| 214 |
|
| 215 |
|
| 216 |
def main(): |
| 217 |
"""Main execution""" |
| 218 |
AzureRMMySqlFlexibleConfiguration() |
| 219 |
|
| 220 |
|
| 221 |
if __name__ == '__main__': |
| 222 |
main() |