OPNsense Forum

English Forums => Development and Code Review => Topic started by: mihak on February 27, 2021, 08:08:18 AM

Title: passing parameters
Post by: mihak on February 27, 2021, 08:08:18 AM
I am trying to pass parameters from index.volt page to Api defined in ServiceController.php - and then pass the same parameters to service defined in action.d/actions.conf file.

1. actions.conf can use parameters with the statement: parameters: %s
      > I struggle to add some logic to the actions.conf commands: i.e. if no parameter is passed, use xxx as default parameter

2. Api/ServiceController.php is the one calling actions in actions.conf using $response = $backend->configdRun()
       > How do I configure a function in Api/ServiceController.php to capture parameters and passing them on to configdRun?

Here is some code to illustrate:

from actions_speedtest.conf
[test]
command:/usr/local/opnsense/scripts/OPNsense/speedtest/speedtest --accept-license --accept-gdpr -fjson -s
parameters: %s
type:script_output
message:Ookla freeBSD speedtest (json)


This config works with configctl speedtest test 5033 (with parameter) but fails with configctl speedtest test - because the last argument doesn't get parameter passed.

from Api/ServiceController.php:
class ServiceController extends ApiControllerBase
{
    public function testAction()
    {
        $backend = new Backend();
        $response = $backend->configdRun("speedtest test");
        return array("response" => $response);
    }
}


How do I capture - and pass - parameter? I'd be ok with a get /api/speedtest?5033 or with nesting /api/speedtest/5033

I did a fair share of googling across phalcon documentation before posting here - but my silly old brain just can't find the right page with a good example...

And as a thank-you to read that far, here is a screenshot of my plugin; I am using both a freeBSD binary and Python implementation of speedtest. What doesn't work is selecting the server top left and passing that selection to Api controller (and then to configctl); at the moment, each speedtest grabs the default (fastest?) server.

Title: Re: passing parameters
Post by: franco on February 27, 2021, 07:12:28 PM
Looks promising, thanks!

A good example is https://github.com/opnsense/core/blob/master/src/opnsense/mvc/app/controllers/OPNsense/Core/Api/FirmwareController.php#L585-L606 where you can see the package name being passed from the API request to the configd call.


Cheers,
Franco
Title: Re: passing parameters
Post by: mihak on February 27, 2021, 09:45:53 PM
That FirmwareController.php actually helped a lot!

Here is how I am now capturing and passing the parameter for speedtest:

    public function testAction(int $serverid)
    {
        $backend = new Backend();
        $response = $backend->configdRun("speedtest test ${serverid}");
        return array("response" => $response);
    }


And the way to call it now is with /api/speedtest/service/pytest/31467
(no validation of serverid yet - stupid numbers generate 'Execute error' by speedtest backed.