nickovs / unificontrol

A high-level Python interface to the Unifi controller software
Apache License 2.0
96 stars 41 forks source link

API extension to enable or disable firewall port forwarding rules #15

Closed kdknigga closed 3 years ago

kdknigga commented 3 years ago

Hello,

I'm writing an API definition to support enabling or disabling firewall port forwarding rules. It sort of works, but I'm not sure how to debug with the metaprogramming stuff going on and was wondering if my problem is obvious.

I've added the following to unifi.py:

    enable_portforwarding_rule = UnifiAPICall(
        """Enable or disable a port forwarding rule

        Args:
            pfr_id (str): ``_id`` value of the portforwarding rule
            enabled (bool): true to enable the rule, false to disable the rule
        """,
        "rest/portforward",
        path_arg_name="pfr_id",
        path_arg_optional=False,
        json_args=["enabled"],
        method="PUT",
        )

Calling enable_portforwarding_rule(pfr_id="_id of portforwarding rule here", enabled=True) works! With requests/http.client debugging turned on I see b'{"enabled": true}' get sent to the correct place and the rule gets enabled.

However, when I call enable_portforwarding_rule(pfr_id="_id of portforwarding rule here", enabled=False), well, that doesn't work. The JSON that gets sent is b'{}'.

Is something getting eaten somewhere in the metaprogramming guts?

Thanks,

Kris

nickovs commented 3 years ago

Hi Kris. Thanks for bringing this up. Yes, this appears to be a bug. I think that the problem is that this line which should test if the argument is not None rather than just testing its truth value. I will look at pushing a fix tomorrow (it's late here) but in the mean time if you want to try changing that line to say if bound.arguments[arg_name] is not None: it might fix your problem.

kdknigga commented 3 years ago

You nailed it. That was the problem.

I can make a pull request to change that if statement in metaprogram.py if you'd like. Or, if you want to make the commit that works for me, too.

Also, if you'd like to add the tiny extension I made, I can make a pull request for that, too.

No matter, thanks for the help and this nice, little project!

nickovs commented 3 years ago

Glad that worked! It would be great if you want to send a pull request with that fix and your improvements.

Cheers, Nicko

On Dec 19, 2020, at 18:07, Kris Knigga notifications@github.com wrote:

 You nailed it. That was the problem.

I can make a pull request to change that if statement in metaprogram.py if you'd like. Or, if you want to make the commit that works for me, too.

Also, if you'd like to add the tiny extension I made, I can make a pull request for that, too.

No matter, thanks for the help and this nice, little project!

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe.

nickovs commented 3 years ago

Fixed by #16.