From e604222f60b782d771c37f78670a95d81e3fde3d Mon Sep 17 00:00:00 2001 From: Denis Kirillov Date: Thu, 7 Jul 2022 17:55:13 +0300 Subject: [PATCH] [#15] Expand container info in list Signed-off-by: Denis Kirillov --- cmd/neofs-rest-gw/integration_test.go | 20 +++++-- gen/models/container_base_info.go | 74 ------------------------- gen/models/container_list.go | 2 +- gen/restapi/embedded_spec.go | 36 ++---------- gen/restapi/operations/put_container.go | 2 +- handlers/containers.go | 70 +++++++++-------------- spec/rest.yaml | 13 +---- 7 files changed, 51 insertions(+), 166 deletions(-) delete mode 100644 gen/models/container_base_info.go diff --git a/cmd/neofs-rest-gw/integration_test.go b/cmd/neofs-rest-gw/integration_test.go index b2c2831..17369ac 100644 --- a/cmd/neofs-rest-gw/integration_test.go +++ b/cmd/neofs-rest-gw/integration_test.go @@ -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 + } + } + + fmt.Println("container found but name doesn't match") + return false + } } - require.Contains(t, list.Containers, expected) + return false } func makeAuthTokenRequest(ctx context.Context, t *testing.T, bearers []*models.Bearer, httpClient *http.Client) []*handlers.BearerToken { diff --git a/gen/models/container_base_info.go b/gen/models/container_base_info.go deleted file mode 100644 index 18342a6..0000000 --- a/gen/models/container_base_info.go +++ /dev/null @@ -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 -} diff --git a/gen/models/container_list.go b/gen/models/container_list.go index c268088..759ae94 100644 --- a/gen/models/container_list.go +++ b/gen/models/container_list.go @@ -22,7 +22,7 @@ type ContainerList struct { // containers // Required: true - Containers []*ContainerBaseInfo `json:"containers"` + Containers []*ContainerInfo `json:"containers"` // size // Required: true diff --git a/gen/restapi/embedded_spec.go b/gen/restapi/embedded_spec.go index a194038..542ab34 100644 --- a/gen/restapi/embedded_spec.go +++ b/gen/restapi/embedded_spec.go @@ -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": { diff --git a/gen/restapi/operations/put_container.go b/gen/restapi/operations/put_container.go index c10faa6..7092413 100644 --- a/gen/restapi/operations/put_container.go +++ b/gen/restapi/operations/put_container.go @@ -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 { diff --git a/handlers/containers.go b/handlers/containers.go index 0047a92..69cabe0 100644 --- a/handlers/containers.go +++ b/handlers/containers.go @@ -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 { diff --git a/spec/rest.yaml b/spec/rest.yaml index 87e7812..866abea 100644 --- a/spec/rest.yaml +++ b/spec/rest.yaml @@ -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: