-
Notifications
You must be signed in to change notification settings - Fork 5k
Expand file tree
/
Copy pathwrite_only.go
More file actions
37 lines (30 loc) · 1.32 KB
/
write_only.go
File metadata and controls
37 lines (30 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package pluginsdk
import (
"fmt"
"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-cty/cty"
)
// GetWriteOnly gets a write only attribute, checking that it is of an expected type and subsequently returns it
func GetWriteOnly(d *ResourceData, name string, attributeType cty.Type) (*cty.Value, error) {
value, diags := d.GetRawConfigAt(cty.GetAttrPath(name))
if diags.HasError() {
return nil, fmt.Errorf("retrieving write-only attribute `%s`: %+v", name, diags)
}
if !value.Type().Equals(attributeType) {
return nil, fmt.Errorf("retrieving write-only attribute `%s`: value is not of type %v", name, attributeType)
}
return pointer.To(value), nil
}
// GetWriteOnlyFromDiff gets a write only attribute from the diff, checking that it is of an expected type and subsequently returns it
func GetWriteOnlyFromDiff(d *ResourceDiff, name string, attributeType cty.Type) (*cty.Value, error) {
value, diags := d.GetRawConfigAt(cty.GetAttrPath(name))
if diags.HasError() {
return nil, fmt.Errorf("retrieving write-only attribute `%s`: %+v", name, diags)
}
if !value.Type().Equals(attributeType) {
return nil, fmt.Errorf("retrieving write-only attribute `%s`: value is not of type %v", name, attributeType)
}
return pointer.To(value), nil
}