User Tools

Site Tools


api_rest_simple_utilizando_python_flask_y_un_broker_redis

Prerequisitos:

sudo apt update
sudo apt full-upgrade -y
sudo apt install redis-server -y
sudo nano /etc/redis/redis.conf
  1. modificar los bindings, ej bind 127.0.0.1 192.168.88.121
  2. en ubuntu se puede controlar el servicio desde systemd habilitando la opción supervised systemd
sudo service redis-server restart
pip install redis

Server:

server.py
from flask import Flask, request, jsonify, abort
import redis
import json
 
app = Flask(__name__)
db = redis.Redis(host='localhost', port=6379, db=0)  # Connect to your Redis instance
 
 
@app.route('/upload', methods=['POST'])
def upload_file():
    # Check for Authorization header
    if 'Authorization' not in request.headers:
        abort(401)  # Unauthorized
 
    # Check if the token is correct
    token = request.headers['Authorization']
    if token != 'your-long-ass-base64-string':
        abort(403)  # Forbidden
 
    data = request.get_json()  # Get JSON data from request
    if data is None:
        return jsonify({'error': 'No JSON received.'}), 400
 
    # Save the entire JSON object to Redis under the hostname key
    hostname = data['hostname']  # Extract hostname from data
    db.set(hostname, json.dumps(data['restorePoints']))  # Save the 'restorePoints' part of the object to Redis
 
    return jsonify({'message': 'Data saved successfully.'}), 200
 
if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=True)

Cliente / endpoint:

sendReport.ps
$uri = "https://api.facundoitest.space/upload"
 
$headers = @{
    "Content-Type" = "application/json"
    "Authorization" = "your-long-ass-base64-string"
}
 
# Import the Veeam Backup PowerShell module
Import-Module Veeam.Backup.PowerShell
 
# Get all Veeam restore points
$restorePoints = Get-VBRRestorePoint
 
# Filter restore points for the specified VM
$vmRestorePoints = $restorePoints #| Where-Object { $_.Name -eq $vmNameToFilter }
 
# Create an array to store all restore points
$restorePointsArray = @()
 
$vmRestorePoints | ForEach-Object {
    $vmName = $_.Name
    $creationTime = $_.CreationTime
    $type = $_.Type
 
    # Create a custom object for each restore point
    $restorePointObject = @{
        "vmname" = $vmName
        "creationtime" = $creationTime
        "type" = $type
        "result" = "Success"  # Replace with actual result
    }
 
    # Add the custom object to the restore points array
    $restorePointsArray += $restorePointObject
}
 
# Create a single JSON object that contains the list of all restore points
$body = @{
    "hostname" = $hostname
    "restorePoints" = $restorePointsArray
} | ConvertTo-Json
 
# Send the JSON data to your API
$response = Invoke-RestMethod -Uri $uri -Method Post -Body $body -Headers $headers

apuntar $uri a la dirección IP local si el nombre no se resuelve por DNS, usar el fqdn con el que llega al servidor, y/o cambiar el transporte de https a http según soporte el host.

api_rest_simple_utilizando_python_flask_y_un_broker_redis.txt · Last modified: 2024/10/17 21:42 by 127.0.0.1