#743: Usar formato JSON en entrada a ruta REST {{{POST /repository/poweron}}}.

git-svn-id: https://opengnsys.es/svn/branches/version1.1@5533 a21b9725-9963-47de-94b9-378ad31fedc9
remotes/github/debian-pkg
ramon 2017-12-14 11:06:36 +00:00
parent b46042c67f
commit fce0af30c3
4 changed files with 17 additions and 61 deletions

View File

@ -114,6 +114,11 @@ $cmd->CreaParametro("@restrambito","",0);
$cmd->CreaParametro("@ordprocedimiento",0,1);
$cmd->CreaParametro("@ordtarea",0,1);
/* PARCHE UHU heredado de la version 1.1.0: Si la accion a realizar es Arrancar incluimos una pagina para arrancar desde el repo */
if($funcion == "nfn=Arrancar".chr(13))
include("wakeonlan_repo.php");
/**/
if($ambito==0){ // Ambito restringido a un subconjuto de ordenadores con formato (idordenador1,idordenador2,etc)
$cmd->ParamSetValor("@restrambito",$idambito);
$idambito=0;

View File

@ -1,6 +1,6 @@
<?php
<?
// Fichero con funciones para trabajar con el webservice
include_once("../../includes/restfunctions.php");
include_once("../../rest/util.php");
/**
En este punto disponemos de tres variables indicando las ips, las macs y las ids de los
@ -50,11 +50,13 @@ foreach($reposAndMacs as $repo => $macs){
if($macs["apikey"] !== ""){
$apiKeyRepo = $macs["apikey"];
unset($macs["apikey"]);
$url = "http://".$repo."/opengnsys/rest/index.php/repository/poweron";
$headers = array('Authorization: '.$apiKeyRepo);
$data = http_build_query(array("macs" => $macs));
$result = callAPI("POST",$url, $data, $headers);
$result = json_decode($result);
// Componer objeto JSON y llamar a la función REST.
$rest[0]['url'] = "https://$repo/opengnsys/rest/repository/poweron";
$rest[0]['header'] = array('Authorization: '. $apiKeyRepo);
$rest[0]['post'] = '{"macs": ["' . implode('","', $macs) . '"]}';
$result = multiRequest($rest);
if (empty($result))
echo "Error de acceso al webservice del repositorio";
}
else{
echo "No hacemos nada, el repositorio no tiene el webservice activo";

View File

@ -69,53 +69,4 @@ function multiRequest($data, $options=array(CURLOPT_SSL_VERIFYHOST => false, CUR
return $result;
}
/**
* @brief Realiza una petición POST, PUT, GET, DELETE a una webservice. Pueden enviarse datos y cabeceras especificas
* @param $method Metodo http (POST, GET, etc)
* @param $url Url del webservice a consultar
* @param $data array de datos a enviar. Ej. array("param" => "value") ==> index.php?param=value
* @param $headers Cabeceras especificas de la peticion. Ej. array('Authorization: "9Ka7wG3EqhcjylUeQXITy0llj2TS8eKe"')
*/
// Method: POST, PUT, GET etc
// Data:
// Ej. callAPI("GET", "http://172.17.11.176/opengnsys/rest/index.php/repository/images?extensions[]=img&extensions[]=sum", array('Authorization: "9Ka7wG3EqhcjylUeQXITy0llj2TS8eKe"'))
function callAPI($method, $url, $data = false, $headers = false)
{
$curl = curl_init();
switch ($method)
{
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_PUT, 1);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
// Optional Authentication:
//curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
//curl_setopt($curl, CURLOPT_USERPWD, "username:password");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
if($headers != false){
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
}
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
?>

View File

@ -183,19 +183,17 @@ $app->get('/repository/image(/:ouname)/:imagename(/)', 'validateRepositoryApiKey
* @return JSON string ok if the power on command was sent
*/
$app->post('/repository/poweron', 'validateRepositoryApiKey',
function() {
$app = \Slim\Slim::getInstance();
function() use($app) {
// Debe venir el parametro macs en el post (objeto JSON con array de MACs)
$data = $app->request()->post();
$data = json_decode($app->request()->getBody());
if(empty($data->macs)){
// Print error message.
$response['message'] = 'Required param macs not found';
jsonResponse(400, $response);
}
else{
$macs = $data->macs;
$strMacs = "";
foreach($macs as $mac){
foreach($data->macs as $mac){
$strMacs .= " ".$mac;
}
// Ejecutar comando wakeonlan, debe estar disponible en el sistema operativo