Functions are built-in operations that execute inside the engine. They provide various helpers to address needs like retrieving the current time, generating random numbers, sorting collections and arrays, etc. Functions may operate on values and/or on reference collections and return values and/or reference collections. Arguments passed to functions are read-only. For example, the sort() function will return the result of the sort rather than modify the argument. Functions use the lower_case() notation.
The first category of functions relate to managing resource collections and arrays of values.
This function is syntactic sugar around the resource type get() action. It is the equivalent of calling the get() action with the given filters and returning the first resource that matches. Filters are specified with a hash or with a string and optional integer. If a string is specified then it is equivalent to a having a hash with a key name and the associated value (i.e. by default filter on name). If a integer is specified then is it equivalent to having a hash with a key revision. So:
@servers = find("servers", "default") @servers = find("servers", { name: "default" })
are equivalent to:
@servers = first(rs.servers.get(filter: ["name==default"]))
and
@server_template = find("server_templates", "MySQL Database Manager", 137) @server_template = find("server_templates", { name: "MySQL Database Manager", revision: 137 })
are equivalent to:
@server_template = first(rs.server_templates.get(filter: ["name==MySQL Database Manager","revision==137"]))
Arguments
Position | Possible Values | Required | Default Value | Comment |
---|---|---|---|---|
1 | string | yes | none | Resource type, e.g. "servers", "deployments", etc. |
2 | string -or- hash | yes | none | Resource name or filters |
3 | string or integer | no | none | Resource revision, if resource is versioned. Not specifying a value for versioned resources returns all resources with the given type and name. |
Result
A resource collection containing the first resource matching the given filter if any.
This function simply returns the size of the resource collection or array of values given as argument.
Arguments
Position | Possible Values | Required | Default Value |
---|---|---|---|
1 | resource collection -or- hash of values -or- string | yes | none |
Result
An integer representing the size of the given array, hash, string or collection.
These functions simply retrieve the first or last element of a resource collection or array of values.
Note: For resource collections, the result is a resource collection itself, consisting of a single resource. However, for an array of values, the result is a single value.
Arguments
The only argument is the collection from which the first or last element should be extracted.
Position | Possible Values | Required | Default Value |
---|---|---|---|
1 | resource collection -or- | yes | none |
Result
A resource collection if the argument is a resource collection or a value if the argument is an array of values.
Examples
$one = first([1, 2, 3]) # $one == 1 @servers = rs.deployments.get(filter: ["name==default"]).servers() @first = first(@servers) # size(@first) == 1
The sort() function allows sorting resource collections and arrays of values. If sorting an array of values, then all values must be of the same type and the array must not contain null. The ordering follows the same logic as the comparison operators:
Hashes cannot be compared directly, instead the sort() function accepts an argument to specify recursively which key should be used for sorting. That same argument can be used on resource collections to specify which field should be used for sorting. If the field ends up being a hash, then recursively what key of the hash should be used. By default, sorting resource collections is done using the name field.
Arguments
Position | Possible Values | Required | Default Value | Comment |
---|---|---|---|---|
1 | resource collection | yes | none | collection to be sorted |
2 | string | only if collection is an array of hashes | "name" for resource collections | name of resource field used to order resource collections |
3 | "asc" or "desc" | no | "asc" | Sorting order (ascendant or descendant) |
Result
The result type is the same as the first argument: a resource collection or an array of values.
Examples
Sorting a resource collection by name:
@servers = rs.deployments.get(filter: "name==default").servers() @sorted_servers = sort(@servers)
Sorting a collection of instances by their user data descending:
@instances = rs.deployments.get(filter: "name==default").server().current_instance() @sorted_instances = sort(@instances, "settings/user_data", "desc")
Sorting an array of hashes (using a key called timestamp in this example):
$data = [{ "value": 42, "timestamp": d"1/1/2012 12:32" }, { "value": 43, "timestamp": d"1/1/2012 16:31" }] $sorted_data = sort($data, "timestamp")
This function checks whether all the elements of a given collection or a specific value are contained in another given collection. The elements may be in different order and they may appear a different number of times, but as long as all elements appear at least once, then the function returns true, otherwise it returns false.
Arguments
Position | Possible Values | Required | Default Value | Comment |
---|---|---|---|---|
1 | resource collection | yes | none | Container being tested |
2 | resource collection | yes | none | Elements that must be in container for function to return true |
Result
true if all elements are contained in given collection, false otherwise.
Examples
$array = [1, 2, 3, 4] contains?($array, [1]) == true contains?($array, [5]) == false contains?($array, [2, 1, 3, 3]) == true contains?($array, [1, 2, 3, 5]) == false @servers = rs.deployments.get(filter: "name==default").servers() @server = rs.get(href: "servers/123") # Assume this server belongs to a deployment named 'default' contains?(@servers, @server) == true
Returns true if the given collection (resource collection or array of values) is empty, false otherwise.
Arguments
Position | Possible Values | Required | Default Value |
---|---|---|---|
1 | resource collection -or- | yes | none |
Result
true if given collection is empty, false otherwise.
This function checks for the existence of an element in an array of JSON values. When only an array is given then returns true if the array contains a value that is neither null nor false. When both an array and a value are provided then returns true if the array contains at least once the given value. If the value is a regular expression, it will return true if there is a least one value that is a string and that matches the regexp. The syntax used to write a regular expressions is "/regexp/".
Arguments
Position | Possible Values | Required | Default Value |
---|---|---|---|
1 | array of values | yes | none |
2 | value or regular expression | no | none |
Result
true or false.
Examples
$array_of_false = [false, false, null] any?($array_of_false) == false # All elements are null or false, result is false $array = [1, 2, 3, "a", "b", "cde"] any?($array) == true # Array contains values that are not either false or null, result is true any?($array, 1) == true # Array contains the value 1 any?($array, "cd") == false # Array does not contain value "cd" any?($array, "/cd/") == true # Value "cde" matches regular expression "cd"
This function checks whether all elements of an array have a given value. When only an array is provided, then it returns true if the array does not contain a value that is either null or false. When both an array and a value is provided, then it returns true if the array values all match the given value. If the value is a regular expression, it will return true if all the values in the array are all strings that match the regular expression.
Arguments
Position | Possible Values | Required | Default Value |
---|---|---|---|
1 | array of values | yes | none |
2 | value or regular expression | no | none |
Result
true or false.
Examples
$array_with_one_false = [1, 2, false] all?($array_with_one_false) == false # One element is null or false, result is false $array = [1, 2, 3, "a", "b", "cde"] all?($array) == true # Array contains no null or false, result is true all?($array, 1) == false # Not all values are one all?($array, "/cd/") == false # Not all values are strings that match regular expression "cd"
This function extracts the elements of a resource collection or of an array of hashes that have fields or values with a given value. The name of the field or hash key that should be selected for comparison correspond to the keys of the hash given as second argument. The values of that hash are the values that the resource fields or hash entries must be selected. If the resource field or hash values are strings, then the selector hash value can represent regular expressions.
Arguments
Position | Possible Values | Required | Default Value | Comment |
---|---|---|---|---|
1 | resource collection | yes | none | Container being tested |
2 | hash | yes | none | Resource field names associated with value that should be filtered on -or- |
Result
A resource collection composed of resources that have fields with the given values or an array of hashes that have the given values for given keys.
Examples
@servers = rs.deployments.get(filter: "name==default").servers() @app_servers = select(@servers, { "name": "/app/" }) # @app_servers contains servers whose name match # the regular expression "app" (i.e. contain the string "app") $hashes = [{ "key": "value1" }, { "key": "value2" }] $hashes_with_value1 = select($hashes, { "key": "value1" }) # $hashes_with_value1 contains hashes whose values # stored in key "key" is "value1"
This function traverses the given collection and returns a new collection made of all the unique elements. Two resources are considered identical if they have the same href.
Arguments
Position | Possible Values | Required | Default Value | Comment |
---|---|---|---|---|
1 | resource collection | yes | none | Container to be traversed and whose unique elements should be extracted |
Result
A collection composed of the distinct elements in the initial collection. The types of the elements is preserved (so the result is a resource collection or an array of values depending on the argument).
Examples
@servers = rs.deployments.get(filter: "name==default").servers() @duplicated_servers = @servers + @servers @unique_servers = unique(@duplicated_servers) assert @servers == @unique_servers
The following functions help you to work with cloud resources. They are helper functions that encapsulate logic to help create and delete resources in a pre-defined way, saving you from having to implement the logic in your code.
This function provisions the resource passed in as a resource declaration. A resource declaration is a description of what a resource ought to be and consists of a hash which defines the resource namespace, type and fields which characterize the resource.
The general behavior is that this function creates the resource and waits until it is in a usable state -- what exactly that means for each resource is a bit different. The following table lists the behavior of the provision function for each RightScale resource.
Resource | Behavior | Failure Behavior |
Server | Create the Server Launch the Server Wait for the Server to become "operational" | Terminate the Server Wait for the Server to terminate Delete the Server |
ServerArray | Create the ServerArray Enable the ServerArray Launch the ServerArray Wait for at least the "min_count" number of instances to become "operational" | Terminate all instances in the ServerArray Wait for all instances to terminate Delete the ServerArray |
Instance | Create the Instance Launch the Instance Wait for the Instance to become "running" | Terminate the Instance Wait for the Instance to terminate Delete the Instance |
IPAddress | Create the IPAddress | N/A |
IPAddressBinding | Create the IPAddressBinding | N/A |
Volume | Create the Volume Wait for the volume to become "available" | Delete the Volume |
VolumeSnapshot | Create the VolumeSnapshot | N/A |
VolumeAttachment | Create the VolumeAttachment | N/A |
This function cleans up and deletes the specified resource. Generally speaking, this function will take care to trigger the resource to be decommissioned and then remove the resource altogether -- the specific actions for each resource are a bit different. The following table lists the behavior of the delete function for each RightScale resource.
Resource | Behavior |
Server | Terminate the Server Wait for the Server to terminate Delete the Server resource |
ServerArray | Disable the ServerArray Terminate all instances in the ServerArray Wait for all instances to terminate Delete the ServerArray |
Instance | Terminate the Instance Wait for the Instance to terminate Delete the Instance |
IPAddress | Delete the IPAddress |
IPAddressBinding | Delete the IPAddressBinding |
Volume | Delete the Volume |
VolumeSnapshot | Delete the VolumeSnapshot |
VolumeAttachment | Delete the VolumeAttachment |
The following set of functions all evaluate to true or false. They provide an alternate notation to using operators that may lend itself better when writing declarative code (e.g. when defining the fields of a declaration).
This function checks whether both arguments are equals, it is equivalent to doing left == right.
Arguments
Position | Possible Values | Required | Default Value |
---|---|---|---|
1 | resource collection | yes | none |
2 | resource collection | yes | none |
Result
true or false.
Examples
$a = 1 $b = 2 $c = 1 equals?($a, $b) == false equals?($a, $c) == true
These functions apply the logical and or logical or operators respectively to their arguments. They are equivalent o $left && $right and $left || $right respectively.
Arguments
Position | Possible Values | Required | Default Value |
---|---|---|---|
1 | value | yes | none |
2 | value | yes | none |
Result
true or false.
Examples
$anything_but_null = "foobar" logic_and($anything_but_null, false) == false logic_and($anything_but_null, true) == true logic_and($anything_but_null, null) == false logic_and(null, null) == false logic_or($anything_but_null, false) == true logic_or($anything_but_null, true) == true logic_or($anything_but_null, null) == true logic_or(null, null) == false
Applies the logical not operator.
Arguments
Position | Possible Values | Required | Default Value |
---|---|---|---|
1 | value | yes | none |
Result
true or false.
Examples
logic_not(true) == false logic_not(false) == true $any_value_but_null = "foobar" logic_not($any_value_but_null) == false logic_not(null) == true
The following set of functions are used to convert values from one type to another.
Convert value to string. The semantic for each type is summarized in the following table:
Type | Result | Example |
---|---|---|
string | no change | to_s("foo") == "foo" |
number | string representation of number | to_s(1) == "1" |
boolan | string representation of boolean | to_s(true) == "true" |
datetime | string representation of datetime | to_s(d"1/1/2012 6:59") == "1/1/2012 6:59" |
null | string "null" | to_s(null) == "null" |
array | JSON representation of array | to_s([1, 2, "3"]) == "[1,2,\"3\"]" |
hash | JSON representation of an hash | to_s({ "one": 1, "two": 2, "three": 3 }) == "{\"one\": 1, \"two\": 2, \"three\": 3}" |
Convert a value to a number. The only types that can be converted to numbers are strings, booleans, and datetimes. Attempting to convert a value of a different type (e.g. an array) will result in an error.
Type | Result | Example |
---|---|---|
string | corresponding number or 0 if string does not represent a number | to_n("1.23123") == 1.23123; to_n("foo") == 0 |
number | no change | to_n(1) == 1 |
boolan | 1 for true, 0 for false | to_n(true) == 1 |
datetime | number of seconds since the epoch | to_n(d"1/26/2012 1:49:35") == 1327542575 |
null | 0 | to_n(null) == 0 |
Convert value to boolean. The only types that can be converted to booleans are strings and numbers. Attempting to convert a value of a different type (e.g. an array) will result in an error.
Type | Result | Example |
---|---|---|
string | true if string is "true", false otherwise | to_b("true") == true; to_b("foo") == false |
number | true if non 0, false otherwise | to_b(42) == true; to_b(0) == false |
boolean | no change | to_b(true) == true |
Convert value to datetime. The only types that can be converted to datetimes are strings and numbers. Attempting to convert a value of a different type (e.g. an array) will result in an error. The accepted syntax for strings representing datetime is:
year/month/day [h:m[:s]] [AM|PM]
Trying to coerce a string that does not match this syntax to a datetime value results in an error.
Type | Result | Example |
---|---|---|
string | Corresponding datetime if syntax is correct, error otherwise | to_d("1/1/2012") == d"1/1/2012" |
number | datetime with corresponding unix timestamp | to_d(42) == d"1/1/1970 00:00:42" |
datetime | no change |
Convert hash to array of pairs. Converting a value that is not a hash will result in an error.
Type | Result | Example |
---|---|---|
hash | Corresponding array of pairs, ordering is random | to_a({ "one": 1, "two": 2, "three": 3 }) == [ ["two", 2], ["one", 1], ["three", 3] ] |
Convert given resource declaration or resource collection into a JSON object. Especially useful to convert a declaration into an object, manipulate that object and assign it back to a declaration so that e.g. provision() may be called on it.
Type | Result | Example |
---|---|---|
collection |
JSON object containing declaration or collection fields. Note that objects created from declarations may be assigned back to a reference. | $data = to_object(@servers) |
Convert a value into a JSON string.
Type | Result | Example |
---|---|---|
any | RCL value | to_json({ "one": 1, "two": 2, "three": 3 }) == '{"one":1,"two":2,"three":3}' |
Convert a string value into a RCL value.
Type | Result | Example |
---|---|---|
string | JSON string | from_json('{"one":1,"two":2,"three":3}') == { "one": 1, "two": 2, "three": 3 } |
This function returns a resource collection made of a single resource: the running process.
Arguments
None
Result
A resource collection consisting of a single element: the current process.
Returns the global name of the current task. See Cloud Workflow Processes for information on tasks.
Arguments
None.
Result
A string that represents the global name of the current task.
Examples
concurrent do sub task_name: launch_app do $servers = rs.tags.by_tag(resource_type: "servers", tags: ["app:name=foo" ]) $arrays = rs.tags.by_tag(resource_type: "server_arrays", tags: [ "app:name=foo" ]) concurrent do sub task_name: launch_servers do $servers.launch() $name = task_name() # $name == launch_app/launch_servers end sub task_name: launch_arrays do $arrays.launch() $name = task_name() # $name == launch_app/launch_arrays end end end sub task_name: notify do # ... do things end end
Arguments
None.
Result
Returns a hash of task status keyed by global task name. Valid values for a task status are: completed, aborted, canceled, paused, or running.
Returns the status of the given task. The task name can be relative or absolute (relative match is tried first then absolute if there is no task with a relative name matching the argument).
Arguments
Position | Possible Values | Required | Default Value | Comment |
---|---|---|---|---|
1 | strings | yes | none | Name of task whose status should be returned |
Result
completed, aborted, canceled, paused or running. Returns null for non-existent tasks.
Returns the name of the type of the given argument. This function is mainly meant to help developing and debugging processes. If the object is a value then the possible type names are: string, number, boolean, datetime, null, array or hash. If the object is a resource collection then the returned name consist of <namespace>.<resource type>. If the object is a declaration, then function will return declaration.
Arguments
Position | Possible Values | Required | Default Values | Comment |
---|---|---|---|---|
1 | resource collection value declaration | yes | none | Object for which type should be retrieved |
Result
A string representation of the object type.
Examples
@servers = rs.deployments.get(filter: "name==default").servers() $type_name = type(@servers) # rs.servers
The inspect() function returns a human readable representation of the object given as argument. This function is meant as a debugging aid.
Arguments
Position | Possible Values | Required | Default Value |
---|---|---|---|
1 | resource collection value | yes | none |
Result
String representation of the given object.
Examples
@servers = rs.deployments.get(filter: "name==default").servers() log_info(inspect(@servers))
Split given string around matches of the given separator or regular expression.
Arguments
Position | Possible Values | Required | Default Value | Comment |
---|---|---|---|---|
1 | String value | yes | none | |
2 | String value | yes | none | May represent a regular expression |
Result
Array of strings.
Examples
$text = "some-dash-delimited--string--" $values = split($text, "/-+/") # ["some", "dash", "delimited", "string"]
Join elements of array into a single string using given separator if any.
Arguments
Position | Possible Values | Required | Default Value | Comment |
---|---|---|---|---|
1 | string value | yes | none | |
2 | string value | no | -- | Empty string by default |
Result
String.
Examples
$values = ["some", "dash", "delimited", "string"] join($values, "-") == "some-dash-delimited-string" join($values) == "somedashdelimitedstring"
This set of functions provides the ability to call HTTP/HTTPS endpoints from within Cloud Workflow. There is one function for each of the well-known HTTP verbs (get, post, head, put, patch, delete, options, and trace). The functions take all request attributes as parameters and return the response in a hash value with the keys "code", "headers", "cookies", and "body" (each containing their respective response attributes).
There is also a generic function called http_request which takes the verb as a parameter for any type of HTTP/HTTPS call.
When using these functions, there is some specific behavior that is of interest:
The following parameters are available for all verb-specific and generic HTTP functions.
Name | Possible Values | Required | Description | Example | Default | |
---|---|---|---|---|---|---|
headers | hash of string -> string values | no | HTTP request headers, the keys are the classical "content-type" (or "content_type"), "accepts", etc. Passing lowercase with underscore instead of dash is OK, the implementation normalizes to standard HTTP header names behind the scenes. | { "X-Api-Version": "1.0" } | ||
body | string (or any value if content-type is JSON) | no | The request body. When unspecified and the method is one of those that expect a body, will default to "" (empty string). A body can be given for methods that don't require it (GET, HEAD) and it will probably get discarded by the server. | |||
raw_response | boolean | no | The default is false. When false (default) and the response is "application/json" (or an extension of it), the response body will contain the parsed value (not the JSON string). In case of XML content (and unless "raw_response" is set to true), the XML is turned into a JSON-compatible data structure (a representation of the XML tree). | true | false | |
basic_auth | an object with keys "username" and "password" | no | Specifies a pair (username, password) for the basic authentication of this request. |
| ||
cookies | Array of cookie objects | no | An array of cookies to send to the server. Only "name" and "value" are allowed. |
| ||
noredirect | boolean | no | The default is false. By default the http method will follow any redirection. Infinite loops are detected and raise an error. | true | false | |
insecure | boolean | no | The default is false. By default the http method will verify SSL certificates. When set to true, the check is not done. | true | false | |
signature | an object with keys "type", "access_key", and "secret_key" | no | Used for signing requests for AWS. The type should be "aws". The "access_key" and "secret_key" fields specify the AWS credentials to use for signing the request. If the "access_key" or "secret_key" fields are not provided, the default AWS credentials (AWS_ACCESS_KEY_ID or AWS_SECRET_ACCESS_KEY) will be used. | { "type": "aws", "access_key": "myaccesskey", "secret_key": "mysecretkey" } |
The return value of every HTTP/S function is a hash with the following elements:
Name | Type | Description | Example | |
---|---|---|---|---|
code | number | The response code. Eg: 200 | 200 | |
headers | hash | The headers from the http response |
| |
cookies | array of objects. | The cookies received in the response | For a cookie received as string, "zz=yy; Domain=.foo.com; path=/; secure", it will be parsed as follows
| |
body |
| The body of the response |
http_get($params), http_post($params), http_head($params), http_put($params), http_patch($params), http_delete($params), http_options($params), http_trace($params)
Name | Possible Values | Required | Description | Example | Default |
---|---|---|---|---|---|
url | string | yes | The url for the request including the scheme (http/https), host, href, as well as query strings. | "https://www.googleapis.com/drive/v2/...iewedDate=true" |
http_request($params)
The general function has the following parameters, in addition to the "Common Parameters" listed above. The return value from these functions is documented above in "HTTP Function Responses".
Name | Possible Values | Required | Description | Example | Default |
---|---|---|---|---|---|
verb | string | yes | The HTTP verb (should be one of get, post, patch, put, delete, options, head) | "get" | |
host | string | yes | The host of the external service | "www.googleapis.com" | |
https | boolean | no | Whether to https/http | true | false |
href | string | no | The href of the target resource relative to the host. | "/drive/v2/files/123" | "" |
query_strings | hash | no | Query-string values (what comes after a "?" in the URL). Keys must be strings. Values are turned into strings (arrays and hashes are JSON encoded). All the values are escaped. | { "updateViewedDate": true } | {} |
Returns the current time in a datetime value.
Arguments
None.
Result
Date time value that represents the current time in UTC.
Returns a string containing a Universally Unique IDentifier.
Arguments
None.
Result
A string containing a UUID.
Makes the current task sleep for the time expressed in seconds.
Arguments
Position | Possible Values | Required | Default Value | Examples |
---|---|---|---|---|
1 | duration | yes | none | sleep(60) # sleep one minute |
Result
None.
This function takes an expression that resolves to a value as argument. If the expression does not resolve to a value, then an error is raised. Sleeps until the expression evaluates to a value that is neither null nor false.
Arguments
Position | Possible Values | Required | Default Value | Comment |
---|---|---|---|---|
1 | expression | yes | none | The expression that should return a value that is neither null nor false for the wait to stop |
Result
None.
This function takes an expression that resolves to a value as argument. If the expression does not resolve to a value then an error is raised. Sleeps while the expression evaluates to a value that is neither null norfalse.
Arguments
Position | Possible Values | Required | Default Value | Comment |
---|---|---|---|---|
1 | expression | yes | none | the expression that is run |
Result
None.
Return an array made of all the keys of the provided hash.
Arguments
Position | Possible Values | Required | Default Value | Comment |
---|---|---|---|---|
1 | hash | yes | none | Hash for which keys should be retrieved |
Result
Array of strings representing all the keys of the provided hash.
Return an array made of all the values of the provided hash.
Arguments
Position | Possible Values | Required | Default Value | Comment |
---|---|---|---|---|
1 | hash | yes | none | Hash for which values should be retrieved |
Result
Array of strings representing all the values of the provided hash.
Return the value of a two level hash given a key and value name. This function is syntactic sugar around the [] operator. It may provide a better notation when writing declarative code e.g. in resource declarations. So doing map($hash, $key, $value) is equivalent to $hash[$key][$value].
Arguments
Position | Possible Values | Required | Default Value | Comment |
---|---|---|---|---|
1 | hash | yes | none | Hash for which value should be retrieved |
2 | string | yes | none | Name of key |
3 | string | yes | none | Name of value |
Result
Hash value at given key and value name.
Using a XPath string, extracts information out of an XML string.
Arguments
Position | Possible Values | Required | Default Value | Comment |
---|---|---|---|---|
1 | string | yes | none | XML string |
2 | string | yes | none | XPath string like "//item[2]/name/text()" or "css:div.li" path |
The xpath argument can be passed a "css:" prefix, in which case the path is a CSS path, not a XPath.
Result
Array of [XML] strings.
RCL | Resources | Cloud Workflows & Definitions | Variables | Attributes & Error Handling | Branching & Looping | Processes | ► Functions | Operators | Mapping |
© 2006-2014 RightScale, Inc. All rights reserved.
RightScale is a registered trademark of RightScale, Inc. All other products and services may be trademarks or servicemarks of their respective owners.