Skip to content

Commit

Permalink
Remove namespace from more manager calls
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Richardson <andrew.richardson@kaleido.io>
  • Loading branch information
awrichar committed Jun 22, 2022
1 parent 8ab22b3 commit 43ed4a7
Show file tree
Hide file tree
Showing 62 changed files with 348 additions and 399 deletions.
2 changes: 1 addition & 1 deletion internal/apiserver/route_get_chart_histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ var getChartHistogram = &ffapi.Route{
if err != nil {
return nil, i18n.NewError(cr.ctx, coremsgs.MsgInvalidChartNumberParam, "buckets")
}
return cr.or.GetChartHistogram(cr.ctx, extractNamespace(r.PP), startTime.UnixNano(), endTime.UnixNano(), buckets, database.CollectionName(r.PP["collection"]))
return cr.or.GetChartHistogram(cr.ctx, startTime.UnixNano(), endTime.UnixNano(), buckets, database.CollectionName(r.PP["collection"]))
},
},
}
2 changes: 1 addition & 1 deletion internal/apiserver/route_get_chart_histogram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestGetChartHistogramSuccess(t *testing.T) {
startTime, _ := fftypes.ParseTimeString("1234567890")
endtime, _ := fftypes.ParseTimeString("1234567891")

o.On("GetChartHistogram", mock.Anything, "mynamespace", startTime.UnixNano(), endtime.UnixNano(), int64(30), database.CollectionName("test")).
o.On("GetChartHistogram", mock.Anything, startTime.UnixNano(), endtime.UnixNano(), int64(30), database.CollectionName("test")).
Return([]*core.ChartHistogram{}, nil)
r.ServeHTTP(res, req)

Expand Down
2 changes: 1 addition & 1 deletion internal/apiserver/route_get_data_blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var getDataBlob = &ffapi.Route{
Extensions: &coreExtensions{
FilterFactory: database.MessageQueryFactory,
CoreJSONHandler: func(r *ffapi.APIRequest, cr *coreRequest) (output interface{}, err error) {
blob, reader, err := cr.or.Data().DownloadBlob(cr.ctx, extractNamespace(r.PP), r.PP["dataid"])
blob, reader, err := cr.or.Data().DownloadBlob(cr.ctx, r.PP["dataid"])
if err == nil {
r.ResponseHeaders.Set(core.HTTPHeadersBlobHashSHA256, blob.Hash.String())
if blob.Size > 0 {
Expand Down
2 changes: 1 addition & 1 deletion internal/apiserver/route_get_data_blob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestGetDataBlob(t *testing.T) {
res := httptest.NewRecorder()

blobHash := fftypes.NewRandB32()
mdm.On("DownloadBlob", mock.Anything, "mynamespace", "abcd1234").
mdm.On("DownloadBlob", mock.Anything, "abcd1234").
Return(&core.Blob{
Hash: blobHash,
Size: 12345,
Expand Down
4 changes: 2 additions & 2 deletions internal/apiserver/route_post_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ var postData = &ffapi.Route{
JSONOutputCodes: []int{http.StatusCreated},
Extensions: &coreExtensions{
CoreJSONHandler: func(r *ffapi.APIRequest, cr *coreRequest) (output interface{}, err error) {
output, err = cr.or.Data().UploadJSON(cr.ctx, extractNamespace(r.PP), r.Input.(*core.DataRefOrValue))
output, err = cr.or.Data().UploadJSON(cr.ctx, r.Input.(*core.DataRefOrValue))
return output, err
},
CoreFormUploadHandler: func(r *ffapi.APIRequest, cr *coreRequest) (output interface{}, err error) {
Expand All @@ -71,7 +71,7 @@ var postData = &ffapi.Route{
}
data.Value = fftypes.JSONAnyPtr(metadata)
}
output, err = cr.or.Data().UploadBlob(cr.ctx, extractNamespace(r.PP), data, r.Part, strings.EqualFold(r.FP["autometa"], "true"))
output, err = cr.or.Data().UploadBlob(cr.ctx, data, r.Part, strings.EqualFold(r.FP["autometa"], "true"))
return output, err
},
},
Expand Down
12 changes: 6 additions & 6 deletions internal/apiserver/route_post_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestPostDataJSON(t *testing.T) {
req.Header.Set("Content-Type", "application/json; charset=utf-8")
res := httptest.NewRecorder()

mdm.On("UploadJSON", mock.Anything, "ns1", mock.AnythingOfType("*core.DataRefOrValue")).
mdm.On("UploadJSON", mock.Anything, mock.AnythingOfType("*core.DataRefOrValue")).
Return(&core.Data{}, nil)
r.ServeHTTP(res, req)

Expand All @@ -60,7 +60,7 @@ func TestPostDataJSONDefaultNS(t *testing.T) {
req.Header.Set("Content-Type", "application/json; charset=utf-8")
res := httptest.NewRecorder()

mdm.On("UploadJSON", mock.Anything, "default", mock.AnythingOfType("*core.DataRefOrValue")).
mdm.On("UploadJSON", mock.Anything, mock.AnythingOfType("*core.DataRefOrValue")).
Return(&core.Data{}, nil)
r.ServeHTTP(res, req)

Expand All @@ -85,7 +85,7 @@ func TestPostDataBinary(t *testing.T) {

res := httptest.NewRecorder()

mdm.On("UploadBlob", mock.Anything, "ns1", mock.AnythingOfType("*core.DataRefOrValue"), mock.AnythingOfType("*ffapi.Multipart"), false).
mdm.On("UploadBlob", mock.Anything, mock.AnythingOfType("*core.DataRefOrValue"), mock.AnythingOfType("*ffapi.Multipart"), false).
Return(&core.Data{}, nil)
r.ServeHTTP(res, req)

Expand Down Expand Up @@ -125,7 +125,7 @@ func TestPostDataBinaryObjAutoMeta(t *testing.T) {

res := httptest.NewRecorder()

mdm.On("UploadBlob", mock.Anything, "ns1", mock.MatchedBy(func(d *core.DataRefOrValue) bool {
mdm.On("UploadBlob", mock.Anything, mock.MatchedBy(func(d *core.DataRefOrValue) bool {
assert.Equal(t, `{"filename":"anything"}`, string(*d.Value))
assert.Equal(t, core.ValidatorTypeJSON, d.Validator)
assert.Equal(t, "fileinfo", d.Datatype.Name)
Expand Down Expand Up @@ -159,7 +159,7 @@ func TestPostDataBinaryStringMetadata(t *testing.T) {

res := httptest.NewRecorder()

mdm.On("UploadBlob", mock.Anything, "ns1", mock.MatchedBy(func(d *core.DataRefOrValue) bool {
mdm.On("UploadBlob", mock.Anything, mock.MatchedBy(func(d *core.DataRefOrValue) bool {
assert.Equal(t, `"string metadata"`, string(*d.Value))
assert.Equal(t, "", string(d.Validator))
assert.Nil(t, d.Datatype)
Expand Down Expand Up @@ -192,7 +192,7 @@ func TestPostDataTrailingMetadata(t *testing.T) {

res := httptest.NewRecorder()

mdm.On("UploadBlob", mock.Anything, "ns1", mock.Anything, mock.AnythingOfType("*ffapi.Multipart"), false).
mdm.On("UploadBlob", mock.Anything, mock.Anything, mock.AnythingOfType("*ffapi.Multipart"), false).
Return(&core.Data{}, nil)
r.ServeHTTP(res, req)

Expand Down
2 changes: 1 addition & 1 deletion internal/apiserver/route_post_new_datatype.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var postNewDatatype = &ffapi.Route{
CoreJSONHandler: func(r *ffapi.APIRequest, cr *coreRequest) (output interface{}, err error) {
waitConfirm := strings.EqualFold(r.QP["confirm"], "true")
r.SuccessStatus = syncRetcode(waitConfirm)
_, err = cr.or.Broadcast().BroadcastDatatype(cr.ctx, extractNamespace(r.PP), r.Input.(*core.Datatype), waitConfirm)
_, err = cr.or.Broadcast().BroadcastDatatype(cr.ctx, r.Input.(*core.Datatype), waitConfirm)
return r.Input, err
},
},
Expand Down
4 changes: 2 additions & 2 deletions internal/apiserver/route_post_new_datatype_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestPostNewDatatypes(t *testing.T) {
req.Header.Set("Content-Type", "application/json; charset=utf-8")
res := httptest.NewRecorder()

mbm.On("BroadcastDatatype", mock.Anything, "ns1", mock.AnythingOfType("*core.Datatype"), false).
mbm.On("BroadcastDatatype", mock.Anything, mock.AnythingOfType("*core.Datatype"), false).
Return(&core.Message{}, nil)
r.ServeHTTP(res, req)

Expand All @@ -57,7 +57,7 @@ func TestPostNewDatatypesSync(t *testing.T) {
req.Header.Set("Content-Type", "application/json; charset=utf-8")
res := httptest.NewRecorder()

mbm.On("BroadcastDatatype", mock.Anything, "ns1", mock.AnythingOfType("*core.Datatype"), true).
mbm.On("BroadcastDatatype", mock.Anything, mock.AnythingOfType("*core.Datatype"), true).
Return(&core.Message{}, nil)
r.ServeHTTP(res, req)

Expand Down
2 changes: 1 addition & 1 deletion internal/apiserver/route_post_op_retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var postOpRetry = &ffapi.Route{
if err != nil {
return nil, err
}
return cr.or.Operations().RetryOperation(cr.ctx, extractNamespace(r.PP), opid)
return cr.or.Operations().RetryOperation(cr.ctx, opid)
},
},
}
2 changes: 1 addition & 1 deletion internal/apiserver/route_post_op_retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestPostOpRetry(t *testing.T) {
req.Header.Set("Content-Type", "application/json; charset=utf-8")
res := httptest.NewRecorder()

mom.On("RetryOperation", mock.Anything, "ns1", opID).
mom.On("RetryOperation", mock.Anything, opID).
Return(&core.Operation{}, nil)
r.ServeHTTP(res, req)

Expand Down
8 changes: 0 additions & 8 deletions internal/apiserver/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package apiserver
import (
"context"

"github.com/hyperledger/firefly-common/pkg/config"
"github.com/hyperledger/firefly-common/pkg/ffapi"
"github.com/hyperledger/firefly/internal/coreconfig"
"github.com/hyperledger/firefly/internal/coremsgs"
Expand Down Expand Up @@ -176,10 +175,3 @@ func namespacedRoutes(routes []*ffapi.Route) []*ffapi.Route {
}
return append(routes, newRoutes...)
}

func extractNamespace(pathParams map[string]string) string {
if ns, ok := pathParams["ns"]; ok {
return ns
}
return config.GetString(coreconfig.NamespacesDefault)
}
29 changes: 15 additions & 14 deletions internal/apiserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,19 @@ func (as *apiServer) contractSwaggerGenerator(mgr namespace.Manager, apiBaseURL
}
}

func getOrchestrator(mgr namespace.Manager, tag string, r *ffapi.APIRequest) orchestrator.Orchestrator {
if tag == routeTagDefaultNamespace {
return mgr.Orchestrator(config.GetString(coreconfig.NamespacesDefault))
}
if tag == routeTagNonDefaultNamespace {
vars := mux.Vars(r.Req)
if ns, ok := vars["ns"]; ok {
return mgr.Orchestrator(ns)
}
}
return nil
}

func (as *apiServer) routeHandler(hf *ffapi.HandlerFactory, mgr namespace.Manager, apiBaseURL string, route *ffapi.Route) http.HandlerFunc {
// We extend the base ffapi functionality, with standardized DB filter support for all core resources.
// We also pass the Orchestrator context through
Expand All @@ -231,15 +244,9 @@ func (as *apiServer) routeHandler(hf *ffapi.HandlerFactory, mgr namespace.Manage
}
}

var or orchestrator.Orchestrator
if route.Tag == routeTagDefaultNamespace || route.Tag == routeTagNonDefaultNamespace {
vars := mux.Vars(r.Req)
or = mgr.Orchestrator(extractNamespace(vars))
}

cr := &coreRequest{
mgr: mgr,
or: or,
or: getOrchestrator(mgr, route.Tag, r),
ctx: r.Req.Context(),
filter: filter,
apiBaseURL: apiBaseURL,
Expand All @@ -248,15 +255,9 @@ func (as *apiServer) routeHandler(hf *ffapi.HandlerFactory, mgr namespace.Manage
}
if ce.CoreFormUploadHandler != nil {
route.FormUploadHandler = func(r *ffapi.APIRequest) (output interface{}, err error) {
var or orchestrator.Orchestrator
if route.Tag == routeTagDefaultNamespace || route.Tag == routeTagNonDefaultNamespace {
vars := mux.Vars(r.Req)
or = mgr.Orchestrator(extractNamespace(vars))
}

cr := &coreRequest{
mgr: mgr,
or: or,
or: getOrchestrator(mgr, route.Tag, r),
ctx: r.Req.Context(),
apiBaseURL: apiBaseURL,
}
Expand Down
8 changes: 4 additions & 4 deletions internal/broadcast/datatype.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ import (
"github.com/hyperledger/firefly/pkg/core"
)

func (bm *broadcastManager) BroadcastDatatype(ctx context.Context, ns string, datatype *core.Datatype, waitConfirm bool) (*core.Message, error) {
func (bm *broadcastManager) BroadcastDatatype(ctx context.Context, datatype *core.Datatype, waitConfirm bool) (*core.Message, error) {

// Validate the input data definition data
datatype.ID = fftypes.NewUUID()
datatype.Created = fftypes.Now()
datatype.Namespace = ns
datatype.Namespace = bm.namespace
if datatype.Validator == "" {
datatype.Validator = core.ValidatorTypeJSON
}
Expand All @@ -41,10 +41,10 @@ func (bm *broadcastManager) BroadcastDatatype(ctx context.Context, ns string, da
datatype.Hash = datatype.Value.Hash()

// Verify the data type is now all valid, before we broadcast it
if err := bm.data.CheckDatatype(ctx, ns, datatype); err != nil {
if err := bm.data.CheckDatatype(ctx, datatype); err != nil {
return nil, err
}
msg, err := bm.BroadcastDefinitionAsNode(ctx, ns, datatype, core.SystemTagDefineDatatype, waitConfirm)
msg, err := bm.BroadcastDefinitionAsNode(ctx, datatype, core.SystemTagDefineDatatype, waitConfirm)
if msg != nil {
datatype.Message = msg.Header.ID
}
Expand Down
20 changes: 10 additions & 10 deletions internal/broadcast/datatype_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
func TestBroadcastDatatypeBadType(t *testing.T) {
bm, cancel := newTestBroadcast(t)
defer cancel()
_, err := bm.BroadcastDatatype(context.Background(), "ns1", &core.Datatype{
_, err := bm.BroadcastDatatype(context.Background(), &core.Datatype{
Validator: core.ValidatorType("wrong"),
}, false)
assert.Regexp(t, "FF00111.*validator", err)
Expand All @@ -45,7 +45,7 @@ func TestBroadcastDatatypeNSGetFail(t *testing.T) {
defer cancel()
mdm := bm.data.(*datamocks.Manager)
mdm.On("VerifyNamespaceExists", mock.Anything, "ns1").Return(fmt.Errorf("pop"))
_, err := bm.BroadcastDatatype(context.Background(), "ns1", &core.Datatype{
_, err := bm.BroadcastDatatype(context.Background(), &core.Datatype{
Name: "name1",
Namespace: "ns1",
Version: "0.0.1",
Expand All @@ -59,10 +59,10 @@ func TestBroadcastDatatypeBadValue(t *testing.T) {
defer cancel()
mdm := bm.data.(*datamocks.Manager)
mdm.On("VerifyNamespaceExists", mock.Anything, "ns1").Return(nil)
mdm.On("CheckDatatype", mock.Anything, "ns1", mock.Anything).Return(nil)
mdm.On("CheckDatatype", mock.Anything, mock.Anything).Return(nil)
mim := bm.identity.(*identitymanagermocks.Manager)
mim.On("ResolveInputSigningIdentity", mock.Anything, mock.Anything).Return(nil)
_, err := bm.BroadcastDatatype(context.Background(), "ns1", &core.Datatype{
_, err := bm.BroadcastDatatype(context.Background(), &core.Datatype{
Namespace: "ns1",
Name: "ent1",
Version: "0.0.1",
Expand All @@ -80,9 +80,9 @@ func TestBroadcastUpsertFail(t *testing.T) {
mim.On("ResolveInputSigningIdentity", mock.Anything, mock.Anything).Return(nil)
mdm.On("WriteNewMessage", mock.Anything, mock.Anything, mock.Anything).Return(fmt.Errorf("pop"))
mdm.On("VerifyNamespaceExists", mock.Anything, "ns1").Return(nil)
mdm.On("CheckDatatype", mock.Anything, "ns1", mock.Anything).Return(nil)
mdm.On("CheckDatatype", mock.Anything, mock.Anything).Return(nil)

_, err := bm.BroadcastDatatype(context.Background(), "ns1", &core.Datatype{
_, err := bm.BroadcastDatatype(context.Background(), &core.Datatype{
Namespace: "ns1",
Name: "ent1",
Version: "0.0.1",
Expand All @@ -104,9 +104,9 @@ func TestBroadcastDatatypeInvalid(t *testing.T) {
mim.On("ResolveInputIdentity", mock.Anything, mock.Anything).Return(nil)
mdi.On("UpsertData", mock.Anything, mock.Anything, database.UpsertOptimizationNew).Return(nil)
mdm.On("VerifyNamespaceExists", mock.Anything, "ns1").Return(nil)
mdm.On("CheckDatatype", mock.Anything, "ns1", mock.Anything).Return(fmt.Errorf("pop"))
mdm.On("CheckDatatype", mock.Anything, mock.Anything).Return(fmt.Errorf("pop"))

_, err := bm.BroadcastDatatype(context.Background(), "ns1", &core.Datatype{
_, err := bm.BroadcastDatatype(context.Background(), &core.Datatype{
Namespace: "ns1",
Name: "ent1",
Version: "0.0.1",
Expand All @@ -123,10 +123,10 @@ func TestBroadcastOk(t *testing.T) {

mim.On("ResolveInputSigningIdentity", mock.Anything, mock.Anything).Return(nil)
mdm.On("VerifyNamespaceExists", mock.Anything, "ns1").Return(nil)
mdm.On("CheckDatatype", mock.Anything, "ns1", mock.Anything).Return(nil)
mdm.On("CheckDatatype", mock.Anything, mock.Anything).Return(nil)
mdm.On("WriteNewMessage", mock.Anything, mock.Anything, mock.Anything).Return(nil)

_, err := bm.BroadcastDatatype(context.Background(), "ns1", &core.Datatype{
_, err := bm.BroadcastDatatype(context.Background(), &core.Datatype{
Namespace: "ns1",
Name: "ent1",
Version: "0.0.1",
Expand Down
18 changes: 9 additions & 9 deletions internal/broadcast/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,39 +28,39 @@ import (
"github.com/hyperledger/firefly/pkg/core"
)

func (bm *broadcastManager) BroadcastDefinitionAsNode(ctx context.Context, ns string, def core.Definition, tag string, waitConfirm bool) (msg *core.Message, err error) {
return bm.BroadcastDefinition(ctx, ns, def, &core.SignerRef{ /* resolve to node default */ }, tag, waitConfirm)
func (bm *broadcastManager) BroadcastDefinitionAsNode(ctx context.Context, def core.Definition, tag string, waitConfirm bool) (msg *core.Message, err error) {
return bm.BroadcastDefinition(ctx, def, &core.SignerRef{ /* resolve to node default */ }, tag, waitConfirm)
}

func (bm *broadcastManager) BroadcastDefinition(ctx context.Context, ns string, def core.Definition, signingIdentity *core.SignerRef, tag string, waitConfirm bool) (msg *core.Message, err error) {
func (bm *broadcastManager) BroadcastDefinition(ctx context.Context, def core.Definition, signingIdentity *core.SignerRef, tag string, waitConfirm bool) (msg *core.Message, err error) {

err = bm.identity.ResolveInputSigningIdentity(ctx, signingIdentity)
if err != nil {
return nil, err
}

return bm.broadcastDefinitionCommon(ctx, ns, def, signingIdentity, tag, waitConfirm)
return bm.broadcastDefinitionCommon(ctx, def, signingIdentity, tag, waitConfirm)
}

// BroadcastIdentityClaim is a special form of BroadcastDefinitionAsNode where the signing identity does not need to have been pre-registered
// The blockchain "key" will be normalized, but the "author" will pass through unchecked
func (bm *broadcastManager) BroadcastIdentityClaim(ctx context.Context, ns string, def *core.IdentityClaim, signingIdentity *core.SignerRef, tag string, waitConfirm bool) (msg *core.Message, err error) {
func (bm *broadcastManager) BroadcastIdentityClaim(ctx context.Context, def *core.IdentityClaim, signingIdentity *core.SignerRef, tag string, waitConfirm bool) (msg *core.Message, err error) {

signingIdentity.Key, err = bm.identity.NormalizeSigningKey(ctx, signingIdentity.Key, identity.KeyNormalizationBlockchainPlugin)
if err != nil {
return nil, err
}

return bm.broadcastDefinitionCommon(ctx, ns, def, signingIdentity, tag, waitConfirm)
return bm.broadcastDefinitionCommon(ctx, def, signingIdentity, tag, waitConfirm)
}

func (bm *broadcastManager) broadcastDefinitionCommon(ctx context.Context, ns string, def core.Definition, signingIdentity *core.SignerRef, tag string, waitConfirm bool) (*core.Message, error) {
func (bm *broadcastManager) broadcastDefinitionCommon(ctx context.Context, def core.Definition, signingIdentity *core.SignerRef, tag string, waitConfirm bool) (*core.Message, error) {

// Serialize it into a data object, as a piece of data we can write to a message
d := &core.Data{
Validator: core.ValidatorTypeSystemDefinition,
ID: fftypes.NewUUID(),
Namespace: ns,
Namespace: bm.namespace,
Created: fftypes.Now(),
}
b, err := json.Marshal(&def)
Expand All @@ -77,7 +77,7 @@ func (bm *broadcastManager) broadcastDefinitionCommon(ctx context.Context, ns st
Message: &core.MessageInOut{
Message: core.Message{
Header: core.MessageHeader{
Namespace: ns,
Namespace: bm.namespace,
Type: core.MessageTypeDefinition,
SignerRef: *signingIdentity,
Topics: core.FFStringArray{def.Topic()},
Expand Down
Loading

0 comments on commit 43ed4a7

Please sign in to comment.