0
0
mirror of https://github.com/bpg/terraform-provider-proxmox.git synced 2025-06-30 18:42:58 +00:00
terraform-provider-proxmox/proxmox/utils.go
Pavel Boldyrev 598c62864d
feat(core): Add known hosts callback check for ssh connections (#217)
* feat(core): Add known hosts callback check for ssh connections

* fix code & add tests
2023-01-24 01:05:31 -05:00

59 lines
1.4 KiB
Go

/* 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"
"fmt"
"io"
"math"
"strconv"
"strings"
"github.com/hashicorp/terraform-plugin-log/tflog"
)
func CloseOrLogError(ctx context.Context) func(io.Closer) {
return func(c io.Closer) {
if err := c.Close(); err != nil {
tflog.Error(ctx, "Failed to close", map[string]interface{}{
"error": err,
})
}
}
}
func ParseDiskSize(size *string) (int, error) {
if size == nil {
return 0, nil
}
if strings.HasSuffix(*size, "T") {
diskSize, err := strconv.Atoi(strings.TrimSuffix(*size, "T"))
if err != nil {
return -1, fmt.Errorf("failed to parse disk size: %w", err)
}
return int(math.Ceil(float64(diskSize) * 1024)), nil
}
if strings.HasSuffix(*size, "G") {
diskSize, err := strconv.Atoi(strings.TrimSuffix(*size, "G"))
if err != nil {
return -1, fmt.Errorf("failed to parse disk size: %w", err)
}
return diskSize, nil
}
if strings.HasSuffix(*size, "M") {
diskSize, err := strconv.Atoi(strings.TrimSuffix(*size, "M"))
if err != nil {
return -1, fmt.Errorf("failed to parse disk size: %w", err)
}
return int(math.Ceil(float64(diskSize) / 1024)), nil
}
return -1, fmt.Errorf("cannot parse disk size \"%s\"", *size)
}