Skip to content

Commit

Permalink
refactor(backend): split validate logic to a new file
Browse files Browse the repository at this point in the history
Signed-off-by: Boris Glimcher <Boris.Glimcher@emc.com>
  • Loading branch information
glimchb committed Sep 22, 2023
1 parent 3981de4 commit 29130ec
Show file tree
Hide file tree
Showing 6 changed files with 234 additions and 133 deletions.
46 changes: 10 additions & 36 deletions pkg/backend/aio.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"go.einride.tech/aip/fieldbehavior"
"go.einride.tech/aip/fieldmask"
"go.einride.tech/aip/resourceid"
"go.einride.tech/aip/resourcename"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
Expand All @@ -35,19 +34,14 @@ func sortAioVolumes(volumes []*pb.AioVolume) {
// CreateAioVolume creates an Aio volume
func (s *Server) CreateAioVolume(_ context.Context, in *pb.CreateAioVolumeRequest) (*pb.AioVolume, error) {
log.Printf("CreateAioVolume: Received from client: %v", in)
// check required fields
if err := fieldbehavior.ValidateRequiredFields(in); err != nil {
// check input correctness
if err := s.validateCreateAioVolumeRequest(in); err != nil {
log.Printf("error: %v", err)
return nil, err
}
// see https://google.aip.dev/133#user-specified-ids
resourceID := resourceid.NewSystemGenerated()
if in.AioVolumeId != "" {
err := resourceid.ValidateUserSettable(in.AioVolumeId)
if err != nil {
log.Printf("error: %v", err)
return nil, err
}
log.Printf("client provided the ID of a resource %v, ignoring the name field %v", in.AioVolumeId, in.AioVolume.Name)
resourceID = in.AioVolumeId
}
Expand Down Expand Up @@ -85,13 +79,8 @@ func (s *Server) CreateAioVolume(_ context.Context, in *pb.CreateAioVolumeReques
// DeleteAioVolume deletes an Aio volume
func (s *Server) DeleteAioVolume(_ context.Context, in *pb.DeleteAioVolumeRequest) (*emptypb.Empty, error) {
log.Printf("DeleteAioVolume: Received from client: %v", in)
// check required fields
if err := fieldbehavior.ValidateRequiredFields(in); err != nil {
log.Printf("error: %v", err)
return nil, err
}
// Validate that a resource name conforms to the restrictions outlined in AIP-122.
if err := resourcename.Validate(in.Name); err != nil {
// check input correctness
if err := s.validateDeleteAioVolumeRequest(in); err != nil {
log.Printf("error: %v", err)
return nil, err
}
Expand Down Expand Up @@ -128,13 +117,8 @@ func (s *Server) DeleteAioVolume(_ context.Context, in *pb.DeleteAioVolumeReques
// UpdateAioVolume updates an Aio volume
func (s *Server) UpdateAioVolume(_ context.Context, in *pb.UpdateAioVolumeRequest) (*pb.AioVolume, error) {
log.Printf("UpdateAioVolume: Received from client: %v", in)
// check required fields
if err := fieldbehavior.ValidateRequiredFields(in); err != nil {
log.Printf("error: %v", err)
return nil, err
}
// Validate that a resource name conforms to the restrictions outlined in AIP-122.
if err := resourcename.Validate(in.AioVolume.Name); err != nil {
// check input correctness
if err := s.validateUpdateAioVolumeRequest(in); err != nil {
log.Printf("error: %v", err)
return nil, err
}
Expand Down Expand Up @@ -252,13 +236,8 @@ func (s *Server) ListAioVolumes(_ context.Context, in *pb.ListAioVolumesRequest)
// GetAioVolume gets an Aio volume
func (s *Server) GetAioVolume(_ context.Context, in *pb.GetAioVolumeRequest) (*pb.AioVolume, error) {
log.Printf("GetAioVolume: Received from client: %v", in)
// check required fields
if err := fieldbehavior.ValidateRequiredFields(in); err != nil {
log.Printf("error: %v", err)
return nil, err
}
// Validate that a resource name conforms to the restrictions outlined in AIP-122.
if err := resourcename.Validate(in.Name); err != nil {
// check input correctness
if err := s.validateGetAioVolumeRequest(in); err != nil {
log.Printf("error: %v", err)
return nil, err
}
Expand Down Expand Up @@ -291,13 +270,8 @@ func (s *Server) GetAioVolume(_ context.Context, in *pb.GetAioVolumeRequest) (*p
// StatsAioVolume gets an Aio volume stats
func (s *Server) StatsAioVolume(_ context.Context, in *pb.StatsAioVolumeRequest) (*pb.StatsAioVolumeResponse, error) {
log.Printf("StatsAioVolume: Received from client: %v", in)
// check required fields
if err := fieldbehavior.ValidateRequiredFields(in); err != nil {
log.Printf("error: %v", err)
return nil, err
}
// Validate that a resource name conforms to the restrictions outlined in AIP-122.
if err := resourcename.Validate(in.Name); err != nil {
// check input correctness
if err := s.validateStatsAioVolumeRequest(in); err != nil {
log.Printf("error: %v", err)
return nil, err
}
Expand Down
59 changes: 59 additions & 0 deletions pkg/backend/aio_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,62 @@

// Package backend implememnts the BackEnd APIs (network facing) of the storage Server
package backend

import (
"go.einride.tech/aip/fieldbehavior"
"go.einride.tech/aip/resourceid"
"go.einride.tech/aip/resourcename"

pb "github.com/opiproject/opi-api/storage/v1alpha1/gen/go"
)

func (s *Server) validateCreateAioVolumeRequest(in *pb.CreateAioVolumeRequest) error {
// check required fields
if err := fieldbehavior.ValidateRequiredFields(in); err != nil {
return err
}
// see https://google.aip.dev/133#user-specified-ids
if in.AioVolumeId != "" {
if err := resourceid.ValidateUserSettable(in.AioVolumeId); err != nil {
return err
}
}
// TODO: validate also: block_size, blocks_count, uuid, filename
return nil
}

func (s *Server) validateDeleteAioVolumeRequest(in *pb.DeleteAioVolumeRequest) error {
// check required fields
if err := fieldbehavior.ValidateRequiredFields(in); err != nil {
return err
}
// Validate that a resource name conforms to the restrictions outlined in AIP-122.
return resourcename.Validate(in.Name)
}

func (s *Server) validateUpdateAioVolumeRequest(in *pb.UpdateAioVolumeRequest) error {
// check required fields
if err := fieldbehavior.ValidateRequiredFields(in); err != nil {
return err
}
// Validate that a resource name conforms to the restrictions outlined in AIP-122.
return resourcename.Validate(in.AioVolume.Name)
}

func (s *Server) validateGetAioVolumeRequest(in *pb.GetAioVolumeRequest) error {
// check required fields
if err := fieldbehavior.ValidateRequiredFields(in); err != nil {
return err
}
// Validate that a resource name conforms to the restrictions outlined in AIP-122.
return resourcename.Validate(in.Name)
}

func (s *Server) validateStatsAioVolumeRequest(in *pb.StatsAioVolumeRequest) error {
// check required fields
if err := fieldbehavior.ValidateRequiredFields(in); err != nil {
return err
}
// Validate that a resource name conforms to the restrictions outlined in AIP-122.
return resourcename.Validate(in.Name)
}
46 changes: 10 additions & 36 deletions pkg/backend/null.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"go.einride.tech/aip/fieldbehavior"
"go.einride.tech/aip/fieldmask"
"go.einride.tech/aip/resourceid"
"go.einride.tech/aip/resourcename"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
Expand All @@ -36,19 +35,14 @@ func sortNullVolumes(volumes []*pb.NullVolume) {
// CreateNullVolume creates a Null volume instance
func (s *Server) CreateNullVolume(_ context.Context, in *pb.CreateNullVolumeRequest) (*pb.NullVolume, error) {
log.Printf("CreateNullVolume: Received from client: %v", in)
// check required fields
if err := fieldbehavior.ValidateRequiredFields(in); err != nil {
// check input correctness
if err := s.validateCreateNullVolumeRequest(in); err != nil {
log.Printf("error: %v", err)
return nil, err
}
// see https://google.aip.dev/133#user-specified-ids
resourceID := resourceid.NewSystemGenerated()
if in.NullVolumeId != "" {
err := resourceid.ValidateUserSettable(in.NullVolumeId)
if err != nil {
log.Printf("error: %v", err)
return nil, err
}
log.Printf("client provided the ID of a resource %v, ignoring the name field %v", in.NullVolumeId, in.NullVolume.Name)
resourceID = in.NullVolumeId
}
Expand Down Expand Up @@ -86,13 +80,8 @@ func (s *Server) CreateNullVolume(_ context.Context, in *pb.CreateNullVolumeRequ
// DeleteNullVolume deletes a Null volume instance
func (s *Server) DeleteNullVolume(_ context.Context, in *pb.DeleteNullVolumeRequest) (*emptypb.Empty, error) {
log.Printf("DeleteNullVolume: Received from client: %v", in)
// check required fields
if err := fieldbehavior.ValidateRequiredFields(in); err != nil {
log.Printf("error: %v", err)
return nil, err
}
// Validate that a resource name conforms to the restrictions outlined in AIP-122.
if err := resourcename.Validate(in.Name); err != nil {
// check input correctness
if err := s.validateDeleteNullVolumeRequest(in); err != nil {
log.Printf("error: %v", err)
return nil, err
}
Expand Down Expand Up @@ -129,13 +118,8 @@ func (s *Server) DeleteNullVolume(_ context.Context, in *pb.DeleteNullVolumeRequ
// UpdateNullVolume updates a Null volume instance
func (s *Server) UpdateNullVolume(_ context.Context, in *pb.UpdateNullVolumeRequest) (*pb.NullVolume, error) {
log.Printf("UpdateNullVolume: Received from client: %v", in)
// check required fields
if err := fieldbehavior.ValidateRequiredFields(in); err != nil {
log.Printf("error: %v", err)
return nil, err
}
// Validate that a resource name conforms to the restrictions outlined in AIP-122.
if err := resourcename.Validate(in.NullVolume.Name); err != nil {
// check input correctness
if err := s.validateUpdateNullVolumeRequest(in); err != nil {
log.Printf("error: %v", err)
return nil, err
}
Expand Down Expand Up @@ -253,13 +237,8 @@ func (s *Server) ListNullVolumes(_ context.Context, in *pb.ListNullVolumesReques
// GetNullVolume gets a a Null volume instance
func (s *Server) GetNullVolume(_ context.Context, in *pb.GetNullVolumeRequest) (*pb.NullVolume, error) {
log.Printf("GetNullVolume: Received from client: %v", in)
// check required fields
if err := fieldbehavior.ValidateRequiredFields(in); err != nil {
log.Printf("error: %v", err)
return nil, err
}
// Validate that a resource name conforms to the restrictions outlined in AIP-122.
if err := resourcename.Validate(in.Name); err != nil {
// check input correctness
if err := s.validateGetNullVolumeRequest(in); err != nil {
log.Printf("error: %v", err)
return nil, err
}
Expand Down Expand Up @@ -292,13 +271,8 @@ func (s *Server) GetNullVolume(_ context.Context, in *pb.GetNullVolumeRequest) (
// StatsNullVolume gets a Null volume instance stats
func (s *Server) StatsNullVolume(_ context.Context, in *pb.StatsNullVolumeRequest) (*pb.StatsNullVolumeResponse, error) {
log.Printf("StatsNullVolume: Received from client: %v", in)
// check required fields
if err := fieldbehavior.ValidateRequiredFields(in); err != nil {
log.Printf("error: %v", err)
return nil, err
}
// Validate that a resource name conforms to the restrictions outlined in AIP-122.
if err := resourcename.Validate(in.Name); err != nil {
// check input correctness
if err := s.validateStatsNullVolumeRequest(in); err != nil {
log.Printf("error: %v", err)
return nil, err
}
Expand Down
59 changes: 59 additions & 0 deletions pkg/backend/null_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,62 @@

// Package backend implememnts the BackEnd APIs (network facing) of the storage Server
package backend

import (
"go.einride.tech/aip/fieldbehavior"
"go.einride.tech/aip/resourceid"
"go.einride.tech/aip/resourcename"

pb "github.com/opiproject/opi-api/storage/v1alpha1/gen/go"
)

func (s *Server) validateCreateNullVolumeRequest(in *pb.CreateNullVolumeRequest) error {
// check required fields
if err := fieldbehavior.ValidateRequiredFields(in); err != nil {
return err
}
// see https://google.aip.dev/133#user-specified-ids
if in.NullVolumeId != "" {
if err := resourceid.ValidateUserSettable(in.NullVolumeId); err != nil {
return err
}
}
// TODO: validate also: block_size, blocks_count, uuid
return nil
}

func (s *Server) validateDeleteNullVolumeRequest(in *pb.DeleteNullVolumeRequest) error {
// check required fields
if err := fieldbehavior.ValidateRequiredFields(in); err != nil {
return err
}
// Validate that a resource name conforms to the restrictions outlined in AIP-122.
return resourcename.Validate(in.Name)
}

func (s *Server) validateUpdateNullVolumeRequest(in *pb.UpdateNullVolumeRequest) error {
// check required fields
if err := fieldbehavior.ValidateRequiredFields(in); err != nil {
return err
}
// Validate that a resource name conforms to the restrictions outlined in AIP-122.
return resourcename.Validate(in.NullVolume.Name)
}

func (s *Server) validateGetNullVolumeRequest(in *pb.GetNullVolumeRequest) error {
// check required fields
if err := fieldbehavior.ValidateRequiredFields(in); err != nil {
return err
}
// Validate that a resource name conforms to the restrictions outlined in AIP-122.
return resourcename.Validate(in.Name)
}

func (s *Server) validateStatsNullVolumeRequest(in *pb.StatsNullVolumeRequest) error {
// check required fields
if err := fieldbehavior.ValidateRequiredFields(in); err != nil {
return err
}
// Validate that a resource name conforms to the restrictions outlined in AIP-122.
return resourcename.Validate(in.Name)
}
Loading

0 comments on commit 29130ec

Please sign in to comment.