feat(storage): rpc based implementation
All checks were successful
arcad/edge/pipeline/pr-master This commit looks good
All checks were successful
arcad/edge/pipeline/pr-master This commit looks good
This commit is contained in:
28
pkg/storage/driver/rpc/server/share/delete_attributes.go
Normal file
28
pkg/storage/driver/rpc/server/share/delete_attributes.go
Normal file
@ -0,0 +1,28 @@
|
||||
package share
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"forge.cadoles.com/arcad/edge/pkg/app"
|
||||
"forge.cadoles.com/arcad/edge/pkg/storage/share"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type DeleteAttributesArgs struct {
|
||||
Origin app.ID
|
||||
ResourceID share.ResourceID
|
||||
Names []string
|
||||
}
|
||||
|
||||
type DeleteAttributesReply struct {
|
||||
}
|
||||
|
||||
func (s *Service) DeleteAttributes(ctx context.Context, args DeleteAttributesArgs, reply *DeleteAttributesReply) error {
|
||||
if err := s.store.DeleteAttributes(ctx, args.Origin, args.ResourceID, args.Names...); err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
*reply = DeleteAttributesReply{}
|
||||
|
||||
return nil
|
||||
}
|
27
pkg/storage/driver/rpc/server/share/delete_resource.go
Normal file
27
pkg/storage/driver/rpc/server/share/delete_resource.go
Normal file
@ -0,0 +1,27 @@
|
||||
package share
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"forge.cadoles.com/arcad/edge/pkg/app"
|
||||
"forge.cadoles.com/arcad/edge/pkg/storage/share"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type DeleteResourceArgs struct {
|
||||
Origin app.ID
|
||||
ResourceID share.ResourceID
|
||||
}
|
||||
|
||||
type DeleteResourceReply struct {
|
||||
}
|
||||
|
||||
func (s *Service) DeleteResource(ctx context.Context, args DeleteResourceArgs, reply *DeleteResourceReply) error {
|
||||
if err := s.store.DeleteResource(ctx, args.Origin, args.ResourceID); err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
*reply = DeleteResourceReply{}
|
||||
|
||||
return nil
|
||||
}
|
41
pkg/storage/driver/rpc/server/share/find_resources.go
Normal file
41
pkg/storage/driver/rpc/server/share/find_resources.go
Normal file
@ -0,0 +1,41 @@
|
||||
package share
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"forge.cadoles.com/arcad/edge/pkg/storage/share"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type FindResourcesArgs struct {
|
||||
Options *share.FindResourcesOptions
|
||||
}
|
||||
|
||||
type FindResourcesReply struct {
|
||||
Resources []*SerializableResource
|
||||
}
|
||||
|
||||
func (s *Service) FindResources(ctx context.Context, args FindResourcesArgs, reply *FindResourcesReply) error {
|
||||
resources, err := s.store.FindResources(ctx, withFindResourcesOptions(args.Options))
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
serializableResources := make([]*SerializableResource, len(resources))
|
||||
for resIdx, r := range resources {
|
||||
serializableResources[resIdx] = FromResource(r)
|
||||
}
|
||||
|
||||
*reply = FindResourcesReply{
|
||||
Resources: serializableResources,
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func withFindResourcesOptions(opts *share.FindResourcesOptions) share.FindResourcesOptionFunc {
|
||||
return func(o *share.FindResourcesOptions) {
|
||||
o.Name = opts.Name
|
||||
o.ValueType = opts.ValueType
|
||||
}
|
||||
}
|
31
pkg/storage/driver/rpc/server/share/get_resource.go
Normal file
31
pkg/storage/driver/rpc/server/share/get_resource.go
Normal file
@ -0,0 +1,31 @@
|
||||
package share
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"forge.cadoles.com/arcad/edge/pkg/app"
|
||||
"forge.cadoles.com/arcad/edge/pkg/storage/share"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type GetResourceArgs struct {
|
||||
Origin app.ID
|
||||
ResourceID share.ResourceID
|
||||
}
|
||||
|
||||
type GetResourceReply struct {
|
||||
Resource *SerializableResource
|
||||
}
|
||||
|
||||
func (s *Service) GetResource(ctx context.Context, args GetResourceArgs, reply *GetResourceReply) error {
|
||||
resource, err := s.store.GetResource(ctx, args.Origin, args.ResourceID)
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
*reply = GetResourceReply{
|
||||
Resource: FromResource(resource),
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
94
pkg/storage/driver/rpc/server/share/serializable.go
Normal file
94
pkg/storage/driver/rpc/server/share/serializable.go
Normal file
@ -0,0 +1,94 @@
|
||||
package share
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"forge.cadoles.com/arcad/edge/pkg/app"
|
||||
"forge.cadoles.com/arcad/edge/pkg/storage/share"
|
||||
)
|
||||
|
||||
func FromResource(res share.Resource) *SerializableResource {
|
||||
serializableAttributes := make([]*SerializableAttribute, len(res.Attributes()))
|
||||
for attrIdx, attr := range res.Attributes() {
|
||||
serializableAttributes[attrIdx] = FromAttribute(attr)
|
||||
}
|
||||
|
||||
return &SerializableResource{
|
||||
ID_: res.ID(),
|
||||
Origin_: res.Origin(),
|
||||
Attributes_: serializableAttributes,
|
||||
}
|
||||
}
|
||||
|
||||
func FromAttribute(attr share.Attribute) *SerializableAttribute {
|
||||
return &SerializableAttribute{
|
||||
Name_: attr.Name(),
|
||||
Value_: attr.Value(),
|
||||
Type_: attr.Type(),
|
||||
UpdatedAt_: attr.UpdatedAt(),
|
||||
CreatedAt_: attr.CreatedAt(),
|
||||
}
|
||||
}
|
||||
|
||||
type SerializableResource struct {
|
||||
ID_ share.ResourceID
|
||||
Origin_ app.ID
|
||||
Attributes_ []*SerializableAttribute
|
||||
}
|
||||
|
||||
// Attributes implements share.Resource.
|
||||
func (r *SerializableResource) Attributes() []share.Attribute {
|
||||
attributes := make([]share.Attribute, len(r.Attributes_))
|
||||
for idx, attr := range r.Attributes_ {
|
||||
attributes[idx] = attr
|
||||
}
|
||||
|
||||
return attributes
|
||||
}
|
||||
|
||||
// ID implements share.Resource.
|
||||
func (r *SerializableResource) ID() share.ResourceID {
|
||||
return r.ID_
|
||||
}
|
||||
|
||||
// Origin implements share.Resource.
|
||||
func (r *SerializableResource) Origin() app.ID {
|
||||
return r.Origin_
|
||||
}
|
||||
|
||||
var _ share.Resource = &SerializableResource{}
|
||||
|
||||
type SerializableAttribute struct {
|
||||
Name_ string
|
||||
Value_ any
|
||||
Type_ share.ValueType
|
||||
UpdatedAt_ time.Time
|
||||
CreatedAt_ time.Time
|
||||
}
|
||||
|
||||
// CreatedAt implements share.Attribute.
|
||||
func (a *SerializableAttribute) CreatedAt() time.Time {
|
||||
return a.CreatedAt_
|
||||
}
|
||||
|
||||
// Name implements share.Attribute.
|
||||
func (a *SerializableAttribute) Name() string {
|
||||
return a.Name_
|
||||
}
|
||||
|
||||
// Type implements share.Attribute.
|
||||
func (a *SerializableAttribute) Type() share.ValueType {
|
||||
return a.Type_
|
||||
}
|
||||
|
||||
// UpdatedAt implements share.Attribute.
|
||||
func (a *SerializableAttribute) UpdatedAt() time.Time {
|
||||
return a.UpdatedAt_
|
||||
}
|
||||
|
||||
// Value implements share.Attribute.
|
||||
func (a *SerializableAttribute) Value() any {
|
||||
return a.Value_
|
||||
}
|
||||
|
||||
var _ share.Attribute = &SerializableAttribute{}
|
13
pkg/storage/driver/rpc/server/share/service.go
Normal file
13
pkg/storage/driver/rpc/server/share/service.go
Normal file
@ -0,0 +1,13 @@
|
||||
package share
|
||||
|
||||
import (
|
||||
"forge.cadoles.com/arcad/edge/pkg/storage/share"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
store share.Store
|
||||
}
|
||||
|
||||
func NewService(store share.Store) *Service {
|
||||
return &Service{store}
|
||||
}
|
37
pkg/storage/driver/rpc/server/share/update_attributes.go
Normal file
37
pkg/storage/driver/rpc/server/share/update_attributes.go
Normal file
@ -0,0 +1,37 @@
|
||||
package share
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"forge.cadoles.com/arcad/edge/pkg/app"
|
||||
"forge.cadoles.com/arcad/edge/pkg/storage/share"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type UpdateAttributesArgs struct {
|
||||
Origin app.ID
|
||||
ResourceID share.ResourceID
|
||||
Attributes []*SerializableAttribute
|
||||
}
|
||||
|
||||
type UpdateAttributesReply struct {
|
||||
Resource *SerializableResource
|
||||
}
|
||||
|
||||
func (s *Service) UpdateAttributes(ctx context.Context, args UpdateAttributesArgs, reply *UpdateAttributesReply) error {
|
||||
attributes := make([]share.Attribute, len(args.Attributes))
|
||||
for idx, attr := range args.Attributes {
|
||||
attributes[idx] = attr
|
||||
}
|
||||
|
||||
resource, err := s.store.UpdateAttributes(ctx, args.Origin, args.ResourceID, attributes...)
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
*reply = UpdateAttributesReply{
|
||||
Resource: FromResource(resource),
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user