Пример #1
0
def test_object_set():
    host = DictObject(obj={"id": "192.168.1.1"})
    module = DictObject(obj={"id": "iam"})

    os = ObjectSet()
    os.add_object("host", host)
    os.add_object("module", module)

    assert os.has_object("host")
    assert os.has_object("module")
    assert not os.has_object("cluster")

    assert os.get("host.id") == "192.168.1.1"
    assert os.get("module.id") == "iam"

    assert os.get("invalidkey") is None

    assert os.get("cluster.id") is None
    assert os.get("host.path") is None

    # get/del
    obj = os.get_object("host")
    assert obj
    assert obj.id == "192.168.1.1"

    os.del_object("host")
    assert not os.has_object("host")
Пример #2
0
def eval_exmaple():
    # create a resource, type is host
    h = {"id": "hello"}
    host = DictObject(h)
    print("host.id =", host.id)
    print("host.notexists =", host.notexists)

    # create a resource, type is module
    class Module(object):
        pass

    m = Module()
    m.id = "world"

    module = DictObject(m)
    print("module.id =", module.id)
    print("module.notexists =", module.notexists)

    print_spearator()

    # make a object set contains two resource type
    s = ObjectSet()
    s.add_object("host", host)
    s.add_object("module", module)

    print("object_set host.id", s.get("host.id"))
    print("object_set host.id", s.get("host.notexists"))
    print("object_set module.id", s.get("module.id"))
    print("object_set module.id", s.get("module.notexists"))
    print("object_set cluster.id", s.get("cluster.id"))

    print_spearator()

    # define a policy
    data = {
        "op":
        "OR",
        "content": [
            {
                "op": "eq",
                "field": "host.id",
                "value": "hello"
            },
            {
                "op": "not_eq",
                "field": "module.id",
                "value": "world"
            },
        ],
    }
    # make a policy expression
    expr = make_expression(data)
    print("the expression:", expr.expr())
    print("the expression render:", expr.render(s))
    print("the eval result:", expr.eval(s))

    print_spearator()

    data1 = {
        "op": "eq",
        "field": "host.id",
        "value": "hello",
    }
    expr1 = make_expression(data1)
    print("the expression:", expr1.expr())
    print("the expression render:", expr1.render(s))
    print("the eval result:", expr1.eval(s))

    print_spearator()

    data1 = {
        "op": "not_eq",
        "field": "host.id",
        "value": "hello",
    }
    expr1 = make_expression(data1)
    print("the expression:", expr1.expr())
    print("the expression render:", expr1.render(s))
    print("the eval result:", expr1.eval(s))