[#15] Expand container info in list

Signed-off-by: Denis Kirillov <denis@nspcc.ru>
This commit is contained in:
Denis Kirillov 2022-07-07 17:55:13 +03:00 committed by Alex Vanin
parent 5c122a4325
commit e604222f60
7 changed files with 51 additions and 166 deletions

View file

@ -824,12 +824,24 @@ func restContainerList(ctx context.Context, t *testing.T, p *pool.Pool, cnrID *c
require.Equal(t, len(ids), int(*list.Size))
expected := &models.ContainerBaseInfo{
ContainerID: util.NewString(cnrID.String()),
Name: containerName,
require.Truef(t, containsContainer(list.Containers, cnrID.String(), containerName), "list doesn't contain cnr '%s' with name '%s'", cnrID.String(), containerName)
}
func containsContainer(containers []*models.ContainerInfo, cnrID, cnrName string) bool {
for _, cnrInfo := range containers {
if *cnrInfo.ContainerID == cnrID {
for _, attr := range cnrInfo.Attributes {
if *attr.Key == container.AttributeName && *attr.Value == cnrName {
return true
}
}
require.Contains(t, list.Containers, expected)
fmt.Println("container found but name doesn't match")
return false
}
}
return false
}
func makeAuthTokenRequest(ctx context.Context, t *testing.T, bearers []*models.Bearer, httpClient *http.Client) []*handlers.BearerToken {

View file

@ -1,74 +0,0 @@
// Code generated by go-swagger; DO NOT EDIT.
package models
// This file was generated by the swagger tool.
// Editing this file might prove futile when you re-run the swagger generate command
import (
"context"
"github.com/go-openapi/errors"
"github.com/go-openapi/strfmt"
"github.com/go-openapi/swag"
"github.com/go-openapi/validate"
)
// ContainerBaseInfo container base info
//
// swagger:model ContainerBaseInfo
type ContainerBaseInfo struct {
// container Id
// Required: true
ContainerID *string `json:"containerId"`
// name
Name string `json:"name,omitempty"`
}
// Validate validates this container base info
func (m *ContainerBaseInfo) Validate(formats strfmt.Registry) error {
var res []error
if err := m.validateContainerID(formats); err != nil {
res = append(res, err)
}
if len(res) > 0 {
return errors.CompositeValidationError(res...)
}
return nil
}
func (m *ContainerBaseInfo) validateContainerID(formats strfmt.Registry) error {
if err := validate.Required("containerId", "body", m.ContainerID); err != nil {
return err
}
return nil
}
// ContextValidate validates this container base info based on context it is used
func (m *ContainerBaseInfo) ContextValidate(ctx context.Context, formats strfmt.Registry) error {
return nil
}
// MarshalBinary interface implementation
func (m *ContainerBaseInfo) MarshalBinary() ([]byte, error) {
if m == nil {
return nil, nil
}
return swag.WriteJSON(m)
}
// UnmarshalBinary interface implementation
func (m *ContainerBaseInfo) UnmarshalBinary(b []byte) error {
var res ContainerBaseInfo
if err := swag.ReadJSON(b, &res); err != nil {
return err
}
*m = res
return nil
}

View file

@ -22,7 +22,7 @@ type ContainerList struct {
// containers
// Required: true
Containers []*ContainerBaseInfo `json:"containers"`
Containers []*ContainerInfo `json:"containers"`
// size
// Required: true

View file

@ -173,7 +173,7 @@ func init() {
},
"example": {
"basicAcl": "public-read-write",
"containerId": "container",
"containerName": "container",
"placementPolicy": "REP 3"
}
}
@ -570,20 +570,6 @@ func init() {
}
}
},
"ContainerBaseInfo": {
"type": "object",
"required": [
"containerId"
],
"properties": {
"containerId": {
"type": "string"
},
"name": {
"type": "string"
}
}
},
"ContainerInfo": {
"type": "object",
"required": [
@ -645,7 +631,7 @@ func init() {
"containers": {
"type": "array",
"items": {
"$ref": "#/definitions/ContainerBaseInfo"
"$ref": "#/definitions/ContainerInfo"
}
},
"size": {
@ -1280,7 +1266,7 @@ func init() {
},
"example": {
"basicAcl": "public-read-write",
"containerId": "container",
"containerName": "container",
"placementPolicy": "REP 3"
}
}
@ -1760,20 +1746,6 @@ func init() {
}
}
},
"ContainerBaseInfo": {
"type": "object",
"required": [
"containerId"
],
"properties": {
"containerId": {
"type": "string"
},
"name": {
"type": "string"
}
}
},
"ContainerInfo": {
"type": "object",
"required": [
@ -1835,7 +1807,7 @@ func init() {
"containers": {
"type": "array",
"items": {
"$ref": "#/definitions/ContainerBaseInfo"
"$ref": "#/definitions/ContainerInfo"
}
},
"size": {

View file

@ -76,7 +76,7 @@ func (o *PutContainer) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
}
// PutContainerBody put container body
// Example: {"basicAcl":"public-read-write","containerId":"container","placementPolicy":"REP 3"}
// Example: {"basicAcl":"public-read-write","containerName":"container","placementPolicy":"REP 3"}
//
// swagger:model PutContainerBody
type PutContainerBody struct {

View file

@ -67,30 +67,19 @@ func (a *API) PutContainers(params operations.PutContainerParams, principal *mod
// GetContainer handler that returns container info.
func (a *API) GetContainer(params operations.GetContainerParams) middleware.Responder {
cnr, err := getContainer(params.HTTPRequest.Context(), a.pool, params.ContainerID)
cnrID, err := parseContainerID(params.ContainerID)
if err != nil {
resp := a.logAndGetErrorResponse("invalid container id", err)
return operations.NewGetContainerBadRequest().WithPayload(resp)
}
cnrInfo, err := getContainerInfo(params.HTTPRequest.Context(), a.pool, *cnrID)
if err != nil {
resp := a.logAndGetErrorResponse("get container", err)
return operations.NewPutContainerBadRequest().WithPayload(resp)
return operations.NewGetContainerBadRequest().WithPayload(resp)
}
attrs := make([]*models.Attribute, len(cnr.Attributes()))
for i, attr := range cnr.Attributes() {
attrs[i] = &models.Attribute{
Key: util.NewString(attr.Key()),
Value: util.NewString(attr.Value()),
}
}
resp := &models.ContainerInfo{
ContainerID: util.NewString(params.ContainerID),
Version: util.NewString(cnr.Version().String()),
OwnerID: util.NewString(cnr.OwnerID().String()),
BasicACL: util.NewString(acl.BasicACL(cnr.BasicACL()).String()),
PlacementPolicy: util.NewString(strings.Join(policy.Encode(cnr.PlacementPolicy()), " ")),
Attributes: attrs,
}
return operations.NewGetContainerOK().WithPayload(resp)
return operations.NewGetContainerOK().WithPayload(cnrInfo)
}
// PutContainerEACL handler that update container eacl.
@ -165,7 +154,7 @@ func (a *API) ListContainer(params operations.ListContainersParams) middleware.R
if offset > len(ids)-1 {
res := &models.ContainerList{
Size: util.NewInteger(0),
Containers: []*models.ContainerBaseInfo{},
Containers: []*models.ContainerInfo{},
}
return operations.NewListContainersOK().WithPayload(res)
}
@ -176,16 +165,16 @@ func (a *API) ListContainer(params operations.ListContainersParams) middleware.R
res := &models.ContainerList{
Size: util.NewInteger(int64(size)),
Containers: make([]*models.ContainerBaseInfo, 0, size),
Containers: make([]*models.ContainerInfo, 0, size),
}
for _, id := range ids[offset : offset+size] {
baseInfo, err := getContainerBaseInfo(ctx, a.pool, id)
cnrInfo, err := getContainerInfo(ctx, a.pool, id)
if err != nil {
resp := a.logAndGetErrorResponse("get container", err, zap.String("cid", id.String()))
return operations.NewListContainersBadRequest().WithPayload(resp)
}
res.Containers = append(res.Containers, baseInfo)
res.Containers = append(res.Containers, cnrInfo)
}
return operations.NewListContainersOK().WithPayload(res)
@ -225,7 +214,7 @@ func (a *API) DeleteContainer(params operations.DeleteContainerParams, principal
return operations.NewDeleteContainerOK().WithPayload(util.NewSuccessResponse())
}
func getContainerBaseInfo(ctx context.Context, p *pool.Pool, cnrID cid.ID) (*models.ContainerBaseInfo, error) {
func getContainerInfo(ctx context.Context, p *pool.Pool, cnrID cid.ID) (*models.ContainerInfo, error) {
var prm pool.PrmContainerGet
prm.SetContainerID(cnrID)
@ -234,15 +223,22 @@ func getContainerBaseInfo(ctx context.Context, p *pool.Pool, cnrID cid.ID) (*mod
return nil, err
}
baseInfo := &models.ContainerBaseInfo{ContainerID: util.NewString(cnrID.String())}
for _, attr := range cnr.Attributes() {
if attr.Key() == container.AttributeName {
baseInfo.Name = attr.Value()
attrs := make([]*models.Attribute, len(cnr.Attributes()))
for i, attr := range cnr.Attributes() {
attrs[i] = &models.Attribute{
Key: util.NewString(attr.Key()),
Value: util.NewString(attr.Value()),
}
}
return baseInfo, nil
return &models.ContainerInfo{
ContainerID: util.NewString(cnrID.String()),
Version: util.NewString(cnr.Version().String()),
OwnerID: util.NewString(cnr.OwnerID().String()),
BasicACL: util.NewString(acl.BasicACL(cnr.BasicACL()).String()),
PlacementPolicy: util.NewString(strings.Join(policy.Encode(cnr.PlacementPolicy()), " ")),
Attributes: attrs,
}, nil
}
func prepareUserAttributes(header http.Header) map[string]string {
@ -252,18 +248,6 @@ func prepareUserAttributes(header http.Header) map[string]string {
return filtered
}
func getContainer(ctx context.Context, p *pool.Pool, containerID string) (*container.Container, error) {
cnrID, err := parseContainerID(containerID)
if err != nil {
return nil, err
}
var prm pool.PrmContainerGet
prm.SetContainerID(*cnrID)
return p.GetContainer(ctx, prm)
}
func parseContainerID(containerID string) (*cid.ID, error) {
var cnrID cid.ID
if err := cnrID.Parse(containerID); err != nil {

View file

@ -238,7 +238,7 @@ paths:
basicAcl:
type: string
example:
containerId: container
containerName: container
placementPolicy: "REP 3"
basicAcl: public-read-write
responses:
@ -549,7 +549,7 @@ definitions:
containers:
type: array
items:
$ref: '#/definitions/ContainerBaseInfo'
$ref: '#/definitions/ContainerInfo'
required:
- size
- containers
@ -582,15 +582,6 @@ definitions:
- MatchStringNotEqual
- MatchNotPresent
- MatchCommonPrefix
ContainerBaseInfo:
type: object
properties:
containerId:
type: string
name:
type: string
required:
- containerId
ObjectList:
type: object
properties: