github.com/cs3org/reva/v2@v2.27.7/internal/grpc/services/preferences/preferences.go (about)

     1  // Copyright 2018-2021 CERN
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // In applying this license, CERN does not waive the privileges and immunities
    16  // granted to it by virtue of its status as an Intergovernmental Organization
    17  // or submit itself to any jurisdiction.
    18  
    19  package preferences
    20  
    21  import (
    22  	"context"
    23  
    24  	"google.golang.org/grpc"
    25  
    26  	preferencespb "github.com/cs3org/go-cs3apis/cs3/preferences/v1beta1"
    27  	"github.com/cs3org/reva/v2/pkg/errtypes"
    28  	"github.com/cs3org/reva/v2/pkg/preferences"
    29  	"github.com/cs3org/reva/v2/pkg/preferences/registry"
    30  	"github.com/cs3org/reva/v2/pkg/rgrpc"
    31  	"github.com/cs3org/reva/v2/pkg/rgrpc/status"
    32  	"github.com/mitchellh/mapstructure"
    33  	"github.com/pkg/errors"
    34  	"github.com/rs/zerolog"
    35  )
    36  
    37  func init() {
    38  	rgrpc.Register("preferences", New)
    39  }
    40  
    41  type config struct {
    42  	Driver  string                            `mapstructure:"driver"`
    43  	Drivers map[string]map[string]interface{} `mapstructure:"drivers"`
    44  }
    45  
    46  func (c *config) init() {
    47  	if c.Driver == "" {
    48  		c.Driver = "memory"
    49  	}
    50  }
    51  
    52  type service struct {
    53  	conf *config
    54  	pm   preferences.Manager
    55  }
    56  
    57  func getPreferencesManager(c *config) (preferences.Manager, error) {
    58  	if f, ok := registry.NewFuncs[c.Driver]; ok {
    59  		return f(c.Drivers[c.Driver])
    60  	}
    61  	return nil, errtypes.NotFound("driver not found: " + c.Driver)
    62  }
    63  
    64  func parseConfig(m map[string]interface{}) (*config, error) {
    65  	c := &config{}
    66  	if err := mapstructure.Decode(m, c); err != nil {
    67  		err = errors.Wrap(err, "error decoding conf")
    68  		return nil, err
    69  	}
    70  	return c, nil
    71  }
    72  
    73  // New returns a new PreferencesServiceServer
    74  func New(m map[string]interface{}, ss *grpc.Server, _ *zerolog.Logger) (rgrpc.Service, error) {
    75  	c, err := parseConfig(m)
    76  	if err != nil {
    77  		return nil, err
    78  	}
    79  
    80  	c.init()
    81  
    82  	pm, err := getPreferencesManager(c)
    83  	if err != nil {
    84  		return nil, err
    85  	}
    86  
    87  	return &service{
    88  		conf: c,
    89  		pm:   pm,
    90  	}, nil
    91  }
    92  
    93  func (s *service) Close() error {
    94  	return nil
    95  }
    96  
    97  func (s *service) UnprotectedEndpoints() []string {
    98  	return []string{}
    99  }
   100  
   101  func (s *service) Register(ss *grpc.Server) {
   102  	preferencespb.RegisterPreferencesAPIServer(ss, s)
   103  }
   104  
   105  func (s *service) SetKey(ctx context.Context, req *preferencespb.SetKeyRequest) (*preferencespb.SetKeyResponse, error) {
   106  	err := s.pm.SetKey(ctx, req.Key.Key, req.Key.Namespace, req.Val)
   107  	if err != nil {
   108  		return &preferencespb.SetKeyResponse{
   109  			Status: status.NewInternal(ctx, "error setting key"),
   110  		}, nil
   111  	}
   112  
   113  	return &preferencespb.SetKeyResponse{
   114  		Status: status.NewOK(ctx),
   115  	}, nil
   116  }
   117  
   118  func (s *service) GetKey(ctx context.Context, req *preferencespb.GetKeyRequest) (*preferencespb.GetKeyResponse, error) {
   119  	val, err := s.pm.GetKey(ctx, req.Key.Key, req.Key.Namespace)
   120  	if err != nil {
   121  		st := status.NewInternal(ctx, "error retrieving key")
   122  		if _, ok := err.(errtypes.IsNotFound); ok {
   123  			st = status.NewNotFound(ctx, "key not found")
   124  		}
   125  		return &preferencespb.GetKeyResponse{
   126  			Status: st,
   127  		}, nil
   128  	}
   129  
   130  	return &preferencespb.GetKeyResponse{
   131  		Status: status.NewOK(ctx),
   132  		Val:    val,
   133  	}, nil
   134  }