OpenNebula PHP API


(Harpreet Singh) #1

I’m PHP developer and making a whmcs module for create VM with opennbula. So, i need some help from you. Can you please share any guide/doc for API connection with opennbula portal and also let me know, what things are requirted for API connection.


(Martin) #2

I think this covers most of what you need.

https://docs.opennebula.org/5.6/integration/system_interfaces/api.html


(Harpreet Singh) #3

Thank you.

I’m using this “https://github.com/tomasliubinas/phponeapi/blob/master/README.md” method for API connection. When i perform any action like “one.hostpool.info”. I’m getting connection refused error with port 2633 and 5443.

What is the reason?

Thanks


(Kristian Feldsam) #4

hello, lst time I used this simple client https://github.com/iddqdby/phponeapi

some example

<?php
require_once 'vendor/autoload.php';

$one = new \PHPOneAPI\Client('username', 'password', 'api-address', false);


$vmInfo = new \SimpleXMLElement($one->vmpoolInfo(-2, -1, -1, -1));

foreach($vmInfo as $vm)
{
  if($vm->TEMPLATE->DISK_COST)
  {
    echo $vm->NAME,' id: ',$vm->ID,' has disk cost set to: ',$vm->TEMPLATE->DISK_COST,"\n";
  }
}

die;

$groupInfo = new \SimpleXMLElement($one->groupInfo(111));

$users = $groupInfo->USERS;
if( ! is_array($users))
{
    $users = array($groupInfo->USERS);
}

foreach ($users as $user) {
    $result = $one->vmpoolShowback((int) $user->ID, 6, 2018, 7, 2018);

    $xml = new \SimpleXMLElement($result);

    print_r($xml);
}

(Harpreet Singh) #5

Thanks Feldsam.

One more thing, can i use API token instead of password?


(Kristian Feldsam) #6

I don’t know, try it


(Harpreet Singh) #7

Hi,

I’ve sussfully made the API connection using token. Can you have any example for create new VM from template with new resources values like “CPU, RAM, HDD, Vmname, etc”?

I’ve tried to found any PHP example, but i did not get any example. Please help me to create VM with auto configured resorces values.

Thanks


(Kristian Feldsam) #8

hello, there is no example like this in PHP, but have to read API docs

I that linked docs you can see onevm table. There are some methods, like one.vm.deploy or one.vmpool.info. When you wanna call some of methods in docs, you have to change it to vmDeploy or vmpoolInfo. So basically, you remove one. use string to next dot vmpool them remove dot and use next string with capital Info. So one.vm.deploy = vmDeploy, one.vmpool.info = vmpoolInfo. Hopu you understand.

Next you need go thought docs and find how to use that oneDeploy method. You can use your browser find function (ctrl+f) and find for ex. one.vm.deploy and you will see method description, arguments (IN), return values (OUT) and so on.

My advice - to create new VM you need to use one.vm.allocate


(Harpreet Singh) #9

Thank you so much Kristian Feldsam :slight_smile:


(Harpreet Singh) #10

Hi,

I need your help again. I want to get VM logs. Please let me know, is there any API to get OpenNebula VM logs?

And also please let me know, there is any way to resize RAM,HDD, CPU while deploying the VM? Beacuse without poweroff, i can not resize the RAM, CPU. If i poweroff then it take time to poweroff. I don’t know how much time it will take to poweroff the VM.

Thanks


(Kristian Feldsam) #11

hello, did you look into docs? on that one.vm.allocate method? What is second input param? A string containing the template for the vm. Syntax can be the usualattribute=valueor XML.

So yes, of course, you can set cpu, ram, disk size and other params on VM create.

Poweroff time of VM depends… on OS, on services running, if ACPI is configured etc…

Looks like you are not good in programming, no offense, but this forum is about OpenNebula, you can find better help on other forums related to programming. I provide you with links to docs, explained some things, but it is about programming and reading docs now…