from brewtils import Plugin, command
class HelloWorldClient(object):
@command (1)
def hello_world(self):
greeting = "Hello, World!"
print(greeting)
return greeting
Plugin Syntax Quick Reference Guide
Beer Garden is a plugin framework that helps developers to create useful, streamlined, event-driven applications in a composable, and easily extendable way. This guide is a quick reference for creating plugins in Beer Garden.
These examples focus almost exclusively on the python language bindings. As more bindings are updated, this guide will split itself out. |
Simple Plugin
1 | The @command decorator marks this method as a Command that’s part of the enclosing Client. |
Local plugin running
from brewtils import Plugin, command
class HelloWorldClient(object):
@command (1)
def say_hello(self):
greeting = "Hello, World!"
print(greeting)
return greeting
def main():
client = HelloWorldClient()
plugin = Plugin(
client=client,
name="hello-world", (2)
version="1.0",
description="Say hello"
)
plugin.run()
if __name__ == "__main__":
main()
1 | The @command decorator marks this method as a Command that’s part of the enclosing Client. |
2 | The values defined here determine how the plugin is listed in the UI. |
Remote plugin running
from brewtils import Plugin, command
class HelloWorldClient(object):
@command (1)
def say_hello(self):
greeting = "Hello, World!"
print(greeting)
return greeting
def main():
client = HelloWorldClient()
plugin = Plugin(
client=client,
name="hello-world", (2)
version="1.0.0",
description="My First Plugin",
bg_host="<HOST>", (3)
bg_port="<PORT>",
ssl_enabled=<SSL_ENABLED>,
)
plugin.run()
if __name__ == "__main__":
main()
1 | The @command decorator marks this method as a Command that’s part of the enclosing Client. |
2 | The values defined here determine how the plugin is listed in the UI. |
3 | Be sure to replace <HOST> , <PORT> , and <SSL_ENABLED> with appropriate values for your garden. |
To review what’s happening here: we added an import Plugin
at the top of our file and created a standard main method. In that method we created a HelloWorldClient
object and a Plugin
object. Notice that when we create the Plugin
we pass it the client and some additional parameters. Don’t worry too much about the additional paramters - we’ll cover them later.
Exception Handling
It is important to be able to tell Beer Garden when something on your system goes wrong. brewtils
takes advantage of Python’s exceptions in order to handle command malfunctions. So if you have a function:
def my_error(self):
raise ValueError("Something went wrong!")
This will result in the request status turning to "ERROR" and its output will be Something went wrong!
. It is expected that plugins will throw errors as a way to notify Beer Garden that something has gone wrong.
If you choose to handle errors and not throw, you will notice something that may be quite confusing to your plugins users. Let’s have an example:
def my_error(self, x):
if x is None:
# This is actually an error, but we will short-circuit
return "An error occurred."
return x
If x
being None
is an error, then you should throw an error. Otherwise, the request will be marked as SUCCESS
while the output will say An error occurred
If your command has a JSON
output type, then Beer Garden will attempt to format your exception as a JSON error message. Here’s an example:
@command(command_type="JSON")
def my_error(self):
raise ValueError("Error Message")
If you call this method, you’ll still notice a status of ERROR
but the output will be something like:
{
"message": "Error Message",
"attributes": {}
}
If you’re asking what the attributes
entry is supposed to represent, it will take the dict
of the exception and attempt to jsonify it. Let’s say you have a custom exception class like the following:
class MyError(Exception):
def __init__(self, *args, **kwargs):
self.foo = kwargs.pop("foo")
self.bar = kwargs.pop("bar")
Then you throw that error during a command, Beer Garden will modify the output as per the following:
{
"message": "Error Message"
"attributes": {"foo": "foo_value", "bar": "bar_value"}
}
If your attributes are not JSON serializable, then a string representation of the dictionary will be provided to the attributes. |
Making a Request
This section goes over various ways to make a request.
Curl
curl -X POST -H 'Content-Type: application/json' -d '{"system": "echo", "command": "say", "parameters": {"message": "My Message" } }' http://localhost:2337/api/v1/requests { "message" : "Successfully Created Request", "request" : "http://localhost:2337/api/v1/requests/555a56fae9a45a2ad182ac16" }
$ curl http://localhost:2337/api/v1/requests/555a56fae9a45a2ad182ac16 { "children": [], "command": "say", "command_type": null, "comment": "", "created_at": 1491927102000, "updated_at": 1491927102000, "error_class": null, "id": "58ed003e71afd45a7bf21e7a", "instance_name": "default", "output": "Hello, World!", "output_type": "STRING", "parameters": { "message": "Hello, World!" }, "parent": null, "status": "SUCCESS", "system": "echo", "system_version": "0.0.1" }
Python
from brewtils.rest.system_client import SystemClient
client = SystemClient("localhost", 2337, system_name="echo") (1)
request = client.echo(message="Hello, World!") (2)
print(request.status)
print(request.output)
1 | Create the client, passing Beer Garden connection parameters and the target System name |
2 | Create a Request for the 'echo' Command as if you were calling a method |
The SystemClient
is a blocking client that will make a rest request that is formatted correctly and returns a request object. This request object can then be queried the same way you would query a normal request object.
If the system you are using is multi-instanced, you can specify the default instance to use at the system level:
client = SystemClient("localhost", 2337, system_name="echo", default_instance="01")
request = client.say(message="Hello, Instance 01!")
You can also specify a different instance when creating a Request:
request = client.say(message="Hello, Instance 02!", _instance_name="02")
Parameters
Quick Table
Argument | Required? | Options | Default | Description |
---|---|---|---|---|
Y |
N/A |
N/A |
Specifies the Argument Name |
|
N |
[String, Integer, Float, Boolean, Any, Dictionary, Base64] |
Any |
Specifies the type of Parameter |
|
N |
[True, False] |
False |
Specifies if the parameter is a list |
|
N |
N/A |
key |
Specifies a Pretty way to refer to the key |
|
N |
[True, False] |
False |
Specifies if the parameter is required |
|
N |
N/A |
N/A |
The Default value of the Parameter |
|
N |
N/A |
N/A |
A short Description of the Parameter |
|
N |
N/A |
N/A |
A list of possible values |
|
N |
N/A |
N/A |
If parameter comes in as a kwarg |
|
N |
N/A |
N/A |
A python Object that has a parameters list |
|
N |
[True, False] |
False |
Specifies if this parameter can be null |
|
N |
Integer |
N/A |
Specifies maximum (See detailed for more info) |
|
N |
Integer |
N/A |
Specifies minimum (See detailed for more info) |
|
N |
N/A |
N/A |
Specifies regex to validate against this value |
|
N |
[textarea] |
N/A |
Specifies the form type to render for this plugin |
Key argument
The key
argument to @parameter
is the only required parameter. It must match the name of an argument in whatever method it is decorating. This is how users of your plugin will identify the parameter they would like to set.
@parameter(key="message")
def do_something(self, message):
print(message)
return message
Type argument
Setting the type
field for a Parameter will let Beer Garden do a couple of things.
First, it lets Beer Garden perform type validation on that parameter. If the value does not match the type (and can’t be converted sensibly) then the Request will be rejected.
Second, it allows the UI to use the best form element for that type of data. For example, you could use a string to represent a date, but setting type
to 'date' means the UI will use a nice datepicker form element.
Finally, several validation constraints can only be applied to specific types. The minimum and maximum constraints are good examples of this - they don’t make sense for booleans, but they definitely do for integers!
@parameter(key="number", type="Integer")
def multiply_by_zero(self, number):
return number * 0
This table outlines the valid type
options.
Type | Valid Constraints | Notes |
---|---|---|
String |
minimum, maximum, regex |
Minimum and Maximum refer to length |
Integer |
minimum, maximum |
|
Float |
minimum, maximum |
|
Boolean |
||
Date |
Form control will not have 'time' option |
|
DateTime |
||
Dictionary |
JSON Object |
|
Base64 |
Allows the user to upload a file as a parameter. This is provided as a file-like object to the plugin. |
|
Any |
Valid JSON (Object, Array, String), number, literal |
The default type is Any . This gives the most flexibility, but it’s a good idea to always specify a type to take advantage of the benefits described above.
|
Multi argument
The multi
field let’s Beer Garden know that the parameter should be a list. Most of the other fields stay the same and continue to describe the individual items in the list.
@parameter(key="list_of_strings", multi=True, type="String")
def do_something(self, list_of_strings):
for s in list_of_strings:
print(s)
Some of the fields do change meaning when you’ve specified that multi is true. See the below table for a more detailed description. |
Argument | How is it changed? |
---|---|
choices |
Choices specify the only valid values, no value can be repeated. |
maximum |
Specifies Maximum length of the list |
minimum |
Specifies Minimum length of the list |
Display name argument
The display_name
field allows you control over how Beer Garden renders the name of the field. This is useful if your argument has a less-than-useful name from the end-users perspective.
@parameter(key="foo", display_name="Name")
def do_something(self, foo):
print("Hi!, my name is: %s" % foo)
Optional argument
The optional
field allows you to specify whether or not the parameter is optional or required. The default depends on if there is a default value in the method definition.
@parameter(key="foo", optional=True, nullable=True))
def do_something(self, foo):
# By default, foo would not be optional but
# it is specified in the param so it's assumed
# the developer will handle the None case.
if foo is None:
print("foo is empty!")
else:
print(foo)
If a default is passed in, then optional will be set to True by default.
If you specify that something is optional, then it must also be nullable if no default is specified. |
Default argument
The default
field allows you to specify the default value for a parameter if it is not given by a user. If there is a default value in the method definition then it will use that.
@parameter(key="foo")
def do_something(self, foo="bar"):
print(foo)
In the above case, if someone utilizes this command but does not pass Beer Garden the foo
parameter, then Beer Garden will default it to bar
. Below is another example of how to use the default argument.
@parameter(key="foo", default="bar"))
def do_something(self, foo):
print(foo)
These are functionally equivalent for Beer Garden.
Description argument
The description
field adds a description to the plugin parameter you are defining.
@parameter(key="foo", description="Your first name")
def do_something(self, foo):
pass
Choices argument
The choices
field allows you to specify possible values for a parameter.
Basic Usage
The easiest way to use choices
is to pass a list of values:
@parameter(key="output_type", choices=["json", "xml"])
def format(self, obj, output_type):
if output_type == "json":
return jsonify(obj)
elif output_type == "xml":
return xmlify(obj)
Sometimes it’s useful to have the display text (what shows up in the UI) be different from the 'real' value (what gets sent to the plugin). To do this, instead of a list of literal values just pass a list of objects with text
and value
keys:
@parameter(key="output_type", choices=[
{"text": "The best", "value": "json"},
{"text": "The worst", "value": "xml"}])
def format(self, output_type):
pass
Additional Configuration
There are some configuration options that control how choices
works. Beer Garden will pick sensible defaults, but to tweak them pass a dictionary to choices
:
@parameter(key="output_type",
choices={'type': 'static', 'value': ['json','xml']})
def format(self, output_type):
...
That way you can add additional key/value pairs to the choices dictionary.
Choices Type
You probably noticed the 'type': 'static'
entry above. Beer Garden is able to figure out exactly what to do when you pass a list of values to choices
, but it needs a hint when you use the dictionary configuration. There are a couple of other ways to populate the choices list (more on those in a bit) so you need to be explicit.
The example above is using the static
type, which tells Beer Garden to expect a list of values in the value
attribute. This is functionally identical to passing a list of values to choices
directly.
The other choice types will be explained in detail in the Choice Sources section.
Display
When you use choices
the UI form control can be a typeahead or a select. To specify which to use just set the display
key:
@parameter(key="output_type",
choices={'type': 'static', 'value': ['json','xml'],
'display': 'typeahead'})
def format(self, output_type):
...
Strictness
The strict
configuration controls whether values that aren’t explicitly listed are allowed. Setting strict
to False will result in a typeahead control that will use the choices
values but still allow any text to be submitted.
@parameter(key="output_type",
choices={'type': 'static', 'value': ['json','xml'],
'strict': False})
def format(self, output_type):
...
Setting strict to False for a select won’t affect the display, but the strict value also controls validation on the backend.
|
Choice Sources
In all the examples so far the list of choices has been a literal list of values. That’s useful, but it’s also useful to have values that can change at runtime. In order to do that you need to provide choices
with instructions on how to populate the choice list instead of the list itself.
In all cases the result of the choices operation must be a valid choices list.
|
URL
Specifying a URL will tell the browser to load choices using an HTTP GET request. You can use type 'url' if using dictionary configuration or just pass the URL as a string:
@parameter(key="p1", choices='https://test.com/p1.json')
@parameter(key="p2", choices={"type": "url",
"value": 'https://test.com/p2.json'})
def format(self, p1, p2):
...
Be aware that the user’s browser will be making this request. So if the Beer Garden UI is being accessed at a secure (https) address then a request to a non-secure (http) URL will likely fail due to mixed-content restrictions. |
Command
Specifying a command will load choices by making a request to the current system. You can use type 'command' if using dictionary configuration or just pass the command as a string. If you’re not using choice parameters (more on those in a minute) you can omit the parenthesis for brevity.
@parameter(key="p1", choices="get_choices()")
@parameter(key="p2", choices={"type": "command",
"value": "get_choices"})
def format(self, p1, p2):
...
@command
def get_choices(self):
return [
{"text": "The best", "value": "json"},
{"text": "The worst", "value": "xml"}
]
Currently you must use a command from the same system (this restriction will be removed in a future release - see issue 269). |
Choice parameters
It’s often useful to have the choices for one parameter depend on the current value of another. To do that you can use choice parameters.
To create a reference on another parameter enclose its key in ${}
. How the parameter is passed depends on what choice source is being used.
When initializing the command creation page, BeerGarden will attempt to update all dependencies for choice parameters at once. If the dependent parameters are defined in such a way that causes side effects inside the command (for example, if A is a choice parameter that depends on B and C, but updating C changes an internal value A and B need), this could lead to unintended consequences or destructive behavior during command load. |
For 'command' types the parameter will be passed as an argument to the command. For example, suppose you have two parameters: day_type
and day_of_week
. You’d like the choices for day_of_week
to depend on what the user has selected for day_type
:
@command
def get_days(self, type):
if type == "Weekday":
return ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
elif type == "Weekend":
return ["Saturday", "Sunday"]
else:
raise Exception("Huh?")
@parameter(key="day_type", choices=["Weekday", "Weekend"])
@parameter(key="day_of_week", choices="get_days(type=${day_type})")
def my_command(self, day_type, day_of_week):
do_something(day_of_week)
return "All done!"
For 'url' types the choice parameter should be used as a query parameter:
@parameter(key="day_type", choices=["Weekday", "Weekend"])
@parameter(key="day_of_week",
choices="https://getthedays.com?type=${day_type}")
def my_command(self, day_type, day_of_week):
do_something(day_of_week)
return "All done!"
Choice parameters also enable using a static choices dictionary with one parameter used as the dictionary key. To do this use type static
and pass the dictionary as the value. Since we can construct the dictionary before defining the command
we can rework the day_of_week
example to look like this:
day_dict = {
"Weekday": ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"],
"Weekend": ["Saturday", "Sunday"]
}
@parameter(key="day_type", choices=["Weekday", "Weekend"])
@parameter(key="day_of_week", choices={'type': 'static',
'value': day_dict,
'key_reference': '${day_type}'})
def my_command(self, day_type, day_of_week):
do_something(day_of_week)
return "All done!"
When using a choices dictionary the None
key can be used to specify the allowed values when the reference key is null
. For example, if we wanted to modify the day_of_week
example to additionally allow any day to be selected if day_type
was null
we could do this:
day_dict = {
"Weekday": ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"],
"Weekend": ["Saturday", "Sunday"],
None: ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
"Saturday", "Sunday"]
}
@parameter(key="day_type", choices=["Weekday", "Weekend"],
nullable=True)
@parameter(key="day_of_week", choices={'type': 'static',
'value': day_dict,
'key_reference': '${day_type}'})
def my_command(self, day_type, day_of_week):
do_something(day_of_week)
return "All done!"
Is kwarg argument
The is_kwarg
argument allows you to name a keyword argument that is otherwise unspecified. This is useful if you take keyword args, but want to call out the normal use-case ones more explicitly while still being compatible to other python libraries calling you.
@parameter(key="foo", is_kwarg=True)
def do_something(self, **kwarg):
foo = kwarg.pop("foo")
print(foo)
Model argument
The model
argument allows you to specify some structure for a complicated object. Have a look at the following for an example of how to use the model
field.
from brewtils.models import Parameter
class Person(object):
name = Parameter(key="name",
type="String",
description="Person's name")
age = Parameter(key="age",
type="Integer",
description="Person's age")
class ExampleClient(object):
@parameter(key="person", model=Person)
def greet(self, person):
print("Hello %s" % person.name)
It is assumed that if you have a model, that the type is "Dictionary" |
Nullable argument
The nullable
argument allows you to specify if the parameter can be null. If the argument is allowed to be null, then you must tell us this is possible. The default is assuming that parameters cannot be null.
If there is a default value for a parameter, then nullable
is set to True.
@parameter(key="foo", nullable=True))
def do_something(self, foo):
if foo is None:
print("That's ok!")
else:
print("That's ok too!")
Maximum argument
The maximum
argument allows you to specify the maximum value for a parameter. This meaning changes based on the type
and whether or not the multi
flag is enabled. If the multi
flag is enabled, then maximum is referring to the list length maximum. Otherwise, if type
is integer, it will compare the value of the parameter to the maximum. Otherwise if the type
is a string, it will ensure the length of the string is within bounds.
@parameter(key="foo", type="String", maximum=1)
@parameter(key="bar", type="Integer", maximum=1)
@parameter(key="bazs", type="String", maximum=1)
def do_something(self, foo, bar, bazs):
# guarantees that foo is 1 character at most
# guarantees that bar is no more than 1
# guarantees that bazs is no more than 1 item long
print(foo)
print(bar)
print(bazs)
Minimum argument
The minimum
argument allows you to specify the minimum value for a parameter. This meaning changes based on the type
and whether or not the multi
flag is enabled. If the multi
flag is enabled, then minimum is referring to the list length minimum. Otherwise, if type
is integer, it will compare the value of the parameter to the minimum. Otherwise if the type
is a string, it will ensure the length of the string is within bounds.
@parameter(key="foo", type="String", minimum=1)
@parameter(key="bar", type="Integer", minimum=1)
@parameter(key="bazs", type="String", minimum=1)
def do_something(self, foo, bar, bazs):
# guarantees that foo is at least 1 character
# guarantees that bar is no less than 1
# guarantees that bazs is no less than 1 item long
print(foo)
print(bar)
print(bazs)