Code Snippets

2 minute readReferenceExtensibilityDeveloper productivity

Introduction

This chapter is provided to share some code snippets that show the usage of the Perl libraries.

Auto-Generated Code

After generation of the plugin using pdk you will see that your plugin has a single main class which inherits FlowPDF. This file will be at dsl/properties/perl/lib/EC/Plugin/YOURCLASS.pm.

For each procedure in the plugin, a corresponding method will be created in a file with the same name as the procedure. For example dsl/procedures/YOUR_PROCEDURE/steps/YOUR_PROCEDURE.pl you will look something like this:

$[/myProject/perl/core/scripts/preamble.pl] use EC::Plugin::NewRest; # Auto generated code of plugin step EC::Plugin::YOURCLASS->runStep('YOURPROCEDURE', 'YOURPROCEDURESTEP', 'functionName');

The main plugin class will have a function called functionName as follows:

sub functionName { my ($pluginObject, $runtimeParameters, $stepResult) = @_; ...; }

Retrieving parameter values from configuration and step

Using one API

Here is a snippet where in both config as well as step parameter values can be retrieved using the same API.

my $runtimeParameters = $context->getRuntimeParameters(); # runtimeParameters is a HASH reference with following fields: # user, password, proxy_user, proxy_password, basic_user, basic_password (taken from config) # requestMethod and requestContent (taken from procedure parameters). print $runtimeParameters->{requestMethod}; print $runtimeParameters->{proxy_user};

Using two different APIs

Here is a snippet where in config and step parameter values are retrieved using different APIs.

my $context = $pluginObject->newContext(); my $parameters = $context->getStepParameters(); my $cred = $parameters->getParameter('credential'); my $url = $parameters->getParameter('url')->getValue(); my $method = undef; if (my $p = $parameters->getParameter('method')) { $method = $p->getValue(); } my ($username, $password); if ($cred) { $username = $cred->getUserName(); $password = $cred->getSecretValue(); }
my $configValues = $context->getConfigValues();

You can then use the methods in FlowPDF::Config object to access the config values.

Setting step results

for my $i (1..100) { $stepResult->setJobStepSummary("Progress: $i\n"); $stepResult->applyAndFlush(); sleep 1; }

or

# Step 1. Creating an object. my $stepResult = $context->newStepResult(); # Step 2. Adding action items. $stepResult->setOutputParameter('executionResult', 'Successfully finished!'); $stepResult->setJobStepSummary('Done with success'); # Step 3. Applying changes. $stepResult->apply();

Performing REST requests

# retrieving new rest client object. my $restClient = $context->newRESTClient(); # creating HTTP::Request object using our wrappers my $req = $restClient->newRequest(GET => 'https://localhost:8080'); # performing request and getting HTTP::Response object. my $response = $restClient->doRequest($req); # printing response content: print $response->decoded_content();

CLI execution

# Step 1 and 2. Loading component and creating CLI executor with working directory of current workspace. my $cli = FlowPDF::ComponentManager->loadComponent('FlowPDF::Component::CLI', { workingDirectory => $ENV{COMMANDER_WORKSPACE} }); # Step 3. Creating new command with ls as shell and -la as parameter. my $command = $cli->newCommand('ls', ['-la']); # adding two more parameters for command $command->addArguments('-lah'); $command->addArguments('-l'); # Step 4. Executing a command my $res = $cli->runCommand($command); # Step 5. Processing a response. print "STDOUT: " . $res->getStdout();

Logging

use FlowPDF::Log; logInfo("This is info"); logError("This is error");

or

my $reference = { one => 'two', three => 'four' }; logInfo("Reference is: ", $reference);

Defining custom fields

You can define custom fields in the pluginInfo subroutine of your main plugin module and retrieve them elsewhere.

sub pluginInfo { return { pluginName => '@PLUGIN_KEY@', pluginVersion => '@PLUGIN_VERSION@', configFields => ['config'], configLocations => ['ec_plugin_cfgs'], defaultConfigValues => { authScheme => 'basic' }, configValues => { one => 'two' } }; }

Here is how you retrieve them:

my $pluginValues = $pluginObject->getPluginValues(); logInfo("Value of one is: $pluginValues->{one}");