pgreco / rpms / ipa

Forked from forks/areguera/rpms/ipa 4 years ago
Clone
Blob Blame History Raw
From a9ebd731b23ebf9a56401139fe20dafc50322ff3 Mon Sep 17 00:00:00 2001
From: David Kupka <dkupka@redhat.com>
Date: Tue, 16 Aug 2016 15:32:47 +0200
Subject: [PATCH] schema cache: Read schema instead of rewriting it when
 SchemaUpToDate

https://fedorahosted.org/freeipa/ticket/6048

Reviewed-By: Jan Cholasta <jcholast@redhat.com>
---
 ipaclient/remote_plugins/schema.py | 46 ++++++++++++++++++++------------------
 1 file changed, 24 insertions(+), 22 deletions(-)

diff --git a/ipaclient/remote_plugins/schema.py b/ipaclient/remote_plugins/schema.py
index 2fc6cce3eca392447cd4230216900116002934f4..e8b3f61bd8460c60630e8710028da79654819bd1 100644
--- a/ipaclient/remote_plugins/schema.py
+++ b/ipaclient/remote_plugins/schema.py
@@ -17,6 +17,7 @@ import six
 
 from ipaclient.frontend import ClientCommand, ClientMethod
 from ipalib import errors, parameters, plugable
+from ipalib.errors import SchemaUpToDate
 from ipalib.frontend import Object
 from ipalib.output import Output
 from ipalib.parameters import DefaultFrom, Flag, Password, Str
@@ -439,15 +440,14 @@ class Schema(object):
             self._dict[ns] = {}
             self._namespaces[ns] = _SchemaNameSpace(self, ns)
 
-        is_known = False
-        if not api.env.force_schema_check:
-            try:
-                self._fingerprint = server_info['fingerprint']
-                self._expiration = server_info['expiration']
-            except KeyError:
-                pass
-            else:
-                is_known = True
+        try:
+            self._fingerprint = server_info['fingerprint']
+            self._expiration = server_info['expiration']
+        except KeyError:
+            is_known = False
+        else:
+            is_known = (not api.env.force_schema_check and
+                        self._expiration > time.time())
 
         if is_known:
             try:
@@ -461,14 +461,15 @@ class Schema(object):
             self._fetch(client)
         except NotAvailable:
             raise
+        except SchemaUpToDate as e:
+            self._fingerprint = e.fingerprint
+            self._expiration = time.time() + e.ttl
+            self._read_schema()
         else:
             self._write_schema()
-        finally:
-            try:
-                server_info['fingerprint'] = self._fingerprint
-                server_info['expiration'] = self._expiration
-            except AttributeError:
-                pass
+
+        server_info['fingerprint'] = self._fingerprint
+        server_info['expiration'] = self._expiration
 
     @contextlib.contextmanager
     def _open(self, filename, mode):
@@ -501,21 +502,22 @@ class Schema(object):
             schema = client.forward(u'schema', **kwargs)['result']
         except errors.CommandError:
             raise NotAvailable()
-        except errors.SchemaUpToDate as e:
-            fp = e.fingerprint
-            ttl = e.ttl
-        else:
+
+        try:
             fp = schema['fingerprint']
-            ttl = schema.pop('ttl', 0)
-            schema.pop('version', None)
+            ttl = schema.pop('ttl')
+            schema.pop('version')
 
             for key, value in schema.items():
                 if key in self.namespaces:
                     value = {m['full_name']: m for m in value}
                 self._dict[key] = value
+        except KeyError as e:
+            logger.warning("Failed to fetch schema: %s", e)
+            raise NotAvailable()
 
         self._fingerprint = fp
-        self._expiration = ttl + time.time()
+        self._expiration = time.time() + ttl
 
     def _read_schema(self):
         self._file.truncate(0)
-- 
2.7.4