Skip to content

Commit 4f725b7

Browse files
committedSep 5, 2024
refactor: add deprecation util for plugin framework
1 parent d70208a commit 4f725b7

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed
 
+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package deprecation
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
"github.com/hashicorp/terraform-plugin-framework/attr"
8+
datasourceschema "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
9+
"github.com/hashicorp/terraform-plugin-framework/diag"
10+
resourceschema "github.com/hashicorp/terraform-plugin-framework/resource/schema"
11+
"github.com/hashicorp/terraform-plugin-framework/types"
12+
"github.com/hetznercloud/hcloud-go/hcloud"
13+
)
14+
15+
type Model struct {
16+
IsDeprecated types.Bool `tfsdk:"is_deprecated"`
17+
DeprecationAnnounced types.String `tfsdk:"deprecation_announced"`
18+
UnavailableAfter types.String `tfsdk:"unavailable_after"`
19+
}
20+
21+
type DeprecationModel = Model //nolint:revive
22+
23+
func NewModel(_ context.Context, in hcloud.Deprecatable) (Model, diag.Diagnostics) {
24+
var data Model
25+
var diags diag.Diagnostics
26+
27+
if in.IsDeprecated() {
28+
data.IsDeprecated = types.BoolValue(true)
29+
data.DeprecationAnnounced = types.StringValue(in.DeprecationAnnounced().Format(time.RFC3339))
30+
data.UnavailableAfter = types.StringValue(in.UnavailableAfter().Format(time.RFC3339))
31+
} else {
32+
data.IsDeprecated = types.BoolValue(false)
33+
34+
// TODO: Stored values should be types.StringNull(), but we use an empty string
35+
// for backward compatibility with the SDK.
36+
data.DeprecationAnnounced = types.StringValue("")
37+
data.UnavailableAfter = types.StringValue("")
38+
}
39+
40+
return data, diags
41+
}
42+
43+
func AttrTypes() map[string]attr.Type {
44+
return map[string]attr.Type{
45+
"is_deprecated": types.BoolType,
46+
"deprecation_announced": types.StringType,
47+
"unavailable_after": types.StringType,
48+
}
49+
}
50+
51+
func DataSourceSchema() map[string]datasourceschema.Attribute {
52+
return map[string]datasourceschema.Attribute{
53+
"is_deprecated": datasourceschema.BoolAttribute{
54+
Computed: true,
55+
},
56+
"deprecation_announced": datasourceschema.StringAttribute{
57+
Computed: true,
58+
Optional: true,
59+
},
60+
"unavailable_after": datasourceschema.StringAttribute{
61+
Computed: true,
62+
Optional: true,
63+
},
64+
}
65+
}
66+
67+
func ResourceSchema() map[string]resourceschema.Attribute {
68+
return map[string]resourceschema.Attribute{
69+
"is_deprecated": resourceschema.BoolAttribute{
70+
Computed: true,
71+
},
72+
"deprecation_announced": resourceschema.StringAttribute{
73+
Computed: true,
74+
Optional: true,
75+
},
76+
"unavailable_after": resourceschema.StringAttribute{
77+
Computed: true,
78+
Optional: true,
79+
},
80+
}
81+
}
File renamed without changes.

0 commit comments

Comments
 (0)
Please sign in to comment.