|
| 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