Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
4
5 from __future__ import absolute_import, division, print_function
6 __metaclass__ = type
7
8
9 DOCUMENTATION = '''
10 ---
11 module: azure_rm_postgresqlflexiblevirtualendpoint
12 version_added: "3.6.0"
13 short_description: Manage PostgreSQL Flexible virtualendpoint instance
14 description:
15 - Create or delete instance of PostgreSQL Flexible virtualendpoint.
16
17 options:
18 resource_group:
19 description:
20 - The name of the resource group that contains the resource. You can obtain this value from the Azure Resource Manager API or the portal.
21 required: True
22 type: str
23 server_name:
24 description:
25 - The name of the server.
26 required: True
27 type: str
28 virtual_endpoint_name:
29 description:
30 - The name of the post gresql flexible virtual endpoint.
31 required: True
32 type: str
33 members:
34 description:
35 - List of virtual endpoints for a server.
36 - The names are the same with I(server_name).
37 type: list
38 elements: str
39 endpoint_type:
40 description:
41 - The endpoint type for the virtual endpoint.
42 type: str
43 choices:
44 - ReadWrite
45 state:
46 description:
47 - Assert the state of the PostgreSQL Flexible virtualendpoint. Use C(present) to create or update a virtualendpoint and C(absent) to delete it.
48 default: present
49 type: str
50 choices:
51 - absent
52 - present
53
54 extends_documentation_fragment:
55 - azure.azcollection.azure
56
57 author:
58 - magodo (@magodo)
59 - xuzhang3 (@xuzhang3)
60 - Fred-sun (@Fred-sun)
61
62 '''
63
64 EXAMPLES = '''
65 - name: Create (or update) PostgreSQL Flexible virtualendpoint
66 azure_rm_postgresqlflexiblevirtualendpoint:
67 resource_group: myResourceGroup
68 server_name: testserver
69 name: db1
70 charset: UTF8
71 collation: en_US.utf8
72
73 - name: Delete PostgreSQL Flexible virtualendpoint
74 azure_rm_postgresqlflexiblevirtualendpoint:
75 resource_group: myResourceGroup
76 server_name: testserver
77 virtual_endpoint_name: vendpoint01
78 '''
79
80 RETURN = '''
81 virtual_endpoint:
82 description:
83 - A list of dictionaries containing facts for PostgreSQL Flexible virtual endpoint.
84 returned: always
85 type: complex
86 contains:
87 id:
88 description:
89 - Resource ID of the postgresql flexible virtual endpoints.
90 returned: always
91 type: str
92 sample: "/subscriptions/xxx-xxx/resourceGroups/testRG/providers/Microsoft.DBforPostgreSQL/flexibleServers/postsql01/virtualendpoints/vendpoint"
93 virtual_endpoint_name:
94 description:
95 - Resource name.
96 returned: always
97 type: str
98 sample: vendpoint
99 endpoint_type:
100 description:
101 - The endpoint type for the virtual endpoint.
102 returned: always
103 type: str
104 sample: ReadWrite
105 resource_group:
106 description:
107 - The resoure group name.
108 returned: always
109 type: str
110 sample: myResourceGroup
111 members:
112 description:
113 - List of members for a virtual endpoint.
114 returned: always
115 type: list
116 sample: ['postsqlrpfx01']
117 server_name:
118 description:
119 - The Post gresql flexibeserver name.
120 returned: always
121 type: str
122 sample: postsql01
123 virtual_endpoints:
124 description:
125 - List of virtual endpoints for a server.
126 returned: always
127 type: list
128 sample: ["vendpoint.writer.postgres.database.azure.com", "vendpoint.reader.postgres.database.azure.com"]
129 type:
130 description:
131 - The type of the resource.
132 returned: always
133 type: str
134 sample: Microsoft.DBforPostgreSQL/flexibleServers/virtualendpoints
135 '''
136
137
138 try:
139 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
140 from azure.core.exceptions import ResourceNotFoundError
141 from azure.core.polling import LROPoller
142 except ImportError:
143 # This is handled in azure_rm_common
144 pass
145
146
147 class AzureRMPostgreSqlFlexibleVirtualEndpoint(AzureRMModuleBase):
148 """Configuration class for an Azure RM PostgreSQL Flexible virtualendpoint resource"""
149
150 def __init__(self):
151 self.module_arg_spec = dict(
152 resource_group=dict(
153 type='str',
154 required=True
155 ),
156 server_name=dict(
157 type='str',
158 required=True
159 ),
160 virtual_endpoint_name=dict(
161 type='str',
162 required=True
163 ),
164 endpoint_type=dict(
165 type='str',
166 choices=['ReadWrite']
167 ),
168 members=dict(
169 type='list',
170 elements='str',
171 ),
172 state=dict(
173 type='str',
174 default='present',
175 choices=['present', 'absent']
176 )
177 )
178
179 self.resource_group = None
180 self.server_name = None
181 self.virtual_endpoint_name = None
182 self.parameters = dict()
183
184 self.results = dict(changed=False)
185 self.state = None
186
187 super(AzureRMPostgreSqlFlexibleVirtualEndpoint, self).__init__(derived_arg_spec=self.module_arg_spec,
188 supports_check_mode=True,
189 supports_tags=False)
190
191 def exec_module(self, **kwargs):
192 """Main module execution method"""
193
194 for key in list(self.module_arg_spec.keys()):
195 if hasattr(self, key):
196 setattr(self, key, kwargs[key])
197 else:
198 self.parameters[key] = kwargs[key]
199
200 old_response = None
201 response = None
202 changed = False
203
204 old_response = self.get_postgresqlflexiblevirtualendpoint()
205
206 if not old_response:
207 self.log("PostgreSQL Flexible virtualendpoint instance doesn't exist")
208 if self.state == 'absent':
209 self.log("Old instance didn't exist")
210 else:
211 changed = True
212 if not self.check_mode:
213 response = self.create_postgresqlflexiblevirtualendpoint(self.parameters)
214 else:
215 self.log("PostgreSQL Flexible virtualendpoint instance already exists")
216 if self.state == 'absent':
217 changed = True
218 if not self.check_mode:
219 response = self.delete_postgresqlflexiblevirtualendpoint()
220 else:
221 if self.check_mode:
222 changed = True
223 response = old_response
224
225 self.results['virtualendpoint'] = response
226 self.results['changed'] = changed
227 return self.results
228
229 def create_postgresqlflexiblevirtualendpoint(self, body):
230 '''
231 Creates PostgreSQL Flexible virtualendpoint with the specified configuration.
232
233 :return: deserialized PostgreSQL Flexible virtualendpoint instance state dictionary
234 '''
235 self.log("Creating the PostgreSQL Flexible virtualendpoint instance {0}".format(self.virtual_endpoint_name))
236
237 try:
238 response = self.postgresql_flexible_client.virtual_endpoints.begin_create(resource_group_name=self.resource_group,
239 server_name=self.server_name,
240 virtual_endpoint_name=self.virtual_endpoint_name,
241 parameters=body)
242 if isinstance(response, LROPoller):
243 response = self.get_poller_result(response)
244
245 except Exception as exc:
246 self.log('Error attempting to create the PostgreSQL Flexible virtualendpoint instance.')
247 self.fail("Error creating the PostgreSQL Flexible virtualendpoint instance: {0}".format(str(exc)))
248 return self.format_item(response)
249
250 def delete_postgresqlflexiblevirtualendpoint(self):
251 '''
252 Deletes specified PostgreSQL Flexible virtualendpoint instance in the specified subscription and resource group.
253
254 :return: True
255 '''
256 self.log("Deleting the PostgreSQL Flexible virtualendpoint instance {0}".format(self.virtual_endpoint_name))
257 try:
258 self.postgresql_flexible_client.virtual_endpoints.begin_delete(resource_group_name=self.resource_group,
259 server_name=self.server_name,
260 virtual_endpoint_name=self.virtual_endpoint_name)
261 except Exception as ec:
262 self.log('Error attempting to delete the PostgreSQL Flexible virtualendpoint instance.')
263 self.fail("Error deleting the PostgreSQL Flexible virtualendpoint instance: {0}".format(str(ec)))
264
265 def get_postgresqlflexiblevirtualendpoint(self):
266 '''
267 Gets the properties of the specified PostgreSQL Flexible virtualendpoint.
268
269 :return: deserialized PostgreSQL Flexible virtualendpoint instance state dictionary
270 '''
271 self.log("Checking if the PostgreSQL Flexible virtualendpoint instance {0} is present".format(self.virtual_endpoint_name))
272 found = False
273 try:
274 response = self.postgresql_flexible_client.virtual_endpoints.get(resource_group_name=self.resource_group,
275 server_name=self.server_name,
276 virtual_endpoint_name=self.virtual_endpoint_name)
277 found = True
278 self.log("Response : {0}".format(response))
279 self.log("PostgreSQL Flexible virtualendpoint instance : {0} found".format(response.name))
280 except ResourceNotFoundError as e:
281 self.log('Did not find the PostgreSQL Flexible virtualendpoint instance. Exception as {0}'.format(e))
282 if found is True:
283 return self.format_item(response)
284
285 return None
286
287 def format_item(self, item):
288 return dict(
289 resource_group=self.resource_group,
290 server_name=self.server_name,
291 id=item.id,
292 endpoint_type=item.endpoint_type,
293 members=item.members,
294 virtual_endpoint_name=item.name,
295 type=item.type,
296 virtual_endpoints=item.virtual_endpoints
297 )
298
299
300 def main():
301 """Main execution"""
302 AzureRMPostgreSqlFlexibleVirtualEndpoint()
303
304
305 if __name__ == '__main__':
306 main()