Option return formats
4 minute read
Cloud Management Platform (CMP) accepts several return shapes from a plugin that generates parameter options. The return must be a list or a dictionary.
List of simple values
The most common return from a generated parameter options action is a list of simple scalar values: strings, integers, decimals, etc. Each value is used as the stored value and the label shown to the user.
List of two-tuples
If you want the stored value to differ from the label shown on the form, return a list of two-tuples. Each tuple must have exactly two elements: the first is the value stored for the parameter, and the second is the human-readable label for the option.
Dictionary
Return a dictionary when you need to change the built-in handling of the options. CMP understands the following keys:
| Key | Type | Purpose |
|---|---|---|
options |
list | The list of options for this parameter, either a simple list of values or a list of two-tuples, as described above. |
initial_value |
matches an option’s value | The value selected first from the resulting options, if it is among them. |
override |
boolean | If True, the returned options are not subject to further processing, so they are not filtered against options defined in the UI. It does not affect the sort order. |
sort |
boolean | If True, CMP sorts the returned options alphabetically. False by default. |
These returns apply to get_options_list. To generate options for an action input, use generate_options_for_<action_input_name> inside the plugin instead, as shown in Example: options for an action input below.
None
You may want to generate options only in a specific context and fall back to the built-in options everywhere else the parameter is used. Accomplish this by returning None explicitly from the orchestration action.
Example: rules that vary by context
This example presents the vmware_datastore parameter differently in each context. The rules are arbitrary; the point is to show every return shape in one file.
The business rules to enforce are:
- When adding disks to a server, always use the same datastore as the root disk.
- When provisioning a new server, if the ordering group has its own dedicated list of datastores, start with that list but also consider the environment’s options.
- When determining what options can be added to a group or an environment, fall back to the built-in behavior.
# sample orchestration for documentation purposes
def get_options_list(
field, blueprint=None, environment=None, group=None, server=None, **kwargs):
if server:
# This is a server modification event, and we want to completely override
# the built-in behavior.
storage = server.disks.first().disk_storage
options = [(storage.id, storage.name)]
return {'options': options, 'override': True}
elif group and environment and blueprint:
# This is a provisioning event, the only time all three objects are
# available at once.
# Get group options for this field using the default hierarchy,
# including options inherited from parent groups.
group_options = group.get_custom_field_options(field=field)
# Get the intersection of the environment and group options.
options = set(environment.custom_field_options.filter(field=field).all())
options = options.intersection(group_options)
# Convert the parameter values to a list of tuples.
ret_list = []
for cfv in options:
ret_list.append((cfv.value.id, cfv.display_value))
return ret_list
# Fall back to the built-in behavior in all other contexts.
return None
To retrieve options from the group alone, excluding options inherited from its parents:
group.custom_field_options.filter(field=field)
Example: options for an action input
This plugin generates the options for an action input named hostname_prefix. It always offers two fixed values, and when a server is in context, it also offers that server’s group and environment.
def generate_options_for_hostname_prefix(server=None, **kwargs):
options = [('test', 'test'), ('example', 'example')]
if server:
options.append((server.group.id, server.group.name))
options.append((server.environment.id, server.environment.name))
return options
The method returns a list of tuples, each holding a value to use and a label to display. The context passed in depends on where the plugin is used. Define the method to accept arbitrary keyword arguments so it continues to work if more context is added later.
You can also define an initial value for the action input as part of the return. Set it only where the value it names is available, so the method still works in a context without a server:
def generate_options_for_hostname_prefix(server=None, **kwargs):
options = [('test', 'test'), ('example', 'example')]
if server:
options.append((server.group.id, server.group.name))
options.append((server.environment.id, server.environment.name))
return {'initial_value': server.group.id, 'options': options}
return {'options': options}
Here, the method returns a dictionary with keys for the initial value and the options, where the options are a list of tuples as before. The initial value should match the first item of one of the option tuples.
These options appear both when setting default values for the action input and when choosing a value at execution time. An initial value is simply the option selected when the widget loads.
Related
- Generated parameter options: Write the plugin that returns options.
- Parameter options: How generated options are filtered.