-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathdatasource_mapper.go
More file actions
167 lines (134 loc) · 5.37 KB
/
datasource_mapper.go
File metadata and controls
167 lines (134 loc) · 5.37 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
// Copyright IBM Corp. 2023, 2026
// SPDX-License-Identifier: MPL-2.0
package mapper
import (
"fmt"
"log/slog"
"github.com/hashicorp/terraform-plugin-codegen-openapi/internal/config"
"github.com/hashicorp/terraform-plugin-codegen-openapi/internal/explorer"
"github.com/hashicorp/terraform-plugin-codegen-openapi/internal/log"
"github.com/hashicorp/terraform-plugin-codegen-openapi/internal/mapper/attrmapper"
"github.com/hashicorp/terraform-plugin-codegen-openapi/internal/mapper/oas"
"github.com/hashicorp/terraform-plugin-codegen-openapi/internal/mapper/util"
"github.com/hashicorp/terraform-plugin-codegen-spec/datasource"
"github.com/hashicorp/terraform-plugin-codegen-spec/schema"
)
var _ DataSourceMapper = dataSourceMapper{}
type DataSourceMapper interface {
MapToIR(*slog.Logger) ([]datasource.DataSource, error)
}
type dataSourceMapper struct {
dataSources map[string]explorer.DataSource
//nolint:unused // Might be useful later!
cfg config.Config
}
func NewDataSourceMapper(dataSources map[string]explorer.DataSource, cfg config.Config) DataSourceMapper {
return dataSourceMapper{
dataSources: dataSources,
cfg: cfg,
}
}
func (m dataSourceMapper) MapToIR(logger *slog.Logger) ([]datasource.DataSource, error) {
dataSourceSchemas := []datasource.DataSource{}
// Guarantee the order of processing
dataSourceNames := util.SortedKeys(m.dataSources)
for _, name := range dataSourceNames {
dataSource := m.dataSources[name]
dLogger := logger.With("data_source", name)
schema, err := generateDataSourceSchema(dLogger, name, dataSource)
if err != nil {
log.WarnLogOnError(dLogger, err, "skipping data source schema mapping")
continue
}
dataSourceSchemas = append(dataSourceSchemas, datasource.DataSource{
Name: name,
Schema: schema,
})
}
return dataSourceSchemas, nil
}
func generateDataSourceSchema(logger *slog.Logger, name string, dataSource explorer.DataSource) (*datasource.Schema, error) {
dataSourceSchema := &datasource.Schema{
Attributes: []datasource.Attribute{},
}
// ********************
// READ Response Body (required)
// ********************
logger.Debug("searching for read operation response body")
schemaOpts := oas.SchemaOpts{
Ignores: dataSource.SchemaOptions.Ignores,
}
globalSchemaOpts := oas.GlobalSchemaOpts{
OverrideComputability: schema.Computed,
}
readResponseSchema, err := oas.BuildSchemaFromResponse(dataSource.ReadOp, schemaOpts, globalSchemaOpts)
if err != nil {
return nil, err
}
readResponseAttributes := attrmapper.DataSourceAttributes{}
if readResponseSchema.Type == util.OAS_type_array {
logger.Debug(fmt.Sprintf("response body is an array, building '%s' set attribute", name))
// API's generally don't guarantee ordering of results for collection/query responses, default mapping to set
readResponseSchema.Format = util.TF_format_set
collectionAttribute, schemaErr := readResponseSchema.BuildDataSourceAttribute(name, schema.Computed)
if schemaErr != nil {
return nil, schemaErr
}
readResponseAttributes = append(readResponseAttributes, collectionAttribute)
} else {
attributes, schemaErr := readResponseSchema.BuildDataSourceAttributes()
if schemaErr != nil {
return nil, schemaErr
}
readResponseAttributes = attributes
}
// ****************
// READ Parameters (optional)
// ****************
readParameterAttributes := attrmapper.DataSourceAttributes{}
for _, param := range dataSource.ReadOpParameters() {
if param.In != util.OAS_param_path && param.In != util.OAS_param_query {
continue
}
pLogger := logger.With("param", param.Name)
schemaOpts := oas.SchemaOpts{
Ignores: dataSource.SchemaOptions.Ignores,
OverrideDescription: param.Description,
}
s, schemaErr := oas.BuildSchema(param.Schema, schemaOpts, oas.GlobalSchemaOpts{})
if schemaErr != nil {
log.WarnLogOnError(pLogger, schemaErr, "skipping mapping of read operation parameter")
continue
}
computability := schema.ComputedOptional
if param.Required != nil && *param.Required {
computability = schema.Required
}
// Check for any aliases and replace the paramater name if found
paramName := param.Name
if aliasedName, ok := dataSource.SchemaOptions.AttributeOptions.Aliases[param.Name]; ok {
pLogger = pLogger.With("param_alias", aliasedName)
paramName = aliasedName
}
if s.IsPropertyIgnored(paramName) {
continue
}
parameterAttribute, schemaErr := s.BuildDataSourceAttribute(paramName, computability)
if schemaErr != nil {
log.WarnLogOnError(pLogger, schemaErr, "skipping mapping of read operation parameter")
continue
}
readParameterAttributes = append(readParameterAttributes, parameterAttribute)
}
// TODO: currently, no errors can be returned from merging, but in the future we should consider raising errors/warnings for unexpected scenarios, like type mismatches between attribute schemas
dataSourceAttributes, _ := readParameterAttributes.Merge(readResponseAttributes)
// TODO: handle error for overrides
dataSourceAttributes, _ = dataSourceAttributes.ApplyOverrides(dataSource.SchemaOptions.AttributeOptions.Overrides)
dataSourceSchema.Attributes = dataSourceAttributes.ToSpec()
// Set the data source-level description from OpenAPI tags if available
if dataSource.Description != "" {
dataSourceSchema.Description = &dataSource.Description
dataSourceSchema.MarkdownDescription = &dataSource.Description
}
return dataSourceSchema, nil
}