From 8f20b1b0006e0417af093079ce9b1e856048a00e Mon Sep 17 00:00:00 2001 From: RusselSand <48719909+RusselSand@users.noreply.github.com> Date: Wed, 14 Aug 2024 14:01:59 +0200 Subject: [PATCH] 443 rds cleanup (#458) 443 rds cleanup Reviewed-by: Anton Sidelnikov --- .stestr.blacklist.functional | 1 + otcextensions/sdk/rds/v3/_proxy.py | 89 +++++++++++++++++++ otcextensions/sdk/rds/v3/instance.py | 2 +- .../tests/functional/sdk/rds/v3/__init__.py | 20 +++++ .../functional/sdk/rds/v3/test_cleanup.py | 23 +++++ 5 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 otcextensions/tests/functional/sdk/rds/v3/__init__.py create mode 100644 otcextensions/tests/functional/sdk/rds/v3/test_cleanup.py diff --git a/.stestr.blacklist.functional b/.stestr.blacklist.functional index 96714dfeb..b67b2ef0c 100644 --- a/.stestr.blacklist.functional +++ b/.stestr.blacklist.functional @@ -29,3 +29,4 @@ otcextensions.tests.functional.sdk.nat.v2.test_dnat* otcextensions.tests.functional.osclient.kms* otcextensions.tests.functional.sdk.imsv1* otcextensions.tests.functional.sdk.imsv2* +otcextensions.tests.functional.sdk.rds.v3* diff --git a/otcextensions/sdk/rds/v3/_proxy.py b/otcextensions/sdk/rds/v3/_proxy.py index 7e10e0ee3..544da0c96 100644 --- a/otcextensions/sdk/rds/v3/_proxy.py +++ b/otcextensions/sdk/rds/v3/_proxy.py @@ -510,3 +510,92 @@ def wait_for_backup(self, backup, status='COMPLETED', failures=None, failures = ['FAILED'] if failures is None else failures return resource.wait_for_status( self, backup, status, failures, interval, wait) + + def wait_for_delete(self, res, interval=2, wait=120, callback=None): + """Wait for a resource to be deleted. + + :param res: The resource to wait on to be deleted. + :type resource: A :class:`~openstack.resource.Resource` object. + :param interval: Number of seconds to wait before to consecutive + checks. Default to 2. + :param wait: Maximum number of seconds to wait before the change. + Default to 120. + :param callback: A callback function. This will be called with a single + value, progress, which is a percentage value from 0-100. + + :returns: The resource is returned on success. + :raises: :class:`~openstack.exceptions.ResourceTimeout` if transition + to delete failed to occur in the specified seconds. + """ + return resource.wait_for_delete(self, res, interval, wait, callback) + + def _get_cleanup_dependencies(self): + return { + 'rds': { + 'before': ['network'] + } + } + + def _service_cleanup( + self, + dry_run=True, + client_status_queue=None, + identified_resources=None, + filters=None, + resource_evaluation_fn=None, + skip_resources=None, + ): + if self.should_skip_resource_cleanup("instance", skip_resources): + return + + instances = [] + for instance in self.instances(): + if not dry_run: + for tag in instance.tags: + self.remove_tag(instance, tag['key']) + self._service_cleanup_del_res( + self.remove_tag, + tag['key'], + dry_run=dry_run, + client_status_queue=client_status_queue, + identified_resources=identified_resources, + filters=filters, + resource_evaluation_fn=resource_evaluation_fn, + ) + for backup in self.backups(instance): + if 'Automated' not in backup.type: + self._service_cleanup_del_res( + self.delete_backup, + backup, + dry_run=dry_run, + client_status_queue=client_status_queue, + identified_resources=identified_resources, + filters=filters, + resource_evaluation_fn=resource_evaluation_fn, + ) + need_delete = self._service_cleanup_del_res( + self.delete_instance, + instance, + dry_run=dry_run, + client_status_queue=client_status_queue, + identified_resources=identified_resources, + filters=filters, + resource_evaluation_fn=resource_evaluation_fn, + ) + if not dry_run and need_delete: + instances.append(instance) + + for instance in instances: + self.wait_for_delete(instance) + + for config in self.configurations(): + if 'Default-' not in config.name: + self._service_cleanup_del_res( + self.delete_configuration, + config, + dry_run=dry_run, + client_status_queue=client_status_queue, + identified_resources=identified_resources, + filters=filters, + resource_evaluation_fn=resource_evaluation_fn, + ) diff --git a/otcextensions/sdk/rds/v3/instance.py b/otcextensions/sdk/rds/v3/instance.py index 8466141ef..6abff6c4d 100644 --- a/otcextensions/sdk/rds/v3/instance.py +++ b/otcextensions/sdk/rds/v3/instance.py @@ -119,7 +119,7 @@ class Instance(_base.Resource): switch_strategy = resource.Body('switch_strategy') #: Lists the tags and their values attached to the instance. #: *Type:dict* - tags = resource.Body('tags', type=dict) + tags = resource.Body('tags', type=list) #: Time Zone. #: *Type:string* time_zone = resource.Body('time_zone') diff --git a/otcextensions/tests/functional/sdk/rds/v3/__init__.py b/otcextensions/tests/functional/sdk/rds/v3/__init__.py new file mode 100644 index 000000000..6eab773d5 --- /dev/null +++ b/otcextensions/tests/functional/sdk/rds/v3/__init__.py @@ -0,0 +1,20 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from otcextensions.tests.functional import base + + +class TestRdsCleanUp(base.BaseFunctionalTest): + + def setUp(self): + super(TestRdsCleanUp, self).setUp() + self.client = self.conn.rds diff --git a/otcextensions/tests/functional/sdk/rds/v3/test_cleanup.py b/otcextensions/tests/functional/sdk/rds/v3/test_cleanup.py new file mode 100644 index 000000000..72782cfb2 --- /dev/null +++ b/otcextensions/tests/functional/sdk/rds/v3/test_cleanup.py @@ -0,0 +1,23 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from otcextensions.tests.functional.sdk.rds.v3 import TestRdsCleanUp + + +class TestCleanup(TestRdsCleanUp): + def setUp(self): + super(TestCleanup, self).setUp() + + def test_01_cleanup(self): + self.client._service_cleanup(dry_run=False) + instances = list(self.client.instances()) + self.assertEqual(len(instances), 0)