0
0
mirror of https://github.com/bpg/terraform-provider-proxmox.git synced 2025-07-04 04:22:59 +00:00
terraform-provider-proxmox/proxmoxtf/resource/validators/file.go
renovate[bot] fbd04ed950
chore(deps): update tools (#1017)
* chore(deps): update tools

| datasource | package                                                       | from    | to      |
| ---------- | ------------------------------------------------------------- | ------- | ------- |
| go         | github.com/golangci/golangci-lint                             | v1.55.2 | v1.56.2 |
| go         | go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp | v0.48.0 | v0.49.0 |

* fix linter errors

Signed-off-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com>

---------

Signed-off-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com>
2024-03-04 21:41:53 -05:00

92 lines
2.1 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 validators
import (
"fmt"
"regexp"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/bpg/terraform-provider-proxmox/proxmox/types"
)
// ContentType returns a schema validation function for a content type on a storage device.
func ContentType() schema.SchemaValidateDiagFunc {
return validation.ToDiagFunc(validation.StringInSlice([]string{
"dump",
"iso",
"snippets",
"vztmpl",
}, false))
}
// FileFormat returns a schema validation function for a file format.
func FileFormat() schema.SchemaValidateDiagFunc {
return validation.ToDiagFunc(validation.StringInSlice([]string{
"qcow2",
"raw",
"vmdk",
}, false))
}
// FileID returns a schema validation function for a file identifier.
func FileID() schema.SchemaValidateDiagFunc {
return validation.ToDiagFunc(func(i interface{}, k string) ([]string, []error) {
v, ok := i.(string)
var ws []string
var es []error
if !ok {
es = append(es, fmt.Errorf("expected type of %s to be string", k))
return ws, es
}
if v != "" {
r := regexp.MustCompile(`^(?i)[a-z\d\-_\.]+:([a-z\d\-_]+/)?.+$`)
ok := r.MatchString(v)
if !ok {
es = append(es, fmt.Errorf(
"expected %s to be a valid file identifier (datastore-name:iso/some-file.img), got %s", k, v,
))
return ws, es
}
}
return ws, es
})
}
// FileSize is a schema validation function for file size.
func FileSize() schema.SchemaValidateDiagFunc {
return validation.ToDiagFunc(func(i interface{}, k string) ([]string, []error) {
v, ok := i.(string)
var es []error
if !ok {
es = append(es, fmt.Errorf("expected type of %s to be string", k))
return nil, es
}
if v != "" {
_, err := types.ParseDiskSize(v)
if err != nil {
es = append(es, fmt.Errorf("expected %s to be a valid file size (100, 1M, 1G), got %s", k, v))
return nil, es
}
}
return []string{}, es
})
}