github.com/cs3org/reva/v2@v2.27.7/internal/grpc/services/appregistry/appregistry.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 appregistry
    20  
    21  import (
    22  	"context"
    23  
    24  	"google.golang.org/grpc"
    25  
    26  	registrypb "github.com/cs3org/go-cs3apis/cs3/app/registry/v1beta1"
    27  	"github.com/cs3org/reva/v2/pkg/app"
    28  	"github.com/cs3org/reva/v2/pkg/app/registry/registry"
    29  	"github.com/cs3org/reva/v2/pkg/errtypes"
    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/rs/zerolog"
    34  )
    35  
    36  func init() {
    37  	rgrpc.Register("appregistry", New)
    38  }
    39  
    40  type svc struct {
    41  	reg app.Registry
    42  }
    43  
    44  func (s *svc) Close() error {
    45  	return nil
    46  }
    47  
    48  func (s *svc) UnprotectedEndpoints() []string {
    49  	return []string{"/cs3.app.registry.v1beta1.RegistryAPI/AddAppProvider", "/cs3.app.registry.v1beta1.RegistryAPI/ListSupportedMimeTypes"}
    50  }
    51  
    52  func (s *svc) Register(ss *grpc.Server) {
    53  	registrypb.RegisterRegistryAPIServer(ss, s)
    54  }
    55  
    56  type config struct {
    57  	Driver  string                            `mapstructure:"driver"`
    58  	Drivers map[string]map[string]interface{} `mapstructure:"drivers"`
    59  }
    60  
    61  func (c *config) init() {
    62  	if c.Driver == "" {
    63  		c.Driver = "static"
    64  	}
    65  }
    66  
    67  // New creates a new StorageRegistryService
    68  func New(m map[string]interface{}, ss *grpc.Server, _ *zerolog.Logger) (rgrpc.Service, error) {
    69  
    70  	c, err := parseConfig(m)
    71  	if err != nil {
    72  		return nil, err
    73  	}
    74  
    75  	reg, err := getRegistry(c)
    76  	if err != nil {
    77  		return nil, err
    78  	}
    79  
    80  	svc := &svc{
    81  		reg: reg,
    82  	}
    83  
    84  	return svc, nil
    85  }
    86  
    87  func parseConfig(m map[string]interface{}) (*config, error) {
    88  	c := &config{}
    89  	if err := mapstructure.Decode(m, c); err != nil {
    90  		return nil, err
    91  	}
    92  	c.init()
    93  	return c, nil
    94  }
    95  
    96  func getRegistry(c *config) (app.Registry, error) {
    97  	if f, ok := registry.NewFuncs[c.Driver]; ok {
    98  		return f(c.Drivers[c.Driver])
    99  	}
   100  	return nil, errtypes.NotFound("appregistrysvc: driver not found: " + c.Driver)
   101  }
   102  
   103  func (s *svc) GetAppProviders(ctx context.Context, req *registrypb.GetAppProvidersRequest) (*registrypb.GetAppProvidersResponse, error) {
   104  	p, err := s.reg.FindProviders(ctx, req.ResourceInfo.MimeType)
   105  	if err != nil {
   106  		return &registrypb.GetAppProvidersResponse{
   107  			Status: status.NewInternal(ctx, "error looking for the app provider"),
   108  		}, nil
   109  	}
   110  
   111  	res := &registrypb.GetAppProvidersResponse{
   112  		Status:    status.NewOK(ctx),
   113  		Providers: p,
   114  	}
   115  	return res, nil
   116  }
   117  
   118  func (s *svc) AddAppProvider(ctx context.Context, req *registrypb.AddAppProviderRequest) (*registrypb.AddAppProviderResponse, error) {
   119  	err := s.reg.AddProvider(ctx, req.Provider)
   120  	if err != nil {
   121  		return &registrypb.AddAppProviderResponse{
   122  			Status: status.NewInternal(ctx, "error adding the app provider"),
   123  		}, nil
   124  	}
   125  
   126  	res := &registrypb.AddAppProviderResponse{
   127  		Status: status.NewOK(ctx),
   128  	}
   129  	return res, nil
   130  }
   131  
   132  func (s *svc) ListAppProviders(ctx context.Context, req *registrypb.ListAppProvidersRequest) (*registrypb.ListAppProvidersResponse, error) {
   133  	providers, err := s.reg.ListProviders(ctx)
   134  	if err != nil {
   135  		return &registrypb.ListAppProvidersResponse{
   136  			Status: status.NewInternal(ctx, "error listing the app providers"),
   137  		}, nil
   138  	}
   139  
   140  	res := &registrypb.ListAppProvidersResponse{
   141  		Status:    status.NewOK(ctx),
   142  		Providers: providers,
   143  	}
   144  	return res, nil
   145  }
   146  
   147  func (s *svc) ListSupportedMimeTypes(ctx context.Context, req *registrypb.ListSupportedMimeTypesRequest) (*registrypb.ListSupportedMimeTypesResponse, error) {
   148  	mimeTypes, err := s.reg.ListSupportedMimeTypes(ctx)
   149  	if err != nil {
   150  		return &registrypb.ListSupportedMimeTypesResponse{
   151  			Status: status.NewInternal(ctx, "error listing the supported mime types"),
   152  		}, nil
   153  	}
   154  
   155  	// hide mimetypes for app providers
   156  	for _, mime := range mimeTypes {
   157  		for _, app := range mime.AppProviders {
   158  			app.MimeTypes = nil
   159  		}
   160  	}
   161  
   162  	res := &registrypb.ListSupportedMimeTypesResponse{
   163  		Status:    status.NewOK(ctx),
   164  		MimeTypes: mimeTypes,
   165  	}
   166  	return res, nil
   167  }
   168  
   169  func (s *svc) GetDefaultAppProviderForMimeType(ctx context.Context, req *registrypb.GetDefaultAppProviderForMimeTypeRequest) (*registrypb.GetDefaultAppProviderForMimeTypeResponse, error) {
   170  	provider, err := s.reg.GetDefaultProviderForMimeType(ctx, req.MimeType)
   171  	if err != nil {
   172  		return &registrypb.GetDefaultAppProviderForMimeTypeResponse{
   173  			Status: status.NewInternal(ctx, "error getting the default app provider for the mimetype"),
   174  		}, nil
   175  	}
   176  
   177  	res := &registrypb.GetDefaultAppProviderForMimeTypeResponse{
   178  		Status:   status.NewOK(ctx),
   179  		Provider: provider,
   180  	}
   181  	return res, nil
   182  }
   183  
   184  func (s *svc) SetDefaultAppProviderForMimeType(ctx context.Context, req *registrypb.SetDefaultAppProviderForMimeTypeRequest) (*registrypb.SetDefaultAppProviderForMimeTypeResponse, error) {
   185  	err := s.reg.SetDefaultProviderForMimeType(ctx, req.MimeType, req.Provider)
   186  	if err != nil {
   187  		return &registrypb.SetDefaultAppProviderForMimeTypeResponse{
   188  			Status: status.NewInternal(ctx, "error setting the default app provider for the mimetype"),
   189  		}, nil
   190  	}
   191  
   192  	res := &registrypb.SetDefaultAppProviderForMimeTypeResponse{
   193  		Status: status.NewOK(ctx),
   194  	}
   195  	return res, nil
   196  }