From 7e58bdd5254439e6e3a5ac0198de17afcf873c8b Mon Sep 17 00:00:00 2001 From: Omer Date: Fri, 3 May 2024 11:04:46 +0200 Subject: [PATCH] Fix YAMLAdapter when optional attributes are missing So far, using YAMLAdapter in pools.yaml context, without providing all of the pools.yaml attributes altogether, failed because of YAMLAdapter. An example could be an end user who wouldn't like to deploy catalog zones in its pools and therefore would drop that attribute from the pools.yaml file, like in designate/tests/resources/pools_yaml/multiple-pools.yaml. Running any designate-manage pool command on a file without all of the pools.yaml attributes will fail with the following error message: designate.exceptions.AdapterNotFound: Adapter for None to format YAML not found This patch fixes it. Closes-Bug: #2064720 Change-Id: I214cfc05a834d97a61d0236ab223470631bfc405 (cherry picked from commit 6cc0a7d8876291f874edaac2ff86b7d7a84a738a) --- designate/objects/adapters/yaml/base.py | 4 +++ .../tests/functional/manage/test_pool.py | 15 ++++++++ .../pools_yaml/pools-catalog-zone.yaml | 36 +++++++++++++++++++ .../tests/resources/pools_yaml/pools.yaml | 4 --- ...h-missing-attributes-3037b2dad30f84e2.yaml | 6 ++++ 5 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 designate/tests/resources/pools_yaml/pools-catalog-zone.yaml create mode 100644 releasenotes/notes/Fix-designate-manage-pool-update-with-missing-attributes-3037b2dad30f84e2.yaml diff --git a/designate/objects/adapters/yaml/base.py b/designate/objects/adapters/yaml/base.py index 3585303bd..c65b95098 100644 --- a/designate/objects/adapters/yaml/base.py +++ b/designate/objects/adapters/yaml/base.py @@ -49,6 +49,10 @@ class YAMLAdapter(base.DesignateAdapter): # if not, move on new_obj = getattr(obj, key, None) obj_key = key + + if new_obj is None: + continue + # Check if this item is a relation (another DesignateObject that # will need to be converted itself if hasattr(obj.FIELDS.get(obj_key, {}), 'objname'): diff --git a/designate/tests/functional/manage/test_pool.py b/designate/tests/functional/manage/test_pool.py index b57fd2559..36c033f5d 100644 --- a/designate/tests/functional/manage/test_pool.py +++ b/designate/tests/functional/manage/test_pool.py @@ -68,6 +68,21 @@ class ManagePoolTestCase(designate.tests.functional.TestCase): 'Default PowerDNS 4 Pool', ''.join(self.command.output_message) ) + def test_show_config_catalog_zone(self): + self.command._setup() + self.command._create_pool(get_pools('pools-catalog-zone.yaml')[0]) + + pool_id = self.central_service.find_pool( + self.admin_context, {'name': 'default'}).id + + self.command.show_config(pool_id) + + self.print_result.assert_called_once() + self.assertIn('Pool Configuration', self.command.output_message[1]) + self.assertIn( + 'Default PowerDNS 4 Pool', ''.join(self.command.output_message) + ) + @mock.patch.object(service.Service, 'find_pool', side_effect=oslo_messaging.MessagingTimeout()) def test_show_config_rpc_timeout(self, mock_find_pool): diff --git a/designate/tests/resources/pools_yaml/pools-catalog-zone.yaml b/designate/tests/resources/pools_yaml/pools-catalog-zone.yaml new file mode 100644 index 000000000..b2533a647 --- /dev/null +++ b/designate/tests/resources/pools_yaml/pools-catalog-zone.yaml @@ -0,0 +1,36 @@ +--- + +- name: default + description: Default PowerDNS 4 Pool + + attributes: + type: internal + + ns_records: + - hostname: ns1-1.example.org. + priority: 1 + - hostname: ns1-2.example.org. + priority: 2 + + nameservers: + - host: 192.0.2.2 + port: 53 + - host: 192.0.2.3 + port: 53 + + targets: + - type: pdns4 + description: PowerDNS 4 Server + masters: + - host: 192.0.2.1 + port: 5354 + options: + api_endpoint: http://192.0.2.1:8081 + api_token: api_key + also_notifies: + - host: 192.0.2.4 + port: 53 + + catalog_zone: + catalog_zone_fqdn: example.org. + catalog_zone_refresh: '60' diff --git a/designate/tests/resources/pools_yaml/pools.yaml b/designate/tests/resources/pools_yaml/pools.yaml index b2533a647..6f05f4a53 100644 --- a/designate/tests/resources/pools_yaml/pools.yaml +++ b/designate/tests/resources/pools_yaml/pools.yaml @@ -30,7 +30,3 @@ also_notifies: - host: 192.0.2.4 port: 53 - - catalog_zone: - catalog_zone_fqdn: example.org. - catalog_zone_refresh: '60' diff --git a/releasenotes/notes/Fix-designate-manage-pool-update-with-missing-attributes-3037b2dad30f84e2.yaml b/releasenotes/notes/Fix-designate-manage-pool-update-with-missing-attributes-3037b2dad30f84e2.yaml new file mode 100644 index 000000000..d48f1eedf --- /dev/null +++ b/releasenotes/notes/Fix-designate-manage-pool-update-with-missing-attributes-3037b2dad30f84e2.yaml @@ -0,0 +1,6 @@ +--- +fixes: + - | + So far, the yaml Adapter made every designate-manage pool command + fail if the pools.yaml file was missing any of the pool attributes. + This patch makes the adapter to continue without raising any error.