Skip to content

Commit

Permalink
refactor: add deprecation util for plugin framework
Browse files Browse the repository at this point in the history
  • Loading branch information
jooola committed Sep 5, 2024
1 parent d70208a commit 4f725b7
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions internal/deprecation/deprecation_plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package deprecation

import (
"context"
"time"

"github.com/hashicorp/terraform-plugin-framework/attr"
datasourceschema "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/diag"
resourceschema "github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hetznercloud/hcloud-go/hcloud"
)

type Model struct {
IsDeprecated types.Bool `tfsdk:"is_deprecated"`
DeprecationAnnounced types.String `tfsdk:"deprecation_announced"`
UnavailableAfter types.String `tfsdk:"unavailable_after"`
}

type DeprecationModel = Model //nolint:revive

func NewModel(_ context.Context, in hcloud.Deprecatable) (Model, diag.Diagnostics) {
var data Model
var diags diag.Diagnostics

if in.IsDeprecated() {
data.IsDeprecated = types.BoolValue(true)
data.DeprecationAnnounced = types.StringValue(in.DeprecationAnnounced().Format(time.RFC3339))
data.UnavailableAfter = types.StringValue(in.UnavailableAfter().Format(time.RFC3339))
} else {
data.IsDeprecated = types.BoolValue(false)

// TODO: Stored values should be types.StringNull(), but we use an empty string
// for backward compatibility with the SDK.
data.DeprecationAnnounced = types.StringValue("")
data.UnavailableAfter = types.StringValue("")
}

return data, diags
}

func AttrTypes() map[string]attr.Type {
return map[string]attr.Type{
"is_deprecated": types.BoolType,
"deprecation_announced": types.StringType,
"unavailable_after": types.StringType,
}
}

func DataSourceSchema() map[string]datasourceschema.Attribute {
return map[string]datasourceschema.Attribute{
"is_deprecated": datasourceschema.BoolAttribute{
Computed: true,
},
"deprecation_announced": datasourceschema.StringAttribute{
Computed: true,
Optional: true,
},
"unavailable_after": datasourceschema.StringAttribute{
Computed: true,
Optional: true,
},
}
}

func ResourceSchema() map[string]resourceschema.Attribute {
return map[string]resourceschema.Attribute{
"is_deprecated": resourceschema.BoolAttribute{
Computed: true,
},
"deprecation_announced": resourceschema.StringAttribute{
Computed: true,
Optional: true,
},
"unavailable_after": resourceschema.StringAttribute{
Computed: true,
Optional: true,
},
}
}
File renamed without changes.

0 comments on commit 4f725b7

Please sign in to comment.