Debian Codegraph @

ansible_collections/azure/azcollection/plugins/modules/azure_rm_postgresqlconfiguration.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_postgresqlconfiguration
14 version_added: "0.1.2"
15 short_description: Manage Azure PostgreSQL Configuration
16 description:
17 - Update or reset Azure PostgreSQL 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 setting. 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 PostgreSQL Server setting
58 azure_rm_postgresqlconfiguration:
59 resource_group: myResourceGroup
60 server_name: myServer
61 name: deadlock_timeout
62 value: 2000
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.DBforPostgreSQL/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.mgmt.rdbms.postgresql.models import Configuration
78 from azure.core.exceptions import ResourceNotFoundError
79 from azure.core.polling import LROPoller
80 except ImportError:
81 # This is handled in azure_rm_common
82 pass
83
84
85 class Actions:
86 NoAction, Create, Update, Delete = range(4)
87
88
89 class AzureRMPostgreSqlConfigurations(AzureRMModuleBase):
90
91 def __init__(self):
92 self.module_arg_spec = dict(
93 resource_group=dict(
94 type='str',
95 required=True
96 ),
97 server_name=dict(
98 type='str',
99 required=True
100 ),
101 name=dict(
102 type='str',
103 required=True
104 ),
105 value=dict(
106 type='str'
107 ),
108 state=dict(
109 type='str',
110 default='present',
111 choices=['present', 'absent']
112 )
113 )
114
115 self.resource_group = None
116 self.server_name = None
117 self.name = None
118 self.value = None
119
120 self.results = dict(changed=False)
121 self.state = None
122 self.to_do = Actions.NoAction
123
124 super(AzureRMPostgreSqlConfigurations, self).__init__(derived_arg_spec=self.module_arg_spec,
125 supports_check_mode=True,
126 supports_tags=False)
127
128 def exec_module(self, **kwargs):
129
130 for key in list(self.module_arg_spec.keys()):
131 if hasattr(self, key):
132 setattr(self, key, kwargs[key])
133
134 old_response = None
135 response = None
136
137 old_response = self.get_configuration()
138
139 if not old_response:
140 self.log("Configuration instance doesn't exist")
141 if self.state == 'absent':
142 self.log("Old instance didn't exist")
143 else:
144 self.to_do = Actions.Create
145 else:
146 self.log("Configuration instance already exists")
147 if self.state == 'absent' and old_response['source'] == 'user-override':
148 self.to_do = Actions.Delete
149 elif self.state == 'present':
150 self.log("Need to check if Configuration instance has to be deleted or may be updated")
151 if self.value != old_response.get('value'):
152 self.to_do = Actions.Update
153
154 if (self.to_do == Actions.Create) or (self.to_do == Actions.Update):
155 self.log("Need to Create / Update the Configuration instance")
156
157 if self.check_mode:
158 self.results['changed'] = True
159 return self.results
160
161 response = self.create_update_configuration()
162
163 self.results['changed'] = True
164 self.log("Creation / Update done")
165 elif self.to_do == Actions.Delete:
166 self.log("Configuration instance deleted")
167 self.results['changed'] = True
168
169 if self.check_mode:
170 return self.results
171
172 self.delete_configuration()
173 else:
174 self.log("Configuration instance unchanged")
175 self.results['changed'] = False
176 response = old_response
177
178 if response:
179 self.results["id"] = response["id"]
180
181 return self.results
182
183 def create_update_configuration(self):
184 self.log("Creating / Updating the Configuration instance {0}".format(self.name))
185
186 try:
187 response = self.postgresql_client.configurations.begin_create_or_update(resource_group_name=self.resource_group,
188 server_name=self.server_name,
189 configuration_name=self.name,
190 parameters=Configuration(
191 value=self.value,
192 source='user-override'
193 ))
194 if isinstance(response, LROPoller):
195 response = self.get_poller_result(response)
196
197 except Exception as exc:
198 self.log('Error attempting to create the Configuration instance.')
199 self.fail("Error creating the Configuration instance: {0}".format(str(exc)))
200 return response.as_dict()
201
202 def delete_configuration(self):
203 self.log("Deleting the Configuration instance {0}".format(self.name))
204 try:
205 response = self.postgresql_client.configurations.begin_create_or_update(resource_group_name=self.resource_group,
206 server_name=self.server_name,
207 configuration_name=self.name,
208 parameters=Configuration(source='system-default'))
209 except Exception as e:
210 self.log('Error attempting to delete the Configuration instance.')
211 self.fail("Error deleting the Configuration instance: {0}".format(str(e)))
212
213 return True
214
215 def get_configuration(self):
216 self.log("Checking if the Configuration instance {0} is present".format(self.name))
217 found = False
218 try:
219 response = self.postgresql_client.configurations.get(resource_group_name=self.resource_group,
220 server_name=self.server_name,
221 configuration_name=self.name)
222 found = True
223 self.log("Response : {0}".format(response))
224 self.log("Configuration instance : {0} found".format(response.name))
225 except ResourceNotFoundError as e:
226 self.log('Did not find the Configuration instance.')
227 if found is True:
228 return response.as_dict()
229
230 return False
231
232
233 def main():
234 """Main execution"""
235 AzureRMPostgreSqlConfigurations()
236
237
238 if __name__ == '__main__':
239 main()