-
Notifications
You must be signed in to change notification settings - Fork 0
/
uuid_type.go
117 lines (97 loc) · 3.25 KB
/
uuid_type.go
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
* Copyright 2022 Matthew Hartstonge <matt@mykro.co.nz>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package uuidtypes
import (
// Standard Library Imports
"context"
"fmt"
// External Imports
"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/attr/xattr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)
// Ensure Implementation matches the expected interfaces.
var (
_ attr.Type = UUIDType{}
_ tftypes.AttributePathStepper = UUIDType{}
_ xattr.TypeWithValidate = UUIDType{}
)
type UUIDType struct{}
// ApplyTerraform5AttributePathStep always returns an error as this type cannot
// be walked any further.
func (u UUIDType) ApplyTerraform5AttributePathStep(step tftypes.AttributePathStep) (interface{}, error) {
return nil, fmt.Errorf("cannot apply AttributePathStep to %T to %s", step, u.String())
}
// Equal returns true if the incoming Type is equal to the UUID type.
func (u UUIDType) Equal(other attr.Type) bool {
_, ok := other.(UUIDType)
return ok
}
// String returns a human-friendly version of the UUID Type.
func (u UUIDType) String() string {
return "uuidtypes.UUIDType"
}
// TerraformType returns tftypes.String.
func (u UUIDType) TerraformType(_ context.Context) tftypes.Type {
return tftypes.String
}
// Validate returns any warnings or errors that occur while attempting to parse
// a UUID value.
func (u UUIDType) Validate(_ context.Context, value tftypes.Value, schemaPath path.Path) diag.Diagnostics {
if value.IsNull() || !value.IsKnown() {
return nil
}
var str string
err := value.As(&str)
if err != nil {
return diag.Diagnostics{
diag.NewAttributeErrorDiagnostic(
schemaPath,
"Invalid UUID Terraform Value",
"An unexpected error occurred while attempting to read a UUID string from the Terraform value. "+
"Please contact the provider developers with the following:\n\n"+
"Error: "+err.Error(),
),
}
}
_, diags := UUIDFromString(str, schemaPath)
return diags
}
// ValueFromTerraform returns a UUID value given a tftypes.Value.
func (u UUIDType) ValueFromTerraform(_ context.Context, value tftypes.Value) (attr.Value, error) {
if value.IsNull() {
return UUIDNull(), nil
}
if !value.IsKnown() {
return UUIDUnknown(), nil
}
var str string
if err := value.As(&str); err != nil {
return UUIDUnknown(), err
}
parsedUUID, err := uuid.Parse(str)
if err != nil {
return UUIDUnknown(), err
}
return UUIDFromGoogleUUID(parsedUUID), nil
}
// ValueType returns attr.Value type returned by ValueFromTerraform.
func (u UUIDType) ValueType(context.Context) attr.Value {
return UUID{}
}