Using JSON for webapplications (PHP)All interactions happens via the file /webcommand.php. This is the file that handles the API for both Kloxo and HyperVM. You can access them as:
http://server:7778/webcommand.php?login-class=clientThe next step is the listing. This is essential because, to add one resource, you will need to provide another as the input, and to do this, you will need to first get the list of the existing resources in Kloxo. You can see
API#Listing Resources Example for more information on that.
Once you have understood the Kloxo JSON object model and how to list any resource, we can move to the next step:
Adding a resource with JSON
Kloxo & HyperVM has a universal method to add any resource object: it is via the action
add. Two parameters are always universally needed:
class and
name. The
class can be anything: client, domain, vps, subdomain etc. Rest of the parameters like 'v-plan_name', 'v-type' will depend on the
class of the object you are trying to add.
We will be focussing here on how to add a client on Kloxo and a VM on HyperVM. For HyperVM, you shouldn't add a client, unless the client has more than 1 VM.
Add a Client to Kloxo
Adding happens via the action
add. To add a client to Kloxo, the parameters you provide are:
- Parameters:
- action = add
- class = client
- name = name
- v-plan_name = resourceplan_name
- v-type = customer/reseller
- v-contactemail = client@lxcenter.org
- v-send_welcome_f = on/off
- v-domain_name = name of the default domain you add with the client
- v-dnstemplate_name = name of the dnstemplate to be used to create the domain.
To provide the v-plan_name, you need to first get the list of resource plans in the account. We have written above a high level function called lxlabs_get_and_print_select_variable see
API#Function lxlabs_get_and_print_select_variable that will get the list of resources, and print a select box.
You can also provide a domain name, in which case a domain will be added to the client's account. To add a domain when adding a client, you have to provide two variables:
v-domain_name and
v-dns_template. You get the dns template by using the "function Listing Resources Example" we explained above
API#Listing Resources Example to list the dns templates.
<form method=post action=$server:7778/bin/webcommand.php>
<input type=hidden name=login-class value=client>
<input type=hidden name=login-name value=admin>
<input type=hidden name=login-password value=lxlabs>
<input type=hidden name=action value=add>
<input type=hidden name=class value=client>
<input type=hidden name=v-type value=customer>
Client name: <br>
<input type=text name=name value=> <br>
Contact Email <br>
<input type=text name=v-contactemail value=> <br>
<input type=hidden name=v-send_welcome_f value=on> <br>
Password<br>
<input type=text name=v-password value=> <br>
<input type=submit name=submit value=submit> <br>
Add a VM to HyperVM
The variables needed are:
- Variables
- action: add
- class: vps
- name: name
- v-type: openvz/xen
- v-num_ipaddress_f: number of ipaddresses
- v-contactemail: contact email address
- v-send_welcome_f: on/off
- v-ostemplate: OS template to use
- v-syncserver: The server on which to configure the vps
- v-plan_name: The resource plan to use
Of the above parameters,
v-ostemplate,
v-syncserver and
v-plan_name has to be selected from a list of values. To get the list of values, please see
API#Listing Resources Example. For instance, to let the customer select from a list of available OStemplates, you have to first query the HyperVM server and get a list of availalbe OStemplates for OpenVZ.
v-send_welcome_f is a flag variable that tells HyperVM whether to send welcome message to the client or not. The values are either 'on' or off'.
$ostemplate_list_json = lxlabs_get_via_json("http", $server", "8888", "action=simplelist&resource=ostemplate_openvz");
$syncserver_list_json = lxlabs_get_via_json("http", $server", "8888", "action=simplelist&resource=vpspserver_openvz");
Below is a webform that can directly add a VM to HyperVM. We are directly using the high level function
API#Function lxlabs_get_and_print_select_variable to automatically query the HyperVM server to get a list of available resources and automatically generate a select box.
<input type=hidden name=login-class value=client>
<input type=hidden name=login-name value=admin>
<input type=hidden name=login-password value=hell>
<input type=hidden name=action value=add>
<input type=hidden name=class value=vps>
<input type=hidden name=v-type value=openvz>
VM Type: openvz <br>
VM name: <br>
<input type=text name=name value=> <br>
Number of IP addresses <br>
<input type=text name=v-num_ipaddress_f value=> <br>
Contact Email <br>
<input type=text name=v-contactemail value=> <br>
<input type=hidden name=v-send_welcome_f value=on> <br>
Password<br>
<input type=text name=v-password value=> <br>
<?php
lxlabs_get_and_print_select_variable("Ostemplate", "ostemplate_openvz", "v-ostemplate");
lxlabs_get_and_print_select_variable("Server", "vpspserver_openvz", "v-syncserver");
lxlabs_get_and_print_select_variable("Plan", "resourceplan", "v-plan_name");
?>
<input type=submit name=submit value=submit> <br>