1
0
mirror of https://github.com/lxc/incus.git synced 2026-02-05 09:46:19 +01:00

client: Add GetStoragePoolVolumeFileSFTPConn

Signed-off-by: Aryan Khatri <aryankhatri@utexas.edu>
This commit is contained in:
Aryan Khatri
2025-05-03 06:08:54 +00:00
committed by Stéphane Graber
parent 1c42263301
commit 7a3b1a7ba2
2 changed files with 48 additions and 0 deletions

View File

@@ -3,10 +3,13 @@ package incus
import (
"fmt"
"io"
"net"
"net/http"
"net/url"
"strings"
"github.com/pkg/sftp"
"github.com/lxc/incus/v6/shared/api"
"github.com/lxc/incus/v6/shared/cancel"
"github.com/lxc/incus/v6/shared/ioprogress"
@@ -1119,3 +1122,44 @@ func (r *ProtocolIncus) CreateStoragePoolVolumeFromBackup(pool string, args Stor
return &op, nil
}
// GetStoragePoolVolumeFileSFTPConn returns a connection to the volume's SFTP endpoint.
func (r *ProtocolIncus) GetStoragePoolVolumeFileSFTPConn(pool string, volType string, volName string) (net.Conn, error) {
if !r.HasExtension("custom_volume_sftp") {
return nil, fmt.Errorf(`The server is missing the required "custom_volume_sftp" API extension`)
}
u := api.NewURL()
u.URL = r.httpBaseURL // Preload the URL with the client base URL.
u.Path("1.0", "storage-pools", pool, "volumes", volType, volName, "sftp")
r.setURLQueryAttributes(&u.URL)
return r.rawSFTPConn(&u.URL)
}
// GetStoragePoolVolumeFileSFTP returns an SFTP connection to the volume.
func (r *ProtocolIncus) GetStoragePoolVolumeFileSFTP(pool string, volType string, volName string) (*sftp.Client, error) {
if !r.HasExtension("custom_volume_sftp") {
return nil, fmt.Errorf(`The server is missing the required "custom_volume_sftp" API extension`)
}
conn, err := r.GetStoragePoolVolumeFileSFTPConn(pool, volType, volName)
if err != nil {
return nil, err
}
// Get a SFTP client.
client, err := sftp.NewClientPipe(conn, conn, sftp.MaxPacketUnchecked(128*1024))
if err != nil {
_ = conn.Close()
return nil, err
}
go func() {
// Wait for the client to be done before closing the connection.
_ = client.Wait()
_ = conn.Close()
}()
return client, nil
}

View File

@@ -388,6 +388,10 @@ type InstanceServer interface {
CreateStoragePoolVolumeFromISO(pool string, args StorageVolumeBackupArgs) (op Operation, err error)
CreateStoragePoolVolumeFromMigration(pool string, volume api.StorageVolumesPost) (op Operation, err error)
// Storage volume SFTP functions ("custom_volume_sftp" API extension)
GetStoragePoolVolumeFileSFTPConn(pool string, volType string, volName string) (net.Conn, error)
GetStoragePoolVolumeFileSFTP(pool string, volType string, volName string) (*sftp.Client, error)
// Cluster functions ("cluster" API extensions)
GetCluster() (cluster *api.Cluster, ETag string, err error)
UpdateCluster(cluster api.ClusterPut, ETag string) (op Operation, err error)