Skip to content

Commit

Permalink
refactor: move data.hcloud_ssh_key(s) to Plugin Framework (#817)
Browse files Browse the repository at this point in the history
Related to #752

Co-authored-by: Julian Tölle <[email protected]>
  • Loading branch information
jooola and apricote authored Mar 11, 2024
1 parent 7d8bf6e commit 3bc9b13
Show file tree
Hide file tree
Showing 13 changed files with 793 additions and 330 deletions.
7 changes: 6 additions & 1 deletion hcloud/plugin_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/hetznercloud/hcloud-go/hcloud"
"github.com/hetznercloud/terraform-provider-hcloud/internal/datacenter"
"github.com/hetznercloud/terraform-provider-hcloud/internal/location"
"github.com/hetznercloud/terraform-provider-hcloud/internal/sshkey"
"github.com/hetznercloud/terraform-provider-hcloud/internal/util/tflogutil"
)

Expand Down Expand Up @@ -171,6 +172,8 @@ func (p *PluginProvider) DataSources(_ context.Context) []func() datasource.Data
datacenter.NewDataSourceList,
location.NewDataSource,
location.NewDataSourceList,
sshkey.NewDataSource,
sshkey.NewDataSourceList,
}
}

Expand All @@ -180,5 +183,7 @@ func (p *PluginProvider) DataSources(_ context.Context) []func() datasource.Data
// The resource type name is determined by the Resource implementing
// the Metadata method. All resources must have unique names.
func (p *PluginProvider) Resources(_ context.Context) []func() resource.Resource {
return []func() resource.Resource{}
return []func() resource.Resource{
sshkey.NewResource,
}
}
4 changes: 0 additions & 4 deletions hcloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"github.com/hetznercloud/terraform-provider-hcloud/internal/rdns"
"github.com/hetznercloud/terraform-provider-hcloud/internal/server"
"github.com/hetznercloud/terraform-provider-hcloud/internal/servertype"
"github.com/hetznercloud/terraform-provider-hcloud/internal/sshkey"
"github.com/hetznercloud/terraform-provider-hcloud/internal/volume"
)

Expand Down Expand Up @@ -96,7 +95,6 @@ func Provider() *schema.Provider {
server.NetworkResourceType: server.NetworkResource(),
server.ResourceType: server.Resource(),
snapshot.ResourceType: snapshot.Resource(),
sshkey.ResourceType: sshkey.Resource(),
volume.AttachmentResourceType: volume.AttachmentResource(),
volume.ResourceType: volume.Resource(),
placementgroup.ResourceType: placementgroup.Resource(),
Expand All @@ -122,8 +120,6 @@ func Provider() *schema.Provider {
server.DataSourceListType: server.DataSourceList(),
servertype.DataSourceType: servertype.DataSource(),
servertype.DataSourceListType: servertype.ServerTypesDataSource(),
sshkey.DataSourceType: sshkey.DataSource(),
sshkey.DataSourceListType: sshkey.DataSourceList(),
volume.DataSourceType: volume.DataSource(),
volume.DataSourceListType: volume.DataSourceList(),
},
Expand Down
4 changes: 0 additions & 4 deletions hcloud/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/hetznercloud/terraform-provider-hcloud/internal/server"
"github.com/hetznercloud/terraform-provider-hcloud/internal/servertype"
"github.com/hetznercloud/terraform-provider-hcloud/internal/snapshot"
"github.com/hetznercloud/terraform-provider-hcloud/internal/sshkey"
"github.com/hetznercloud/terraform-provider-hcloud/internal/volume"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -48,7 +47,6 @@ func TestProvider_Resources(t *testing.T) {
server.NetworkResourceType,
server.ResourceType,
snapshot.ResourceType,
sshkey.ResourceType,
volume.AttachmentResourceType,
volume.ResourceType,
placementgroup.ResourceType,
Expand Down Expand Up @@ -85,8 +83,6 @@ func TestProvider_DataSources(t *testing.T) {
server.DataSourceListType,
servertype.DataSourceType,
servertype.DataSourceListType,
sshkey.DataSourceType,
sshkey.DataSourceListType,
volume.DataSourceType,
volume.DataSourceListType,
}
Expand Down
34 changes: 34 additions & 0 deletions internal/sshkey/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package sshkey

import (
"context"

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

"github.com/hetznercloud/hcloud-go/hcloud"
"github.com/hetznercloud/terraform-provider-hcloud/internal/util/resourceutil"
)

type resourceData struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Fingerprint types.String `tfsdk:"fingerprint"`
PublicKey types.String `tfsdk:"public_key"`
Labels types.Map `tfsdk:"labels"`
}

func populateResourceData(ctx context.Context, data *resourceData, in *hcloud.SSHKey) diag.Diagnostics {
var diags diag.Diagnostics
var newDiags diag.Diagnostics

data.ID = resourceutil.IDStringValue(in.ID)
data.Name = types.StringValue(in.Name)
data.Fingerprint = types.StringValue(in.Fingerprint)
data.PublicKey = types.StringValue(in.PublicKey)

data.Labels, newDiags = resourceutil.LabelsMapValueFrom(ctx, in.Labels)
diags.Append(newDiags...)

return diags
}
Loading

0 comments on commit 3bc9b13

Please sign in to comment.