URL: https://github.com/freeipa/freeipa/pull/515
Author: tiran
 Title: #515: Re-add ipapython.config.config for backwards compatibilty
Action: opened

PR body:
"""
IPAConfig, config and init_config were removed in rev 7b966e85. Ipsilon uses
ipapython.config to get realm, domain and server of an enrolled host. Re-add
a simplified version that reads settings from api.env. init_config() does
not perform DNS discovery.

Depends on PR #492 to get meaningful defaults.

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

Signed-off-by: Christian Heimes <chei...@redhat.com>
"""

To pull the PR as Git branch:
git remote add ghfreeipa https://github.com/freeipa/freeipa
git fetch ghfreeipa pull/515/head:pr515
git checkout pr515
From 2ad5c38dac384f1d173af03b4696d00e58032788 Mon Sep 17 00:00:00 2001
From: Christian Heimes <chei...@redhat.com>
Date: Mon, 27 Feb 2017 17:13:14 +0100
Subject: [PATCH] Re-add ipapython.config.config for backwards compatibilty

IPAConfig, config and init_config were removed in rev 7b966e85. Ipsilon uses
ipapython.config to get realm, domain and server of an enrolled host. Re-add
a simplified version that reads settings from api.env. init_config() does
not perform DNS discovery.

Depends on PR #492 to get meaningful defaults.

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

Signed-off-by: Christian Heimes <chei...@redhat.com>
---
 ipapython/config.py | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 78 insertions(+)

diff --git a/ipapython/config.py b/ipapython/config.py
index 5f1295c..c059d16 100644
--- a/ipapython/config.py
+++ b/ipapython/config.py
@@ -24,6 +24,7 @@
 from copy import copy
 
 from ipapython.dn import DN
+import ipalib
 
 
 class IPAFormatter(IndentedHelpFormatter):
@@ -116,3 +117,80 @@ def add_standard_options(parser):
     parser.add_option("--server", dest="server",
                       help="Override default FQDN of IPA server")
     parser.add_option("--domain", dest="domain", help="Override default IPA DNS domain")
+
+
+class IPAConfigError(Exception):
+    pass
+
+
+class IPAConfig(object):
+    def __init__(self):
+        self.default_realm = None
+        self.default_server = []
+        self.default_domain = None
+
+    def get_realm(self):
+        if self.default_realm:
+            return self.default_realm
+        else:
+            raise IPAConfigError("no default realm")
+
+    def get_server(self):
+        if len(self.default_server):
+            return self.default_server
+        else:
+            raise IPAConfigError("no default server")
+
+    def get_domain(self):
+        if self.default_domain:
+            return self.default_domain
+        else:
+            raise IPAConfigError("no default domain")
+
+
+# Global library config
+config = IPAConfig()
+
+
+def init_config(options=None):
+    """Simple init_config for backwards compatibility
+    """
+    if options is not None:
+        config.default_realm = options.realm
+        config.default_domain = options.domain
+        if options.server:
+            config.default_server.extend(options.server.split(","))
+
+    if not all((config.default_realm, config.default_domain,
+                config.default_server)):
+        if ipalib.api.isdone("bootstrap"):
+            # re-use bootstrapped api
+            api = ipalib.api
+        else:
+            # or create a new temporary API object
+            api = ipalib.create_api(None)
+            api.bootstrap()
+
+        if not config.default_realm:
+            config.default_realm = api.env.realm
+        if not config.default_domain:
+            config.default_domain = api.env.domain
+        server = api.env.server
+        if server not in config.default_server:
+            config.default_server.append(server)
+
+    if not config.default_realm:
+        raise IPAConfigError(
+            "IPA realm not found in config file (/etc/ipa/default.conf) "
+            "or on the command line."
+        )
+    if not config.default_server:
+        raise IPAConfigError(
+            "IPA server not found in the config file (/etc/ipa/default.conf) "
+            "or on the command line."
+        )
+    if not config.default_domain:
+        raise IPAConfigError(
+            "IPA domain not found in the config file (/etc/ipa/default.conf) "
+            "or on the command line."
+        )
-- 
Manage your subscription for the Freeipa-devel mailing list:
https://www.redhat.com/mailman/listinfo/freeipa-devel
Contribute to FreeIPA: http://www.freeipa.org/page/Contribute/Code

Reply via email to