« Général:FAQ API vol fs » : différence entre les versions

De WikiFr_dbSQWare
Aller à la navigation Aller à la recherche
Ligne 214 : Ligne 214 :
===PowerShell===
===PowerShell===
<syntaxhighlight lang="powershell" line>
<syntaxhighlight lang="powershell" line>
$v_static_token = 123456
$staticToken = ""
$v_tempfile = ".\api_dbSQWare.json"
$tempfile    = ".\api_dbSQWare.json"


<#
<#
     f_post_vol_fs_trace create new vol fs trace object
     Create new vol fs trace.


     :param t_data : JSON object data
     :param data : JSON object data
     :return       : request response
     :return     : Request response
#>
#>
function f_post_vol_fs_trace($t_data){
function postVolFsTrace($data) {
     $t_params = @{
     $params = @{
         Method      = "Post"
         Method      = 'Post'
         Uri        = "http://admin-webdba.dbsqware.com/lib/apiAudit.php?type=vol_fs&token=$v_static_token"  
         Uri        = "http://admin-webdba.dbsqware.com/lib/apiAudit.php?type=vol_fs&token=$staticToken"  
         Body        = $t_data | ConvertTo-Json
         Body        = $data | ConvertTo-Json
         ContentType = "application/json"
         ContentType = 'application/json'
     }
     }
     Invoke-RestMethod @t_params -OutFile $v_tempfile
     Invoke-RestMethod @t_params -OutFile $tempfile


     $v_response = Get-Content -Path $v_tempfile | ConvertFrom-Json
    # API request
     $response = Get-Content -Path $tempfile | ConvertFrom-Json


     if(!$v_response.error_message){
     if (-not $response) {
         Write-Output "Insert with success !"
         Write-Output 'Failure of the API request !'
         return 1
         return 0
     }
     }
     else{
 
        $v_error_message = $v_response.error_message
     if ($response.PSObject.Properties.Name -contains 'error_message') {
         Write-Output "Error message [$v_error_message]"
         Write-Output "Error message [$($response.error_message)]"
         return 0
         return 0
     }
     }
}
}


function main{
function main {
     # Post request API
     # Post new vol fs trace
     $t_data = @{
     $data = @{
         dbalias    = "AATestAPI_Upd"
         dbalias    = "AATestAPI_Upd"
         gather_date = "2022-07-22"
         gather_date = "2022-07-22"
Ligne 257 : Ligne 258 :
         mount      = "\/var"
         mount      = "\/var"
     }
     }
     f_post_vol_fs_trace($t_data)
 
     postVolFsTrace($data)
}
}


main
main
</syntaxhighlight>
</syntaxhighlight>

Version du 25 juin 2025 à 16:32

Généralités

Limites de cette section

Cette section n’a pas la prétention de traiter tous les cas possibles d'utilisation de l'API de mise à jour des indicateurs.
Vous devez être à l'aise avec le concept des API et la gestion de token, si ce n'est pas le cas, faites appel au support.
Attention, vous allez mettre à jour les tables de traçage des jobs, vous devez savoir ce que vous faites !

Paramétrage préalable

Afin de pouvoir utiliser l'API des indicateurs, vous devez au préalable avoir paramétré un token statique pour l'instance cible.

Exemples d'utilisation

Vous devez adapter les exemples suivants avec vos propres données (URL, user, password, ...)
Ceci représente une base pour du Python, PHP, Shell et PowerShell.
Vous pouvez bien évidemment utiliser le langage de votre choix.

Python

import requests
import json

staticToken = ""

"""
Does API request.

:param method  : Request method
:param url     : Website url
:param headers : JSON object headers 
:param data    : JSON object data
:return        : API request return
"""
def apiRequest(method, url, headers = {}, data = {}):
    response = requests.request(method, url, headers=headers, data=data)
    return response  

"""
Create new vol fs trace.

:param data : JSON object data
:return     : Request response
"""
def postVolFsTrace(data):
    # Init request
    method  = 'POST'
    url     = f"http://admin-webdba.dbsqware.com/lib/apiAudit.php?type=vol_fs&token={staticToken}"
    headers = {'Content-Type': 'application/json'}
    data    = json.dumps(data)

    # API request
    response = apiRequest(method, url, headers=headers, data=data)
    if response == '':
        print('Failure of the API request !')
        return 0

    response = json.loads(response.text)
    if 'error_message' in response:
        print(f"Error message [{response['error_message']}]")
        return 0
    
    print('Insert with success !')
    return 1

def main():
    # Post new vol fs trace
    data = {
        "dbalias"    : "AATestAPI_Upd",
        "gather_date": "2022-07-21",
        "host_name"  : "tynle204",
        "filesystem" : "\/dev\/export",
        "size_fs"    : "521000.26",
        "used"       : "21000.26",
        "free"       : "500000",
        "mount"      : "\/var"
    }

    postVolFsTrace(data)

if __name__ == '__main__':
    main()

PHP

<?php

$staticToken = "";

/**
 * Does API request.
 * 
 * @param  string  $method : Request method
 * @param  string  $url    : Website url
 * @param  array  $headers : JSON object headers 
 * @param  string  $data   : JSON object data
 * @return string
 */
function apiRequest(string $method, string $url, array $headers = [], $data = '{}'): string
{
    $curl = curl_init();

    curl_setopt_array($curl, [
        CURLOPT_URL            => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING       => '',
        CURLOPT_MAXREDIRS      => 10,
        CURLOPT_TIMEOUT        => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION   => CURL_HTTP_VERSION_1_1,
        CURLOPT_SSL_VERIFYHOST => 0,
        CURLOPT_SSL_VERIFYPEER => 0,
        CURLOPT_CUSTOMREQUEST  => $method,
        CURLOPT_POSTFIELDS     => $data,
        CURLOPT_HTTPHEADER     => $headers,
    ]);

    $response = curl_exec($curl);
    curl_close($curl);

    return $response;
}

/**
 * Create new vol fs trace.
 * 
 * @param  array  $data : JSON object data
 * @return bool
 */
function postVolFsTrace($data): bool
{
    global $staticToken;

    // Init request
    $method  = 'POST';
    $url     = "http://admin-webdba.dbsqware.com/lib/apiAudit.php?type=vol_fs&token={$staticToken}";
    $headers = ['Content-Type: application/json'];
    $data    = json_encode($data, JSON_FORCE_OBJECT);

    // API request
    $response = apiRequest($method, $url, $headers=$headers, $data=$data);
    if (empty($response)) {
        echo('Failure of the API request !');
        return 0;
    }

    $response = json_decode($response, true);
    if (array_key_exists('error_message', $response)) {
        echo("Error message [{$response['error_message']}]");
        return 0;
    }

    echo('Insert with success !');
    return 1;
}

function main(): void
{
    // Post new vol fs trace
    $data = [
        "dbalias"     => "AATestAPI_Upd",
        "gather_date" => "2022-07-20",
        "host_name"   => "tynle204",
        "filesystem"  => "\/dev\/export",
        "size_fs"     => "521000.26",
        "used"        => "21000.26",
        "free"        => "500000",
        "mount"       => "\/var"
    ];

    postVolFsTrace($data);
}

main();

?>

Shell

#!/bin/sh

v_static_token="123456"
v_tempfile=api_dbSQWare.tmp

<<COMMENTS
    f_post_vol_fs_trace create new vol fs trace object

    :param t_data : JSON object data
    :return       : request response
COMMENTS
f_post_vol_fs_trace(){
    t_data="$1"

    curl --location --request POST "http://admin-webdba.dbsqware.com/lib/apiAudit.php?type=vol_fs&token=$v_static_token" -H "Content-Type:application/json" -d "$t_data" > $v_tempfile 2>$v_tempfile.err
	if [ $(grep -c '"error_message"' $v_tempfile) -eq 0 ]
	then
		echo "Insert with success !"
		return 0
	else
		v_error_message=`cat $v_tempfile|grep '\"error_message\": \"' | cut -d'"' -f4`
        echo "Error message [$v_error_message]"
		return 1
	fi
}

main(){
    # Post new backup trace
    t_data='{"dbalias":"AATestAPI_Upd","gather_date":"2022-07-23","host_name":"tynle204","filesystem":"\/dev\/export","size_fs":"521000.26","used":"21000.26","free":"500000","mount":"\/var"}'
    f_post_vol_fs_trace "$t_data"
}

main

PowerShell

$staticToken = ""
$tempfile    = ".\api_dbSQWare.json"

<#
    Create new vol fs trace.

    :param data : JSON object data
    :return     : Request response
#>
function postVolFsTrace($data) {
    $params = @{
        Method      = 'Post'
        Uri         = "http://admin-webdba.dbsqware.com/lib/apiAudit.php?type=vol_fs&token=$staticToken" 
        Body        = $data | ConvertTo-Json
        ContentType = 'application/json'
    }
    Invoke-RestMethod @t_params -OutFile $tempfile

    # API request
    $response = Get-Content -Path $tempfile | ConvertFrom-Json

    if (-not $response) {
        Write-Output 'Failure of the API request !'
        return 0
    }

    if ($response.PSObject.Properties.Name -contains 'error_message') {
        Write-Output "Error message [$($response.error_message)]"
        return 0
    }
}

function main {
    # Post new vol fs trace
    $data = @{
        dbalias     = "AATestAPI_Upd"
        gather_date = "2022-07-22"
        host_name   = "tynle204"
        filesystem  = "\/dev\/export"
        size_fs     = "521000.26"
        used        = "21000.26"
        free        = "500000"
        mount       = "\/var"
    }

    postVolFsTrace($data)
}

main