summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ipalib/parameters.py20
-rw-r--r--ipalib/plugins/baseldap.py16
-rw-r--r--ipalib/util.py6
3 files changed, 40 insertions, 2 deletions
diff --git a/ipalib/parameters.py b/ipalib/parameters.py
index f97c42218..a628b6f53 100644
--- a/ipalib/parameters.py
+++ b/ipalib/parameters.py
@@ -100,15 +100,17 @@ a more detailed description for clarity.
"""
import re
+import csv
+import json
+
from types import NoneType
-from util import make_repr
+from util import make_repr, json_serialize
from request import ugettext
from plugable import ReadOnly, lock, check_name
from errors import ConversionError, RequirementError, ValidationError
from errors import PasswordMismatch
from constants import NULLS, TYPE_ERROR, CALLABLE_ERROR
from text import Gettext, FixMe
-import csv
class DefaultFrom(ReadOnly):
@@ -852,6 +854,20 @@ class Param(ReadOnly):
pass
return self.default
+ def __json__(self):
+ json_dict = {}
+ for (a, k, d) in self.kwargs:
+ if k in (callable, DefaultFrom):
+ continue
+ elif isinstance(getattr(self, a), frozenset):
+ json_dict[a] = [k for k in getattr(self, a, [])]
+ else:
+ json_dict[a] = getattr(self, a, '')
+ json_dict['class'] = self.__class__.__name__
+ json_dict['name'] = self.name
+ json_dict['type'] = self.type.__name__
+ return json_dict
+
class Bool(Param):
"""
diff --git a/ipalib/plugins/baseldap.py b/ipalib/plugins/baseldap.py
index ffd60e7eb..a05f7f29a 100644
--- a/ipalib/plugins/baseldap.py
+++ b/ipalib/plugins/baseldap.py
@@ -21,6 +21,7 @@ Base classes for LDAP plugins.
"""
import re
+import json
from ipalib import crud, errors, uuid
from ipalib import Method, Object
@@ -29,6 +30,7 @@ from ipalib.base import NameSpace
from ipalib.cli import to_cli, from_cli
from ipalib import output
from ipalib.text import _
+from ipalib.util import json_serialize
def validate_add_attribute(ugettext, attr):
@@ -119,6 +121,20 @@ class LDAPObject(Object):
}
)
+ json_friendly_attributes = (
+ 'parent_object', 'container_dn', 'object_name', 'object_name_plural',
+ 'object_class', 'object_class_config', 'default_attributes', 'label',
+ 'hidden_attributes', 'uuid_attribute', 'attribute_members', 'name',
+ 'takes_params',
+ )
+ def __json__(self):
+ json_dict = dict(
+ (a, getattr(self, a)) for a in self.json_friendly_attributes
+ )
+ json_dict['primary_key'] = self.primary_key.name
+ json_dict['methods'] = [m for m in self.methods]
+ return json_dict
+
# Options used by create and update.
_attr_options = (
diff --git a/ipalib/util.py b/ipalib/util.py
index 76be9a6d7..f7405967d 100644
--- a/ipalib/util.py
+++ b/ipalib/util.py
@@ -30,6 +30,12 @@ import socket
from ipalib import errors
+def json_serialize(obj):
+ if not callable(getattr(obj, '__json__', None)):
+ # raise TypeError('%r is not JSON serializable')
+ return ''
+ return obj.__json__()
+
def get_current_principal():
try:
return unicode(krbV.default_context().default_ccache().principal().name)