Debian Codegraph @

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

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