-
Notifications
You must be signed in to change notification settings - Fork 43
/
validate.go
209 lines (177 loc) · 5.5 KB
/
validate.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// 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 info
import (
"context"
"errors"
"fmt"
shim "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim"
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim/util"
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim/walk"
)
// Validate [Provider].
//
// Validate is automatically called as part of `make tfgen`.
func (p *Provider) Validate(context.Context) error {
c := new(infoCheck)
res := p.P.ResourcesMap()
for tk, schema := range p.Resources {
tf, ok := res.GetOk(tk)
if !ok {
// This is checked elsewhere.
continue
}
c.checkResource(tk, tf.Schema(), schema.Fields)
}
return c.errorOrNil()
}
func (c *infoCheck) errorOrNil() error {
errs := make([]error, len(c.errors))
for i, e := range c.errors {
errs[i] = e
}
return errors.Join(errs...)
}
// During tfgen time, we validate that providers are correctly configured.
//
// That means that we error if non-existent fields are specified or if overrides have no effect.
type checkError struct {
tfToken string
path walk.SchemaPath
err error
}
func (e checkError) Unwrap() error { return e.err }
func (e checkError) Error() string {
return fmt.Sprintf("%s: %s: %s", e.tfToken, e.path, e.err.Error())
}
type infoCheck struct {
errors []checkError
finishError func(e *checkError)
}
func (c *infoCheck) error(path walk.SchemaPath, err error) {
e := checkError{
path: path,
err: err,
}
c.finishError(&e)
c.errors = append(c.errors, e)
}
var (
errNoCorrespondingField = fmt.Errorf("overriding non-existent field")
errNoElemToOverride = fmt.Errorf("overriding non-existent elem")
errCannotSpecifyFieldsOnListOrSet = fmt.Errorf("cannot specify .Fields on a List[T] or Set[T] type")
errCannotSpecifyNameOnElem = fmt.Errorf("cannot specify .Name on a List[T], Map[T] or Set[T] type")
errCannotSetMaxItemsOne = fmt.Errorf("cannot specify .MaxItemsOne on a scalar type")
errElemForObject = fmt.Errorf("cannot set .Elem on a singly nested object block")
)
func (c *infoCheck) checkResource(tfToken string, schema shim.SchemaMap, info map[string]*Schema) {
c.finishError = func(e *checkError) {
e.tfToken = tfToken
}
defer func() {
c.finishError = nil
}()
c.checkFields(walk.NewSchemaPath(), schema, info)
}
func (c *infoCheck) checkProperty(path walk.SchemaPath, tfs shim.Schema, ps *Schema) {
// If there is no override, then there were no mistakes.
if ps == nil {
return
}
// s.Elem() case 2
//
// `path` represents a singly-nested Terraform block.
//
// Conceptually `path` and `path.Elem` represent the same object.
//
// To prevent confusion, users are barred from specifying information on
// the associated Elem. All information should be specified directly on
// this SchemaInfo.
if obj, ok := util.CastToTypeObject(tfs); ok {
if ps.Elem != nil {
c.error(path, errElemForObject)
}
if ps.MaxItemsOne != nil {
c.error(path.Element(), errCannotSetMaxItemsOne)
}
// Now check sub-fields
c.checkFields(path, obj, ps.Fields)
return
}
if len(ps.Fields) > 0 {
c.error(path, errCannotSpecifyFieldsOnListOrSet)
}
switch elem := tfs.Elem().(type) {
// s.Elem() case 1
//
// There is a simple element type here, so we just recursively validate that.
case shim.Schema:
c.checkElem(path.Element(), elem, ps.Elem)
// Either `path` represents an object nested under a list or set, or `path` is
// itself an object, depending on the .Type() property.
case shim.Resource:
// s.Elem() case 3
//
// `path` represents a List[elem] or Set[elem].
// Check the nested fields
if ps.Elem != nil {
c.checkFields(path.Element(), elem.Schema(), ps.Elem.Fields)
}
// There is no element for this shim.Schema shape.
//
// The only thing the user can do wrong here is to customize the element.
case nil:
switch tfs.Type() {
// s.Elem() case 5
case shim.TypeMap, shim.TypeList, shim.TypeSet:
// The type is unknown, but specifying overrides is invalid.
//
// We can't check any deeper, so return
return
}
// s.Elem() case 4
//
// It is not valid to specify .Elem
if ps.Elem != nil {
c.error(path.Element(), errNoElemToOverride)
}
if ps.MaxItemsOne != nil {
c.error(path.Element(), errCannotSetMaxItemsOne)
}
}
}
// Check a nested element.
func (c *infoCheck) checkElem(path walk.SchemaPath, tfs shim.Schema, ps *Schema) {
if ps == nil {
return
}
if ps.Name != "" {
// If we are an element type, and we are not an object (which has a name),
// then it doesn't make sense to provide a name since the elem will be
// accessed by an index.
c.error(path, errCannotSpecifyNameOnElem)
}
c.checkProperty(path, tfs, ps)
}
func (c *infoCheck) checkFields(path walk.SchemaPath, tfs shim.SchemaMap, ps map[string]*Schema) {
for k, p := range ps {
elemPath := path.GetAttr(k)
elemTfs, ok := tfs.GetOk(k)
if !ok {
c.error(elemPath, errNoCorrespondingField)
continue
}
c.checkProperty(elemPath, elemTfs, p)
}
}