Pair program with me! profile for carousel at Stack Overflow, Q&A for professional and enthusiast programmers

4/27/2014

Laravel with OnApp cloud

Developing in (and for) the cloud has become defacto standard skill for any modern web developer. Without knowing how to, for example, setup Apache server on AWS Linux instance - important tools will be missing in your development toolset.
Many providers are moving to cloud. Fortrabbit (my provider of choice) is offering SSH connection to all application resources, so it is important to become familiar with cloud concepts.

These days I am working on one awesome project - it is basically Laravel SaaS application that connects to cloud servers, with the help of OnApp. OnApp is cloud management system for service providers.

There is no Laravel package for OnApp, but there is great  OnApp PHP wrapper, for API calls to virtual server. That wrapper is just an abstraction over php_curl, which internally handles all HTTP calls to servers, and returns response in one of two forms - XML of JSON.

Now, that PHP wrapper is very nicely designed, and allows easily communication with servers through native API. It has some very neat solutions, that makes pleasure working with API.

I will point some key features, that are crucial for understanding how it works.

First, to make use of wrapper, you will have to include it in your app. First line of code should be call to OnAppInit.php file, which autoloads all dependencies. Something like:

//based on my path
require "/lib/Onapp/OnAppInit.php";


With this line, all classes are autoloaded, and we can move to actual connection to server. This is where the beauty comes. Connection is made with the help of Factory class instance, which implements factory design patterns, that can instantiate all internal OnApp classes. Next, all OnApp classes inherits from base OnApp class, and allows unified interface for all inherited classes. It is done with the help of factory.

To connect to server, just call Factory class like this:


$factory = new OnApp_Factory($server,$user,$password);



Where $server represents full url to server (not just server.com), but requires protocol (http://server.com). If you made mistake, leaving out the protocol type, you can spend days trying to figure out where is the problem :)

After that you are connected, and ready to use API. But with basic connection, you can't go far. Factory class has factory method, that is responsible for instatiation of all other classes.
To make it more clear, here if that factory method (from the main Factory class):

public function factory( $name, $debug = false ) {
    $class_name = 'OnApp_' . $name;

    $result = new $class_name();
    $result->logger->setDebug( $debug );

    $result->setOption( ONAPP_OPTION_DEBUG_MODE, $debug );
    $result->logger->setTimezone();
    $result->version = $this->getAPIVersion();
    $result->options = $this->options;
    $result->_ch     = $this->_ch;
    $result->initFields( $this->getAPIVersion() );
    return $result;
}


Now things are more clear. Factory argument name will become OnApp clas name. Factory method also serves as configuration method, that will return new instance for chaining.

So to return User class, just call this:

$user = $factory->factory("User");

//or some other, useful classes
$user = $factory->factory("Log");

//or
$user = $factory->factory("BillingPlan");


Now, with those instances, you can use OnApp methods, that allows easily working with API. All instances have getList method. User class is used commonly (there have to be users :), so to list all users, you will do:

//list all server users
$user = $factory->factory("User")->getList($user_id);

// or to list all server logs
$user = $factory->factory("Log")->getList($user_id);


Remember that you are connected as user, with the help of base Factory class. Depending on you permisions (user of admin) you can list different users.

In a nutshell, this API allows creating powerfull SaaS applications, from your base code. In my case, all incoming data (in the form of JSON) are part of application, and I can manage users and server state easily from one central point. There is much more then this - you can pull statistics, work with hypervisors, work with virtual machines... It is really rich set of tools for cloud management.

Thanks for reading.

No comments:

Post a Comment