How to Consume Microservice Methods from Workflow

Thanks to the MSActivator's DevOps friendly nature, consuming these APIs (explained in Microservices Order Command REST API) in Workflows will no longer be a pain point.

There are APIs ready to be consumed using simple PHP methods that are already implemented in the MSActivator library. There are two methods: one to consume the import method and the other to consume the remaining three methods.

Consume IMPORT Method from Workflow

In the MSActivator repository, under Reference -> Common → Library, there is a PHP file named "msa_common.php". In this file a method is implemented to invoke an import method of a Microservice definition. This definition is of a particular MSActivator device. More specifically:

  • First, an empty array object has to be initialized in the PHP by using the "Microservice definition" name without the .xml (as explained in the http body of the api [root element name]).
  • Then invoke import_objects($msa_device_seq_num, $initialized_array)

As an example, assume there is a Microservice definition to manage all VM instances on an OpenStack cloud with "servers.xml" as its name. To import all the instances of servers' Microservice definitions, do the following in the PHP implementation of the task:

$arr= array("servers");
$response = import_objects($openstack_device_id, $array);

$openstack_device_id is the numeric part of the MSActivator device ID, representing the OpenStack device in the MSActivator.

The $response variable contains the encoded JSON object response, as mentioned in the http response in Microservices Order Command REST API. For a more simplified usage of that encoded JSON, it can be decoded into an array variable in PHP, as shown below:

$response = json_decode($response, true);
    if ($response['wo_status'] !== ENDED) {
        //There is some error while executing import_objects() library method, hence the 'wo_status' value is not "ENDED"
        $response = json_encode($response);
        return $response;
    }
//The response in now decoded into array variable, now extract the interested section of the array
$servers = $response['wo_newparams']["servers"];

The $servers array can now be accessed/iterated as shown below:

foreach ($servers as $server_object_id => $server_params) {
//for each servers instance extract instance variables, for example is name is an instance variable in servers microservice
        $name = $server_params['name'];
}

Consume UPDATE/CREATE/DELETE Methods of Microservices from Workflow

Under Reference -> Common -> Library in the MSActivator repository, there is a PHP file with name msa_common.php. In this file a method is implemented to consume the remaining methods of Microservices.

execute_command_and_verify_response ($device_id, $command_name, $object_parameters, $comment, $connection_timeout = 60, $max_time = 60).

Where:
$device_id : is the numeric part of the MSActivator device ID on which you need to invoke microservice methods.
$command_name : is the same as the {commandName} mentioned in How to Manage Microservices from REST API.
$object_parameters : is the decoded JSON object in PHP array variable.

For example, if the http body has to have the following JSON data:

{'address_ip_netmask':{
    first_address":{
        "address":"10.20.30.40",
        "masklen":"2"
        }
    }
}

The equivalent $object_parameters will have to be defined such as in the following in PHP:

$object_parameters = array('address_ip_netmask' =>               
    array("first_address" =>
        array('address' => "10.20.30.40",
              'masklen' => "24"
             )
         )
     );

Then, invoke the method in the PHP implementation of the task as shown below:

$response = execute_command_and_verify_response($device_id, 'CREATE', $object_parameters, "CREATE IP NETMASK", 180, 180);