From ee1a6718d7adb53dc00b85346138f96f40d79ed9 Mon Sep 17 00:00:00 2001 From: Alex <33497058+bexsoft@users.noreply.github.com> Date: Thu, 4 Feb 2021 17:57:10 -0600 Subject: [PATCH] Changed policy API to receive name param in query instead or URL (#591) Co-authored-by: Benjamin Perez --- .../screens/Console/Policies/DeletePolicy.tsx | 2 +- restapi/embedded_spec.go | 12 +++--- restapi/operations/admin_api/policy_info.go | 2 +- .../admin_api/policy_info_parameters.go | 20 +++++++--- .../admin_api/policy_info_urlbuilder.go | 19 +++++----- restapi/operations/admin_api/remove_policy.go | 2 +- .../admin_api/remove_policy_parameters.go | 20 +++++++--- .../admin_api/remove_policy_urlbuilder.go | 19 +++++----- restapi/operations/console_api.go | 4 +- swagger.yml | 38 +++++++++---------- 10 files changed, 80 insertions(+), 58 deletions(-) diff --git a/portal-ui/src/screens/Console/Policies/DeletePolicy.tsx b/portal-ui/src/screens/Console/Policies/DeletePolicy.tsx index 36d33e090..b3f4f9ceb 100644 --- a/portal-ui/src/screens/Console/Policies/DeletePolicy.tsx +++ b/portal-ui/src/screens/Console/Policies/DeletePolicy.tsx @@ -49,7 +49,7 @@ const DeletePolicy = ({ } setDeleteLoading(true); api - .invoke("DELETE", `/api/v1/policies/${selectedPolicy}`) + .invoke("DELETE", `/api/v1/policy?name=${selectedPolicy}`) .then((res: PolicyList) => { setDeleteLoading(false); diff --git a/restapi/embedded_spec.go b/restapi/embedded_spec.go index 7a4439645..868ef8d12 100644 --- a/restapi/embedded_spec.go +++ b/restapi/embedded_spec.go @@ -2263,7 +2263,7 @@ func init() { } } }, - "/policies/{name}": { + "/policy": { "get": { "tags": [ "AdminAPI" @@ -2274,7 +2274,7 @@ func init() { { "type": "string", "name": "name", - "in": "path", + "in": "query", "required": true } ], @@ -2303,7 +2303,7 @@ func init() { { "type": "string", "name": "name", - "in": "path", + "in": "query", "required": true } ], @@ -7735,7 +7735,7 @@ func init() { } } }, - "/policies/{name}": { + "/policy": { "get": { "tags": [ "AdminAPI" @@ -7746,7 +7746,7 @@ func init() { { "type": "string", "name": "name", - "in": "path", + "in": "query", "required": true } ], @@ -7775,7 +7775,7 @@ func init() { { "type": "string", "name": "name", - "in": "path", + "in": "query", "required": true } ], diff --git a/restapi/operations/admin_api/policy_info.go b/restapi/operations/admin_api/policy_info.go index c1616c04f..686ff4ee5 100644 --- a/restapi/operations/admin_api/policy_info.go +++ b/restapi/operations/admin_api/policy_info.go @@ -48,7 +48,7 @@ func NewPolicyInfo(ctx *middleware.Context, handler PolicyInfoHandler) *PolicyIn return &PolicyInfo{Context: ctx, Handler: handler} } -/*PolicyInfo swagger:route GET /policies/{name} AdminAPI policyInfo +/*PolicyInfo swagger:route GET /policy AdminAPI policyInfo Policy info diff --git a/restapi/operations/admin_api/policy_info_parameters.go b/restapi/operations/admin_api/policy_info_parameters.go index e048ed461..92e6fa21a 100644 --- a/restapi/operations/admin_api/policy_info_parameters.go +++ b/restapi/operations/admin_api/policy_info_parameters.go @@ -26,8 +26,10 @@ import ( "net/http" "github.com/go-openapi/errors" + "github.com/go-openapi/runtime" "github.com/go-openapi/runtime/middleware" "github.com/go-openapi/strfmt" + "github.com/go-openapi/validate" ) // NewPolicyInfoParams creates a new PolicyInfoParams object @@ -48,7 +50,7 @@ type PolicyInfoParams struct { /* Required: true - In: path + In: query */ Name string } @@ -62,8 +64,10 @@ func (o *PolicyInfoParams) BindRequest(r *http.Request, route *middleware.Matche o.HTTPRequest = r - rName, rhkName, _ := route.Params.GetOK("name") - if err := o.bindName(rName, rhkName, route.Formats); err != nil { + qs := runtime.Values(r.URL.Query()) + + qName, qhkName, _ := qs.GetOK("name") + if err := o.bindName(qName, qhkName, route.Formats); err != nil { res = append(res, err) } @@ -73,15 +77,21 @@ func (o *PolicyInfoParams) BindRequest(r *http.Request, route *middleware.Matche return nil } -// bindName binds and validates parameter Name from path. +// bindName binds and validates parameter Name from query. func (o *PolicyInfoParams) bindName(rawData []string, hasKey bool, formats strfmt.Registry) error { + if !hasKey { + return errors.Required("name", "query", rawData) + } var raw string if len(rawData) > 0 { raw = rawData[len(rawData)-1] } // Required: true - // Parameter is provided by construction from the route + // AllowEmptyValue: false + if err := validate.RequiredString("name", "query", raw); err != nil { + return err + } o.Name = raw diff --git a/restapi/operations/admin_api/policy_info_urlbuilder.go b/restapi/operations/admin_api/policy_info_urlbuilder.go index 946404727..aa8b4bc0b 100644 --- a/restapi/operations/admin_api/policy_info_urlbuilder.go +++ b/restapi/operations/admin_api/policy_info_urlbuilder.go @@ -26,7 +26,6 @@ import ( "errors" "net/url" golangswaggerpaths "path" - "strings" ) // PolicyInfoURL generates an URL for the policy info operation @@ -57,14 +56,7 @@ func (o *PolicyInfoURL) SetBasePath(bp string) { func (o *PolicyInfoURL) Build() (*url.URL, error) { var _result url.URL - var _path = "/policies/{name}" - - name := o.Name - if name != "" { - _path = strings.Replace(_path, "{name}", name, -1) - } else { - return nil, errors.New("name is required on PolicyInfoURL") - } + var _path = "/policy" _basePath := o._basePath if _basePath == "" { @@ -72,6 +64,15 @@ func (o *PolicyInfoURL) Build() (*url.URL, error) { } _result.Path = golangswaggerpaths.Join(_basePath, _path) + qs := make(url.Values) + + nameQ := o.Name + if nameQ != "" { + qs.Set("name", nameQ) + } + + _result.RawQuery = qs.Encode() + return &_result, nil } diff --git a/restapi/operations/admin_api/remove_policy.go b/restapi/operations/admin_api/remove_policy.go index a80df451f..1efe44c8f 100644 --- a/restapi/operations/admin_api/remove_policy.go +++ b/restapi/operations/admin_api/remove_policy.go @@ -48,7 +48,7 @@ func NewRemovePolicy(ctx *middleware.Context, handler RemovePolicyHandler) *Remo return &RemovePolicy{Context: ctx, Handler: handler} } -/*RemovePolicy swagger:route DELETE /policies/{name} AdminAPI removePolicy +/*RemovePolicy swagger:route DELETE /policy AdminAPI removePolicy Remove policy diff --git a/restapi/operations/admin_api/remove_policy_parameters.go b/restapi/operations/admin_api/remove_policy_parameters.go index 2df84218f..5cbc7de48 100644 --- a/restapi/operations/admin_api/remove_policy_parameters.go +++ b/restapi/operations/admin_api/remove_policy_parameters.go @@ -26,8 +26,10 @@ import ( "net/http" "github.com/go-openapi/errors" + "github.com/go-openapi/runtime" "github.com/go-openapi/runtime/middleware" "github.com/go-openapi/strfmt" + "github.com/go-openapi/validate" ) // NewRemovePolicyParams creates a new RemovePolicyParams object @@ -48,7 +50,7 @@ type RemovePolicyParams struct { /* Required: true - In: path + In: query */ Name string } @@ -62,8 +64,10 @@ func (o *RemovePolicyParams) BindRequest(r *http.Request, route *middleware.Matc o.HTTPRequest = r - rName, rhkName, _ := route.Params.GetOK("name") - if err := o.bindName(rName, rhkName, route.Formats); err != nil { + qs := runtime.Values(r.URL.Query()) + + qName, qhkName, _ := qs.GetOK("name") + if err := o.bindName(qName, qhkName, route.Formats); err != nil { res = append(res, err) } @@ -73,15 +77,21 @@ func (o *RemovePolicyParams) BindRequest(r *http.Request, route *middleware.Matc return nil } -// bindName binds and validates parameter Name from path. +// bindName binds and validates parameter Name from query. func (o *RemovePolicyParams) bindName(rawData []string, hasKey bool, formats strfmt.Registry) error { + if !hasKey { + return errors.Required("name", "query", rawData) + } var raw string if len(rawData) > 0 { raw = rawData[len(rawData)-1] } // Required: true - // Parameter is provided by construction from the route + // AllowEmptyValue: false + if err := validate.RequiredString("name", "query", raw); err != nil { + return err + } o.Name = raw diff --git a/restapi/operations/admin_api/remove_policy_urlbuilder.go b/restapi/operations/admin_api/remove_policy_urlbuilder.go index 85eb2b10b..3e68c6af7 100644 --- a/restapi/operations/admin_api/remove_policy_urlbuilder.go +++ b/restapi/operations/admin_api/remove_policy_urlbuilder.go @@ -26,7 +26,6 @@ import ( "errors" "net/url" golangswaggerpaths "path" - "strings" ) // RemovePolicyURL generates an URL for the remove policy operation @@ -57,14 +56,7 @@ func (o *RemovePolicyURL) SetBasePath(bp string) { func (o *RemovePolicyURL) Build() (*url.URL, error) { var _result url.URL - var _path = "/policies/{name}" - - name := o.Name - if name != "" { - _path = strings.Replace(_path, "{name}", name, -1) - } else { - return nil, errors.New("name is required on RemovePolicyURL") - } + var _path = "/policy" _basePath := o._basePath if _basePath == "" { @@ -72,6 +64,15 @@ func (o *RemovePolicyURL) Build() (*url.URL, error) { } _result.Path = golangswaggerpaths.Join(_basePath, _path) + qs := make(url.Values) + + nameQ := o.Name + if nameQ != "" { + qs.Set("name", nameQ) + } + + _result.RawQuery = qs.Encode() + return &_result, nil } diff --git a/restapi/operations/console_api.go b/restapi/operations/console_api.go index b1a47423e..02235d71d 100644 --- a/restapi/operations/console_api.go +++ b/restapi/operations/console_api.go @@ -1225,7 +1225,7 @@ func (o *ConsoleAPI) initHandlerCache() { if o.handlers["GET"] == nil { o.handlers["GET"] = make(map[string]http.Handler) } - o.handlers["GET"]["/policies/{name}"] = admin_api.NewPolicyInfo(o.context, o.AdminAPIPolicyInfoHandler) + o.handlers["GET"]["/policy"] = admin_api.NewPolicyInfo(o.context, o.AdminAPIPolicyInfoHandler) if o.handlers["POST"] == nil { o.handlers["POST"] = make(map[string]http.Handler) } @@ -1261,7 +1261,7 @@ func (o *ConsoleAPI) initHandlerCache() { if o.handlers["DELETE"] == nil { o.handlers["DELETE"] = make(map[string]http.Handler) } - o.handlers["DELETE"]["/policies/{name}"] = admin_api.NewRemovePolicy(o.context, o.AdminAPIRemovePolicyHandler) + o.handlers["DELETE"]["/policy"] = admin_api.NewRemovePolicy(o.context, o.AdminAPIRemovePolicyHandler) if o.handlers["DELETE"] == nil { o.handlers["DELETE"] = make(map[string]http.Handler) } diff --git a/swagger.yml b/swagger.yml index 1238cf1f5..1514f2e99 100644 --- a/swagger.yml +++ b/swagger.yml @@ -1234,30 +1234,13 @@ paths: tags: - AdminAPI - /policies/{name}: - delete: - summary: Remove policy - operationId: RemovePolicy - parameters: - - name: name - in: path - required: true - type: string - responses: - 204: - description: A successful response. - default: - description: Generic error response. - schema: - $ref: "#/definitions/error" - tags: - - AdminAPI + /policy: get: summary: Policy info operationId: PolicyInfo parameters: - name: name - in: path + in: query required: true type: string responses: @@ -1271,6 +1254,23 @@ paths: $ref: "#/definitions/error" tags: - AdminAPI + delete: + summary: Remove policy + operationId: RemovePolicy + parameters: + - name: name + in: query + required: true + type: string + responses: + 204: + description: A successful response. + default: + description: Generic error response. + schema: + $ref: "#/definitions/error" + tags: + - AdminAPI /configs: get: