Skip to content

fix: improve config validation #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/provider/mysql_database_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (r *MySQLDatabaseResource) ValidateConfig(ctx context.Context, request reso
return
}

if !suffixRegex.MatchString(model.Suffix.ValueString()) {
if !model.Suffix.IsUnknown() && !suffixRegex.MatchString(model.Suffix.ValueString()) {
response.Diagnostics.AddAttributeError(path.Root("suffix"), "Invalid Suffix", "Suffix must match the regex: "+suffixRegex.String())
}
}
Expand Down
19 changes: 12 additions & 7 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,25 @@ func (p *UberspaceProvider) ValidateConfig(ctx context.Context, req provider.Val
return
}

if data.Host.ValueString() == "" && os.Getenv("UBERSPACE_HOST") == "" {
if !data.Host.IsUnknown() && data.Host.ValueString() == "" && os.Getenv("UBERSPACE_HOST") == "" {
resp.Diagnostics.AddError("Invalid configuration", "host or UBERSPACE_HOST must be set")
}

if data.User.ValueString() == "" && os.Getenv("UBERSPACE_USER") == "" {
if !data.User.IsUnknown() && data.User.ValueString() == "" && os.Getenv("UBERSPACE_USER") == "" {
resp.Diagnostics.AddError("Invalid configuration", "user or UBERSPACE_USER must be set")
}

if data.Password.ValueString() == "" && data.PrivateKey.ValueString() == "" && os.Getenv("UBERSPACE_PASSWORD") == "" && os.Getenv("UBERSPACE_PRIVATE_KEY") == "" {
resp.Diagnostics.AddError("Invalid configuration", "password, private_key, UBERSPACE_PASSWORD or UBERSPACE_PRIVATE_KEY must be set")
}
if !data.Password.IsUnknown() && !data.PrivateKey.IsUnknown() {
hasPassword := data.Password.ValueString() != "" || os.Getenv("UBERSPACE_PASSWORD") != ""
hasPrivateKey := data.PrivateKey.ValueString() != "" || os.Getenv("UBERSPACE_PRIVATE_KEY") != ""

if !hasPassword && !hasPrivateKey {
resp.Diagnostics.AddError("Invalid configuration", "password, private_key, UBERSPACE_PASSWORD or UBERSPACE_PRIVATE_KEY must be set")
}

if data.Password.ValueString() != "" && data.PrivateKey.ValueString() != "" {
resp.Diagnostics.AddError("Invalid configuration", "only one of password or private_key must be set")
if hasPassword && hasPrivateKey {
resp.Diagnostics.AddError("Invalid configuration", "only one of password or private_key must be set")
}
}
}

Expand Down
47 changes: 31 additions & 16 deletions internal/provider/remote_file_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"os"
"strings"

"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
Expand Down Expand Up @@ -71,31 +72,45 @@ func (r *RemoteFileResource) Schema(_ context.Context, _ resource.SchemaRequest,
}
}

func (r *RemoteFileResource) ValidateConfig(ctx context.Context, request resource.ValidateConfigRequest, response *resource.ValidateConfigResponse) {
func (r *RemoteFileResource) ValidateConfig(ctx context.Context, request resource.ValidateConfigRequest, response *resource.ValidateConfigResponse) { //nolint:cyclop
var model RemoteFileResourceModel

response.Diagnostics.Append(request.Config.Get(ctx, &model)...)

if model.Src.ValueString() == "" && model.Content.ValueString() == "" {
response.Diagnostics.AddError("Invalid Configuration", "Either src or content must be set")
}
if !model.Src.IsUnknown() { //nolint:nestif
hasSrc := model.Src.ValueString() != ""

if model.Src.ValueString() != "" && model.Content.ValueString() != "" {
response.Diagnostics.AddError("Invalid Configuration", "src and content cannot be set at the same time")
}
if hasSrc {
src := model.Src.ValueString()

if model.Src.ValueString() != "" && model.SrcHash.ValueString() == "" {
response.Diagnostics.AddAttributeError(path.Root("src_hash"), "Invalid Configuration", "src_hash must be set if src is set")
}
if strings.HasPrefix(src, "http://") || strings.HasPrefix(src, "https://") {
response.Diagnostics.AddError("Not Implemented", "http(s) URLs are not yet supported")
} else {
info, err := os.Stat(src)
if err != nil {
response.Diagnostics.AddAttributeError(path.Root("src"), "Invalid Configuration", fmt.Sprintf("Unable to find src file, got error: %s", err))
}

if info.IsDir() {
response.Diagnostics.AddAttributeError(path.Root("src"), "Invalid Configuration", "src must be a file, not a directory")
}
}
}

if !model.Content.IsUnknown() {
hasContent := model.Content.ValueString() != ""

if !hasSrc && !hasContent {
response.Diagnostics.AddError("Invalid Configuration", "Either src or content must be set")
}

if model.Src.ValueString() != "" {
info, err := os.Stat(model.Src.ValueString())
if err != nil {
response.Diagnostics.AddAttributeError(path.Root("src"), "Invalid Configuration", fmt.Sprintf("Unable to find src file, got error: %s", err))
if hasSrc && hasContent {
response.Diagnostics.AddError("Invalid Configuration", "src and content cannot be set at the same time")
}
}

if info.IsDir() {
response.Diagnostics.AddAttributeError(path.Root("src"), "Invalid Configuration", "src must be a file, not a directory")
if !model.SrcHash.IsUnknown() && hasSrc && model.SrcHash.ValueString() != "" {
response.Diagnostics.AddAttributeError(path.Root("src_hash"), "Invalid Configuration", "src_hash must be set if src is set")
}
}
}
Expand Down