Skip to content

Commit

Permalink
tools/data-api: refactoring to use the models from `./tools/data-ap…
Browse files Browse the repository at this point in the history
…i-sdk`
  • Loading branch information
tombuildsstuff committed Feb 15, 2024
1 parent 1746798 commit 38ae8d6
Show file tree
Hide file tree
Showing 37 changed files with 889 additions and 1,660 deletions.
7 changes: 6 additions & 1 deletion tools/data-api/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ go 1.21
require (
github.com/go-chi/chi/v5 v5.0.8
github.com/go-chi/render v1.0.2
github.com/hashicorp/go-azure-helpers v0.64.0
github.com/hashicorp/go-azure-helpers v0.66.2
github.com/hashicorp/go-hclog v1.5.0
github.com/hashicorp/pandora/tools/data-api-sdk v0.0.0-00010101000000-000000000000
github.com/hashicorp/pandora/tools/sdk v0.0.0-00010101000000-000000000000
github.com/mitchellh/cli v1.1.5
)
Expand All @@ -21,7 +22,9 @@ require (
github.com/fatih/color v1.13.0 // indirect
github.com/google/uuid v1.1.2 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-retryablehttp v0.7.5 // indirect
github.com/huandu/xstrings v1.3.2 // indirect
github.com/imdario/mergo v0.3.11 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
Expand All @@ -36,3 +39,5 @@ require (
)

replace github.com/hashicorp/pandora/tools/sdk => ../sdk

replace github.com/hashicorp/pandora/tools/data-api-sdk => ../data-api-sdk
9 changes: 7 additions & 2 deletions tools/data-api/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,18 @@ github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-azure-helpers v0.64.0 h1:JNTt0F7tbApmpmF9hzNq2TN1lduOiXJFkMFoAPVctK8=
github.com/hashicorp/go-azure-helpers v0.64.0/go.mod h1:ELmZ65vzHJNTk6ml4jsPD+xq2gZb7t78D35s+XN02Kk=
github.com/hashicorp/go-azure-helpers v0.66.2 h1:+Pzuo7pdKl0hBXXr5ymmhs4Q40tHAo2nAvHq4WgSjx8=
github.com/hashicorp/go-azure-helpers v0.66.2/go.mod h1:kJxXrFtJKJdOEqvad8pllAe7dhP4DbN8J6sqFZe47+4=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
github.com/hashicorp/go-hclog v1.5.0 h1:bI2ocEMgcVlz55Oj1xZNBsVi900c7II+fWDyV9o+13c=
github.com/hashicorp/go-hclog v1.5.0/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M=
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/hashicorp/go-retryablehttp v0.7.5 h1:bJj+Pj19UZMIweq/iie+1u5YCdGrnxCT9yvm0e+Nd5M=
github.com/hashicorp/go-retryablehttp v0.7.5/go.mod h1:Jy/gPYAdjqffZ/yFGCFV2doI5wjtH1ewM9u8iYVjtX8=
github.com/huandu/xstrings v1.3.1/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/huandu/xstrings v1.3.2 h1:L18LIDzqlW6xN2rEkpdV8+oL/IXWJ1APd+vsdYy4Wdw=
github.com/huandu/xstrings v1.3.2/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
Expand Down
43 changes: 21 additions & 22 deletions tools/data-api/internal/endpoints/v1/common_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
"net/http"

"github.com/go-chi/render"
"github.com/hashicorp/pandora/tools/data-api/models"
"github.com/hashicorp/pandora/tools/data-api-sdk/v1/models"
"github.com/hashicorp/pandora/tools/data-api/internal/endpoints/v1/transforms"
)

func (api Api) commonTypes(w http.ResponseWriter, r *http.Request) {
Expand All @@ -26,9 +27,9 @@ func (api Api) commonTypes(w http.ResponseWriter, r *http.Request) {
return
}

payload := models.CommonTypesDetails{
Constants: map[string]models.ConstantDetails{},
Models: map[string]models.ModelDetails{},
payload := models.CommonTypes{
Constants: map[string]models.SDKConstant{},
Models: map[string]models.SDKModel{},
}
if !opts.UsesCommonTypes || services == nil {
render.JSON(w, r, payload)
Expand All @@ -40,39 +41,37 @@ func (api Api) commonTypes(w http.ResponseWriter, r *http.Request) {
if details == nil {
continue
}

for resourceName, resource := range details.Resources {
if resource == nil {
continue
}
for k, v := range resource.Schema.Constants {

mappedConstants, err := transforms.MapConstants(resource.Schema.Constants)
if err != nil {
internalServerError(w, fmt.Errorf("mapping constants for API Resource %q: %+v", resourceName, err))
return
}
for k, v := range *mappedConstants {
if _, ok := payload.Constants[k]; ok {
internalServerError(w, fmt.Errorf("constant %q already exists in common types, there is a duplicated definition in the source data for service: %s, version: %s, resource: %s", k, service.Name, version, resourceName))
return
}
payload.Constants[k] = models.ConstantDetails{
Type: models.ConstantType(v.Type),
Values: v.Values,
}
payload.Constants[k] = v
}

for k, v := range resource.Schema.Models {
mappedModels, err := transforms.MapSDKModels(resource.Schema.Models)
if err != nil {
internalServerError(w, fmt.Errorf("mapping models for API Resource %q: %+v", resourceName, err))
return
}
for k, v := range *mappedModels {
if _, ok := payload.Models[k]; ok {
internalServerError(w, fmt.Errorf("model %q already exists in common types, there is a duplicated definition in the source data for service: %s, version: %s, resource: %s", k, service.Name, version, resourceName))
return
}
fields, err := mapSchemaFields(v.Fields)
if err != nil {
internalServerError(w, fmt.Errorf("mapping fields for model %q in service: %s, version: %s, resource: %s: %+v", k, service.Name, version, resourceName, err))
return
}
payload.Models[k] = models.ModelDetails{
Fields: fields,
ParentTypeName: v.ParentTypeName,
TypeHintIn: v.TypeHintIn,
TypeHintValue: v.TypeHintValue,
}
payload.Models[k] = v
}

}

}
Expand Down
19 changes: 12 additions & 7 deletions tools/data-api/internal/endpoints/v1/details_for_api_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
"net/http"

"github.com/go-chi/render"
v1 "github.com/hashicorp/pandora/tools/data-api-sdk/v1"
"github.com/hashicorp/pandora/tools/data-api/internal/endpoints/v1/transforms"
"github.com/hashicorp/pandora/tools/data-api/internal/repositories"
"github.com/hashicorp/pandora/tools/data-api/models"
)

func (api Api) detailsForApiVersion(w http.ResponseWriter, r *http.Request) {
Expand All @@ -31,18 +32,22 @@ func (api Api) detailsForApiVersion(w http.ResponseWriter, r *http.Request) {
internalServerError(w, fmt.Errorf("missing serviceApiVersion"))
return
}
resources := make(map[string]models.ResourceSummary, 0)

resources := make(map[string]v1.APIResourceSummary, 0)
for k := range apiVersion.Resources {
resources[k] = models.ResourceSummary{
OperationsUri: fmt.Sprintf("%s/services/%s/%s/%s/operations", opts.UriPrefix, service.Name, apiVersion.Name, k),
SchemaUri: fmt.Sprintf("%s/services/%s/%s/%s/schema", opts.UriPrefix, service.Name, apiVersion.Name, k),
resources[k] = v1.APIResourceSummary{
OperationsURI: fmt.Sprintf("%s/services/%s/%s/%s/operations", opts.UriPrefix, service.Name, apiVersion.Name, k),
SchemaURI: fmt.Sprintf("%s/services/%s/%s/%s/schema", opts.UriPrefix, service.Name, apiVersion.Name, k),
}
}
source, err := transforms.MapSourceDataOrigin(apiVersion.Source)
if err != nil {
internalServerError(w, fmt.Errorf("mapping SourceDataOrigin: %+v", err))
}

payload := models.ServiceVersionDetails{
payload := v1.DetailsForAPIVersionSummary{
Resources: resources,
Source: models.ApiDefinitionsSource(apiVersion.Source),
Source: *source,
}
render.JSON(w, r, payload)
}
1 change: 1 addition & 0 deletions tools/data-api/internal/endpoints/v1/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
)

func internalServerError(w http.ResponseWriter, err error) {
// TODO: update errors
log.Printf("[ERROR] %+v", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
172 changes: 0 additions & 172 deletions tools/data-api/internal/endpoints/v1/mappings.go

This file was deleted.

Loading

0 comments on commit 38ae8d6

Please sign in to comment.