Skip to content

Commit 7eba2e7

Browse files
committed
add tfsdk object
1 parent dd0bcbc commit 7eba2e7

File tree

3 files changed

+579
-0
lines changed

3 files changed

+579
-0
lines changed

internal/fwschemadata/data_description.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ const (
1919
// DataDescriptionEphemeralResultData is used for Data that represents
2020
// the result of an ephemeral operation.
2121
DataDescriptionEphemeralResultData DataDescription = "ephemeral result data"
22+
23+
// DataDescriptionResourceIdentity is used for Data that represents
24+
// a managed resource identity.
25+
DataDescriptionResourceIdentity DataDescription = "resource identity"
2226
)
2327

2428
// DataDescription is a human friendly type for Data. Used in error
@@ -46,6 +50,8 @@ func (d DataDescription) Title() string {
4650
return "State"
4751
case DataDescriptionEphemeralResultData:
4852
return "Ephemeral Result Data"
53+
case DataDescriptionResourceIdentity:
54+
return "Resource Identity"
4955
default:
5056
return "Data"
5157
}

tfsdk/resource_identity.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package tfsdk
5+
6+
import (
7+
"context"
8+
9+
"github.com/hashicorp/terraform-plugin-framework/diag"
10+
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
11+
"github.com/hashicorp/terraform-plugin-framework/internal/fwschemadata"
12+
"github.com/hashicorp/terraform-plugin-framework/path"
13+
"github.com/hashicorp/terraform-plugin-go/tftypes"
14+
)
15+
16+
// ResourceIdentity represents the identity data for a managed resource.
17+
type ResourceIdentity struct {
18+
Raw tftypes.Value
19+
Schema fwschema.Schema
20+
}
21+
22+
// Get populates the struct passed as `target` with the entire identity.
23+
func (s ResourceIdentity) Get(ctx context.Context, target interface{}) diag.Diagnostics {
24+
return s.data().Get(ctx, target)
25+
}
26+
27+
// GetAttribute retrieves the attribute found at `path` and populates
28+
// the `target` with the value.
29+
//
30+
// Elements under null or unknown collections return null values, however this
31+
// behavior is not protected by compatibility promises.
32+
func (s ResourceIdentity) GetAttribute(ctx context.Context, path path.Path, target interface{}) diag.Diagnostics {
33+
return s.data().GetAtPath(ctx, path, target)
34+
}
35+
36+
// PathMatches returns all matching path.Paths from the given path.Expression.
37+
//
38+
// If a parent path is null or unknown, which would prevent a full expression
39+
// from matching, the parent path is returned rather than no match to prevent
40+
// false positives.
41+
func (s ResourceIdentity) PathMatches(ctx context.Context, pathExpr path.Expression) (path.Paths, diag.Diagnostics) {
42+
return s.data().PathMatches(ctx, pathExpr)
43+
}
44+
45+
// Set populates the entire identity using the supplied Go value. The value `val`
46+
// should be a struct whose values have one of the attr.Value types. Each field
47+
// must be tagged with the corresponding schema field.
48+
func (s *ResourceIdentity) Set(ctx context.Context, val interface{}) diag.Diagnostics {
49+
data := s.data()
50+
diags := data.Set(ctx, val)
51+
52+
if diags.HasError() {
53+
return diags
54+
}
55+
56+
s.Raw = data.TerraformValue
57+
58+
return diags
59+
}
60+
61+
// SetAttribute sets the attribute at `path` using the supplied Go value.
62+
//
63+
// The attribute path and value must be valid with the current schema. If the
64+
// attribute path already has a value, it will be overwritten. If the attribute
65+
// path does not have a value, it will be added.
66+
//
67+
// The value must not be an untyped nil. Use a typed nil or types package null
68+
// value function instead. For example with a types.StringType attribute,
69+
// use (*string)(nil) or types.StringNull().
70+
//
71+
// Lists can only have the next element added according to the current length.
72+
func (s *ResourceIdentity) SetAttribute(ctx context.Context, path path.Path, val interface{}) diag.Diagnostics {
73+
data := s.data()
74+
diags := data.SetAtPath(ctx, path, val)
75+
76+
if diags.HasError() {
77+
return diags
78+
}
79+
80+
s.Raw = data.TerraformValue
81+
82+
return diags
83+
}
84+
85+
func (s ResourceIdentity) data() *fwschemadata.Data {
86+
return &fwschemadata.Data{
87+
Description: fwschemadata.DataDescriptionResourceIdentity,
88+
Schema: s.Schema,
89+
TerraformValue: s.Raw,
90+
}
91+
}

0 commit comments

Comments
 (0)