-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathconfig_explorer.go
More file actions
220 lines (185 loc) · 6.89 KB
/
config_explorer.go
File metadata and controls
220 lines (185 loc) · 6.89 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
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
210
211
212
213
214
215
216
217
218
219
220
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package explorer
import (
"context"
"errors"
"fmt"
"strings"
"github.com/hashicorp/terraform-plugin-codegen-openapi/internal/config"
highbase "github.com/pb33f/libopenapi/datamodel/high/base"
high "github.com/pb33f/libopenapi/datamodel/high/v3"
lowmodel "github.com/pb33f/libopenapi/datamodel/low"
lowbase "github.com/pb33f/libopenapi/datamodel/low/base"
low "github.com/pb33f/libopenapi/datamodel/low/v3"
)
var _ Explorer = configExplorer{}
type configExplorer struct {
spec high.Document
config config.Config
}
// A ConfigExplorer will use an additional config file to identify resource and data source operations in a provided
// OpenAPIv3 spec. This additional config file will provide information such as:
// - Create/Read/Update/Delete endpoints/URLs (schema will be automatically grabbed via request/response body and parameters in mapper)
// - Resource + Data Source names
func NewConfigExplorer(spec high.Document, cfg config.Config) Explorer {
return configExplorer{
spec: spec,
config: cfg,
}
}
func (e configExplorer) FindProvider() (Provider, error) {
foundProvider := Provider{
Name: e.config.Provider.Name,
}
if e.config.Provider.SchemaRef == "" {
return foundProvider, nil
}
schemaProxy, err := extractSchemaProxy(e.spec, e.config.Provider.SchemaRef)
if err != nil {
return Provider{}, fmt.Errorf("error extracting provider schema from ref: %w", err)
}
foundProvider.SchemaProxy = schemaProxy
foundProvider.Ignores = e.config.Provider.Ignores
return foundProvider, nil
}
func (e configExplorer) FindResources() (map[string]Resource, error) {
resources := map[string]Resource{}
var errResult error
for name, resourceConfig := range e.config.Resources {
createOp, err := extractOp(e.spec.Paths, resourceConfig.Create)
if err != nil {
errResult = errors.Join(errResult, fmt.Errorf("failed to extract '%s.create': %w", name, err))
continue
}
readOp, err := extractOp(e.spec.Paths, resourceConfig.Read)
if err != nil {
errResult = errors.Join(errResult, fmt.Errorf("failed to extract '%s.read': %w", name, err))
continue
}
updateOp, err := extractOp(e.spec.Paths, resourceConfig.Update)
if err != nil {
errResult = errors.Join(errResult, fmt.Errorf("failed to extract '%s.update': %w", name, err))
continue
}
deleteOp, err := extractOp(e.spec.Paths, resourceConfig.Delete)
if err != nil {
errResult = errors.Join(errResult, fmt.Errorf("failed to extract '%s.delete': %w", name, err))
continue
}
commonParameters, err := extractCommonParameters(e.spec.Paths, resourceConfig.Read.Path)
if err != nil {
errResult = errors.Join(errResult, fmt.Errorf("failed to extract '%s' common parameters: %w", name, err))
continue
}
resources[name] = Resource{
CreateOp: createOp,
ReadOp: readOp,
UpdateOp: updateOp,
DeleteOp: deleteOp,
CommonParameters: commonParameters,
SchemaOptions: extractSchemaOptions(resourceConfig.SchemaOptions),
}
}
return resources, errResult
}
func (e configExplorer) FindDataSources() (map[string]DataSource, error) {
dataSources := map[string]DataSource{}
var errResult error
for name, dataSourceConfig := range e.config.DataSources {
readOp, err := extractOp(e.spec.Paths, dataSourceConfig.Read)
if err != nil {
errResult = errors.Join(errResult, fmt.Errorf("failed to extract '%s.read': %w", name, err))
continue
}
commonParameters, err := extractCommonParameters(e.spec.Paths, dataSourceConfig.Read.Path)
if err != nil {
errResult = errors.Join(errResult, fmt.Errorf("failed to extract '%s' common parameters: %w", name, err))
continue
}
dataSources[name] = DataSource{
ReadOp: readOp,
CommonParameters: commonParameters,
SchemaOptions: extractSchemaOptions(dataSourceConfig.SchemaOptions),
}
}
return dataSources, errResult
}
func extractOp(paths *high.Paths, oasLocation *config.OpenApiSpecLocation) (*high.Operation, error) {
// No need to search OAS if not defined
if oasLocation == nil {
return nil, nil
}
if paths == nil || paths.PathItems == nil || paths.PathItems.GetOrZero(oasLocation.Path) == nil {
return nil, fmt.Errorf("path '%s' not found in OpenAPI spec", oasLocation.Path)
}
pathItem, _ := paths.PathItems.Get(oasLocation.Path)
switch strings.ToLower(oasLocation.Method) {
case low.PostLabel:
return pathItem.Post, nil
case low.GetLabel:
return pathItem.Get, nil
case low.PutLabel:
return pathItem.Put, nil
case low.DeleteLabel:
return pathItem.Delete, nil
case low.PatchLabel:
return pathItem.Patch, nil
case low.OptionsLabel:
return pathItem.Options, nil
case low.HeadLabel:
return pathItem.Head, nil
case low.TraceLabel:
return pathItem.Trace, nil
default:
return nil, fmt.Errorf("method '%s' not found at OpenAPI path '%s'", oasLocation.Method, oasLocation.Path)
}
}
func extractCommonParameters(paths *high.Paths, path string) ([]*high.Parameter, error) {
// No need to search OAS if not defined
if paths.PathItems.GetOrZero(path) == nil {
return nil, fmt.Errorf("path '%s' not found in OpenAPI spec", path)
}
pathItem, _ := paths.PathItems.Get(path)
return pathItem.Parameters, nil
}
func extractSchemaProxy(document high.Document, componentRef string) (*highbase.SchemaProxy, error) {
// find the reference using the root document.Index
indexRef := document.Index.FindComponentInRoot(componentRef)
if indexRef == nil {
return nil, fmt.Errorf("unable to find reference: %s", componentRef)
}
// build low-level schema using YAML node
var lowSchema lowbase.Schema
err := lowmodel.BuildModel(indexRef.Node, &lowSchema)
if err != nil {
return nil, fmt.Errorf("error building low-level schema: %w", err)
}
// populate low-level schema, using root document.Index for resolving
err = lowSchema.Build(context.TODO(), indexRef.Node, document.Index)
if err != nil {
return nil, fmt.Errorf("error populating low-level schema: %w", err)
}
// build high-level schema from low-level schema
highSchema := highbase.NewSchema(&lowSchema)
// wrap in a schema proxy for mapping with `oas` package
return highbase.CreateSchemaProxy(highSchema), nil
}
func extractSchemaOptions(cfgSchemaOpts config.SchemaOptions) config.SchemaOptions {
return config.SchemaOptions{
Ignores: cfgSchemaOpts.Ignores,
AttributeOptions: config.AttributeOptions{
Aliases: cfgSchemaOpts.AttributeOptions.Aliases,
Overrides: extractOverrides(cfgSchemaOpts.AttributeOptions.Overrides),
},
}
}
func extractOverrides(cfgOverrides map[string]config.Override) map[string]config.Override {
overrides := make(map[string]config.Override, len(cfgOverrides))
for key, cfgOverride := range cfgOverrides {
overrides[key] = config.Override{Description: cfgOverride.Description}
overrides[key] = config.Override{CustomOverrides: cfgOverride.CustomOverrides}
overrides[key] = config.Override{Schema: cfgOverride.Schema}
}
return overrides
}