NOISSUE - Fix Bootstrap thing creation flow (#2083)

Signed-off-by: Arvindh <arvindh91@gmail.com>
This commit is contained in:
Arvindh
2024-02-19 20:33:21 +05:30
committed by GitHub
parent 21c5813a96
commit 4c206ec375
10 changed files with 106 additions and 54 deletions
+3 -2
View File
@@ -29,6 +29,7 @@ import (
"github.com/absmach/magistrala/pkg/errors"
mgsdk "github.com/absmach/magistrala/pkg/sdk/go"
sdkmocks "github.com/absmach/magistrala/pkg/sdk/mocks"
"github.com/absmach/magistrala/pkg/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
@@ -180,8 +181,8 @@ func newService() (bootstrap.Service, *authmocks.AuthClient, *sdkmocks.SDK) {
things := mocks.NewConfigsRepository()
auth := new(authmocks.AuthClient)
sdk := new(sdkmocks.SDK)
return bootstrap.New(auth, things, sdk, encKey), auth, sdk
idp := uuid.NewMock()
return bootstrap.New(auth, things, sdk, encKey, idp), auth, sdk
}
func newBootstrapServer(svc bootstrap.Service) *httptest.Server {
+7 -6
View File
@@ -23,11 +23,12 @@ import (
)
const (
contentType = "application/json"
offsetKey = "offset"
limitKey = "limit"
defOffset = 0
defLimit = 10
contentType = "application/json"
byteContentType = "application/octet-stream"
offsetKey = "offset"
limitKey = "limit"
defOffset = 0
defLimit = 10
)
var (
@@ -259,7 +260,7 @@ func encodeResponse(_ context.Context, w http.ResponseWriter, response interface
}
func encodeSecureRes(_ context.Context, w http.ResponseWriter, response interface{}) error {
w.Header().Set("Content-Type", contentType)
w.Header().Set("Content-Type", byteContentType)
w.WriteHeader(http.StatusOK)
if b, ok := response.([]byte); ok {
if _, err := w.Write(b); err != nil {
+3 -2
View File
@@ -23,6 +23,7 @@ import (
"github.com/absmach/magistrala/pkg/events/store"
mgsdk "github.com/absmach/magistrala/pkg/sdk/go"
sdkmocks "github.com/absmach/magistrala/pkg/sdk/mocks"
"github.com/absmach/magistrala/pkg/uuid"
"github.com/go-redis/redis/v8"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
@@ -81,8 +82,8 @@ func newService(t *testing.T, url string) (bootstrap.Service, *authmocks.AuthCli
things := mocks.NewConfigsRepository()
auth := new(authmocks.AuthClient)
sdk := new(sdkmocks.SDK)
svc := bootstrap.New(auth, things, sdk, encKey)
idp := uuid.NewMock()
svc := bootstrap.New(auth, things, sdk, encKey, idp)
publisher, err := store.NewPublisher(context.Background(), url, streamID)
require.Nil(t, err, fmt.Sprintf("got unexpected error: %s", err))
svc = producer.NewEventStoreMiddleware(svc, publisher)
+24 -25
View File
@@ -104,19 +104,21 @@ type ConfigReader interface {
}
type bootstrapService struct {
auth magistrala.AuthServiceClient
configs ConfigRepository
sdk mgsdk.SDK
encKey []byte
auth magistrala.AuthServiceClient
configs ConfigRepository
sdk mgsdk.SDK
encKey []byte
idProvider magistrala.IDProvider
}
// New returns new Bootstrap service.
func New(auth magistrala.AuthServiceClient, configs ConfigRepository, sdk mgsdk.SDK, encKey []byte) Service {
func New(auth magistrala.AuthServiceClient, configs ConfigRepository, sdk mgsdk.SDK, encKey []byte, idp magistrala.IDProvider) Service {
return &bootstrapService{
configs: configs,
sdk: sdk,
auth: auth,
encKey: encKey,
configs: configs,
sdk: sdk,
auth: auth,
encKey: encKey,
idProvider: idp,
}
}
@@ -152,8 +154,10 @@ func (bs bootstrapService) Add(ctx context.Context, token string, cfg Config) (C
saved, err := bs.configs.Save(ctx, cfg, toConnect)
if err != nil {
// If id is empty, then a new thing has been created function - bs.thing(id, token)
// So, on bootstrap config save error , delete the newly created thing.
if id == "" {
if _, errT := bs.sdk.DisableThing(cfg.ThingID, token); errT != nil {
if errT := bs.sdk.DeleteThing(cfg.ThingID, token); errT != nil {
err = errors.Wrap(err, errT)
}
}
@@ -381,29 +385,24 @@ func (bs bootstrapService) identify(ctx context.Context, token string) (string,
// Method thing retrieves Magistrala Thing creating one if an empty ID is passed.
func (bs bootstrapService) thing(id, token string) (mgsdk.Thing, error) {
var thing mgsdk.Thing
var err error
var sdkErr errors.SDKError
thing.ID = id
// If Thing ID is not provided, then create new thing.
if id == "" {
thing, sdkErr = bs.sdk.CreateThing(mgsdk.Thing{}, token)
id, err := bs.idProvider.ID()
if err != nil {
return mgsdk.Thing{}, errors.Wrap(errCreateThing, err)
}
thing, sdkErr := bs.sdk.CreateThing(mgsdk.Thing{ID: id, Name: "Bootstrapped Thing " + id}, token)
if sdkErr != nil {
return mgsdk.Thing{}, errors.Wrap(errCreateThing, errors.New(sdkErr.Err().Msg()))
}
return thing, nil
}
thing, sdkErr = bs.sdk.Thing(thing.ID, token)
// If Thing ID is provided, then retrieve thing
thing, sdkErr := bs.sdk.Thing(id, token)
if sdkErr != nil {
err = errors.New(sdkErr.Error())
if id != "" {
if _, sdkErr2 := bs.sdk.DisableThing(thing.ID, token); sdkErr2 != nil {
err = errors.Wrap(errors.New(sdkErr.Msg()), errors.New(sdkErr2.Msg()))
}
}
return mgsdk.Thing{}, errors.Wrap(ErrThings, err)
return mgsdk.Thing{}, errors.Wrap(ErrThings, sdkErr)
}
return thing, nil
}
+3 -1
View File
@@ -23,6 +23,7 @@ import (
svcerr "github.com/absmach/magistrala/pkg/errors/service"
mgsdk "github.com/absmach/magistrala/pkg/sdk/go"
sdkmocks "github.com/absmach/magistrala/pkg/sdk/mocks"
"github.com/absmach/magistrala/pkg/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
@@ -60,8 +61,9 @@ func newService() (bootstrap.Service, *authmocks.AuthClient, *sdkmocks.SDK) {
things := mocks.NewConfigsRepository()
auth := new(authmocks.AuthClient)
sdk := new(sdkmocks.SDK)
idp := uuid.NewMock()
return bootstrap.New(auth, things, sdk, encKey), auth, sdk
return bootstrap.New(auth, things, sdk, encKey, idp), auth, sdk
}
func enc(in []byte) ([]byte, error) {