Debian Codegraph @

ansible_collections/azure/azcollection/plugins/modules/azure_rm_mysqlconfiguration.py

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2019 Zim Kalinowski, (@zikalino)
4 #
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8 __metaclass__ = type
9
10
11 DOCUMENTATION = '''
12 ---
13 module: azure_rm_mysqlconfiguration
14 version_added: "0.1.2"
15 short_description: Manage Configuration instance
16 description:
17 - Create, update and delete instance of 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 - Value of the configuration.
38 type: str
39 state:
40 description:
41 - Assert the state of the MySQL configuration. Use C(present) to update setting, or C(absent) to reset to default value.
42 default: present
43 type: str
44 choices:
45 - absent
46 - present
47
48 extends_documentation_fragment:
49 - azure.azcollection.azure
50
51 author:
52 - Zim Kalinowski (@zikalino)
53
54 '''
55
56 EXAMPLES = '''
57 - name: Update SQL Server setting
58 azure_rm_mysqlconfiguration:
59 resource_group: myResourceGroup
60 server_name: myServer
61 name: event_scheduler
62 value: "ON"
63 '''
64
65 RETURN = '''
66 id:
67 description:
68 - Resource ID.
69 returned: always
70 type: str
71 sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.DBforMySQL/servers/myServer/confi
72 gurations/event_scheduler"
73 '''
74
75 try:
76 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
77 from azure.core.exceptions import ResourceNotFoundError
78 from azure.core.polling import LROPoller
79 except ImportError:
80 # This is handled in azure_rm_common
81 pass
82
83
84 class Actions:
85 NoAction, Create, Update, Delete = range(4)
86
87
88 class AzureRMMySqlConfiguration(AzureRMModuleBase):
89
90 def __init__(self):
91 self.module_arg_spec = dict(
92 resource_group=dict(
93 type='str',
94 required=True
95 ),
96 server_name=dict(
97 type='str',
98 required=True
99 ),
100 name=dict(
101 type='str',
102 required=True
103 ),
104 value=dict(
105 type='str'
106 ),
107 state=dict(
108 type='str',
109 default='present',
110 choices=['present', 'absent']
111 )
112 )
113
114 self.resource_group = None
115 self.server_name = None
116 self.name = None
117 self.value = None
118
119 self.results = dict(changed=False)
120 self.state = None
121 self.to_do = Actions.NoAction
122
123 super(AzureRMMySqlConfiguration, self).__init__(derived_arg_spec=self.module_arg_spec,
124 supports_check_mode=True,
125 supports_tags=False)
126
127 def exec_module(self, **kwargs):
128
129 for key in list(self.module_arg_spec.keys()):
130 if hasattr(self, key):
131 setattr(self, key, kwargs[key])
132
133 old_response = None
134 response = None
135
136 old_response = self.get_configuration()
137
138 if not old_response:
139 self.log("Configuration instance doesn't exist")
140 if self.state == 'absent':
141 self.log("Old instance didn't exist")
142 else:
143 self.to_do = Actions.Create
144 else:
145 self.log("Configuration instance already exists")
146 if self.state == 'absent' and old_response['source'] == 'user-override':
147 self.to_do = Actions.Delete
148 elif self.state == 'present':
149 self.log("Need to check if Configuration instance has to be deleted or may be updated")
150 if self.value != old_response.get('value'):
151 self.to_do = Actions.Update
152
153 if (self.to_do == Actions.Create) or (self.to_do == Actions.Update):
154 self.log("Need to Create / Update the Configuration instance")
155
156 if self.check_mode:
157 self.results['changed'] = True
158 return self.results
159
160 response = self.create_update_configuration()
161
162 self.results['changed'] = True
163 self.log("Creation / Update done")
164 elif self.to_do == Actions.Delete:
165 self.log("Configuration instance deleted")
166 self.results['changed'] = True
167
168 if self.check_mode:
169 return self.results
170
171 self.delete_configuration()
172 else:
173 self.log("Configuration instance unchanged")
174 self.results['changed'] = False
175 response = old_response
176
177 if response:
178 self.results["id"] = response["id"]
179
180 return self.results
181
182 def create_update_configuration(self):
183 self.log("Creating / Updating the Configuration instance {0}".format(self.name))
184
185 try:
186 response = self.mysql_client.configurations.begin_create_or_update(resource_group_name=self.resource_group,
187 server_name=self.server_name,
188 configuration_name=self.name,
189 parameters={'value': self.value, 'source': 'user-override'})
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 delete_configuration(self):
199 self.log("Deleting the Configuration instance {0}".format(self.name))
200 try:
201 response = self.mysql_client.configurations.begin_create_or_update(resource_group_name=self.resource_group,
202 server_name=self.server_name,
203 configuration_name=self.name,
204 parameters={'source': 'system-default'})
205 except Exception as e:
206 self.log('Error attempting to delete the Configuration instance.')
207 self.fail("Error deleting the Configuration instance: {0}".format(str(e)))
208
209 return True
210
211 def get_configuration(self):
212 self.log("Checking if the Configuration instance {0} is present".format(self.name))
213 found = False
214 try:
215 response = self.mysql_client.configurations.get(resource_group_name=self.resource_group,
216 server_name=self.server_name,
217 configuration_name=self.name)
218 found = True
219 self.log("Response : {0}".format(response))
220 self.log("Configuration instance : {0} found".format(response.name))
221 except ResourceNotFoundError as e:
222 self.log('Did not find the Configuration instance.')
223 if found is True:
224 return response.as_dict()
225
226 return False
227
228
229 def main():
230 """Main execution"""
231 AzureRMMySqlConfiguration()
232
233
234 if __name__ == '__main__':
235 main()