From 70e25e6cafb08380c418177a997fd1e30fa9e0d0 Mon Sep 17 00:00:00 2001 From: Denis Kirillov Date: Wed, 15 Jun 2022 17:32:28 +0300 Subject: [PATCH] [#7] Make container name optional Signed-off-by: Denis Kirillov --- cmd/neofs-rest-gw/integration_test.go | 24 +++++++++++++++---- gen/restapi/embedded_spec.go | 14 ++++------- gen/restapi/operations/put_container.go | 21 +--------------- .../operations/put_container_parameters.go | 22 ++++++++--------- handlers/containers.go | 12 +++++++--- spec/rest.yaml | 6 ++--- 6 files changed, 46 insertions(+), 53 deletions(-) diff --git a/cmd/neofs-rest-gw/integration_test.go b/cmd/neofs-rest-gw/integration_test.go index d4ee7a7..3f113a7 100644 --- a/cmd/neofs-rest-gw/integration_test.go +++ b/cmd/neofs-rest-gw/integration_test.go @@ -772,22 +772,36 @@ func restContainerPut(ctx context.Context, t *testing.T, clientPool *pool.Pool) attrKey: attrValue, } - req := operations.PutContainerBody{ - ContainerName: util.NewString("cnr"), - } - body, err := json.Marshal(&req) + // try to create container without name but with name-scope-global + body, err := json.Marshal(&operations.PutContainerBody{}) require.NoError(t, err) reqURL, err := url.Parse(testHost + "/v1/containers") require.NoError(t, err) query := reqURL.Query() - query.Add("skip-native-name", "true") + query.Add("name-scope-global", "true") query.Add(walletConnectQuery, strconv.FormatBool(useWalletConnect)) reqURL.RawQuery = query.Encode() request, err := http.NewRequest(http.MethodPut, reqURL.String(), bytes.NewReader(body)) require.NoError(t, err) prepareCommonHeaders(request.Header, bearerToken) + + doRequest(t, httpClient, request, http.StatusBadRequest, nil) + + // create container with name in local scope + body, err = json.Marshal(&operations.PutContainerBody{}) + require.NoError(t, err) + + reqURL, err = url.Parse(testHost + "/v1/containers") + require.NoError(t, err) + query = reqURL.Query() + query.Add(walletConnectQuery, strconv.FormatBool(useWalletConnect)) + reqURL.RawQuery = query.Encode() + + request, err = http.NewRequest(http.MethodPut, reqURL.String(), bytes.NewReader(body)) + require.NoError(t, err) + prepareCommonHeaders(request.Header, bearerToken) request.Header.Add("X-Attribute-"+attrKey, attrValue) addr := &operations.PutContainerOKBody{} diff --git a/gen/restapi/embedded_spec.go b/gen/restapi/embedded_spec.go index d5b4c83..e52e143 100644 --- a/gen/restapi/embedded_spec.go +++ b/gen/restapi/embedded_spec.go @@ -154,8 +154,8 @@ func init() { { "type": "boolean", "default": false, - "description": "Provide this parameter to skip registration container name in NNS service", - "name": "skip-native-name", + "description": "Provide this parameter to register container name in NNS service", + "name": "name-scope-global", "in": "query" }, { @@ -165,9 +165,6 @@ func init() { "required": true, "schema": { "type": "object", - "required": [ - "containerName" - ], "properties": { "basicAcl": { "type": "string" @@ -1221,8 +1218,8 @@ func init() { { "type": "boolean", "default": false, - "description": "Provide this parameter to skip registration container name in NNS service", - "name": "skip-native-name", + "description": "Provide this parameter to register container name in NNS service", + "name": "name-scope-global", "in": "query" }, { @@ -1232,9 +1229,6 @@ func init() { "required": true, "schema": { "type": "object", - "required": [ - "containerName" - ], "properties": { "basicAcl": { "type": "string" diff --git a/gen/restapi/operations/put_container.go b/gen/restapi/operations/put_container.go index f4a87c0..c10faa6 100644 --- a/gen/restapi/operations/put_container.go +++ b/gen/restapi/operations/put_container.go @@ -85,8 +85,7 @@ type PutContainerBody struct { BasicACL string `json:"basicAcl,omitempty"` // container name - // Required: true - ContainerName *string `json:"containerName"` + ContainerName string `json:"containerName,omitempty"` // placement policy PlacementPolicy string `json:"placementPolicy,omitempty"` @@ -94,24 +93,6 @@ type PutContainerBody struct { // Validate validates this put container body func (o *PutContainerBody) Validate(formats strfmt.Registry) error { - var res []error - - if err := o.validateContainerName(formats); err != nil { - res = append(res, err) - } - - if len(res) > 0 { - return errors.CompositeValidationError(res...) - } - return nil -} - -func (o *PutContainerBody) validateContainerName(formats strfmt.Registry) error { - - if err := validate.Required("container"+"."+"containerName", "body", o.ContainerName); err != nil { - return err - } - return nil } diff --git a/gen/restapi/operations/put_container_parameters.go b/gen/restapi/operations/put_container_parameters.go index 2d100aa..76e141a 100644 --- a/gen/restapi/operations/put_container_parameters.go +++ b/gen/restapi/operations/put_container_parameters.go @@ -25,12 +25,12 @@ func NewPutContainerParams() PutContainerParams { var ( // initialize parameters with default values - skipNativeNameDefault = bool(false) - walletConnectDefault = bool(false) + nameScopeGlobalDefault = bool(false) + walletConnectDefault = bool(false) ) return PutContainerParams{ - SkipNativeName: &skipNativeNameDefault, + NameScopeGlobal: &nameScopeGlobalDefault, WalletConnect: &walletConnectDefault, } @@ -60,11 +60,11 @@ type PutContainerParams struct { In: body */ Container PutContainerBody - /*Provide this parameter to skip registration container name in NNS service + /*Provide this parameter to register container name in NNS service In: query Default: false */ - SkipNativeName *bool + NameScopeGlobal *bool /*Use wallect connect signature scheme or not In: query Default: false @@ -119,8 +119,8 @@ func (o *PutContainerParams) BindRequest(r *http.Request, route *middleware.Matc res = append(res, errors.Required("container", "body", "")) } - qSkipNativeName, qhkSkipNativeName, _ := qs.GetOK("skip-native-name") - if err := o.bindSkipNativeName(qSkipNativeName, qhkSkipNativeName, route.Formats); err != nil { + qNameScopeGlobal, qhkNameScopeGlobal, _ := qs.GetOK("name-scope-global") + if err := o.bindNameScopeGlobal(qNameScopeGlobal, qhkNameScopeGlobal, route.Formats); err != nil { res = append(res, err) } @@ -174,8 +174,8 @@ func (o *PutContainerParams) bindXBearerSignatureKey(rawData []string, hasKey bo return nil } -// bindSkipNativeName binds and validates parameter SkipNativeName from query. -func (o *PutContainerParams) bindSkipNativeName(rawData []string, hasKey bool, formats strfmt.Registry) error { +// bindNameScopeGlobal binds and validates parameter NameScopeGlobal from query. +func (o *PutContainerParams) bindNameScopeGlobal(rawData []string, hasKey bool, formats strfmt.Registry) error { var raw string if len(rawData) > 0 { raw = rawData[len(rawData)-1] @@ -191,9 +191,9 @@ func (o *PutContainerParams) bindSkipNativeName(rawData []string, hasKey bool, f value, err := swag.ConvertBool(raw) if err != nil { - return errors.InvalidType("skip-native-name", "query", "bool", raw) + return errors.InvalidType("name-scope-global", "query", "bool", raw) } - o.SkipNativeName = &value + o.NameScopeGlobal = &value return nil } diff --git a/handlers/containers.go b/handlers/containers.go index e7c5b81..d404ee8 100644 --- a/handlers/containers.go +++ b/handlers/containers.go @@ -320,10 +320,13 @@ func createContainer(ctx context.Context, p *pool.Pool, stoken *session.Token, p cnrOptions := []container.Option{ container.WithPolicy(pp), container.WithCustomBasicACL(basicACL), - container.WithAttribute(container.AttributeName, *request.ContainerName), container.WithAttribute(container.AttributeTimestamp, strconv.FormatInt(time.Now().Unix(), 10)), } + if request.ContainerName != "" { + container.WithAttribute(container.AttributeName, request.ContainerName) + } + for key, val := range userAttrs { cnrOptions = append(cnrOptions, container.WithAttribute(key, val)) } @@ -332,8 +335,11 @@ func createContainer(ctx context.Context, p *pool.Pool, stoken *session.Token, p cnr.SetOwnerID(stoken.OwnerID()) cnr.SetSessionToken(stoken) - if !*params.SkipNativeName { // we don't check for nil because there is default false value - container.SetNativeName(cnr, *request.ContainerName) + if *params.NameScopeGlobal { // we don't check for nil because there is default false value + if request.ContainerName == "" { + return nil, fmt.Errorf("container name must not be empty to be registered in NNS") + } + container.SetNativeName(cnr, request.ContainerName) } var prm pool.PrmContainerPut diff --git a/spec/rest.yaml b/spec/rest.yaml index 0403176..0d72c0d 100644 --- a/spec/rest.yaml +++ b/spec/rest.yaml @@ -222,8 +222,8 @@ paths: - $ref: '#/parameters/signatureKeyParam' - $ref: '#/parameters/signatureScheme' - in: query - name: skip-native-name - description: Provide this parameter to skip registration container name in NNS service + name: name-scope-global + description: Provide this parameter to register container name in NNS service type: boolean default: false - in: body @@ -239,8 +239,6 @@ paths: type: string basicAcl: type: string - required: - - containerName example: containerId: container placementPolicy: "REP 3"