JsonOpt Support for oslo.config

Registered by Sudheendra Murthy on 2014-08-06

In oslo.config.cfg, it will be useful to have an Option for specifying Json, in addition to the existing types such as StrOpt, DictOpt, etc. For example, this will allows specifying a valid Json string as value to a config parameter as shown below:

[custom_security_group]
default=[{"direction":"egress","ethertype":"IPv4"},{"direction":"ingress","protocol":"tcp","port_range_min":80,"port_range_max":80}]

Although this can be done in an indirect way by treating this as a StrOpt and then loading it into json after reading it from config file, it will be useful to have a native JsonOpt that performs this functionality.

The implementation of this is straight-forward with something like the following. Note that the below code needs to be enhanced with error checking, etc.

class Json(object):
 def __init__(self):
        super(Json, self).__init__()

    def __call__(self, value):
        import json
        return json.loads(str(value))

    def __repr__(self):
        return 'Json'
   ....

class JsonOpt(Opt):
    def __init__(self, name, **kwargs):
        super(JsonOpt, self).__init__(name,
                                      type=Json(),
                                      **kwargs)

Blueprint information

Status:
Started
Approver:
None
Priority:
Undefined
Drafter:
Sudheendra Murthy
Direction:
Needs approval
Assignee:
Sudheendra Murthy
Definition:
New
Series goal:
None
Implementation:
Good progress
Milestone target:
None
Started by
Sudheendra Murthy on 2014-08-06

Related branches

Sprints

Whiteboard

(?)

Work Items

This blueprint contains Public information 
Everyone can see this information.

Subscribers

No subscribers.