mirror of
https://github.com/bpg/terraform-provider-proxmox.git
synced 2025-07-03 03:52:58 +00:00
fix(file): "Permission denied" error when creating a file by a non-root user (#291)
* fix(file): "Permission denied" error when creating a file by a non-root user * fix linter errors
This commit is contained in:
parent
b7e73ed360
commit
401b39782f
382
proxmox/datastores.go
Normal file
382
proxmox/datastores.go
Normal file
@ -0,0 +1,382 @@
|
||||
/*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
package proxmox
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||
"golang.org/x/crypto/ssh"
|
||||
|
||||
"github.com/pkg/sftp"
|
||||
)
|
||||
|
||||
// GetDatastore retrieves information about a datastore.
|
||||
/*
|
||||
Using undocumented API endpoints is not recommended, but sometimes it's the only way to get things done.
|
||||
$ pvesh get /storage/local
|
||||
┌─────────┬───────────────────────────────────────────┐
|
||||
│ key │ value │
|
||||
╞═════════╪═══════════════════════════════════════════╡
|
||||
│ content │ images,vztmpl,iso,backup,snippets,rootdir │
|
||||
├─────────┼───────────────────────────────────────────┤
|
||||
│ digest │ 5b65ede80f34631d6039e6922845cfa4abc956be │
|
||||
├─────────┼───────────────────────────────────────────┤
|
||||
│ path │ /var/lib/vz │
|
||||
├─────────┼───────────────────────────────────────────┤
|
||||
│ shared │ 0 │
|
||||
├─────────┼───────────────────────────────────────────┤
|
||||
│ storage │ local │
|
||||
├─────────┼───────────────────────────────────────────┤
|
||||
│ type │ dir │
|
||||
└─────────┴───────────────────────────────────────────┘
|
||||
*/
|
||||
func (c *VirtualEnvironmentClient) GetDatastore(
|
||||
ctx context.Context,
|
||||
datastoreID string,
|
||||
) (*DatastoreGetResponseData, error) {
|
||||
resBody := &DatastoreGetResponseBody{}
|
||||
err := c.DoRequest(
|
||||
ctx,
|
||||
http.MethodGet,
|
||||
fmt.Sprintf("storage/%s", url.PathEscape(datastoreID)),
|
||||
nil,
|
||||
resBody,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return resBody.Data, nil
|
||||
}
|
||||
|
||||
// DeleteDatastoreFile deletes a file in a datastore.
|
||||
func (c *VirtualEnvironmentClient) DeleteDatastoreFile(
|
||||
ctx context.Context,
|
||||
nodeName, datastoreID, volumeID string,
|
||||
) error {
|
||||
err := c.DoRequest(
|
||||
ctx,
|
||||
http.MethodDelete,
|
||||
fmt.Sprintf(
|
||||
"nodes/%s/storage/%s/content/%s",
|
||||
url.PathEscape(nodeName),
|
||||
url.PathEscape(datastoreID),
|
||||
url.PathEscape(volumeID),
|
||||
),
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetDatastoreStatus gets status information for a given datastore.
|
||||
func (c *VirtualEnvironmentClient) GetDatastoreStatus(
|
||||
ctx context.Context,
|
||||
nodeName, datastoreID string,
|
||||
) (*DatastoreGetStatusResponseData, error) {
|
||||
resBody := &DatastoreGetStatusResponseBody{}
|
||||
err := c.DoRequest(
|
||||
ctx,
|
||||
http.MethodGet,
|
||||
fmt.Sprintf(
|
||||
"nodes/%s/storage/%s/status",
|
||||
url.PathEscape(nodeName),
|
||||
url.PathEscape(datastoreID),
|
||||
),
|
||||
nil,
|
||||
resBody,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if resBody.Data == nil {
|
||||
return nil, errors.New("the server did not include a data object in the response")
|
||||
}
|
||||
|
||||
return resBody.Data, nil
|
||||
}
|
||||
|
||||
// ListDatastoreFiles retrieves a list of the files in a datastore.
|
||||
func (c *VirtualEnvironmentClient) ListDatastoreFiles(
|
||||
ctx context.Context,
|
||||
nodeName, datastoreID string,
|
||||
) ([]*DatastoreFileListResponseData, error) {
|
||||
resBody := &DatastoreFileListResponseBody{}
|
||||
err := c.DoRequest(
|
||||
ctx,
|
||||
http.MethodGet,
|
||||
fmt.Sprintf(
|
||||
"nodes/%s/storage/%s/content",
|
||||
url.PathEscape(nodeName),
|
||||
url.PathEscape(datastoreID),
|
||||
),
|
||||
nil,
|
||||
resBody,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if resBody.Data == nil {
|
||||
return nil, errors.New("the server did not include a data object in the response")
|
||||
}
|
||||
|
||||
sort.Slice(resBody.Data, func(i, j int) bool {
|
||||
return resBody.Data[i].VolumeID < resBody.Data[j].VolumeID
|
||||
})
|
||||
|
||||
return resBody.Data, nil
|
||||
}
|
||||
|
||||
// ListDatastores retrieves a list of nodes.
|
||||
func (c *VirtualEnvironmentClient) ListDatastores(
|
||||
ctx context.Context,
|
||||
nodeName string,
|
||||
d *DatastoreListRequestBody,
|
||||
) ([]*DatastoreListResponseData, error) {
|
||||
resBody := &DatastoreListResponseBody{}
|
||||
err := c.DoRequest(
|
||||
ctx,
|
||||
http.MethodGet,
|
||||
fmt.Sprintf("nodes/%s/storage", url.PathEscape(nodeName)),
|
||||
d,
|
||||
resBody,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if resBody.Data == nil {
|
||||
return nil, errors.New("the server did not include a data object in the response")
|
||||
}
|
||||
|
||||
sort.Slice(resBody.Data, func(i, j int) bool {
|
||||
return resBody.Data[i].ID < resBody.Data[j].ID
|
||||
})
|
||||
|
||||
return resBody.Data, nil
|
||||
}
|
||||
|
||||
// UploadFileToDatastore uploads a file to a datastore.
|
||||
func (c *VirtualEnvironmentClient) UploadFileToDatastore(
|
||||
ctx context.Context,
|
||||
d *DatastoreUploadRequestBody,
|
||||
) (*DatastoreUploadResponseBody, error) {
|
||||
switch d.ContentType {
|
||||
case "iso", "vztmpl":
|
||||
r, w := io.Pipe()
|
||||
|
||||
defer func(r *io.PipeReader) {
|
||||
err := r.Close()
|
||||
if err != nil {
|
||||
tflog.Error(ctx, "failed to close pipe reader", map[string]interface{}{
|
||||
"error": err,
|
||||
})
|
||||
}
|
||||
}(r)
|
||||
|
||||
m := multipart.NewWriter(w)
|
||||
|
||||
go func() {
|
||||
defer func(w *io.PipeWriter) {
|
||||
err := w.Close()
|
||||
if err != nil {
|
||||
tflog.Error(ctx, "failed to close pipe writer", map[string]interface{}{
|
||||
"error": err,
|
||||
})
|
||||
}
|
||||
}(w)
|
||||
defer func(m *multipart.Writer) {
|
||||
err := m.Close()
|
||||
if err != nil {
|
||||
tflog.Error(ctx, "failed to close multipart writer", map[string]interface{}{
|
||||
"error": err,
|
||||
})
|
||||
}
|
||||
}(m)
|
||||
|
||||
err := m.WriteField("content", d.ContentType)
|
||||
if err != nil {
|
||||
tflog.Error(ctx, "failed to write 'content' field", map[string]interface{}{
|
||||
"error": err,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
part, err := m.CreateFormFile("filename", d.FileName)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
_, err = io.Copy(part, d.FileReader)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}()
|
||||
|
||||
// We need to store the multipart content in a temporary file to avoid using high amounts of memory.
|
||||
// This is necessary due to Proxmox VE not supporting chunked transfers in v6.1 and earlier versions.
|
||||
tempMultipartFile, err := os.CreateTemp("", "multipart")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create temporary file: %w", err)
|
||||
}
|
||||
|
||||
tempMultipartFileName := tempMultipartFile.Name()
|
||||
|
||||
_, err = io.Copy(tempMultipartFile, r)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to copy multipart data to temporary file: %w", err)
|
||||
}
|
||||
|
||||
err = tempMultipartFile.Close()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to close temporary file: %w", err)
|
||||
}
|
||||
|
||||
defer func(name string) {
|
||||
err := os.Remove(name)
|
||||
if err != nil {
|
||||
tflog.Error(ctx, "failed to remove temporary file", map[string]interface{}{
|
||||
"error": err,
|
||||
})
|
||||
}
|
||||
}(tempMultipartFileName)
|
||||
|
||||
// Now that the multipart data is stored in a file, we can go ahead and do a HTTP POST request.
|
||||
fileReader, err := os.Open(tempMultipartFileName)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to open temporary file: %w", err)
|
||||
}
|
||||
|
||||
defer func(fileReader *os.File) {
|
||||
err := fileReader.Close()
|
||||
if err != nil {
|
||||
tflog.Error(ctx, "failed to close file reader", map[string]interface{}{
|
||||
"error": err,
|
||||
})
|
||||
}
|
||||
}(fileReader)
|
||||
|
||||
fileInfo, err := fileReader.Stat()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get file info: %w", err)
|
||||
}
|
||||
|
||||
fileSize := fileInfo.Size()
|
||||
|
||||
reqBody := &VirtualEnvironmentMultiPartData{
|
||||
Boundary: m.Boundary(),
|
||||
Reader: fileReader,
|
||||
Size: &fileSize,
|
||||
}
|
||||
|
||||
resBody := &DatastoreUploadResponseBody{}
|
||||
err = c.DoRequest(
|
||||
ctx,
|
||||
http.MethodPost,
|
||||
fmt.Sprintf(
|
||||
"nodes/%s/storage/%s/upload",
|
||||
url.PathEscape(d.NodeName),
|
||||
url.PathEscape(d.DatastoreID),
|
||||
),
|
||||
reqBody,
|
||||
resBody,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return resBody, nil
|
||||
default:
|
||||
// We need to upload all other files using SFTP due to API limitations.
|
||||
// Hopefully, this will not be required in future releases of Proxmox VE.
|
||||
sshClient, err := c.OpenNodeShell(ctx, d.NodeName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer func(sshClient *ssh.Client) {
|
||||
err := sshClient.Close()
|
||||
if err != nil {
|
||||
tflog.Error(ctx, "failed to close SSH client", map[string]interface{}{
|
||||
"error": err,
|
||||
})
|
||||
}
|
||||
}(sshClient)
|
||||
|
||||
datastore, err := c.GetDatastore(ctx, d.DatastoreID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get datastore: %w", err)
|
||||
}
|
||||
if datastore.Path == nil || *datastore.Path == "" {
|
||||
return nil, errors.New("failed to determine the datastore path")
|
||||
}
|
||||
|
||||
remoteFileDir := *datastore.Path
|
||||
if d.ContentType != "" {
|
||||
remoteFileDir = filepath.Join(remoteFileDir, d.ContentType)
|
||||
}
|
||||
|
||||
remoteFilePath := filepath.Join(remoteFileDir, d.FileName)
|
||||
sftpClient, err := sftp.NewClient(sshClient)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create SFTP client: %w", err)
|
||||
}
|
||||
|
||||
defer func(sftpClient *sftp.Client) {
|
||||
err := sftpClient.Close()
|
||||
if err != nil {
|
||||
tflog.Error(ctx, "failed to close SFTP client", map[string]interface{}{
|
||||
"error": err,
|
||||
})
|
||||
}
|
||||
}(sftpClient)
|
||||
|
||||
err = sftpClient.MkdirAll(remoteFileDir)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create directory %s: %w", remoteFileDir, err)
|
||||
}
|
||||
|
||||
remoteFile, err := sftpClient.Create(remoteFilePath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create file %s: %w", remoteFilePath, err)
|
||||
}
|
||||
|
||||
defer func(remoteFile *sftp.File) {
|
||||
err := remoteFile.Close()
|
||||
if err != nil {
|
||||
tflog.Error(ctx, "failed to close remote file", map[string]interface{}{
|
||||
"error": err,
|
||||
})
|
||||
}
|
||||
}(remoteFile)
|
||||
|
||||
_, err = remoteFile.ReadFrom(d.FileReader)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to upload file %s: %w", remoteFilePath, err)
|
||||
}
|
||||
|
||||
return &DatastoreUploadResponseBody{}, nil
|
||||
}
|
||||
}
|
@ -10,13 +10,28 @@ import (
|
||||
"github.com/bpg/terraform-provider-proxmox/proxmox/types"
|
||||
)
|
||||
|
||||
// VirtualEnvironmentDatastoreFileListResponseBody contains the body from a datastore content list response.
|
||||
type VirtualEnvironmentDatastoreFileListResponseBody struct {
|
||||
Data []*VirtualEnvironmentDatastoreFileListResponseData `json:"data,omitempty"`
|
||||
// DatastoreGetResponseBody contains the body from a datastore get response.
|
||||
type DatastoreGetResponseBody struct {
|
||||
Data *DatastoreGetResponseData `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
// VirtualEnvironmentDatastoreFileListResponseData contains the data from a datastore content list response.
|
||||
type VirtualEnvironmentDatastoreFileListResponseData struct {
|
||||
// DatastoreGetResponseData contains the data from a datastore get response.
|
||||
type DatastoreGetResponseData struct {
|
||||
Content types.CustomCommaSeparatedList `json:"content,omitempty" url:"content,omitempty,comma"`
|
||||
Digest *string `json:"digest,omitempty"`
|
||||
Path *string `json:"path,omitempty"`
|
||||
Shared *types.CustomBool `json:"shared,omitempty"`
|
||||
Storage *string `json:"storage,omitempty"`
|
||||
Type *string `json:"type,omitempty"`
|
||||
}
|
||||
|
||||
// DatastoreFileListResponseBody contains the body from a datastore content list response.
|
||||
type DatastoreFileListResponseBody struct {
|
||||
Data []*DatastoreFileListResponseData `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
// DatastoreFileListResponseData contains the data from a datastore content list response.
|
||||
type DatastoreFileListResponseData struct {
|
||||
ContentType string `json:"content"`
|
||||
FileFormat string `json:"format"`
|
||||
FileSize int `json:"size"`
|
||||
@ -26,13 +41,13 @@ type VirtualEnvironmentDatastoreFileListResponseData struct {
|
||||
VolumeID string `json:"volid"`
|
||||
}
|
||||
|
||||
// VirtualEnvironmentDatastoreGetStatusResponseBody contains the body from a datastore status get request.
|
||||
type VirtualEnvironmentDatastoreGetStatusResponseBody struct {
|
||||
Data *VirtualEnvironmentDatastoreGetStatusResponseData `json:"data,omitempty"`
|
||||
// DatastoreGetStatusResponseBody contains the body from a datastore status get request.
|
||||
type DatastoreGetStatusResponseBody struct {
|
||||
Data *DatastoreGetStatusResponseData `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
// VirtualEnvironmentDatastoreGetStatusResponseBody contains the data from a datastore status get request.
|
||||
type VirtualEnvironmentDatastoreGetStatusResponseData struct {
|
||||
// DatastoreGetStatusResponseData contains the data from a datastore status get request.
|
||||
type DatastoreGetStatusResponseData struct {
|
||||
Active *types.CustomBool `json:"active,omitempty"`
|
||||
AvailableBytes *int64 `json:"avail,omitempty"`
|
||||
Content *types.CustomCommaSeparatedList `json:"content,omitempty" url:"content,omitempty,comma"`
|
||||
@ -43,8 +58,8 @@ type VirtualEnvironmentDatastoreGetStatusResponseData struct {
|
||||
UsedBytes *int64 `json:"used,omitempty"`
|
||||
}
|
||||
|
||||
// VirtualEnvironmentDatastoreListRequestBody contains the body for a datastore list request.
|
||||
type VirtualEnvironmentDatastoreListRequestBody struct {
|
||||
// DatastoreListRequestBody contains the body for a datastore list request.
|
||||
type DatastoreListRequestBody struct {
|
||||
ContentTypes types.CustomCommaSeparatedList `json:"content,omitempty" url:"content,omitempty,comma"`
|
||||
Enabled *types.CustomBool `json:"enabled,omitempty" url:"enabled,omitempty,int"`
|
||||
Format *types.CustomBool `json:"format,omitempty" url:"format,omitempty,int"`
|
||||
@ -52,13 +67,13 @@ type VirtualEnvironmentDatastoreListRequestBody struct {
|
||||
Target *string `json:"target,omitempty" url:"target,omitempty"`
|
||||
}
|
||||
|
||||
// VirtualEnvironmentDatastoreListResponseBody contains the body from a datastore list response.
|
||||
type VirtualEnvironmentDatastoreListResponseBody struct {
|
||||
Data []*VirtualEnvironmentDatastoreListResponseData `json:"data,omitempty"`
|
||||
// DatastoreListResponseBody contains the body from a datastore list response.
|
||||
type DatastoreListResponseBody struct {
|
||||
Data []*DatastoreListResponseData `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
// VirtualEnvironmentDatastoreListResponseData contains the data from a datastore list response.
|
||||
type VirtualEnvironmentDatastoreListResponseData struct {
|
||||
// DatastoreListResponseData contains the data from a datastore list response.
|
||||
type DatastoreListResponseData struct {
|
||||
Active *types.CustomBool `json:"active,omitempty"`
|
||||
ContentTypes *types.CustomCommaSeparatedList `json:"content,omitempty"`
|
||||
Enabled *types.CustomBool `json:"enabled,omitempty"`
|
||||
@ -71,8 +86,8 @@ type VirtualEnvironmentDatastoreListResponseData struct {
|
||||
Type string `json:"type,omitempty"`
|
||||
}
|
||||
|
||||
// VirtualEnvironmentDatastoreUploadRequestBody contains the body for a datastore upload request.
|
||||
type VirtualEnvironmentDatastoreUploadRequestBody struct {
|
||||
// DatastoreUploadRequestBody contains the body for a datastore upload request.
|
||||
type DatastoreUploadRequestBody struct {
|
||||
ContentType string `json:"content,omitempty"`
|
||||
DatastoreID string `json:"storage,omitempty"`
|
||||
FileName string `json:"filename,omitempty"`
|
||||
@ -80,7 +95,7 @@ type VirtualEnvironmentDatastoreUploadRequestBody struct {
|
||||
NodeName string `json:"node,omitempty"`
|
||||
}
|
||||
|
||||
// VirtualEnvironmentDatastoreUploadResponseBody contains the body from a datastore upload response.
|
||||
type VirtualEnvironmentDatastoreUploadResponseBody struct {
|
||||
// DatastoreUploadResponseBody contains the body from a datastore upload response.
|
||||
type DatastoreUploadResponseBody struct {
|
||||
UploadID *string `json:"data,omitempty"`
|
||||
}
|
@ -1,306 +0,0 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package proxmox
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||
|
||||
"github.com/pkg/sftp"
|
||||
)
|
||||
|
||||
// DeleteDatastoreFile deletes a file in a datastore.
|
||||
func (c *VirtualEnvironmentClient) DeleteDatastoreFile(
|
||||
ctx context.Context,
|
||||
nodeName, datastoreID, volumeID string,
|
||||
) error {
|
||||
err := c.DoRequest(
|
||||
ctx,
|
||||
http.MethodDelete,
|
||||
fmt.Sprintf(
|
||||
"nodes/%s/storage/%s/content/%s",
|
||||
url.PathEscape(nodeName),
|
||||
url.PathEscape(datastoreID),
|
||||
url.PathEscape(volumeID),
|
||||
),
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetDatastoreStatus gets status information for a given datastore.
|
||||
func (c *VirtualEnvironmentClient) GetDatastoreStatus(
|
||||
ctx context.Context,
|
||||
nodeName, datastoreID string,
|
||||
) (*VirtualEnvironmentDatastoreGetStatusResponseData, error) {
|
||||
resBody := &VirtualEnvironmentDatastoreGetStatusResponseBody{}
|
||||
err := c.DoRequest(
|
||||
ctx,
|
||||
http.MethodGet,
|
||||
fmt.Sprintf(
|
||||
"nodes/%s/storage/%s/status",
|
||||
url.PathEscape(nodeName),
|
||||
url.PathEscape(datastoreID),
|
||||
),
|
||||
nil,
|
||||
resBody,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if resBody.Data == nil {
|
||||
return nil, errors.New("the server did not include a data object in the response")
|
||||
}
|
||||
|
||||
return resBody.Data, nil
|
||||
}
|
||||
|
||||
// ListDatastoreFiles retrieves a list of the files in a datastore.
|
||||
func (c *VirtualEnvironmentClient) ListDatastoreFiles(
|
||||
ctx context.Context,
|
||||
nodeName, datastoreID string,
|
||||
) ([]*VirtualEnvironmentDatastoreFileListResponseData, error) {
|
||||
resBody := &VirtualEnvironmentDatastoreFileListResponseBody{}
|
||||
err := c.DoRequest(
|
||||
ctx,
|
||||
http.MethodGet,
|
||||
fmt.Sprintf(
|
||||
"nodes/%s/storage/%s/content",
|
||||
url.PathEscape(nodeName),
|
||||
url.PathEscape(datastoreID),
|
||||
),
|
||||
nil,
|
||||
resBody,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if resBody.Data == nil {
|
||||
return nil, errors.New("the server did not include a data object in the response")
|
||||
}
|
||||
|
||||
sort.Slice(resBody.Data, func(i, j int) bool {
|
||||
return resBody.Data[i].VolumeID < resBody.Data[j].VolumeID
|
||||
})
|
||||
|
||||
return resBody.Data, nil
|
||||
}
|
||||
|
||||
// ListDatastores retrieves a list of nodes.
|
||||
func (c *VirtualEnvironmentClient) ListDatastores(
|
||||
ctx context.Context,
|
||||
nodeName string,
|
||||
d *VirtualEnvironmentDatastoreListRequestBody,
|
||||
) ([]*VirtualEnvironmentDatastoreListResponseData, error) {
|
||||
resBody := &VirtualEnvironmentDatastoreListResponseBody{}
|
||||
err := c.DoRequest(
|
||||
ctx,
|
||||
http.MethodGet,
|
||||
fmt.Sprintf("nodes/%s/storage", url.PathEscape(nodeName)),
|
||||
d,
|
||||
resBody,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if resBody.Data == nil {
|
||||
return nil, errors.New("the server did not include a data object in the response")
|
||||
}
|
||||
|
||||
sort.Slice(resBody.Data, func(i, j int) bool {
|
||||
return resBody.Data[i].ID < resBody.Data[j].ID
|
||||
})
|
||||
|
||||
return resBody.Data, nil
|
||||
}
|
||||
|
||||
// UploadFileToDatastore uploads a file to a datastore.
|
||||
func (c *VirtualEnvironmentClient) UploadFileToDatastore(
|
||||
ctx context.Context,
|
||||
d *VirtualEnvironmentDatastoreUploadRequestBody,
|
||||
) (*VirtualEnvironmentDatastoreUploadResponseBody, error) {
|
||||
switch d.ContentType {
|
||||
case "iso", "vztmpl":
|
||||
r, w := io.Pipe()
|
||||
|
||||
defer r.Close()
|
||||
|
||||
m := multipart.NewWriter(w)
|
||||
|
||||
go func() {
|
||||
defer w.Close()
|
||||
defer m.Close()
|
||||
|
||||
err := m.WriteField("content", d.ContentType)
|
||||
if err != nil {
|
||||
tflog.Error(ctx, "failed to write 'content' field", map[string]interface{}{
|
||||
"error": err,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
part, err := m.CreateFormFile("filename", d.FileName)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
_, err = io.Copy(part, d.FileReader)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}()
|
||||
|
||||
// We need to store the multipart content in a temporary file to avoid using high amounts of memory.
|
||||
// This is necessary due to Proxmox VE not supporting chunked transfers in v6.1 and earlier versions.
|
||||
tempMultipartFile, err := os.CreateTemp("", "multipart")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tempMultipartFileName := tempMultipartFile.Name()
|
||||
|
||||
_, err = io.Copy(tempMultipartFile, r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = tempMultipartFile.Close()
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer os.Remove(tempMultipartFileName)
|
||||
|
||||
// Now that the multipart data is stored in a file, we can go ahead and do a HTTP POST request.
|
||||
fileReader, err := os.Open(tempMultipartFileName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer fileReader.Close()
|
||||
|
||||
fileInfo, err := fileReader.Stat()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fileSize := fileInfo.Size()
|
||||
|
||||
reqBody := &VirtualEnvironmentMultiPartData{
|
||||
Boundary: m.Boundary(),
|
||||
Reader: fileReader,
|
||||
Size: &fileSize,
|
||||
}
|
||||
|
||||
resBody := &VirtualEnvironmentDatastoreUploadResponseBody{}
|
||||
err = c.DoRequest(
|
||||
ctx,
|
||||
http.MethodPost,
|
||||
fmt.Sprintf(
|
||||
"nodes/%s/storage/%s/upload",
|
||||
url.PathEscape(d.NodeName),
|
||||
url.PathEscape(d.DatastoreID),
|
||||
),
|
||||
reqBody,
|
||||
resBody,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return resBody, nil
|
||||
default:
|
||||
// We need to upload all other files using SFTP due to API limitations.
|
||||
// Hopefully, this will not be required in future releases of Proxmox VE.
|
||||
sshClient, err := c.OpenNodeShell(ctx, d.NodeName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer sshClient.Close()
|
||||
|
||||
sshSession, err := sshClient.NewSession()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
buf, err := sshSession.CombinedOutput(
|
||||
fmt.Sprintf(
|
||||
`awk "/.+: %s$/,/^$/" /etc/pve/storage.cfg | grep -oP '(?<=path[ ])[^\s]+' | head -c -1`,
|
||||
d.DatastoreID,
|
||||
),
|
||||
)
|
||||
if err != nil {
|
||||
sshSession.Close()
|
||||
|
||||
return nil, err
|
||||
}
|
||||
|
||||
sshSession.Close()
|
||||
|
||||
datastorePath := strings.Trim(string(buf), "\000")
|
||||
|
||||
if datastorePath == "" {
|
||||
return nil, errors.New("failed to determine the datastore path")
|
||||
}
|
||||
|
||||
remoteFileDir := datastorePath
|
||||
|
||||
switch d.ContentType {
|
||||
default:
|
||||
remoteFileDir += fmt.Sprintf("/%s", d.ContentType)
|
||||
}
|
||||
|
||||
remoteFilePath := fmt.Sprintf("%s/%s", remoteFileDir, d.FileName)
|
||||
sftpClient, err := sftp.NewClient(sshClient)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer sftpClient.Close()
|
||||
|
||||
err = sftpClient.MkdirAll(remoteFileDir)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
remoteFile, err := sftpClient.Create(remoteFilePath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer remoteFile.Close()
|
||||
|
||||
_, err = remoteFile.ReadFrom(d.FileReader)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &VirtualEnvironmentDatastoreUploadResponseBody{}, nil
|
||||
}
|
||||
}
|
@ -378,7 +378,7 @@ func fileCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag
|
||||
}
|
||||
}(file)
|
||||
|
||||
body := &proxmox.VirtualEnvironmentDatastoreUploadRequestBody{
|
||||
body := &proxmox.DatastoreUploadRequestBody{
|
||||
ContentType: *contentType,
|
||||
DatastoreID: datastoreID,
|
||||
FileName: *fileName,
|
||||
|
Loading…
Reference in New Issue
Block a user