Introduction to Embedded Methods
Last updated
Last updated
A useful new feature with CloudForms 4.6 (ManageIQ Gaprindashvili) is the ability to be able to create reusable libraries of Ruby automation methods, and include (embed) these into other Ruby methods to use. This promotes code re-use, makes methods smaller, avoids code duplication, and simplifies testing.
An embedded method must be added to a calling method's definition in the WebUI before it can be used. In the Embedded Methods section of the Automate method editor screen, click the Add Method button and browse to the location of the method to embed (see screenshot Adding an Embedded Method).
Once the embedded method has been added, any of its contained methods are available for the calling method to use.
Embedded methods (often called library methods) can be written in several different ways, depending on their intended use. Some use the nested module structure described in Defining Automate Methods as Classes, and as classes containing class methods, although this is not mandatory. Each style of writing has its advantages. The most common styles are as follows.
Encapsulating the embedded methods in a class with its own initializer allows the class to be unit tested by allowing the injection of a mock $evm
into the @handle
variable, as follows:
Embedded methods written in this way can be invoked in a calling method as follows:
Alternatively the embedded method can be written as a straightforward class method, as follows:
These embedded methods can be invoked in the following way in a calling method:
Note
For unit testing, class methods can also optionally take a handle, for example:
def self.log_and_exit(msg, exit_code, handle = $evm)
The code could be written as a mixin without a class, as follows:
The calling method must include the embedded module's module path, which imports the embedded methods into its own namespace. The embedded methods can then be invoked without specifying their module path, for example:
Alternatively the embedded methods can be written as simple bare method definitions, as follows:
These embedded methods can be invoked in the following way in a calling method:
More than one embedded method can be added to an Automate method definition, allowing various library methods to be written in separate modules. The Automation Engine combines all of the embedded methods into a single preamble injected into to the Automate method at run-time. Code will execute in the order that it was listed in the embedded methods list (see screenshot Multiple Embedded Methods).
Note
Embedded methods cannot themselves use embedded methods as there is currently no support for nesting (as of CloudForms 4.6.4 and ManageIQ Gaprindashvili-4). This capability will however be added in a future release.
The use of an embedded method can be traced in automation.log. Before an Automate method is launched the Automation Engine will log Loading embedded method
if any are defined, as follows:
If an embedded method throws an error, the fully qualified method name (Domain/Namespace/Class/Method) and the line number in the method that threw the exception is logged for ease of debugging.
The chapter has introduced embedded methods, and shown how useful they can be to promote code re-use. They can be written in several different styles to suit the coding and unit testing standards in use in an organisation.
The next chapter shows an example of an embedded method that defines two small reusable methods that each use the RHV SDK to connect to a Red Hat Virtualization Manager.