-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathresource_mapper.go
More file actions
193 lines (162 loc) · 6.57 KB
/
resource_mapper.go
File metadata and controls
193 lines (162 loc) · 6.57 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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package mapper
import (
"errors"
"log/slog"
"strings"
"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/resource"
"github.com/hashicorp/terraform-plugin-codegen-spec/schema"
)
var _ ResourceMapper = resourceMapper{}
type ResourceMapper interface {
MapToIR(*slog.Logger) ([]resource.Resource, error)
}
type resourceMapper struct {
resources map[string]explorer.Resource
//nolint:unused // Might be useful later!
cfg config.Config
}
func NewResourceMapper(resources map[string]explorer.Resource, cfg config.Config) ResourceMapper {
return resourceMapper{
resources: resources,
cfg: cfg,
}
}
func (m resourceMapper) MapToIR(logger *slog.Logger) ([]resource.Resource, error) {
resourceSchemas := []resource.Resource{}
// Guarantee the order of processing
resourceNames := util.SortedKeys(m.resources)
for _, name := range resourceNames {
explorerResource := m.resources[name]
rLogger := logger.With("resource", name)
schema, err := generateResourceSchema(rLogger, explorerResource)
if err != nil {
log.WarnLogOnError(rLogger, err, "skipping resource schema mapping")
continue
}
resourceSchemas = append(resourceSchemas, resource.Resource{
Name: name,
Schema: schema,
})
}
return resourceSchemas, nil
}
func generateResourceSchema(logger *slog.Logger, explorerResource explorer.Resource) (*resource.Schema, error) {
resourceSchema := &resource.Schema{
Attributes: []resource.Attribute{},
}
// ********************
// Create Request Body (required)
// ********************
logger.Debug("searching for create operation request body")
schemaOpts := oas.SchemaOpts{
Ignores: explorerResource.SchemaOptions.Ignores,
}
createRequestSchema, err := oas.BuildSchemaFromRequest(explorerResource.CreateOp, schemaOpts, oas.GlobalSchemaOpts{})
if err != nil {
return nil, err
}
createRequestAttributes, schemaErr := createRequestSchema.BuildResourceAttributes()
if schemaErr != nil {
return nil, schemaErr
}
// *********************
// Create Response Body (optional)
// *********************
logger.Debug("searching for create operation response body")
createResponseAttributes := attrmapper.ResourceAttributes{}
schemaOpts = oas.SchemaOpts{
Ignores: explorerResource.SchemaOptions.Ignores,
}
globalSchemaOpts := oas.GlobalSchemaOpts{
OverrideComputability: schema.Computed,
}
createResponseSchema, err := oas.BuildSchemaFromResponse(explorerResource.CreateOp, schemaOpts, globalSchemaOpts)
if err != nil {
if errors.Is(err, oas.ErrSchemaNotFound) {
// Demote log to INFO if there was no schema found
logger.Info("skipping mapping of create operation response body", "err", err)
} else {
logger.Warn("skipping mapping of create operation response body", "err", err)
}
} else {
createResponseAttributes, schemaErr = createResponseSchema.BuildResourceAttributes()
if schemaErr != nil {
log.WarnLogOnError(logger, schemaErr, "skipping mapping of create operation response body")
}
}
// *******************
// READ Response Body (optional)
// *******************
logger.Debug("searching for read operation response body")
readResponseAttributes := attrmapper.ResourceAttributes{}
schemaOpts = oas.SchemaOpts{
Ignores: explorerResource.SchemaOptions.Ignores,
}
globalSchemaOpts = oas.GlobalSchemaOpts{
OverrideComputability: schema.Computed,
}
readResponseSchema, err := oas.BuildSchemaFromResponse(explorerResource.ReadOp, schemaOpts, globalSchemaOpts)
if err != nil {
if errors.Is(err, oas.ErrSchemaNotFound) {
// Demote log to INFO if there was no schema found
logger.Info("skipping mapping of read operation response body", "err", err)
} else {
logger.Warn("skipping mapping of read operation response body", "err", err)
}
} else {
readResponseAttributes, schemaErr = readResponseSchema.BuildResourceAttributes()
if schemaErr != nil {
log.WarnLogOnError(logger, schemaErr, "skipping mapping of read operation response body")
}
}
// ****************
// READ Parameters (optional)
// ****************
for _, param := range explorerResource.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: explorerResource.SchemaOptions.Ignores,
OverrideDescription: param.Description,
}
globalSchemaOpts := oas.GlobalSchemaOpts{OverrideComputability: schema.ComputedOptional}
s, schemaErr := oas.BuildSchema(param.Schema, schemaOpts, globalSchemaOpts)
if schemaErr != nil {
log.WarnLogOnError(pLogger, schemaErr, "skipping mapping of read operation parameter")
continue
}
// Check for any aliases and replace the paramater name if found
paramName := param.Name
if aliasedName, ok := explorerResource.SchemaOptions.AttributeOptions.Aliases[param.Name]; ok {
pLogger = pLogger.With("param_alias", aliasedName)
paramName = aliasedName
}
if s.IsPropertyIgnored(paramName) {
continue
}
paramPath := strings.Split(paramName, ".")
parameterAttribute, schemaErr := s.BuildResourceAttribute(paramPath[len(paramPath)-1], schema.ComputedOptional)
if schemaErr != nil {
log.WarnLogOnError(pLogger, schemaErr, "skipping mapping of read operation parameter")
continue
}
// 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
readResponseAttributes, _ = readResponseAttributes.MergeAttribute(paramPath, parameterAttribute, schema.ComputedOptional)
}
// 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
resourceAttributes, _ := createRequestAttributes.Merge(createResponseAttributes, readResponseAttributes)
// TODO: handle error for overrides
resourceAttributes, _ = resourceAttributes.ApplyOverrides(explorerResource.SchemaOptions.AttributeOptions.Overrides)
resourceSchema.Attributes = resourceAttributes.ToSpec()
return resourceSchema, nil
}