test_scaleway_consumption.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. """
  2. Test de l'API consumption Scaleway (Billing).
  3. À supprimer : script de test manuel pour vérifier l'appel à l'API Billing.
  4. """
  5. import os
  6. import sys
  7. # Ajouter le chemin agent_docker pour importer le service
  8. agent_docker = os.path.join(os.path.dirname(__file__), "..", "agent_docker")
  9. sys.path.insert(0, agent_docker)
  10. # Charger le .env de agent_docker
  11. from dotenv import load_dotenv
  12. load_dotenv(os.path.join(agent_docker, ".env"))
  13. from src.service.scaleway_billing_service import get_scaleway_consumption
  14. if __name__ == "__main__":
  15. # Vérifier que les variables d'environnement sont définies
  16. if not os.getenv("SCW_API_KEY") and not os.getenv("SCW_SECRET_KEY"):
  17. print("Erreur : définir SCW_API_KEY ou SCW_SECRET_KEY dans .env")
  18. sys.exit(1)
  19. if not os.getenv("SCW_ORGANIZATION_ID") and not os.getenv("SCW_ENDPOINT"):
  20. print("Erreur : définir SCW_ORGANIZATION_ID ou SCW_ENDPOINT (project_id) dans .env")
  21. sys.exit(1)
  22. print("Appel API Billing Scaleway (mois courant)...")
  23. result = get_scaleway_consumption(timeout=10)
  24. if result is None:
  25. print("Aucun résultat (config manquante)")
  26. sys.exit(1)
  27. if result.get("error"):
  28. print(f"Erreur : {result['error']}")
  29. sys.exit(1)
  30. print(f"Période : {result.get('period', 'N/A')}")
  31. print(f"Total : {result.get('total_eur', 0):.2f} {result.get('currency', 'EUR')}")
  32. if result.get("consumptions"):
  33. print("Détail par catégorie :")
  34. for c in result["consumptions"]:
  35. print(f" - {c['category']}: {c['amount_eur']:.2f} €")
  36. else:
  37. print("Aucune consommation détaillée.")
  38. print("OK")