Starting 4.1, everything that provides an API in CloudStack is a PluggableService. This means all APIs exposed by plugins such as ACL, network, hypervisors and even ManagementServer is a PluggableService, and all PluggableService expose an interface "getCommands()" which returns a list of api cmd classes. This helps other components within CloudStack to discover each other, for example ApiServer and ApiDiscoveryService uses this interface to get commands provided by all the PluggableService implementations.

How to write an Apache CloudStack 4.1+ API:

1. Create a directory off of the plugins folder. The name does not matter. Example: cloudstack/plugins/api/timeofday

2. Create a pom.xml in cloudstack/plugins/api/timeofday. Make sure your "parent" relative path points back to plugins/pom.xml, otherwise it won't build. The main entries that matter are the artifactId (cloud-plugin-timeofday), as well as the dependencies.

    <project xmlns="" xmlns:xsi=""
      <name>Apache CloudStack Plugin - TimeOfDay</name>

3. Add a module entry in the modules section of cloudstack/plugins/pom.xml for the new plugin, which will let mvn know that the plugin module needs to be built:


4. Now create a src, target and test directory off of cloud-plugin-timeofday.

5. Create your code hierarchy ie: For this example I use plugins/api/timeofday/src/com/cloud/test

6. Create an interface that extends from PluggableService



public interface TimeOfDayManager extends PluggableService { }

7. Create an implementation of your newly created interface, overriding the getCommands method, and populating it with the classes of each command you want to expose.


import java.util.List;
import java.util.ArrayList;
import org.apache.log4j.Logger;
import javax.annotation.PostConstruct;
import org.springframework.stereotype.Component;
import javax.ejb.Local;

@Local(value = { TimeOfDayManager.class })
public class TimeOfDayManagerImpl implements TimeOfDayManager {
    private static final Logger s_logger = Logger.getLogger(TimeOfDayManagerImpl.class);

    public TimeOfDayManagerImpl() {

    public List<Class<?>> getCommands() {
        List<Class<?>> cmdList = new ArrayList<Class<?>>();
        return cmdList;

8. Write a command class that implements the correct annotations for a command: (Note: (1) by using authorized annotation, we can enabled this API by default for a given set of role types, (2) by using the validations annotation on @Parameter field we can perform ApiArgValidator.NotNullOrEmpty checks on string types and ApiArgValidator.PositiveNumber on number types such as short, int, long.)


import javax.inject.Inject;
import org.apache.log4j.Logger;

import org.apache.cloudstack.api.ApiArgValidator;
import org.apache.cloudstack.api.BaseCmd;
import org.apache.cloudstack.api.APICommand;
import org.apache.cloudstack.api.Parameter;
import org.apache.cloudstack.acl.RoleType;
@APICommand(name = "getTimeOfDay", description="Get Cloudstack's time of day", responseObject = GetTimeOfDayCmdResponse.class, includeInApiDoc=true, authorized = {RoleType.Admin, RoleType.ResourceAdmin, RoleType.DomainAdmin, RoleType.User})
public class GetTimeOfDayCmd extends BaseCmd {
    public static final Logger s_logger = Logger.getLogger(GetTimeOfDayCmd.class.getName());
    private static final String s_name = "gettimeofdayresponse";

    @Parameter(name="example", type=CommandType.STRING, required=false, description="Just an example string that will be uppercased",  validations = {ApiArgValidator.NotNullOrEmpty})
    private String example;

    public String getExample() {
        return this.example;

    public void execute()
        GetTimeOfDayCmdResponse response = new GetTimeOfDayCmdResponse();
        response.setObjectName("timeofday"); // the inner part of the json structure
        response.setResponseName(getCommandName()); // the outer part of the json structure


    public String getCommandName() {
        return s_name;

    public long getEntityOwnerId() {
        return 0;

9. Write a Response class for the command:


import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.BaseResponse;

import java.util.Date;
import java.text.SimpleDateFormat;

public class GetTimeOfDayCmdResponse extends BaseResponse {
    @SerializedName(ApiConstants.IS_ASYNC) @Param(description="true if api is asynchronous")
    private Boolean isAsync;
    @SerializedName("timeOfDay") @Param(description="The time of day from CloudStack")
    private String  timeOfDay;
    @SerializedName("exampleEcho") @Param(description="An upper cased string")
    private String  exampleEcho;

    public GetTimeOfDayCmdResponse(){
        this.isAsync   = false;

        SimpleDateFormat dateformatYYYYMMDD = new SimpleDateFormat("yyyyMMdd hh:mm:ss");
        this.setTimeOfDay( (new StringBuilder( dateformatYYYYMMDD.format( new Date() ) )).toString() );

    public void setAsync(Boolean isAsync) {
        this.isAsync = isAsync;

    public boolean getAsync() {
        return isAsync;

    public void setTimeOfDay(String timeOfDay) {
        this.timeOfDay = timeOfDay;

    public void setExampleEcho(String exampleEcho) {
        this.exampleEcho = exampleEcho.toUpperCase();

10. Update client/tomcatconf/ and add your new manager to its configuration:

<bean id="timeOfDayManagerImpl" class=""> </bean>

The above is now obsolete (at least since 4.5.0, please fix the exact version here). Instead in your plugin folder you need to add this context, for example in this case:

  a. Add under plugins/api/timeofday/resources/META-INF/cloudstack/timeofday with following content:


b. Add spring-timeofday-context.xml under plugins/api/timeofday/resources/META-INF/cloudstack/timeofday with following content:

<beans xmlns=""
    xmlns:xsi="" xmlns:context=""
<bean id="timeOfDayManagerImpl" class=""> </bean>

11. For CloudStack version 4.8.x and below update client/tomcatconf/, and add the command name (for the example this would be getTimeOfDay as stated in the @APICommand annotation). For CloudStack 4.9.x and above, the way has been deprecated and API authors are encouraged to enable their API by default using authorized annotation as in the example above which can be used to enable the API for a given set of user role types.

12. Along with this you need to add your plugin as a dependency to client/pom.xml. You will see examples within the pom, you will require your plugins artifact ID.

13. You can test your new command via the browser, cloudmonkey or even curl. Here is a simple perl script that shows how to test the new getTimeOfDay command.

#!/usr/bin/env perl
use strict;
use Data::Dumper;
use LWP::Simple;
use Mojo::JSON 'j';
my $content = j(get("http://localhost:8096/client/api?command=getTimeOfDay&response=json&example=moo"));
print Dumper($content);

And here is the output

$VAR1 = {
          'gettimeofdayresponse' => {
                                    'timeofday' => {
                                                   'timeOfDay' => '20130404 09:24:00',
                                                   'exampleEcho' => 'MOO',
                                                   'isasync' => bless( do{\(my $o = 0)}, 'Mojo::JSON::_Bool' )
  • No labels