Skip to content

Commit

Permalink
Rename Client in PveClient, improve Result
Browse files Browse the repository at this point in the history
  • Loading branch information
franklupo committed Oct 8, 2019
1 parent 744d8b7 commit 886408f
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 47 deletions.
32 changes: 16 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

[![License](https://img.shields.io/github/license/Corsinvest/cv4pve-api-php.svg)](https://www.gnu.org/licenses/gpl-3.0.en.html) ![Packagist Version](https://img.shields.io/packagist/v/corsinvest/cv4pve-api-php.svg) [![Donate to this project using Paypal](https://img.shields.io/badge/paypal-donate-yellow.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=PPM9JHLQLRV2S&item_name=Open+Source+Project&currency_code=EUR&source=url)

ProxmoVE Client API PHP
Proxmox VE Client API PHP

[ProxmoxVE Api](https://pve.proxmox.com/pve-docs/api-viewer/)
[Proxmox VE Api](https://pve.proxmox.com/pve-docs/api-viewer/)

[Packagist](https://packagist.org/packages/Corsinvest/cv4pve-api-php)

Expand All @@ -26,17 +26,17 @@ Corsinvest for Proxmox VE Api Client (Made in Italy)

## General

The client is generated from a JSON Api on ProxmoxVE.
The client is generated from a JSON Api on Proxmox VE.

This PHP 5.4+ library allows you to interact with your Proxmox server via API.
The client is generated from a JSON Api on ProxmoxVE.
The client is generated from a JSON Api on Proxmox VE.

## Result

The result is class **Result** and contain methods:

* **getResponse()** returned from ProxmoxVE (data,errors,...) Object/Array
* **responseInError** (bool) : Contains errors from ProxmoxVE.
* **getResponse()** returned from Proxmox VE (data,errors,...) Object/Array
* **responseInError** (bool) : Contains errors from Proxmox VE.
* **getStatusCode()** (int) : Status code of the HTTP response.
* **getReasonPhrase()** (string): The reason phrase which typically is sent by servers together with the status code.
* **isSuccessStatusCode()** (bool) : Gets a value that indicates if the HTTP response was successful.
Expand All @@ -45,7 +45,7 @@ The result is class **Result** and contain methods:
## Main features

* Easy to learn
* No dependency external lybrary only native curl
* No dependency external library only native curl
* Method named
* Method no named rest (same parameters)
* getRest
Expand All @@ -59,15 +59,15 @@ The result is class **Result** and contain methods:
* Tree structure
* $client->getNodes()->get("pve1")->getQemu()->get(100)->getSnapshot()->snapshotList()->getResponse()->data
* Return data proxmox
* Return result status
* getStatusCode
* getReasonPhrase
* isSuccessStatusCode
* Return result
* Request
* Response
* Status
* Wait task finish task
* waitForTaskToFinish
* taskIsRunning
* getExitStatusTask
* Method directry access
* Method direct access
* get
* set
* create
Expand Down Expand Up @@ -106,7 +106,7 @@ Or add this to your `composer.json` file:
// Require the autoloader
require_once 'vendor/autoload.php';

$client = new Corsinvest\ProxmoxVE\Api\Client("192.168.0.24");
$client = new Corsinvest\ProxmoxVE\Api\PveClient("192.168.0.24");

//login check bool
if($client->login('root','password','pam')){
Expand Down Expand Up @@ -146,14 +146,14 @@ if($client->login('root','password','pam')){
var_dump($retArr);
echo "\n" . $retArr['data']['release'];

//eneble return objet
//enable return objet
$client->setResultIsObject(true);

//image rrd
$client->setResponseType('png');
echo "<img src='{$client->getNodes()->get("pve1")->getRrd()->rrd('cpu','day')->getResponse()}' \>";

//resewt json result
//result json result
$client->setResponseType('json');
var_dump($client->get('/version')->getResponse());
}
Expand Down Expand Up @@ -208,4 +208,4 @@ The parameter indexed end with '[n]' in documentation (method createVM in Qemu p
1 => "....",
3 => "....",
]
```
```
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
{
"name": "corsinvest/cv4pve-api-php",
"description": "Corsinvest ProxmoVE Client API PHP",
"description": "Corsinvest Proxmox VE Client API PHP",
"keywords": [
"Proxmox",
"ProxmoxVE",
"Proxmox VE",
"API",
"KVM",
"LXC",
"Virtualization",
"Cluster",
"Client API",
"Corsinvest"
],
"type": "library",
Expand Down
22 changes: 20 additions & 2 deletions src/Client.php → src/PveClient.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
<?php

/*
* This file is part of the cv4pve-api-php https://github.com/Corsinvest/cv4pve-api-php,
* Copyright (C) 2016 Corsinvest Srl
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

/**
* Proxmox VE Client Api
*/
Expand All @@ -12,15 +30,15 @@
*
* Proxmox VE Client
*/
class Client extends ClientBase {
class PveClient extends PveClientBase {

/**
* @ignore
*/
private $client;

/**
* Client constructorctor.
* Client constructor.
* @param string $hostname Host Proxmox VE
* @param int $port Port connection default 8006
*/
Expand Down
90 changes: 69 additions & 21 deletions src/ClientBase.php → src/PveClientBase.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
<?php

/*
* This file is part of the cv4pve-api-php https://github.com/Corsinvest/cv4pve-api-php,
* Copyright (C) 2016 Corsinvest Srl
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

namespace Corsinvest\ProxmoxVE\Api;

/**
Expand All @@ -8,7 +26,7 @@
*
* Proxmox VE Client Base
*/
class ClientBase {
class PveClientBase {

/**
* @ignore
Expand Down Expand Up @@ -168,47 +186,47 @@ function login($userName, $password, $realm = "pam") {
/**
* Execute method GET
* @param string $resource Url request
* @param array $params Additional parameters
* @param array $parameters Additional parameters
* @return Result
*/
public function get($resource, $params = []) {
return $this->executeAction($resource, 'GET', $params);
public function get($resource, $parameters = []) {
return $this->executeAction($resource, 'GET', $parameters);
}

/**
* Execute method PUT
* @param string $resource Url request
* @param array $params Additional parameters
* @param array $parameters Additional parameters
* @return Result
*/
public function set($resource, $params = []) {
return $this->executeAction($resource, 'PUT', $params);
public function set($resource, $parameters = []) {
return $this->executeAction($resource, 'PUT', $parameters);
}

/**
* Execute method POST
* @param string $resource Url request
* @param array $params Additional parameters
* @param array $parameters Additional parameters
* @return Result
*/
public function create($resource, $params = []) {
return $this->executeAction($resource, 'POST', $params);
public function create($resource, $parameters = []) {
return $this->executeAction($resource, 'POST', $parameters);
}

/**
* Execute method DELETE
* @param string $resource Url request
* @param array $params Additional parameters
* @param array $parameters Additional parameters
* @return Result
*/
public function delete($resource, $params = []) {
return $this->executeAction($resource, 'DELETE', $params);
public function delete($resource, $parameters = []) {
return $this->executeAction($resource, 'DELETE', $parameters);
}

/**
* @ignore
*/
private function executeAction($resource, $method, $params = []) {
private function executeAction($resource, $method, $parameters = []) {
//url resource
$url = "{$this->getApiUrl()}{$resource}";
//$cookies = [];
Expand All @@ -218,7 +236,7 @@ private function executeAction($resource, $method, $params = []) {
}

//remove null params
$params = array_filter($params, function ($value) {
$params = array_filter($parameters, function ($value) {
return null !== $value;
});

Expand All @@ -230,12 +248,14 @@ private function executeAction($resource, $method, $params = []) {
}
}

$methodType = "";
$prox_ch = curl_init();
switch ($method) {
case "GET":
$action_postfields_string = http_build_query($params);
$url .= '?' . $action_postfields_string;
unset($action_postfields_string);
$methodType = "GET";
break;

case "PUT":
Expand All @@ -244,6 +264,7 @@ private function executeAction($resource, $method, $params = []) {
curl_setopt($prox_ch, CURLOPT_POSTFIELDS, $action_postfields_string);
unset($action_postfields_string);
curl_setopt($prox_ch, CURLOPT_HTTPHEADER, $headers);
$methodType = "SET";
break;

case "POST":
Expand All @@ -252,11 +273,13 @@ private function executeAction($resource, $method, $params = []) {
curl_setopt($prox_ch, CURLOPT_POSTFIELDS, $action_postfields_string);
unset($action_postfields_string);
curl_setopt($prox_ch, CURLOPT_HTTPHEADER, $headers);
$methodType = "CREATE";
break;

case "DELETE":
curl_setopt($prox_ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($prox_ch, CURLOPT_HTTPHEADER, $headers);
$methodType = "DELETE";
break;
}

Expand All @@ -279,29 +302,40 @@ private function executeAction($resource, $method, $params = []) {

$obj = null;
switch ($this->responseType) {
case 'json': $obj = json_decode($body, !$this->getResultIsObject());
case 'json':
$obj = json_decode($body, !$this->getResultIsObject());
break;
case 'png': $obj = 'data:image/png;base64,' . base64_encode($body);

case 'png':
$obj = 'data:image/png;base64,' . base64_encode($body);
break;
}
unset($body);

$lastResult = new Result($obj, $reasonCode, $reasonPhrase, $this->resultIsObject);
$lastResult = new Result($obj,
$reasonCode,
$reasonPhrase,
$this->resultIsObject,
$resource,
$parameters,
$methodType,
$this->responseType);

if ($this->getDebugLevel() >= 2) {
echo $obj . "\n";
echo "StatusCode: " . $lastResult->getStatusCode() . "\n";
echo "ReasonPhrase: " . $lastResult->getReasonPhrase() . "\n";
echo "IsSuccessStatusCode: " . $lastResult->isSuccessStatusCode() . "\n";
}
if ($this->getDebugLevel() > 0) {

if ($this->getDebugLevel() > 0) {
echo "=============================";
}
return $lastResult;
}

/**
* Gets the lasr result action
* Gets the last result action
* @return Result
*/
public function getLastResult() {
Expand All @@ -314,8 +348,9 @@ public function getLastResult() {
* @param string $task Task identifier
* @param int $wait Millisecond wait next check
* @param int $timeOut Millisecond timeout
* @return int 0 Success
*/
function waitForTaskToFinish($node, $task, $wait = 500, $timeOut = 10000) {
public function waitForTaskToFinish($node, $task, $wait = 500, $timeOut = 10000) {
$isRunning = true;
if ($wait <= 0) {
$wait = 500;
Expand All @@ -331,6 +366,19 @@ function waitForTaskToFinish($node, $task, $wait = 500, $timeOut = 10000) {
$isRunning = taskIsRunning($node, $task);
}
}

return $timeStart - time() < $timeOut ? 0 : 1;
}

/**
* Wait for task to finish
* @param string $task Task identifier
* @param int $wait Millisecond wait next check
* @param int $timeOut Millisecond timeout
* @return int 0 Success
*/
function waitForTaskToFinish1($task, $wait = 500, $timeOut = 10000) {
return waitForTaskToFinish(split(':', $task)[1], $task, $wait, $timeOut);
}

/**
Expand Down
Loading

0 comments on commit 886408f

Please sign in to comment.