Skip to content

Commit 6e4cbf2

Browse files
dragonchaserfschade
authored andcommitted
add new function
Signed-off-by: Christian Richter <c.richter@opencloud.eu>
1 parent 9e7f448 commit 6e4cbf2

4 files changed

Lines changed: 56 additions & 27 deletions

File tree

pkg/systemstorageclient/systemstorageclient.go

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,61 +2,81 @@ package systemstorageclient
22

33
import (
44
"context"
5-
"fmt"
6-
"github.com/opencloud-eu/opencloud/pkg/log"
5+
"path"
6+
"sync"
77

8+
"github.com/opencloud-eu/opencloud/pkg/log"
89
"github.com/opencloud-eu/reva/v2/pkg/storage/utils/metadata"
910
)
1011

12+
var (
13+
managerName = "systemdata"
14+
)
15+
1116
type SystemDataStorageClient struct {
1217
mds metadata.Storage
18+
l *sync.Mutex
1319
}
1420

15-
func (s SystemDataStorageClient) SimpleDownload(ctx context.Context, userID, identifier string) ([]byte, error) {
21+
func (s *SystemDataStorageClient) SimpleDownload(ctx context.Context, userID, identifier string) ([]byte, error) {
1622
//TODO implement me
1723
panic("implement me")
1824
}
1925

20-
func (s SystemDataStorageClient) SimpleUpload(ctx context.Context, userID, identifier string, content []byte) error {
21-
s.mds.SimpleUpload(ctx, fmt.Sprintf("%s/%s", userID, identifier), content)
22-
return nil
26+
func (s *SystemDataStorageClient) SimpleUpload(ctx context.Context, userID, identifier string, content []byte) error {
27+
return s.mds.SimpleUpload(ctx, path.Join(userID, identifier), content)
2328
}
2429

25-
func (s SystemDataStorageClient) Delete(ctx context.Context, userID, identifier string) error {
30+
func (s *SystemDataStorageClient) Delete(ctx context.Context, userID, identifier string) error {
2631
//TODO implement me
2732
panic("implement me")
2833
}
2934

30-
func (s SystemDataStorageClient) ReadDir(ctx context.Context, userID, identifier string) ([]string, error) {
35+
func (s *SystemDataStorageClient) ReadDir(ctx context.Context, userID, identifier string) ([]string, error) {
3136
//TODO implement me
3237
panic("implement me")
3338
}
3439

35-
func (s SystemDataStorageClient) MakeDirIfNotExist(ctx context.Context, userID, identifier string) error {
40+
func (s *SystemDataStorageClient) MakeDirIfNotExist(ctx context.Context, userID, identifier string) error {
3641
//TODO implement me
3742
panic("implement me")
3843
}
3944

40-
func (s SystemDataStorageClient) Init(ctx context.Context, userID, identifier string) error {
41-
//TODO implement me
42-
panic("implement me")
45+
// New initialize the store once, later calls are noops
46+
func (s *SystemDataStorageClient) New(ctx context.Context,
47+
logger *log.Logger,
48+
scope, namespace string,
49+
gatewayAddress, storageAddress, systemUserID, systemUserIDP, systemUserAPIKey string,
50+
) {
51+
if s.mds != nil {
52+
return
53+
}
54+
55+
s.l.Lock()
56+
defer s.l.Unlock()
57+
58+
if s.mds != nil {
59+
return
60+
}
61+
62+
mds, err := metadata.NewCS3Storage(gatewayAddress, storageAddress, systemUserID, systemUserIDP, systemUserAPIKey)
63+
if err != nil {
64+
logger.Fatal().Err(err).Msg("could not create profile storage client")
65+
}
66+
s.mds = mds
4367
}
4468

4569
// NewProfileStorageClient creates a new ProfileStorageClient
4670
func NewSystemStorageClient(scope, nameSpace string,
4771
logger *log.Logger,
48-
gatewayAddress, storageAddress, systemUserID, systemUserIDP, systemUserAPIKey string) SystemDataStorageClient {
72+
gatewayAddress, storageAddress, systemUserID, systemUserIDP, systemUserAPIKey string) *SystemDataStorageClient {
4973

5074
// scope: the scope the data should be persistet in (e.g. user)
5175
// namespace: the namespace the data should be persistet in (e.g. profilephoto)
5276
// results in the following path: /<scope>/*/<namespace>/*
77+
// e.g. /user/<uid>/profilephoto/profilephoto.jpg
5378

54-
sdsci := SystemDataStorageClient{}
55-
logger.Debug().Msg("NewSystemStorageClient called")
56-
sdsc, err := metadata.NewCS3Storage(gatewayAddress, storageAddress, systemUserID, systemUserIDP, systemUserAPIKey)
57-
if err != nil {
58-
logger.Fatal().Err(err).Msg("could not create profile storage client")
59-
}
60-
sdsci.mds = sdsc
79+
sdsci := &SystemDataStorageClient{}
80+
sdsci.New(context.TODO(), logger, scope, nameSpace, gatewayAddress, storageAddress, systemUserID, systemUserIDP, systemUserAPIKey)
6181
return sdsci
6282
}

services/graph/pkg/config/config.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,11 @@ type ServiceAccount struct {
158158

159159
// SystemStorageClient configures the metadata store to use
160160
type SystemStorageClient struct {
161-
GatewayAddress string `yaml:"gateway_addr" env:"SETTINGS_STORAGE_GATEWAY_GRPC_ADDR;STORAGE_GATEWAY_GRPC_ADDR" desc:"GRPC address of the STORAGE-SYSTEM service." introductionVersion:"1.0.0"`
162-
StorageAddress string `yaml:"storage_addr" env:"SETTINGS_STORAGE_GRPC_ADDR;STORAGE_GRPC_ADDR" desc:"GRPC address of the STORAGE-SYSTEM service." introductionVersion:"1.0.0"`
161+
GatewayAddress string `yaml:"gateway_addr" env:"GRAPH_STORAGE_GATEWAY_GRPC_ADDR;STORAGE_GATEWAY_GRPC_ADDR" desc:"GRPC address of the STORAGE-SYSTEM service." introductionVersion:"1.0.0"`
162+
StorageAddress string `yaml:"storage_addr" env:"GRAPH_STORAGE_GRPC_ADDR;STORAGE_GRPC_ADDR" desc:"GRPC address of the STORAGE-SYSTEM service." introductionVersion:"1.0.0"`
163163

164-
SystemUserID string `yaml:"system_user_id" env:"OC_SYSTEM_USER_ID;SETTINGS_SYSTEM_USER_ID" desc:"ID of the OpenCloud STORAGE-SYSTEM system user. Admins need to set the ID for the STORAGE-SYSTEM system user in this config option which is then used to reference the user. Any reasonable long string is possible, preferably this would be an UUIDv4 format." introductionVersion:"1.0.0"`
165-
SystemUserIDP string `yaml:"system_user_idp" env:"OC_SYSTEM_USER_IDP;SETTINGS_SYSTEM_USER_IDP" desc:"IDP of the OpenCloud STORAGE-SYSTEM system user." introductionVersion:"1.0.0"`
164+
SystemUserID string `yaml:"system_user_id" env:"OC_SYSTEM_USER_ID;GRAPH_SYSTEM_USER_ID" desc:"ID of the OpenCloud STORAGE-SYSTEM system user. Admins need to set the ID for the STORAGE-SYSTEM system user in this config option which is then used to reference the user. Any reasonable long string is possible, preferably this would be an UUIDv4 format." introductionVersion:"1.0.0"`
165+
SystemUserIDP string `yaml:"system_user_idp" env:"OC_SYSTEM_USER_IDP;GRAPH_SYSTEM_USER_IDP" desc:"IDP of the OpenCloud STORAGE-SYSTEM system user." introductionVersion:"1.0.0"`
166166
SystemUserAPIKey string `yaml:"system_user_api_key" env:"OC_SYSTEM_USER_API_KEY" desc:"API key for the STORAGE-SYSTEM system user." introductionVersion:"1.0.0"`
167167
Cache *Cache `yaml:"cache"`
168168
}

services/graph/pkg/config/defaults/defaultconfig.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,15 @@ func EnsureDefaults(cfg *config.Config) {
202202
cfg.UnifiedRoles.AvailableRoles = append(cfg.UnifiedRoles.AvailableRoles, definition.GetId())
203203
}
204204
}
205+
206+
if cfg.SystemStorageClient.SystemUserAPIKey == "" && cfg.Commons != nil && cfg.Commons.SystemUserAPIKey != "" {
207+
cfg.SystemStorageClient.SystemUserAPIKey = cfg.Commons.SystemUserAPIKey
208+
}
209+
210+
if cfg.SystemStorageClient.SystemUserID == "" && cfg.Commons != nil && cfg.Commons.SystemUserID != "" {
211+
cfg.SystemStorageClient.SystemUserID = cfg.Commons.SystemUserID
212+
}
213+
205214
}
206215

207216
// Sanitize sanitized the configuration

services/graph/pkg/service/v0/photo.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func (g Graph) UpdatePhoto(w http.ResponseWriter, r *http.Request) {
9191
func (g Graph) updatePhoto(w http.ResponseWriter, r *http.Request, u *userpb.UserId) {
9292
logger := g.logger.SubloggerWithRequestID(r.Context())
9393
logger.Debug().Msg("UpdatePhoto called")
94-
g.getSystemStorageClient()
94+
client := g.getSystemStorageClient()
9595
content, err := io.ReadAll(r.Body)
9696
if err != nil {
9797
logger.Debug().Err(err).Msg("could not read body")
@@ -103,7 +103,7 @@ func (g Graph) updatePhoto(w http.ResponseWriter, r *http.Request, u *userpb.Use
103103
errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, "empty body")
104104
return
105105
}
106-
err = g.sdsc.SimpleUpload(r.Context(), u.GetOpaqueId(), identifier, content)
106+
err = client.SimpleUpload(r.Context(), u.GetOpaqueId(), identifier, content)
107107
if err != nil {
108108
logger.Debug().Err(err).Msg("could not upload photo")
109109
errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, "could not upload photo")
@@ -115,7 +115,7 @@ func (g Graph) updatePhoto(w http.ResponseWriter, r *http.Request, u *userpb.Use
115115

116116
func (g Graph) getSystemStorageClient() systemstorageclient.SystemDataStorageClient {
117117
// TODO: this needs a check if the client is already initialized and if not, initialize it
118-
g.sdsc = systemstorageclient.NewSystemStorageClient(
118+
g.sdsc = *systemstorageclient.NewSystemStorageClient(
119119
scope,
120120
namespace,
121121
g.logger,

0 commit comments

Comments
 (0)