Blob Blame History Raw
From 54e49943b636eab9453189381e93c68050c1e423 Mon Sep 17 00:00:00 2001
From: Gris Ge <fge@redhat.com>
Date: Sun, 28 Jun 2020 20:05:08 +0800
Subject: [PATCH] dns: Fix remove dns config

Current code raise NmstateVerificationError when user not providing
the full state:
        DNS.CONFIG: {DNS.SERVER: [], DNS.SEARCH: []}

Now supporting remove static DNS config via:
    * DNS.CONFIG: {}
    * DNS.CONFIG: {DNS.SERVER: []}
    * DNS.CONFIG: {DNS.SEARCH: []}
    * DNS.CONFIG: {DNS.SERVER: [], DNS.SEARCH: []}

Test case updated for this.

Signed-off-by: Gris Ge <fge@redhat.com>
---
 libnmstate/dns.py | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/libnmstate/dns.py b/libnmstate/dns.py
index 3196fc4..1ec0d81 100644
--- a/libnmstate/dns.py
+++ b/libnmstate/dns.py
@@ -33,14 +33,16 @@ class DnsState:
 
     def __init__(self, des_dns_state, cur_dns_state):
         self._config_changed = False
-        if des_dns_state:
+        if des_dns_state is None or des_dns_state.get(DNS.CONFIG) is None:
+            # Use current config if DNS.KEY not defined or DNS.CONFIG not
+            # defined.
+            self._dns_state = cur_dns_state or {}
+        else:
             self._dns_state = des_dns_state
             self._validate()
             self._config_changed = _is_dns_config_changed(
                 des_dns_state, cur_dns_state
             )
-        else:
-            self._dns_state = cur_dns_state or {}
         self._cur_dns_state = deepcopy(cur_dns_state) if cur_dns_state else {}
 
     @property
@@ -179,7 +181,9 @@ class DnsState:
 
     def verify(self, cur_dns_state):
         cur_dns = DnsState(des_dns_state=None, cur_dns_state=cur_dns_state,)
-        if self.config != cur_dns.config:
+        if self.config.get(DNS.SERVER) != cur_dns.config.get(
+            DNS.SERVER
+        ) or self.config.get(DNS.SEARCH) != cur_dns.config.get(DNS.SEARCH):
             raise NmstateVerificationError(
                 format_desired_current_state_diff(
                     {DNS.KEY: self.config}, {DNS.KEY: cur_dns.config},
-- 
2.27.0