Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Delete device profile in use should return 409 #4941

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions internal/core/metadata/application/deviceprofile.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,22 @@ func DeleteDeviceProfileByName(name string, ctx context.Context, dic *di.Contain
if err != nil {
return errors.NewCommonEdgeXWrapper(err)
}
// Check the associated Device and ProvisionWatcher existence
devices, edgeXErr := dbClient.DevicesByProfileName(0, 1, name)
if edgeXErr != nil {
return errors.NewCommonEdgeXWrapper(edgeXErr)
}
if len(devices) > 0 {
return errors.NewCommonEdgeX(errors.KindStatusConflict, "fail to delete the device profile when associated device exists", nil)
}
provisionWatchers, edgeXErr := dbClient.ProvisionWatchersByProfileName(0, 1, name)
if edgeXErr != nil {
return errors.NewCommonEdgeXWrapper(edgeXErr)
}
if len(provisionWatchers) > 0 {
return errors.NewCommonEdgeX(errors.KindStatusConflict, "fail to delete the device profile when associated provisionWatcher exists", nil)
}

err = dbClient.DeleteDeviceProfileByName(name)
if err != nil {
return errors.NewCommonEdgeXWrapper(err)
Expand Down
16 changes: 16 additions & 0 deletions internal/core/metadata/application/deviceservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,22 @@ func DeleteDeviceServiceByName(name string, ctx context.Context, dic *di.Contain
if err != nil {
return errors.NewCommonEdgeXWrapper(err)
}
// Check the associated Device and ProvisionWatcher existence
devices, edgeXErr := dbClient.DevicesByServiceName(0, 1, name)
if edgeXErr != nil {
return errors.NewCommonEdgeXWrapper(edgeXErr)
}
if len(devices) > 0 {
return errors.NewCommonEdgeX(errors.KindStatusConflict, "fail to delete the device service when associated device exists", nil)
}
provisionWatchers, edgeXErr := dbClient.ProvisionWatchersByServiceName(0, 1, name)
if edgeXErr != nil {
return errors.NewCommonEdgeXWrapper(edgeXErr)
}
if len(provisionWatchers) > 0 {
return errors.NewCommonEdgeX(errors.KindStatusConflict, "fail to delete the device service when associated provisionWatcher exists", nil)
}

err = dbClient.DeleteDeviceServiceByName(name)
if err != nil {
return errors.NewCommonEdgeXWrapper(err)
Expand Down
18 changes: 10 additions & 8 deletions internal/core/metadata/controller/http/deviceprofile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1113,18 +1113,20 @@ func TestDeleteDeviceProfileByName(t *testing.T) {

dic := mockDic()
dbClientMock := &mocks.DBClient{}
dbClientMock.On("DeviceProfileByName", deviceProfile.Name).Return(models.DeviceProfile{}, nil)
dbClientMock.On("DevicesByProfileName", 0, 1, deviceProfile.Name).Return([]models.Device{}, nil)
dbClientMock.On("ProvisionWatchersByProfileName", 0, 1, deviceProfile.Name).Return([]models.ProvisionWatcher{}, nil)
dbClientMock.On("DeleteDeviceProfileByName", deviceProfile.Name).Return(nil)
dbClientMock.On("DevicesByProfileName", 0, 1, notFoundName).Return([]models.Device{}, nil)
dbClientMock.On("ProvisionWatchersByProfileName", 0, 1, notFoundName).Return([]models.ProvisionWatcher{}, nil)
dbClientMock.On("DeleteDeviceProfileByName", notFoundName).Return(errors.NewCommonEdgeX(errors.KindEntityDoesNotExist, "device profile doesn't exist in the database", nil))
dbClientMock.On("DeleteDeviceProfileByName", deviceExists).Return(errors.NewCommonEdgeX(
errors.KindStatusConflict, "fail to delete the device profile when associated device exists", nil))
dbClientMock.On("DeleteDeviceProfileByName", provisionWatcherExists).Return(errors.NewCommonEdgeX(
errors.KindStatusConflict, "fail to delete the device profile when associated provisionWatcher exists", nil))

dbClientMock.On("DeviceProfileByName", notFoundName).Return(models.DeviceProfile{}, errors.NewCommonEdgeX(errors.KindEntityDoesNotExist, "not found", nil))

dbClientMock.On("DeviceProfileByName", deviceExists).Return(models.DeviceProfile{}, nil)
dbClientMock.On("DevicesByProfileName", 0, 1, deviceExists).Return([]models.Device{{ServiceName: testDeviceServiceName}}, nil)

dbClientMock.On("DeviceProfileByName", provisionWatcherExists).Return(models.DeviceProfile{}, nil)
dbClientMock.On("DevicesByProfileName", 0, 1, provisionWatcherExists).Return([]models.Device{}, nil)
dbClientMock.On("ProvisionWatchersByProfileName", 0, 1, provisionWatcherExists).Return([]models.ProvisionWatcher{models.ProvisionWatcher{}}, nil)
dbClientMock.On("DeviceProfileByName", mock.Anything).Return(models.DeviceProfile{}, nil)

dic.Update(di.ServiceConstructorMap{
container.DBClientInterfaceName: func(get di.Get) interface{} {
return dbClientMock
Expand Down
18 changes: 10 additions & 8 deletions internal/core/metadata/controller/http/deviceservice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,18 +489,20 @@ func TestDeleteDeviceServiceByName(t *testing.T) {

dic := mockDic()
dbClientMock := &dbMock.DBClient{}
dbClientMock.On("DeviceServiceByName", deviceService.Name).Return(models.DeviceService{}, nil)
dbClientMock.On("DevicesByServiceName", 0, 1, deviceService.Name).Return([]models.Device{}, nil)
dbClientMock.On("ProvisionWatchersByServiceName", 0, 1, deviceService.Name).Return([]models.ProvisionWatcher{}, nil)
dbClientMock.On("DeleteDeviceServiceByName", deviceService.Name).Return(nil)
dbClientMock.On("DevicesByServiceName", 0, 1, notFoundName).Return([]models.Device{}, nil)
dbClientMock.On("ProvisionWatchersByServiceName", 0, 1, notFoundName).Return([]models.ProvisionWatcher{}, nil)
dbClientMock.On("DeleteDeviceServiceByName", notFoundName).Return(errors.NewCommonEdgeX(errors.KindEntityDoesNotExist, "device service doesn't exist in the database", nil))
dbClientMock.On("DeleteDeviceServiceByName", deviceExists).Return(errors.NewCommonEdgeX(
errors.KindStatusConflict, "fail to delete the device service when associated device exists", nil))
dbClientMock.On("DeleteDeviceServiceByName", provisionWatcherExists).Return(errors.NewCommonEdgeX(
errors.KindStatusConflict, "fail to delete the device service when associated provisionWatcher exists", nil))

dbClientMock.On("DeviceServiceByName", notFoundName).Return(models.DeviceService{}, errors.NewCommonEdgeX(errors.KindEntityDoesNotExist, "device service doesn't exist in the database", nil))

dbClientMock.On("DeviceServiceByName", deviceExists).Return(models.DeviceService{}, nil)
dbClientMock.On("DevicesByServiceName", 0, 1, deviceExists).Return([]models.Device{{ServiceName: testDeviceServiceName}}, nil)

dbClientMock.On("DeviceServiceByName", provisionWatcherExists).Return(models.DeviceService{}, nil)
dbClientMock.On("DevicesByServiceName", 0, 1, provisionWatcherExists).Return([]models.Device{}, nil)
dbClientMock.On("ProvisionWatchersByServiceName", 0, 1, provisionWatcherExists).Return([]models.ProvisionWatcher{models.ProvisionWatcher{}}, nil)
dbClientMock.On("DeviceServiceByName", mock.Anything).Return(models.DeviceService{}, nil)

dic.Update(di.ServiceConstructorMap{
container.DBClientInterfaceName: func(get di.Get) interface{} {
return dbClientMock
Expand Down
Loading