Skip to content

Commit

Permalink
fix: Delete device profile in use should return 409
Browse files Browse the repository at this point in the history
close #4937

Signed-off-by: Ginny Guan <[email protected]>
  • Loading branch information
jinlinGuan committed Oct 7, 2024
1 parent 2495dbd commit c6c279e
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 8 deletions.
16 changes: 12 additions & 4 deletions internal/pkg/infrastructure/postgres/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,13 @@ func (c *Client) DeviceCountByLabels(labels []string) (uint32, errors.EdgeX) {
// DeviceCountByProfileName returns the count of Devices associated with specified profile
func (c *Client) DeviceCountByProfileName(profileName string) (uint32, errors.EdgeX) {
ctx := context.Background()
queryObj := map[string]any{profileNameField: profileName}
return getTotalRowsCount(ctx, c.ConnPool, sqlQueryCountByJSONField(deviceTableName), queryObj)
return deviceCountByProfileName(ctx, c.ConnPool, profileName)
}

// DeviceCountByServiceName returns the count of Devices associated with specified service
func (c *Client) DeviceCountByServiceName(serviceName string) (uint32, errors.EdgeX) {
ctx := context.Background()
queryObj := map[string]any{serviceNameField: serviceName}
return getTotalRowsCount(ctx, c.ConnPool, sqlQueryCountByJSONField(deviceTableName), queryObj)
return deviceCountByServiceName(ctx, c.ConnPool, serviceName)
}

func deviceNameExists(ctx context.Context, connPool *pgxpool.Pool, name string) (bool, errors.EdgeX) {
Expand Down Expand Up @@ -253,3 +251,13 @@ func queryDevices(ctx context.Context, connPool *pgxpool.Pool, sql string, args

return devices, nil
}

func deviceCountByServiceName(ctx context.Context, connPool *pgxpool.Pool, serviceName string) (uint32, errors.EdgeX) {
queryObj := map[string]any{serviceNameField: serviceName}
return getTotalRowsCount(ctx, connPool, sqlQueryCountByJSONField(deviceTableName), queryObj)
}

func deviceCountByProfileName(ctx context.Context, connPool *pgxpool.Pool, profileName string) (uint32, errors.EdgeX) {
queryObj := map[string]any{profileNameField: profileName}
return getTotalRowsCount(ctx, connPool, sqlQueryCountByJSONField(deviceTableName), queryObj)
}
16 changes: 16 additions & 0 deletions internal/pkg/infrastructure/postgres/device_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,22 @@ func (c *Client) DeleteDeviceProfileById(id string) errors.EdgeX {
func (c *Client) DeleteDeviceProfileByName(name string) errors.EdgeX {
ctx := context.Background()

// Check the associated Device and ProvisionWatcher existence
deviceCount, edgeXErr := deviceCountByProfileName(ctx, c.ConnPool, name)
if edgeXErr != nil {
return errors.NewCommonEdgeXWrapper(edgeXErr)
}
if deviceCount > 0 {
return errors.NewCommonEdgeX(errors.KindStatusConflict, "fail to delete the device profile when associated device exists", nil)
}
pwCount, edgeXErr := provisionWatcherCountByProfileName(ctx, c.ConnPool, name)
if edgeXErr != nil {
return errors.NewCommonEdgeXWrapper(edgeXErr)
}
if pwCount > 0 {
return errors.NewCommonEdgeX(errors.KindStatusConflict, "fail to delete the device profile when associated provisionWatcher exists", nil)
}

queryObj := map[string]any{nameField: name}
_, err := c.ConnPool.Exec(ctx, sqlDeleteByJSONField(deviceProfileTableName), queryObj)
if err != nil {
Expand Down
16 changes: 16 additions & 0 deletions internal/pkg/infrastructure/postgres/device_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,22 @@ func (c *Client) DeleteDeviceServiceById(id string) errors.EdgeX {
func (c *Client) DeleteDeviceServiceByName(name string) errors.EdgeX {
ctx := context.Background()

// Check the associated Device and ProvisionWatcher existence
deviceCount, edgeXErr := deviceCountByServiceName(ctx, c.ConnPool, name)
if edgeXErr != nil {
return errors.NewCommonEdgeXWrapper(edgeXErr)
}
if deviceCount > 0 {
return errors.NewCommonEdgeX(errors.KindStatusConflict, "fail to delete the device service when associated device exists", nil)
}
pwCount, edgeXErr := provisionWatcherCountByServiceName(ctx, c.ConnPool, name)
if edgeXErr != nil {
return errors.NewCommonEdgeXWrapper(edgeXErr)
}
if pwCount > 0 {
return errors.NewCommonEdgeX(errors.KindStatusConflict, "fail to delete the device service when associated provisionWatcher exists", nil)
}

queryObj := map[string]any{nameField: name}
_, err := c.ConnPool.Exec(ctx, sqlDeleteByJSONField(deviceServiceTableName), queryObj)
if err != nil {
Expand Down
16 changes: 12 additions & 4 deletions internal/pkg/infrastructure/postgres/provision_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,13 @@ func (c *Client) ProvisionWatcherCountByLabels(labels []string) (uint32, errors.
// ProvisionWatcherCountByServiceName returns the count of Provision Watcher associated with specified service
func (c *Client) ProvisionWatcherCountByServiceName(name string) (uint32, errors.EdgeX) {
ctx := context.Background()
queryObj := map[string]any{serviceNameField: name}
return getTotalRowsCount(ctx, c.ConnPool, sqlQueryCountByJSONField(provisionWatcherTableName), queryObj)
return provisionWatcherCountByServiceName(ctx, c.ConnPool, name)
}

// ProvisionWatcherCountByProfileName returns the count of Provision Watcher associated with specified profile
func (c *Client) ProvisionWatcherCountByProfileName(name string) (uint32, errors.EdgeX) {
ctx := context.Background()
queryObj := map[string]any{"DiscoveredDevice": map[string]any{profileNameField: name}}
return getTotalRowsCount(ctx, c.ConnPool, sqlQueryCountByJSONField(provisionWatcherTableName), queryObj)
return provisionWatcherCountByProfileName(ctx, c.ConnPool, name)
}

func provisionWatcherNameExists(ctx context.Context, connPool *pgxpool.Pool, name string) (bool, errors.EdgeX) {
Expand Down Expand Up @@ -218,3 +216,13 @@ func queryProvisionWatchers(ctx context.Context, connPool *pgxpool.Pool, sql str

return pws, nil
}

func provisionWatcherCountByServiceName(ctx context.Context, connPool *pgxpool.Pool, serviceName string) (uint32, errors.EdgeX) {
queryObj := map[string]any{serviceNameField: serviceName}
return getTotalRowsCount(ctx, connPool, sqlQueryCountByJSONField(provisionWatcherTableName), queryObj)
}

func provisionWatcherCountByProfileName(ctx context.Context, connPool *pgxpool.Pool, profileName string) (uint32, errors.EdgeX) {
queryObj := map[string]any{"DiscoveredDevice": map[string]any{profileNameField: profileName}}
return getTotalRowsCount(ctx, connPool, sqlQueryCountByJSONField(provisionWatcherTableName), queryObj)
}

0 comments on commit c6c279e

Please sign in to comment.