[#7] Make container name optional

Signed-off-by: Denis Kirillov <denis@nspcc.ru>
This commit is contained in:
Denis Kirillov 2022-06-15 17:32:28 +03:00 committed by Alex Vanin
parent 3e7e04b3b1
commit 70e25e6caf
6 changed files with 46 additions and 53 deletions

View file

@ -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{}

View file

@ -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"

View file

@ -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
}

View file

@ -25,12 +25,12 @@ func NewPutContainerParams() PutContainerParams {
var (
// initialize parameters with default values
skipNativeNameDefault = 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
}

View file

@ -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

View file

@ -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"