[#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)) require.Equal(t, len(ids), int(*list.Size))
expected := &models.ContainerBaseInfo{ require.Truef(t, containsContainer(list.Containers, cnrID.String(), containerName), "list doesn't contain cnr '%s' with name '%s'", cnrID.String(), containerName)
ContainerID: util.NewString(cnrID.String()),
Name: containerName,
} }
require.Contains(t, list.Containers, expected) 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
}
}
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 { 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 // containers
// Required: true // Required: true
Containers []*ContainerBaseInfo `json:"containers"` Containers []*ContainerInfo `json:"containers"`
// size // size
// Required: true // Required: true

View file

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

View file

@ -76,7 +76,7 @@ func (o *PutContainer) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
} }
// PutContainerBody put container body // 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 // swagger:model PutContainerBody
type PutContainerBody struct { type PutContainerBody struct {

View file

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

View file

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