[#85] apemanager: Generate protobufs for ape package

* Regenerate protobufs as APE specific type are defined
  as separate package;
* Move `ape` package related utils methods from `apemanager`.

Signed-off-by: Airat Arifullin <a.arifullin@yadro.com>
This commit is contained in:
Airat Arifullin 2024-05-28 11:04:07 +03:00
parent 0803bc6ded
commit 9789b79b1d
16 changed files with 443 additions and 402 deletions

132
ape/convert.go Normal file
View file

@ -0,0 +1,132 @@
package ape
import (
"fmt"
ape "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/ape/grpc"
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/grpc"
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/message"
)
func TargetTypeToGRPCField(typ TargetType) ape.TargetType {
switch typ {
case TargetTypeNamespace:
return ape.TargetType_NAMESPACE
case TargetTypeContainer:
return ape.TargetType_CONTAINER
case TargetTypeUser:
return ape.TargetType_USER
case TargetTypeGroup:
return ape.TargetType_GROUP
default:
return ape.TargetType_UNDEFINED
}
}
func TargetTypeFromGRPCField(typ ape.TargetType) TargetType {
switch typ {
case ape.TargetType_NAMESPACE:
return TargetTypeNamespace
case ape.TargetType_CONTAINER:
return TargetTypeContainer
case ape.TargetType_USER:
return TargetTypeUser
case ape.TargetType_GROUP:
return TargetTypeGroup
default:
return TargetTypeUndefined
}
}
func TargetTypeToGRPC(typ TargetType) ape.TargetType {
return ape.TargetType(typ)
}
func TargetTypeFromGRPC(typ ape.TargetType) TargetType {
return TargetType(typ)
}
func (v2 *ChainTarget) ToGRPCMessage() grpc.Message {
var mgrpc *ape.ChainTarget
if v2 != nil {
mgrpc = new(ape.ChainTarget)
mgrpc.SetType(TargetTypeToGRPC(v2.GetTargetType()))
mgrpc.SetName(v2.GetName())
}
return mgrpc
}
func (v2 *ChainTarget) FromGRPCMessage(m grpc.Message) error {
mgrpc, ok := m.(*ape.ChainTarget)
if !ok {
return message.NewUnexpectedMessageType(m, mgrpc)
}
v2.SetTargetType(TargetTypeFromGRPC(mgrpc.GetType()))
v2.SetName(mgrpc.GetName())
return nil
}
func (v2 *ChainRaw) ToGRPCMessage() grpc.Message {
var mgrpc *ape.Chain_Raw
if v2 != nil {
mgrpc = new(ape.Chain_Raw)
mgrpc.SetRaw(v2.GetRaw())
}
return mgrpc
}
func (v2 *ChainRaw) FromGRPCMessage(m grpc.Message) error {
mgrpc, ok := m.(*ape.Chain_Raw)
if !ok {
return message.NewUnexpectedMessageType(m, mgrpc)
}
v2.SetRaw(mgrpc.GetRaw())
return nil
}
func (v2 *Chain) ToGRPCMessage() grpc.Message {
var mgrpc *ape.Chain
if v2 != nil {
mgrpc = new(ape.Chain)
switch chainKind := v2.GetKind().(type) {
default:
panic(fmt.Sprintf("unsupported chain kind: %T", chainKind))
case *ChainRaw:
mgrpc.SetKind(chainKind.ToGRPCMessage().(*ape.Chain_Raw))
}
}
return mgrpc
}
func (v2 *Chain) FromGRPCMessage(m grpc.Message) error {
mgrpc, ok := m.(*ape.Chain)
if !ok {
return message.NewUnexpectedMessageType(m, mgrpc)
}
switch chainKind := mgrpc.GetKind().(type) {
default:
return fmt.Errorf("unsupported chain kind: %T", chainKind)
case *ape.Chain_Raw:
chainRaw := new(ChainRaw)
if err := chainRaw.FromGRPCMessage(chainKind); err != nil {
return err
}
v2.SetKind(chainRaw)
}
return nil
}

View file

@ -1,4 +1,4 @@
package apemanager
package ape
func (t *ChainTarget) SetType(typ TargetType) {
t.Type = typ

View file

@ -1,7 +1,7 @@
package apemanager
package ape
import (
apemanager_grpc "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/apemanager/grpc"
ape "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/ape/grpc"
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/message"
)
@ -10,5 +10,5 @@ func (t *ChainTarget) MarshalJSON() ([]byte, error) {
}
func (t *ChainTarget) UnmarshalJSON(data []byte) error {
return message.UnmarshalJSON(t, data, new(apemanager_grpc.ChainTarget))
return message.UnmarshalJSON(t, data, new(ape.ChainTarget))
}

92
ape/marshal.go Normal file
View file

@ -0,0 +1,92 @@
package ape
import (
"fmt"
ape "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/ape/grpc"
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/message"
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/util/proto"
)
const (
chainTargetTargetTypeField = 1
chainTargetNameField = 2
chainRawField = 1
)
func (t *ChainTarget) StableSize() (size int) {
if t == nil {
return 0
}
size += proto.EnumSize(chainTargetTargetTypeField, int32(t.targeType))
size += proto.StringSize(chainTargetNameField, t.name)
return size
}
func (t *ChainTarget) StableMarshal(buf []byte) []byte {
if t == nil {
return []byte{}
}
if buf == nil {
buf = make([]byte, t.StableSize())
}
var offset int
offset += proto.EnumMarshal(chainTargetTargetTypeField, buf[offset:], int32(t.targeType))
proto.StringMarshal(chainTargetNameField, buf[offset:], t.name)
return buf
}
func (t *ChainTarget) Unmarshal(data []byte) error {
return message.Unmarshal(t, data, new(ape.ChainTarget))
}
func (c *Chain) StableSize() (size int) {
if c == nil {
return 0
}
switch v := c.GetKind().(type) {
case *ChainRaw:
if v != nil {
size += proto.BytesSize(chainRawField, v.GetRaw())
}
default:
panic(fmt.Sprintf("unsupported chain kind: %T", v))
}
return size
}
func (c *Chain) StableMarshal(buf []byte) []byte {
if c == nil {
return []byte{}
}
if buf == nil {
buf = make([]byte, c.StableSize())
}
var offset int
switch v := c.GetKind().(type) {
case *ChainRaw:
if v != nil {
proto.BytesMarshal(chainRawField, buf[offset:], v.GetRaw())
}
default:
panic(fmt.Sprintf("unsupported chain kind: %T", v))
}
return buf
}
func (c *Chain) Unmarshal(data []byte) error {
return message.Unmarshal(c, data, new(ape.Chain))
}

15
ape/message_test.go Normal file
View file

@ -0,0 +1,15 @@
package ape_test
import (
"testing"
apetest "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/ape/test"
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/message"
messagetest "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/message/test"
)
func TestMessageConvert(t *testing.T) {
messagetest.TestRPCMessage(t,
func(empty bool) message.Message { return apetest.GenerateChainTarget(empty) },
)
}

View file

@ -1,7 +1,7 @@
package apemanager
package ape
import (
apemanager_grpc "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/apemanager/grpc"
apegrpc "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/ape/grpc"
)
func (tt TargetType) String() string {
@ -9,7 +9,7 @@ func (tt TargetType) String() string {
}
func (tt *TargetType) FromString(s string) bool {
i, ok := apemanager_grpc.TargetType_value[s]
i, ok := apegrpc.TargetType_value[s]
if ok {
*tt = TargetType(i)
}

71
ape/test/generate.go Normal file
View file

@ -0,0 +1,71 @@
package test
import (
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/ape"
)
func GenerateRawChains(empty bool, n int) []*ape.Chain {
if empty {
return []*ape.Chain{}
}
res := make([]*ape.Chain, n)
for i := range res {
res[i] = GenerateRawChain(empty)
}
return res
}
func GenerateRawChain(empty bool) *ape.Chain {
chRaw := new(ape.ChainRaw)
if empty {
chRaw.SetRaw([]byte("{}"))
} else {
chRaw.SetRaw([]byte(`{
"ID": "",
"Rules": [
{
"Status": "Allow",
"Actions": {
"Inverted": false,
"Names": [
"GetObject"
]
},
"Resources": {
"Inverted": false,
"Names": [
"native:object/*"
]
},
"Any": false,
"Condition": [
{
"Op": "StringEquals",
"Object": "Resource",
"Key": "Department",
"Value": "HR"
}
]
}
],
"MatchType": "DenyPriority"
}`))
}
ch := new(ape.Chain)
ch.SetKind(chRaw)
return ch
}
func GenerateChainTarget(empty bool) *ape.ChainTarget {
m := new(ape.ChainTarget)
if !empty {
m.SetTargetType(ape.TargetTypeContainer)
m.SetName("BzQw5HH3feoxFDD5tCT87Y1726qzgLfxEE7wgtoRzB3R")
}
return m
}

71
ape/types.go Normal file
View file

@ -0,0 +1,71 @@
package ape
type TargetType uint32
const (
TargetTypeUndefined TargetType = iota
TargetTypeNamespace
TargetTypeContainer
TargetTypeUser
TargetTypeGroup
)
type ChainTarget struct {
targeType TargetType
name string
}
func (ct *ChainTarget) SetTargetType(targeType TargetType) {
ct.targeType = targeType
}
func (ct *ChainTarget) SetName(name string) {
ct.name = name
}
func (ct *ChainTarget) GetTargetType() TargetType {
if ct != nil {
return ct.targeType
}
return 0
}
func (ct *ChainTarget) GetName() string {
if ct != nil {
return ct.name
}
return ""
}
type chainKind interface {
isChainKind()
}
type Chain struct {
kind chainKind
}
func (c *Chain) SetKind(kind chainKind) {
c.kind = kind
}
func (c *Chain) GetKind() chainKind {
return c.kind
}
type ChainRaw struct {
Raw []byte
}
func (*ChainRaw) isChainKind() {}
func (c *ChainRaw) SetRaw(raw []byte) {
c.Raw = raw
}
func (c *ChainRaw) GetRaw() []byte {
return c.Raw
}

View file

@ -1,144 +1,21 @@
package apemanager
import (
"fmt"
ape "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/ape"
apeGRPC "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/ape/grpc"
apemanager "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/apemanager/grpc"
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/grpc"
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/message"
)
func TargetTypeToGRPCField(typ TargetType) apemanager.TargetType {
switch typ {
case TargetTypeNamespace:
return apemanager.TargetType_NAMESPACE
case TargetTypeContainer:
return apemanager.TargetType_CONTAINER
case TargetTypeUser:
return apemanager.TargetType_USER
case TargetTypeGroup:
return apemanager.TargetType_GROUP
default:
return apemanager.TargetType_UNDEFINED
}
}
func TargetTypeFromGRPCField(typ apemanager.TargetType) TargetType {
switch typ {
case apemanager.TargetType_NAMESPACE:
return TargetTypeNamespace
case apemanager.TargetType_CONTAINER:
return TargetTypeContainer
case apemanager.TargetType_USER:
return TargetTypeUser
case apemanager.TargetType_GROUP:
return TargetTypeGroup
default:
return TargetTypeUndefined
}
}
func TargetTypeToGRPC(typ TargetType) apemanager.TargetType {
return apemanager.TargetType(typ)
}
func TargetTypeFromGRPC(typ apemanager.TargetType) TargetType {
return TargetType(typ)
}
func (v2 *ChainTarget) ToGRPCMessage() grpc.Message {
var mgrpc *apemanager.ChainTarget
if v2 != nil {
mgrpc = new(apemanager.ChainTarget)
mgrpc.SetType(TargetTypeToGRPC(v2.GetTargetType()))
mgrpc.SetName(v2.GetName())
}
return mgrpc
}
func (v2 *ChainTarget) FromGRPCMessage(m grpc.Message) error {
mgrpc, ok := m.(*apemanager.ChainTarget)
if !ok {
return message.NewUnexpectedMessageType(m, mgrpc)
}
v2.SetTargetType(TargetTypeFromGRPC(mgrpc.GetType()))
v2.SetName(mgrpc.GetName())
return nil
}
func (v2 *ChainRaw) ToGRPCMessage() grpc.Message {
var mgrpc *apemanager.Chain_Raw
if v2 != nil {
mgrpc = new(apemanager.Chain_Raw)
mgrpc.SetRaw(v2.GetRaw())
}
return mgrpc
}
func (v2 *ChainRaw) FromGRPCMessage(m grpc.Message) error {
mgrpc, ok := m.(*apemanager.Chain_Raw)
if !ok {
return message.NewUnexpectedMessageType(m, mgrpc)
}
v2.SetRaw(mgrpc.GetRaw())
return nil
}
func (v2 *Chain) ToGRPCMessage() grpc.Message {
var mgrpc *apemanager.Chain
if v2 != nil {
mgrpc = new(apemanager.Chain)
switch chainKind := v2.GetKind().(type) {
default:
panic(fmt.Sprintf("unsupported chain kind: %T", chainKind))
case *ChainRaw:
mgrpc.SetKind(chainKind.ToGRPCMessage().(*apemanager.Chain_Raw))
}
}
return mgrpc
}
func (v2 *Chain) FromGRPCMessage(m grpc.Message) error {
mgrpc, ok := m.(*apemanager.Chain)
if !ok {
return message.NewUnexpectedMessageType(m, mgrpc)
}
switch chainKind := mgrpc.GetKind().(type) {
default:
return fmt.Errorf("unsupported chain kind: %T", chainKind)
case *apemanager.Chain_Raw:
chainRaw := new(ChainRaw)
if err := chainRaw.FromGRPCMessage(chainKind); err != nil {
return err
}
v2.SetKind(chainRaw)
}
return nil
}
func (reqBody *AddChainRequestBody) ToGRPCMessage() grpc.Message {
var reqBodygrpc *apemanager.AddChainRequest_Body
if reqBody != nil {
reqBodygrpc = new(apemanager.AddChainRequest_Body)
reqBodygrpc.SetTarget(reqBody.GetTarget().ToGRPCMessage().(*apemanager.ChainTarget))
reqBodygrpc.SetChain(reqBody.GetChain().ToGRPCMessage().(*apemanager.Chain))
reqBodygrpc.SetTarget(reqBody.GetTarget().ToGRPCMessage().(*apeGRPC.ChainTarget))
reqBodygrpc.SetChain(reqBody.GetChain().ToGRPCMessage().(*apeGRPC.Chain))
}
return reqBodygrpc
@ -151,14 +28,14 @@ func (reqBody *AddChainRequestBody) FromGRPCMessage(m grpc.Message) error {
}
if targetgrpc := reqBodygrpc.GetTarget(); targetgrpc != nil {
reqBody.target = new(ChainTarget)
reqBody.target = new(ape.ChainTarget)
if err := reqBody.target.FromGRPCMessage(targetgrpc); err != nil {
return err
}
}
if chaingrpc := reqBodygrpc.GetChain(); chaingrpc != nil {
reqBody.chain = new(Chain)
reqBody.chain = new(ape.Chain)
if err := reqBody.GetChain().FromGRPCMessage(chaingrpc); err != nil {
return err
}
@ -254,7 +131,7 @@ func (reqBody *RemoveChainRequestBody) ToGRPCMessage() grpc.Message {
if reqBody != nil {
reqBodygrpc = new(apemanager.RemoveChainRequest_Body)
reqBodygrpc.SetTarget(reqBody.target.ToGRPCMessage().(*apemanager.ChainTarget))
reqBodygrpc.SetTarget(reqBody.target.ToGRPCMessage().(*apeGRPC.ChainTarget))
reqBodygrpc.SetChainId(reqBody.GetChainID())
}
@ -268,7 +145,7 @@ func (reqBody *RemoveChainRequestBody) FromGRPCMessage(m grpc.Message) error {
}
if targetgrpc := reqBodygrpc.GetTarget(); targetgrpc != nil {
reqBody.target = new(ChainTarget)
reqBody.target = new(ape.ChainTarget)
if err := reqBody.target.FromGRPCMessage(targetgrpc); err != nil {
return err
}
@ -362,7 +239,7 @@ func (reqBody *ListChainsRequestBody) ToGRPCMessage() grpc.Message {
if reqBody != nil {
reqBodygrpc = new(apemanager.ListChainsRequest_Body)
reqBodygrpc.SetTarget(reqBody.target.ToGRPCMessage().(*apemanager.ChainTarget))
reqBodygrpc.SetTarget(reqBody.target.ToGRPCMessage().(*apeGRPC.ChainTarget))
}
return reqBodygrpc
@ -375,7 +252,7 @@ func (reqBody *ListChainsRequestBody) FromGRPCMessage(m grpc.Message) error {
}
if targetgrpc := reqBodygrpc.GetTarget(); targetgrpc != nil {
reqBody.target = new(ChainTarget)
reqBody.target = new(ape.ChainTarget)
if err := reqBody.target.FromGRPCMessage(targetgrpc); err != nil {
return err
}
@ -419,9 +296,9 @@ func (respBody *ListChainsResponseBody) ToGRPCMessage() grpc.Message {
if respBody != nil {
respBodygrpc = new(apemanager.ListChainsResponse_Body)
chainsgrpc := make([]*apemanager.Chain, 0, len(respBody.GetChains()))
chainsgrpc := make([]*apeGRPC.Chain, 0, len(respBody.GetChains()))
for _, chain := range respBody.GetChains() {
chainsgrpc = append(chainsgrpc, chain.ToGRPCMessage().(*apemanager.Chain))
chainsgrpc = append(chainsgrpc, chain.ToGRPCMessage().(*apeGRPC.Chain))
}
respBodygrpc.SetChains(chainsgrpc)
@ -436,10 +313,10 @@ func (respBody *ListChainsResponseBody) FromGRPCMessage(m grpc.Message) error {
return message.NewUnexpectedMessageType(m, respBodygrpc)
}
chains := make([]*Chain, 0, len(respBodygrpc.GetChains()))
chains := make([]*ape.Chain, 0, len(respBodygrpc.GetChains()))
for _, chaingrpc := range respBodygrpc.GetChains() {
chain := new(Chain)
chain := new(ape.Chain)
if err := chain.FromGRPCMessage(chaingrpc); err != nil {
return err
}

View file

@ -1,14 +1,15 @@
package apemanager
import (
session_grpc "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/session/grpc"
ape "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/ape/grpc"
session "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/session/grpc"
)
func (rb *AddChainRequest_Body) SetTarget(t *ChainTarget) {
func (rb *AddChainRequest_Body) SetTarget(t *ape.ChainTarget) {
rb.Target = t
}
func (rb *AddChainRequest_Body) SetChain(chain *Chain) {
func (rb *AddChainRequest_Body) SetChain(chain *ape.Chain) {
rb.Chain = chain
}
@ -16,11 +17,11 @@ func (r *AddChainRequest) SetBody(rb *AddChainRequest_Body) {
r.Body = rb
}
func (r *AddChainRequest) SetMetaHeader(mh *session_grpc.RequestMetaHeader) {
func (r *AddChainRequest) SetMetaHeader(mh *session.RequestMetaHeader) {
r.MetaHeader = mh
}
func (r *AddChainRequest) SetVerifyHeader(vh *session_grpc.RequestVerificationHeader) {
func (r *AddChainRequest) SetVerifyHeader(vh *session.RequestVerificationHeader) {
r.VerifyHeader = vh
}
@ -32,15 +33,15 @@ func (r *AddChainResponse) SetBody(rb *AddChainResponse_Body) {
r.Body = rb
}
func (r *AddChainResponse) SetMetaHeader(mh *session_grpc.ResponseMetaHeader) {
func (r *AddChainResponse) SetMetaHeader(mh *session.ResponseMetaHeader) {
r.MetaHeader = mh
}
func (r *AddChainResponse) SetVerifyHeader(vh *session_grpc.ResponseVerificationHeader) {
func (r *AddChainResponse) SetVerifyHeader(vh *session.ResponseVerificationHeader) {
r.VerifyHeader = vh
}
func (rb *RemoveChainRequest_Body) SetTarget(t *ChainTarget) {
func (rb *RemoveChainRequest_Body) SetTarget(t *ape.ChainTarget) {
rb.Target = t
}
@ -52,11 +53,11 @@ func (r *RemoveChainRequest) SetBody(rb *RemoveChainRequest_Body) {
r.Body = rb
}
func (r *RemoveChainRequest) SetMetaHeader(mh *session_grpc.RequestMetaHeader) {
func (r *RemoveChainRequest) SetMetaHeader(mh *session.RequestMetaHeader) {
r.MetaHeader = mh
}
func (r *RemoveChainRequest) SetVerifyHeader(vh *session_grpc.RequestVerificationHeader) {
func (r *RemoveChainRequest) SetVerifyHeader(vh *session.RequestVerificationHeader) {
r.VerifyHeader = vh
}
@ -64,15 +65,15 @@ func (r *RemoveChainResponse) SetBody(rb *RemoveChainResponse_Body) {
r.Body = rb
}
func (r *RemoveChainResponse) SetMetaHeader(mh *session_grpc.ResponseMetaHeader) {
func (r *RemoveChainResponse) SetMetaHeader(mh *session.ResponseMetaHeader) {
r.MetaHeader = mh
}
func (r *RemoveChainResponse) SetVerifyHeader(vh *session_grpc.ResponseVerificationHeader) {
func (r *RemoveChainResponse) SetVerifyHeader(vh *session.ResponseVerificationHeader) {
r.VerifyHeader = vh
}
func (r *ListChainsRequest_Body) SetTarget(t *ChainTarget) {
func (r *ListChainsRequest_Body) SetTarget(t *ape.ChainTarget) {
r.Target = t
}
@ -80,15 +81,15 @@ func (r *ListChainsRequest) SetBody(rb *ListChainsRequest_Body) {
r.Body = rb
}
func (r *ListChainsRequest) SetMetaHeader(mh *session_grpc.RequestMetaHeader) {
func (r *ListChainsRequest) SetMetaHeader(mh *session.RequestMetaHeader) {
r.MetaHeader = mh
}
func (r *ListChainsRequest) SetVerifyHeader(vh *session_grpc.RequestVerificationHeader) {
func (r *ListChainsRequest) SetVerifyHeader(vh *session.RequestVerificationHeader) {
r.VerifyHeader = vh
}
func (rb *ListChainsResponse_Body) SetChains(chains []*Chain) {
func (rb *ListChainsResponse_Body) SetChains(chains []*ape.Chain) {
rb.Chains = chains
}
@ -96,10 +97,10 @@ func (r *ListChainsResponse) SetBody(rb *ListChainsResponse_Body) {
r.Body = rb
}
func (r *ListChainsResponse) SetMetaHeader(mh *session_grpc.ResponseMetaHeader) {
func (r *ListChainsResponse) SetMetaHeader(mh *session.ResponseMetaHeader) {
r.MetaHeader = mh
}
func (r *ListChainsResponse) SetVerifyHeader(vh *session_grpc.ResponseVerificationHeader) {
func (r *ListChainsResponse) SetVerifyHeader(vh *session.ResponseVerificationHeader) {
r.VerifyHeader = vh
}

Binary file not shown.

View file

@ -1,19 +1,12 @@
package apemanager
import (
"fmt"
apemanager "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/apemanager/grpc"
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/message"
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/util/proto"
)
const (
chainTargetTargetTypeField = 1
chainTargetNameField = 2
chainRawField = 1
addChainReqBodyTargetField = 1
addChainReqBodyChainField = 2
@ -31,82 +24,6 @@ const (
listChainsRespBodyChainsField = 1
)
func (t *ChainTarget) StableSize() (size int) {
if t == nil {
return 0
}
size += proto.EnumSize(chainTargetTargetTypeField, int32(t.targeType))
size += proto.StringSize(chainTargetNameField, t.name)
return size
}
func (t *ChainTarget) StableMarshal(buf []byte) []byte {
if t == nil {
return []byte{}
}
if buf == nil {
buf = make([]byte, t.StableSize())
}
var offset int
offset += proto.EnumMarshal(chainTargetTargetTypeField, buf[offset:], int32(t.targeType))
proto.StringMarshal(chainTargetNameField, buf[offset:], t.name)
return buf
}
func (t *ChainTarget) Unmarshal(data []byte) error {
return message.Unmarshal(t, data, new(apemanager.ChainTarget))
}
func (c *Chain) StableSize() (size int) {
if c == nil {
return 0
}
switch v := c.GetKind().(type) {
case *ChainRaw:
if v != nil {
size += proto.BytesSize(chainRawField, v.GetRaw())
}
default:
panic(fmt.Sprintf("unsupported chain kind: %T", v))
}
return size
}
func (c *Chain) StableMarshal(buf []byte) []byte {
if c == nil {
return []byte{}
}
if buf == nil {
buf = make([]byte, c.StableSize())
}
var offset int
switch v := c.GetKind().(type) {
case *ChainRaw:
if v != nil {
proto.BytesMarshal(chainRawField, buf[offset:], v.GetRaw())
}
default:
panic(fmt.Sprintf("unsupported chain kind: %T", v))
}
return buf
}
func (c *Chain) Unmarshal(data []byte) error {
return message.Unmarshal(c, data, new(apemanager.Chain))
}
func (rb *AddChainRequestBody) StableSize() (size int) {
if rb == nil {
return 0

View file

@ -10,7 +10,6 @@ import (
func TestMessageConvert(t *testing.T) {
messagetest.TestRPCMessage(t,
func(empty bool) message.Message { return apemanagertest.GenerateChainTarget(empty) },
func(empty bool) message.Message { return apemanagertest.GenerateAddChainRequestBody(empty) },
func(empty bool) message.Message { return apemanagertest.GenerateAddChainRequest(empty) },
func(empty bool) message.Message { return apemanagertest.GenerateAddChainResponseBody(empty) },

View file

@ -1,6 +1,7 @@
package apemanagertest
import (
apetest "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/ape/test"
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/apemanager"
sessiontest "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/session/test"
)
@ -13,78 +14,12 @@ func generateChainID(empty bool) []byte {
return []byte("616c6c6f774f626a476574436e72")
}
func generateRawChains(empty bool, n int) []*apemanager.Chain {
if empty {
return []*apemanager.Chain{}
}
res := make([]*apemanager.Chain, n)
for i := range res {
res[i] = generateRawChain(empty)
}
return res
}
func generateRawChain(empty bool) *apemanager.Chain {
chRaw := new(apemanager.ChainRaw)
if empty {
chRaw.SetRaw([]byte("{}"))
} else {
chRaw.SetRaw([]byte(`{
"ID": "",
"Rules": [
{
"Status": "Allow",
"Actions": {
"Inverted": false,
"Names": [
"GetObject"
]
},
"Resources": {
"Inverted": false,
"Names": [
"native:object/*"
]
},
"Any": false,
"Condition": [
{
"Op": "StringEquals",
"Object": "Resource",
"Key": "Department",
"Value": "HR"
}
]
}
],
"MatchType": "DenyPriority"
}`))
}
ch := new(apemanager.Chain)
ch.SetKind(chRaw)
return ch
}
func GenerateChainTarget(empty bool) *apemanager.ChainTarget {
m := new(apemanager.ChainTarget)
if !empty {
m.SetTargetType(apemanager.TargetTypeContainer)
m.SetName("BzQw5HH3feoxFDD5tCT87Y1726qzgLfxEE7wgtoRzB3R")
}
return m
}
func GenerateAddChainRequestBody(empty bool) *apemanager.AddChainRequestBody {
m := new(apemanager.AddChainRequestBody)
if !empty {
m.SetTarget(GenerateChainTarget(empty))
m.SetChain(generateRawChain(empty))
m.SetTarget(apetest.GenerateChainTarget(empty))
m.SetChain(apetest.GenerateRawChain(empty))
}
return m
@ -129,7 +64,7 @@ func GenerateRemoveChainRequestBody(empty bool) *apemanager.RemoveChainRequestBo
if !empty {
m.SetChainID(generateChainID(empty))
m.SetTarget(GenerateChainTarget(empty))
m.SetTarget(apetest.GenerateChainTarget(empty))
}
return m
@ -167,7 +102,7 @@ func GenerateListChainsRequestBody(empty bool) *apemanager.ListChainsRequestBody
m := new(apemanager.ListChainsRequestBody)
if !empty {
m.SetTarget(GenerateChainTarget(empty))
m.SetTarget(apetest.GenerateChainTarget(empty))
}
return m
@ -189,7 +124,7 @@ func GenerateListChainsResponseBody(empty bool) *apemanager.ListChainsResponseBo
m := new(apemanager.ListChainsResponseBody)
if !empty {
m.SetChains(generateRawChains(empty, 10))
m.SetChains(apetest.GenerateRawChains(empty, 10))
}
return m

View file

@ -1,49 +1,10 @@
package apemanager
import (
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/ape"
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/session"
)
type TargetType uint32
const (
TargetTypeUndefined TargetType = iota
TargetTypeNamespace
TargetTypeContainer
TargetTypeUser
TargetTypeGroup
)
type ChainTarget struct {
targeType TargetType
name string
}
func (ct *ChainTarget) SetTargetType(targeType TargetType) {
ct.targeType = targeType
}
func (ct *ChainTarget) SetName(name string) {
ct.name = name
}
func (ct *ChainTarget) GetTargetType() TargetType {
if ct != nil {
return ct.targeType
}
return 0
}
func (ct *ChainTarget) GetName() string {
if ct != nil {
return ct.name
}
return ""
}
type AddChainRequest struct {
body *AddChainRequestBody
@ -58,55 +19,25 @@ func (r *AddChainRequest) GetBody() *AddChainRequestBody {
return r.body
}
type chainKind interface {
isChainKind()
}
type Chain struct {
kind chainKind
}
func (c *Chain) SetKind(kind chainKind) {
c.kind = kind
}
func (c *Chain) GetKind() chainKind {
return c.kind
}
type ChainRaw struct {
Raw []byte
}
func (*ChainRaw) isChainKind() {}
func (c *ChainRaw) SetRaw(raw []byte) {
c.Raw = raw
}
func (c *ChainRaw) GetRaw() []byte {
return c.Raw
}
type AddChainRequestBody struct {
target *ChainTarget
target *ape.ChainTarget
chain *Chain
chain *ape.Chain
}
func (rb *AddChainRequestBody) SetTarget(target *ChainTarget) {
func (rb *AddChainRequestBody) SetTarget(target *ape.ChainTarget) {
rb.target = target
}
func (rb *AddChainRequestBody) GetTarget() *ChainTarget {
func (rb *AddChainRequestBody) GetTarget() *ape.ChainTarget {
return rb.target
}
func (rb *AddChainRequestBody) SetChain(chain *Chain) {
func (rb *AddChainRequestBody) SetChain(chain *ape.Chain) {
rb.chain = chain
}
func (rb *AddChainRequestBody) GetChain() *Chain {
func (rb *AddChainRequestBody) GetChain() *ape.Chain {
return rb.chain
}
@ -151,16 +82,16 @@ func (r *RemoveChainRequest) GetBody() *RemoveChainRequestBody {
}
type RemoveChainRequestBody struct {
target *ChainTarget
target *ape.ChainTarget
chainID []byte
}
func (rb *RemoveChainRequestBody) SetTarget(target *ChainTarget) {
func (rb *RemoveChainRequestBody) SetTarget(target *ape.ChainTarget) {
rb.target = target
}
func (rb *RemoveChainRequestBody) GetTarget() *ChainTarget {
func (rb *RemoveChainRequestBody) GetTarget() *ape.ChainTarget {
return rb.target
}
@ -204,14 +135,14 @@ func (r *ListChainsRequest) GetBody() *ListChainsRequestBody {
}
type ListChainsRequestBody struct {
target *ChainTarget
target *ape.ChainTarget
}
func (rb *ListChainsRequestBody) SetTarget(target *ChainTarget) {
func (rb *ListChainsRequestBody) SetTarget(target *ape.ChainTarget) {
rb.target = target
}
func (rb *ListChainsRequestBody) GetTarget() *ChainTarget {
func (rb *ListChainsRequestBody) GetTarget() *ape.ChainTarget {
return rb.target
}
@ -230,15 +161,15 @@ func (r *ListChainsResponse) GetBody() *ListChainsResponseBody {
}
type ListChainsResponseBody struct {
chains []*Chain
chains []*ape.Chain
session.RequestHeaders
}
func (r *ListChainsResponseBody) SetChains(chains []*Chain) {
func (r *ListChainsResponseBody) SetChains(chains []*ape.Chain) {
r.chains = chains
}
func (r *ListChainsResponseBody) GetChains() []*Chain {
func (r *ListChainsResponseBody) GetChains() []*ape.Chain {
return r.chains
}