Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: relocate request #16

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions internal/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ type GenerateCommand struct {
type SpecificationWithSDK struct {
spec.Specification
Provider *mapper.ProviderWithEndpoint `json:"provider"`
Requests []mapper.Request `json:"requests"`
Resources []mapper.ResourceWithRefreshObjectName `json:"resources"`
DataSources []mapper.DataSourceWithRefreshObjectName `json:"datasources"`
}
Expand Down Expand Up @@ -235,17 +234,10 @@ func generateProviderCodeSpec(logger *slog.Logger, dora explorer.Explorer, cfg c
return nil, fmt.Errorf("error generating provider code spec for provider: %w", err)
}

requestMapper := mapper.NewRequestMapper(explorerResources, explorerDataSources, cfg)
requestsIR, err := requestMapper.MapToIR(logger)
if err != nil {
return nil, fmt.Errorf("error generating provider code spec for request: %w", err)
}

return &SpecificationWithSDK{
Specification: spec.Specification{
Version: spec.Version0_1,
},
Requests: requestsIR,
Provider: providerIR,
Resources: resourcesIR,
DataSources: dataSourcesIR,
Expand Down
15 changes: 12 additions & 3 deletions internal/mapper/datasource_mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ type DataSourceMapper interface {

type DataSourceWithRefreshObjectName struct {
datasource.DataSource
RefreshObjectName string `json:"refresh_object_name"`
ImportStateOverride string `json:"import_state_override"`
Id string `json:"id"`
CRUDParameters CRUDParameters `json:"crud_parameters"`
RefreshObjectName string `json:"refresh_object_name"`
ImportStateOverride string `json:"import_state_override"`
Id string `json:"id"`
}

type dataSourceMapper struct {
Expand All @@ -56,6 +57,13 @@ func (m dataSourceMapper) MapToIR(logger *slog.Logger) ([]DataSourceWithRefreshO
id := m.cfg.DataSources[name].Id
importStateOverride := m.cfg.DataSources[name].ImportStateOverride

requestMapper := NewDataSourceRequestMapper(dataSource, name, m.cfg)
datasourceRequestIR, err := requestMapper.MapToIR(logger)
if err != nil {
log.WarnLogOnError(dLogger, err, "skipping resource request mapping")
continue
}

var refreshObjectName string
t := m.cfg.DataSources[name].RefreshObjectName

Expand Down Expand Up @@ -87,6 +95,7 @@ func (m dataSourceMapper) MapToIR(logger *slog.Logger) ([]DataSourceWithRefreshO
Name: name,
Schema: schema,
},
CRUDParameters: datasourceRequestIR,
RefreshObjectName: refreshObjectName,
ImportStateOverride: importStateOverride,
Id: id,
Expand Down
69 changes: 69 additions & 0 deletions internal/mapper/datasource_request_mapper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package mapper

import (
"log/slog"

"github.com/NaverCloudPlatform/terraform-plugin-codegen-openapi/internal/config"
"github.com/NaverCloudPlatform/terraform-plugin-codegen-openapi/internal/explorer"
"github.com/NaverCloudPlatform/terraform-plugin-codegen-openapi/internal/log"
"github.com/NaverCloudPlatform/terraform-plugin-codegen-openapi/internal/mapper/oas"
"github.com/NaverCloudPlatform/terraform-plugin-codegen-spec/spec"
)

var _ RequestMapper = datasourceRequestMapper{}

type datasourceRequestMapper struct {
datasource explorer.DataSource
name string
cfg config.Config
}

func NewDataSourceRequestMapper(datasource explorer.DataSource, name string, cfg config.Config) datasourceRequestMapper {
return datasourceRequestMapper{
datasource: datasource,
name: name,
cfg: cfg,
}
}

func (m datasourceRequestMapper) MapToIR(logger *slog.Logger) (CRUDParameters, error) {
rLogger := logger.With("request", m.name)

requestType, err := generateRequestDataSourceType(rLogger, m.datasource, m.name, m.cfg)
if err != nil {
log.WarnLogOnError(rLogger, err, "skipping resource request type mapping")
}

return requestType, nil
}

func generateRequestDataSourceType(logger *slog.Logger, explorerDataSource explorer.DataSource, name string, config config.Config) (CRUDParameters, error) {
schemaOpts := oas.SchemaOpts{
Ignores: explorerDataSource.SchemaOptions.Ignores,
}

logger.Debug("searching for read operation parameters and request body")
requestBody, err := extractRequestBody(explorerDataSource.ReadOp, schemaOpts)
if err != nil {
log.WarnLogOnError(logger, err, "skipping mapping of read operation request body")
}
response, err := extractResponse(explorerDataSource.ReadOp, schemaOpts)
if err != nil {
log.WarnLogOnError(logger, err, "skipping mappin gof read operation response")
}
readRequest := NcloudCommonRequestType{
DetailedRequestType: DetailedRequestType{
RequestType: spec.RequestType{
Response: response,
},
Parameters: extractParametersInfo(explorerDataSource.ReadOp),
RequestBody: requestBody,
},
Method: config.DataSources[name].Read.Method,
Path: config.DataSources[name].Read.Path,
}

return CRUDParameters{
Read: readRequest,
}, nil
}
15 changes: 12 additions & 3 deletions internal/mapper/resource_mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ type ResourceMapper interface {

type ResourceWithRefreshObjectName struct {
resource.Resource
RefreshObjectName string `json:"refresh_object_name"`
ImportStateOverride string `json:"import_state_override"`
Id string `json:"id"`
CRUDParameters CRUDParameters `json:"crud_parameters"`
RefreshObjectName string `json:"refresh_object_name"`
ImportStateOverride string `json:"import_state_override"`
Id string `json:"id"`
}

type resourceMapper struct {
Expand All @@ -55,6 +56,13 @@ func (m resourceMapper) MapToIR(logger *slog.Logger) ([]ResourceWithRefreshObjec
id := m.cfg.Resources[name].Id
importStateOverride := m.cfg.Resources[name].ImportStateOverride

requestMapper := NewResourceRequestMapper(explorerResource, name, m.cfg)
resourceRequestIR, err := requestMapper.MapToIR(logger)
if err != nil {
log.WarnLogOnError(rLogger, err, "skipping resource request mapping")
continue
}

var refreshObjectName string
t := m.cfg.Resources[name].RefreshObjectName

Expand Down Expand Up @@ -86,6 +94,7 @@ func (m resourceMapper) MapToIR(logger *slog.Logger) ([]ResourceWithRefreshObjec
Name: name,
Schema: schema,
},
CRUDParameters: resourceRequestIR,
RefreshObjectName: refreshObjectName,
ImportStateOverride: importStateOverride,
Id: id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import (
"github.com/pb33f/libopenapi/orderedmap"
)

var _ RequestMapper = requestMapper{}
var _ RequestMapper = resourceRequestMapper{}

type RequestMapper interface {
MapToIR(*slog.Logger) ([]Request, error)
MapToIR(*slog.Logger) (CRUDParameters, error)
}

type NcloudCommonRequestType struct {
Expand All @@ -27,22 +27,17 @@ type NcloudCommonRequestType struct {
Path string `json:"path,omitempty"`
}

type RequestOperations struct {
type CRUDParameters struct {
Create NcloudCommonRequestType `json:"create,omitempty"`
Read NcloudCommonRequestType `json:"read,omitempty"`
Update []*NcloudCommonRequestType `json:"update,omitempty"`
Delete NcloudCommonRequestType `json:"delete,omitempty"`
}

type Request struct {
RequestOperations
Name string `json:"name,omitempty"`
}

type requestMapper struct {
resources map[string]explorer.Resource
dataSources map[string]explorer.DataSource
cfg config.Config
type resourceRequestMapper struct {
resource explorer.Resource
name string
cfg config.Config
}

type NcloudRequestBody struct {
Expand All @@ -68,50 +63,26 @@ type DetailedRequestType struct {
RequestBody *NcloudRequestBody `json:"request_body,omitempty"`
}

func NewRequestMapper(resources map[string]explorer.Resource, dataSources map[string]explorer.DataSource, cfg config.Config) RequestMapper {
return requestMapper{
resources: resources,
dataSources: dataSources,
cfg: cfg,
func NewResourceRequestMapper(resource explorer.Resource, name string, cfg config.Config) resourceRequestMapper {
return resourceRequestMapper{
resource: resource,
name: name,
cfg: cfg,
}
}

func (m requestMapper) MapToIR(logger *slog.Logger) ([]Request, error) {
requestSchemas := []Request{}

resourceNames := util.SortedKeys(m.resources)
dataSourceNames := util.SortedKeys(m.dataSources)

for _, name := range resourceNames {
explorerResource := m.resources[name]
rLogger := logger.With("request", name)

requestType, err := generateRequestType(rLogger, explorerResource, name, m.cfg)
if err != nil {
log.WarnLogOnError(rLogger, err, "skipping resource request type mapping")
continue
}

requestSchemas = append(requestSchemas, requestType)
}

for _, name := range dataSourceNames {
explorerDataSource := m.dataSources[name]
dsLogger := logger.With("request", name)
func (m resourceRequestMapper) MapToIR(logger *slog.Logger) (CRUDParameters, error) {
rLogger := logger.With("request", m.name)

requestType, err := generateRequestDataSourceType(dsLogger, explorerDataSource, name, m.cfg)
if err != nil {
log.WarnLogOnError(dsLogger, err, "skipping data source request type mapping")
continue
}

requestSchemas = append(requestSchemas, requestType)
requestType, err := generateResourceRequestType(rLogger, m.resource, m.name, m.cfg)
if err != nil {
log.WarnLogOnError(rLogger, err, "skipping resource request type mapping")
}

return requestSchemas, nil
return requestType, nil
}

func generateRequestType(logger *slog.Logger, explorerResource explorer.Resource, name string, config config.Config) (Request, error) {
func generateResourceRequestType(logger *slog.Logger, explorerResource explorer.Resource, name string, config config.Config) (CRUDParameters, error) {
schemaOpts := oas.SchemaOpts{
Ignores: explorerResource.SchemaOptions.Ignores,
}
Expand Down Expand Up @@ -203,48 +174,11 @@ func generateRequestType(logger *slog.Logger, explorerResource explorer.Resource
Path: config.Resources[name].Delete.Path,
}

return Request{
Name: name,
RequestOperations: RequestOperations{
Create: createRequest,
Read: readRequest,
Update: updateRequest,
Delete: deleteRequest,
},
}, nil
}

func generateRequestDataSourceType(logger *slog.Logger, explorerDataSource explorer.DataSource, name string, config config.Config) (Request, error) {
schemaOpts := oas.SchemaOpts{
Ignores: explorerDataSource.SchemaOptions.Ignores,
}

logger.Debug("searching for read operation parameters and request body")
requestBody, err := extractRequestBody(explorerDataSource.ReadOp, schemaOpts)
if err != nil {
log.WarnLogOnError(logger, err, "skipping mapping of read operation request body")
}
response, err := extractResponse(explorerDataSource.ReadOp, schemaOpts)
if err != nil {
log.WarnLogOnError(logger, err, "skipping mappin gof read operation response")
}
readRequest := NcloudCommonRequestType{
DetailedRequestType: DetailedRequestType{
RequestType: spec.RequestType{
Response: response,
},
Parameters: extractParametersInfo(explorerDataSource.ReadOp),
RequestBody: requestBody,
},
Method: config.DataSources[name].Read.Method,
Path: config.DataSources[name].Read.Path,
}

return Request{
Name: name,
RequestOperations: RequestOperations{
Read: readRequest,
},
return CRUDParameters{
Create: createRequest,
Read: readRequest,
Update: updateRequest,
Delete: deleteRequest,
}, nil
}

Expand Down