Skip to content

Commit 5ca793b

Browse files
committed
feat: add support for path-prefix
some APIs add prefixes to all paths, rather than embedding them into the server URL. This provides a configuration to work around that.
1 parent 944d5a6 commit 5ca793b

4 files changed

Lines changed: 12 additions & 67 deletions

File tree

cmd/aepcli/main.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@ func aepcli() error {
4141
}
4242

4343
var rawHeaders []string
44+
var pathPrefix string
4445
rootCmd.Flags().SetInterspersed(false) // allow sub parsers to parse subsequent flags after the resource
4546
rootCmd.PersistentFlags().StringArrayVar(&rawHeaders, "header", []string{}, "Specify headers in the format key=value")
4647
rootCmd.PersistentFlags().StringVar(&logLevel, "log-level", "info", "Set the logging level (debug, info, warn, error)")
48+
rootCmd.PersistentFlags().StringVar(&pathPrefix, "path-prefix", "", "Specify a path prefix that is prepended to all paths in the openapi schema. This will strip them when evaluating the resource hierarchy paths.")
4749
rootCmd.MarkPersistentFlagRequired("host")
4850

4951
if err := rootCmd.Execute(); err != nil {
@@ -67,6 +69,9 @@ func aepcli() error {
6769
os.Exit(1)
6870
}
6971
fileOrAlias = filepath.Join(cd, api.OpenAPIPath)
72+
if api.PathPrefix == "" {
73+
pathPrefix = api.PathPrefix
74+
}
7075
rawHeaders = append(rawHeaders, api.Headers...)
7176
}
7277

@@ -75,7 +80,7 @@ func aepcli() error {
7580
fmt.Println(err)
7681
os.Exit(1)
7782
}
78-
serviceDefinition, err := service.GetServiceDefinition(openapi)
83+
serviceDefinition, err := service.GetServiceDefinition(openapi, pathPrefix)
7984
if err != nil {
8085
fmt.Println(err)
8186
os.Exit(1)
@@ -91,8 +96,7 @@ func aepcli() error {
9196

9297
result, err := s.ExecuteCommand(resource, additionalArgs)
9398
if err != nil {
94-
fmt.Println("an error occurred: %v", err)
95-
os.Exit(1)
99+
return err
96100
}
97101
fmt.Println(result)
98102
return nil

internal/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type API struct {
1616
Name string
1717
OpenAPIPath string
1818
Headers []string
19+
PathPrefix string
1920
}
2021

2122
// ReadConfig reads the configuration from the default configuration file.

internal/service/service_definition.go

Lines changed: 3 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ type ServiceDefinition struct {
1515
Resources map[string]*Resource
1616
}
1717

18-
func GetServiceDefinition2(api *openapi.OpenAPI) (*ServiceDefinition, error) {
18+
func GetServiceDefinition(api *openapi.OpenAPI, pathPrefix string) (*ServiceDefinition, error) {
1919
resourceBySingular := make(map[string]*Resource)
2020
// we try to parse the paths to find possible resources, since
2121
// they may not always be annotated as such.
2222
for path, pathItem := range api.Paths {
23+
path = strings.TrimPrefix(path, pathPrefix)
2324
var r Resource
2425
var sRef *openapi.Schema
2526
p := getPatternInfo(path)
@@ -103,7 +104,7 @@ func GetServiceDefinition2(api *openapi.OpenAPI) (*ServiceDefinition, error) {
103104
// get the first serverURL url
104105
serverURL := ""
105106
for _, s := range api.Servers {
106-
serverURL = s.URL
107+
serverURL = s.URL + pathPrefix
107108
}
108109
if serverURL == "" {
109110
return nil, errors.New("no servers found in the OpenAPI definition. Cannot find a server to send a request to")
@@ -115,31 +116,6 @@ func GetServiceDefinition2(api *openapi.OpenAPI) (*ServiceDefinition, error) {
115116
}, nil
116117
}
117118

118-
func GetServiceDefinition(api *openapi.OpenAPI) (*ServiceDefinition, error) {
119-
resources := make(map[string]*Resource)
120-
for name, s := range api.Components.Schemas {
121-
if s.XAEPResource != nil {
122-
_, err := addResourceToMap(s, resources, api)
123-
if err != nil {
124-
return nil, fmt.Errorf("error adding resource %q to map: %v", name, err)
125-
}
126-
}
127-
}
128-
// get the first serverURL url
129-
serverURL := ""
130-
for _, s := range api.Servers {
131-
serverURL = s.URL
132-
}
133-
if serverURL == "" {
134-
return nil, errors.New("no servers found in the OpenAPI definition. Cannot find a server to send a request to")
135-
}
136-
137-
return &ServiceDefinition{
138-
ServerURL: serverURL,
139-
Resources: resources,
140-
}, nil
141-
}
142-
143119
func (s *ServiceDefinition) GetResource(resource string) (*Resource, error) {
144120
r, ok := (*s).Resources[resource]
145121
if !ok {
@@ -148,42 +124,6 @@ func (s *ServiceDefinition) GetResource(resource string) (*Resource, error) {
148124
return r, nil
149125
}
150126

151-
func addResourceToMap(s openapi.Schema, resourceMap map[string]*Resource, api *openapi.OpenAPI) (*Resource, error) {
152-
r := s.XAEPResource
153-
if r == nil {
154-
return nil, fmt.Errorf("schema does not have the x-aep-resource annotation")
155-
}
156-
singular := strings.ToLower(r.Singular)
157-
if r, ok := resourceMap[singular]; ok {
158-
return r, nil
159-
}
160-
parents := []*Resource{}
161-
for _, p := range r.Parents {
162-
s, ok := api.Components.Schemas[p]
163-
if !ok {
164-
return nil, fmt.Errorf("resource %q parent %q not found", r.Singular, p)
165-
}
166-
parentResource, err := addResourceToMap(s, resourceMap, api)
167-
if err != nil {
168-
return nil, fmt.Errorf("error parsing resource %q parent %q: %v", r.Singular, p, err)
169-
}
170-
parents = append(parents, parentResource)
171-
}
172-
173-
resource := Resource{
174-
Singular: r.Singular,
175-
Plural: r.Plural,
176-
Parents: parents,
177-
Pattern: strings.Split(r.Patterns[0], "/")[1:],
178-
Schema: &s,
179-
}
180-
if existingResource, found := resourceMap[strings.ToLower(r.Plural)]; found {
181-
foldResourceMethods(existingResource, &resource)
182-
}
183-
resourceMap[singular] = &resource
184-
return &resource, nil
185-
}
186-
187127
type PatternInfo struct {
188128
// if true, the pattern represents an individual resource,
189129
// otherwise it represents a path to a collection of resources

internal/service/service_definition_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func TestGetServiceDefinition2(t *testing.T) {
182182

183183
for _, tt := range tests {
184184
t.Run(tt.name, func(t *testing.T) {
185-
result, err := GetServiceDefinition2(tt.api)
185+
result, err := GetServiceDefinition(tt.api)
186186

187187
if tt.expectedError != "" {
188188
assert.Error(t, err)

0 commit comments

Comments
 (0)