Ce document a été traduit à l'aide d'une technologie de traduction automatique. Bien que nous nous efforcions de fournir des traductions exactes, nous ne fournissons aucune garantie quant à l'exhaustivité, l'exactitude ou la fiabilité du contenu traduit. En cas de divergence, la version originale anglaise prévaut et fait foi.

Il s'agit d'une documentation non publiée pour SUSE® Storage 1.12 (Dev).

Client Python

Vous pouvez accéder à l’API Longhorn via la liaison Python.

  1. Obtenez le point de terminaison Longhorn

    Une façon de communiquer avec Longhorn est via longhorn-frontend le service.

    Si vous exécutez votre outil d’automatisation/script dans le même cluster où Longhorn est installé, connectez-vous au point de terminaison http://longhorn-frontend.longhorn-system/v1.

    Si vous exécutez votre outil d’automatisation/script sur votre machine locale, utilisez kubectl port-forward pour transférer longhorn-frontend le service vers hôte local :

    kubectl port-forward services/longhorn-frontend 8080:http -n longhorn-system

    et connectez-vous au point de terminaison http://localhost:8080/v1

  2. Utilisation du Client Python

    Importez le fichier longhorn.py qui contient le client Python dans votre script Python et créez un client à partir du point de terminaison :

     import longhorn
    
     # If automation/scripting tool is inside the same cluster in which Longhorn is installed
     longhorn_url = 'http://longhorn-frontend.longhorn-system/v1'
     # If forwarding `longhorn-frontend` service to localhost
     longhorn_url = 'http://localhost:8080/v1'
    
     client = longhorn.Client(url=longhorn_url)
    
     # Volume operations
     # List all volumes
     volumes = client.list_volume()
     # Get volume by NAME/ID
     testvol1 = client.by_id_volume(id="testvol1")
     # Attach TESTVOL1
     testvol1 = testvol1.attach(hostId="worker-1")
     # Detach TESTVOL1
     testvol1.detach()
     # Create a snapshot of TESTVOL1 with NAME
     snapshot1 = testvol1.snapshotCreate(name="snapshot1")
     # Create a backup from a snapshot NAME
     testvol1.snapshotBackup(name=snapshot1.name)
     # Update the number of replicas of TESTVOL1
     testvol1.updateReplicaCount(replicaCount=2)
     # Find more examples in Longhorn integration tests https://github.com/longhorn/longhorn-tests/tree/master/manager/integration/tests
    
     # Node operations
     # List all nodes
     nodes = client.list_node()
     # Get node by NAME/ID
     node1 = client.by_id_node(id="worker-1")
     # Disable scheduling for NODE1
     client.update(node1, allowScheduling=False)
     # Enable scheduling for NODE1
     client.update(node1, allowScheduling=True)
     # Find more examples in Longhorn integration tests https://github.com/longhorn/longhorn-tests/tree/master/manager/integration/tests
    
     # Setting operations
     # List all settings
     settings = client.list_setting()
     # Get setting by NAME/ID
     backupTargetsetting = client.by_id_setting(id="backup-target")
     # Update a setting
     backupTargetsetting = client.update(backupTargetsetting, value="s3://backupbucket@us-east-1/")
     # Find more examples in Longhorn integration tests https://github.com/longhorn/longhorn-tests/tree/master/manager/integration/tests