Skip to content

fix: provider model #11

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
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
34 changes: 14 additions & 20 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package provider

import (
"cmp"
"context"
"os"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/provider/schema"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"

"github.com/uberspace-community/terraform-provider-uberspace/ssh"
"github.com/uberspace-community/terraform-provider-uberspace/uberspace"
Expand All @@ -26,10 +28,10 @@ type UberspaceProvider struct {

// UberspaceProviderModel describes the provider data model.
type UberspaceProviderModel struct {
Host *string `tfsdk:"host"`
User *string `tfsdk:"user"`
Password *string `tfsdk:"password"`
PrivateKey *string `tfsdk:"private_key"`
Host types.String `tfsdk:"host"`
User types.String `tfsdk:"user"`
Password types.String `tfsdk:"password"`
PrivateKey types.String `tfsdk:"private_key"`
}

func (p *UberspaceProvider) Metadata(_ context.Context, _ provider.MetadataRequest, resp *provider.MetadataResponse) {
Expand Down Expand Up @@ -71,19 +73,19 @@ func (p *UberspaceProvider) ValidateConfig(ctx context.Context, req provider.Val
return
}

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

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

if data.Password == nil && data.PrivateKey == nil && os.Getenv("UBERSPACE_PASSWORD") == "" && os.Getenv("UBERSPACE_PRIVATE_KEY") == "" {
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.PrivateKey != nil && data.Password != nil {
if data.Password.ValueString() != "" && data.PrivateKey.ValueString() != "" {
resp.Diagnostics.AddError("Invalid configuration", "only one of password or private_key must be set")
}
}
Expand All @@ -97,13 +99,13 @@ func (p *UberspaceProvider) Configure(ctx context.Context, req provider.Configur
return
}

user := configWithFallback(data.User, os.Getenv("UBERSPACE_USER"))
user := cmp.Or(data.User.ValueString(), os.Getenv("UBERSPACE_USER"))

sshClient, err := ssh.NewClient(&ssh.Config{
Host: configWithFallback(data.Host, os.Getenv("UBERSPACE_HOST")),
Host: cmp.Or(data.Host.ValueString(), os.Getenv("UBERSPACE_HOST")),
User: user,
Password: configWithFallback(data.Password, os.Getenv("UBERSPACE_PASSWORD")),
PrivateKey: configWithFallback(data.PrivateKey, os.Getenv("UBERSPACE_PRIVATE_KEY")),
Password: cmp.Or(data.Password.ValueString(), os.Getenv("UBERSPACE_PASSWORD")),
PrivateKey: cmp.Or(data.PrivateKey.ValueString(), os.Getenv("UBERSPACE_PRIVATE_KEY")),
})
if err != nil {
resp.Diagnostics.AddError("Failed to create SSH client", err.Error())
Expand All @@ -115,14 +117,6 @@ func (p *UberspaceProvider) Configure(ctx context.Context, req provider.Configur
resp.ResourceData = client
}

func configWithFallback(a *string, b string) string {
if a != nil {
return *a
}

return b
}

func (p *UberspaceProvider) Resources(_ context.Context) []func() resource.Resource {
return []func() resource.Resource{
NewCronTabEntryResource,
Expand Down