-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathattr_schema.go
146 lines (122 loc) · 4.33 KB
/
attr_schema.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2016-2023, Pulumi Corporation.
//
// 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 schemashim
import (
pfattr "github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/pulumi/pulumi-terraform-bridge/pf/internal/pfutils"
bridge "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfbridge"
shim "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim"
)
type attrSchema struct {
key string
attr pfutils.Attr
}
var _ shim.Schema = (*attrSchema)(nil)
func (s *attrSchema) Type() shim.ValueType {
ty := s.attr.GetType()
vt, err := convertType(ty)
if err != nil {
panic(err)
}
return vt
}
func (s *attrSchema) Optional() bool {
return s.attr.IsOptional()
}
func (s *attrSchema) Required() bool {
return s.attr.IsRequired()
}
func (*attrSchema) Default() interface{} {
panic("Default() should not be called during schema generation")
}
func (*attrSchema) DefaultFunc() shim.SchemaDefaultFunc {
panic("DefaultFunc() should not be called during schema generation")
}
func (*attrSchema) DefaultValue() (interface{}, error) {
// DefaultValue() should not be called by tfgen, but it currently may be called by ExtractInputsFromOutputs, so
// returning nil is better than a panic.
return nil, bridge.ErrSchemaDefaultValue
}
func (s *attrSchema) Description() string {
if desc := s.attr.GetMarkdownDescription(); desc != "" {
return desc
}
return s.attr.GetDescription()
}
func (s *attrSchema) Computed() bool {
return s.attr.IsComputed()
}
func (s *attrSchema) ForceNew() bool {
// TODO[pulumi/pulumi-terraform-bridge#818] try harder to detect
return false
}
func (*attrSchema) StateFunc() shim.SchemaStateFunc {
// StateFunc() should not be called by tfgen, but it currently may be called by ExtractInputsFromOutputs, so
// returning nil is better than a panic.
return nil
}
// Needs to return a shim.Schema, a shim.Resource, or nil.
func (s *attrSchema) Elem() interface{} {
switch t := s.attr.GetType().(type) {
// The ObjectType can be triggered through tfsdk.SingleNestedAttributes. Logically it defines an attribute with
// a type that is an Object type. To encode the schema of the Object type in a way the shim layer understands,
// Elem() needes to return a Resource value.
//
// See also: documentation on shim.Schema.Elem().
case basetypes.ObjectTypable:
var res shim.Resource = newObjectPseudoResource(t, s.attr.Nested(), nil)
return res
case pfattr.TypeWithElementTypes:
var res shim.Resource = newTuplePseudoResource(t)
return res
case pfattr.TypeWithElementType:
return shim.Schema(newTypeSchema(t.ElementType(), s.attr.Nested()))
// t does not support any kind of element type.
default:
return nil
}
}
func (*attrSchema) MaxItems() int {
// There seems to be no way to find MaxItems/MinItems on attributes. There are workarounds employed to find them
// for Blocks.
return 0
}
func (*attrSchema) MinItems() int {
// There seems to be no way to find MaxItems/MinItems on attributes. There are workarounds employed to find them
// for Blocks.
return 0
}
func (*attrSchema) ConflictsWith() []string {
panic("ConflictsWith() should not be called during schema generation")
}
func (*attrSchema) ExactlyOneOf() []string {
panic("ExactlyOneOf() should not be called during schema generation")
}
func (s *attrSchema) Deprecated() string {
return s.attr.GetDeprecationMessage()
}
func (*attrSchema) Removed() string {
// Following v2, returning empty string here. This does not seem to be supported.
return ""
}
func (s *attrSchema) Sensitive() bool {
return s.attr.IsSensitive()
}
func (*attrSchema) SetElement(config interface{}) (interface{}, error) {
panic("SetElement() should not be called during schema generation")
}
func (*attrSchema) SetHash(v interface{}) int {
panic("SetHash() should not be called during schema generation")
}