Skip to content

Commit

Permalink
refactor: extract datasource and resource ConfigureClient
Browse files Browse the repository at this point in the history
  • Loading branch information
apricote committed Oct 24, 2023
1 parent 7de7501 commit 303bc2e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
32 changes: 8 additions & 24 deletions internal/datacenter/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,21 +135,13 @@ func (d *datacenterDataSource) Metadata(_ context.Context, _ datasource.Metadata
// provider-defined DataSource type. It is separately executed for each
// ReadDataSource RPC.
func (d *datacenterDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
var newDiags diag.Diagnostics

// TODO: refactor to a reusable function
client, ok := req.ProviderData.(*hcloud.Client)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *hcloud.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
d.client, newDiags = hcclient.ConfigureClient(req.ProviderData)
resp.Diagnostics.Append(newDiags...)
if resp.Diagnostics.HasError() {
return
}

d.client = client
}

// Schema should return the schema for this data source.
Expand Down Expand Up @@ -254,21 +246,13 @@ func (d *datacenterListDataSource) Metadata(_ context.Context, _ datasource.Meta
// provider-defined DataSource type. It is separately executed for each
// ReadDataSource RPC.
func (d *datacenterListDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
var newDiags diag.Diagnostics

// TODO: refactor to a reusable function
client, ok := req.ProviderData.(*hcloud.Client)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *hcloud.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
d.client, newDiags = hcclient.ConfigureClient(req.ProviderData)
resp.Diagnostics.Append(newDiags...)
if resp.Diagnostics.HasError() {
return
}

d.client = client
}

// Schema should return the schema for this data source.
Expand Down
28 changes: 28 additions & 0 deletions internal/hcclient/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package hcclient

import (
"fmt"

"github.com/hashicorp/terraform-plugin-framework/diag"

"github.com/hetznercloud/hcloud-go/hcloud"
)

func ConfigureClient(providerData any) (*hcloud.Client, diag.Diagnostics) {
var diagnostics diag.Diagnostics

if providerData == nil {
return nil, diagnostics
}

client, ok := providerData.(*hcloud.Client)
if !ok {
diagnostics.AddError(
"Unexpected Configure Type",
fmt.Sprintf("Expected *hcloud.Client, got: %T. Please report this issue to the provider developers.", providerData),
)
return nil, diagnostics
}

return client, diagnostics
}

0 comments on commit 303bc2e

Please sign in to comment.