Browse Source

fix(iot-hub): the server keeps the package's own entities, the name fallback only without the installed version

pull/16466/head
dpinkevych 1 month ago
parent
commit
f536a08e21
Failed to extract signature
  1. 10
      application/src/main/java/org/thingsboard/server/service/iot_hub/DefaultDevicePackageUpdateService.java
  2. 14
      application/src/test/java/org/thingsboard/server/service/iot_hub/DefaultDevicePackageUpdateServiceTest.java
  3. 2
      ui-ngx/src/app/modules/home/components/iot-hub/device-install-dialog/device-install-dialog.component.ts

10
application/src/main/java/org/thingsboard/server/service/iot_hub/DefaultDevicePackageUpdateService.java

@ -271,6 +271,7 @@ public class DefaultDevicePackageUpdateService extends AbstractTbEntityService i
private Applied applySteps(TenantId tenantId, DevicePackage devicePackage, DeviceUpdateRequest request, JsonNode versionInfo) {
IotHubInstalledItem installedItem = lockInstalledItem(tenantId, devicePackage, request);
DeviceInstalledItemDescriptor previous = devicePackage.descriptor();
Set<UUID> ownedIds = createdIds(previous).keySet();
Map<String, ObjectNode> aliasOutputs = new HashMap<>();
Map<String, ObjectNode> outputs = new LinkedHashMap<>();
Map<String, ObjectNode> installState = new LinkedHashMap<>();
@ -303,7 +304,10 @@ public class DefaultDevicePackageUpdateService extends AbstractTbEntityService i
outputs.put(step.getName(), output);
ObjectNode state = JacksonUtil.newObjectNode();
state.set("entityOutput", output);
state.put("resolution", step.getResolution() != null ? step.getResolution() : "created");
// The package's own entity stays the package's, whatever the request says, so later versions update it
boolean owned = step.getEntityId() != null && ownedIds.contains(step.getEntityId());
String resolution = step.getResolution() == null || (owned && "use-existing".equals(step.getResolution())) ? "created" : step.getResolution();
state.put("resolution", resolution);
// A kept entity keeps the checksum of what the package left: the current one would take the
// user's changes for the package's, and the next update would overwrite them without a warning
String checksum = step.getAction() == DeviceUpdateRequest.Action.KEEP ? previousChecksum(previous, step.getEntityId()) : result.checksum();
@ -311,7 +315,7 @@ public class DefaultDevicePackageUpdateService extends AbstractTbEntityService i
state.put("checksum", checksum);
}
installState.put(step.getId() != null ? step.getId() : step.getName(), state);
if (!"use-existing".equals(step.getResolution())) {
if (!"use-existing".equals(resolution)) {
stepEntityIds.add(result.entityId());
}
if (result.entityId() instanceof DashboardId id) {
@ -320,7 +324,7 @@ public class DefaultDevicePackageUpdateService extends AbstractTbEntityService i
if (result.saved() != null) {
saved.add(result.saved());
}
if (step.getAction() != DeviceUpdateRequest.Action.KEEP && !"use-existing".equals(step.getResolution())) {
if (step.getAction() != DeviceUpdateRequest.Action.KEEP && !"use-existing".equals(resolution)) {
// As at install, the step's attributes go with the entity it saved; written after commit
attributes.add(new StepAttributes(step.getName(), result.entityId(), step.getServerAttributes(), step.getSharedAttributes()));
}

14
application/src/test/java/org/thingsboard/server/service/iot_hub/DefaultDevicePackageUpdateServiceTest.java

@ -256,6 +256,20 @@ class DefaultDevicePackageUpdateServiceTest {
assertThat(descriptor.getInstallState().get("Dashboard").has("checksum")).isFalse();
}
@Test
void useExistingOfAPackageEntity_staysThePackages() {
when(dashboardService.findDashboardById(tenantId, dashboardId)).thenReturn(dashboard(9L));
DeviceUpdateRequest.Step step = keep("Overview", "DASHBOARD", dashboardId);
step.setResolution("use-existing");
DeviceUpdateResult result = service.apply(user, installedItem.getId(), request(step));
assertThat(result.isSuccess()).isTrue();
DeviceInstalledItemDescriptor descriptor = (DeviceInstalledItemDescriptor) installedItem.getDescriptor();
assertThat(descriptor.getInstallState().get("Overview").get("resolution").asText()).isEqualTo("created");
assertThat(descriptor.getCreatedEntityIds()).contains(dashboardId);
}
@Test
void twoStepsWithOneKey_areRejected() {
DeviceUpdateRequest.Step first = keep("Overview", "DASHBOARD", dashboardId);

2
ui-ngx/src/app/modules/home/components/iot-hub/device-install-dialog/device-install-dialog.component.ts

@ -2344,7 +2344,7 @@ export class TbDeviceInstallDialogComponent extends DialogComponent<TbDeviceInst
const installState = this.data.update?.descriptor.installState ?? {};
const previousStep = this.findPreviousStep(step);
const keys = [...(previousStep ? this.stateKeysOf(previousStep) : []), ...this.stateKeysOf(step)];
if (!previousStep && step.id) {
if (!this.previousPackageInfo && step.id) {
keys.push(step.name, this.resolveVariables(step.name));
}
const key = keys.find(k => k && installState[k]);

Loading…
Cancel
Save