Browse Source

fix(iot-hub): a package owns the entities its install state records, except use-existing ones

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

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

@ -204,7 +204,7 @@ public class DefaultDevicePackageUpdateService extends AbstractTbEntityService i
if (request.getSteps() == null || request.getSteps().isEmpty()) {
throw new IllegalArgumentException("The update has no steps");
}
Set<UUID> ownedIds = createdIds(devicePackage.descriptor()).keySet();
Set<UUID> ownedIds = ownedIds(devicePackage.descriptor());
Set<String> stateKeys = new HashSet<>();
for (DeviceUpdateRequest.Step step : request.getSteps()) {
if (step.getName() == null || step.getAction() == null || !isKnownType(step.getType())) {
@ -273,7 +273,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();
Set<UUID> ownedIds = ownedIds(previous);
Map<String, ObjectNode> aliasOutputs = new HashMap<>();
Map<String, ObjectNode> outputs = new LinkedHashMap<>();
Map<String, ObjectNode> installState = new LinkedHashMap<>();
@ -840,6 +840,26 @@ public class DefaultDevicePackageUpdateService extends AbstractTbEntityService i
return type != null && (STEP_ALIASES.containsKey(type) || "GATEWAY_CONNECTOR".equals(type));
}
/**
* The entities the package owns: those its install state records, except the ones the user chose to use as they
* were. Not createdEntityIds: the install adds the use-existing ones to it too.
*/
private static Set<UUID> ownedIds(DeviceInstalledItemDescriptor descriptor) {
Set<UUID> ids = new HashSet<>();
if (descriptor.getInstallState() != null) {
descriptor.getInstallState().values().forEach(state -> {
JsonNode id = state.path("entityOutput").path("id");
if (id.isTextual() && !"use-existing".equals(state.path("resolution").asText())) {
try {
ids.add(UUID.fromString(id.asText()));
} catch (IllegalArgumentException ignored) {
}
}
});
}
return ids;
}
private static Map<UUID, EntityId> createdIds(DeviceInstalledItemDescriptor descriptor) {
Map<UUID, EntityId> ids = new HashMap<>();
if (descriptor.getCreatedEntityIds() != null) {

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

@ -135,7 +135,9 @@ class DefaultDevicePackageUpdateServiceTest {
descriptor.setSelectedInstallMethod("INTEGRATION_TTN");
descriptor.setCreatedEntityIds(List.of(profileId, deviceId, dashboardId, integrationId));
descriptor.setDashboardId(dashboardId);
descriptor.setInstallState(Map.of());
// The package created these entities
descriptor.setInstallState(Map.of("Profile", stateOf(profileId, "created"), "AM308", stateOf(deviceId, "created"),
"Dashboard", stateOf(dashboardId, "created"), "Integration", stateOf(integrationId, "created")));
installedItem = new IotHubInstalledItem();
installedItem.setId(new IotHubInstalledItemId(UUID.randomUUID()));
installedItem.setTenantId(tenantId);
@ -273,6 +275,32 @@ class DefaultDevicePackageUpdateServiceTest {
assertThat(descriptor.getCreatedEntityIds()).contains(dashboardId);
}
@Test
void useExistingAtInstall_staysUseExisting() {
// At install the user picked their own dashboard: the install adds it to createdEntityIds too
((DeviceInstalledItemDescriptor) installedItem.getDescriptor()).setInstallState(Map.of("Dashboard", stateOf(dashboardId, "use-existing")));
when(dashboardService.findDashboardById(tenantId, dashboardId)).thenReturn(dashboard(9L));
DeviceUpdateRequest.Step step = keep("Dashboard", "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("Dashboard").get("resolution").asText()).isEqualTo("use-existing");
}
@Test
void overwritingAnEntityUsedAtInstall_needsAConfirmation() {
((DeviceInstalledItemDescriptor) installedItem.getDescriptor()).setInstallState(Map.of("Dashboard", stateOf(dashboardId, "use-existing")));
DeviceUpdateRequest.Step step = overwrite("Dashboard", "DASHBOARD", dashboardId, 9L, JacksonUtil.newObjectNode().put("title", "Dashboard"));
DeviceUpdateResult result = service.apply(user, installedItem.getId(), request(step));
assertThat(result.isSuccess()).isFalse();
verify(installedItemService, never()).save(any(), any());
}
@Test
void twoStepsWithOneKey_areRejected() {
DeviceUpdateRequest.Step first = keep("Overview", "DASHBOARD", dashboardId);
@ -593,4 +621,11 @@ class DefaultDevicePackageUpdateServiceTest {
return step;
}
private static ObjectNode stateOf(EntityId id, String resolution) {
ObjectNode state = JacksonUtil.newObjectNode().put("resolution", resolution);
state.set("entityOutput", JacksonUtil.newObjectNode().put("id", id.getId().toString()));
return state;
}
}

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

@ -2429,9 +2429,13 @@ export class TbDeviceInstallDialogComponent extends DialogComponent<TbDeviceInst
return new Set(fields.map(field => field.key).filter(key => !previousKeys.has(key)));
}
/** Update: true when the entity is one the installed package created. */
/**
* Update: true when the entity is one the installed package owns: its install state records it, and not as one
* the user chose to use as it was. Not createdEntityIds: the install adds the use-existing ones to it too.
*/
private isPackageEntity(id: string): boolean {
return !!this.data.update?.descriptor.createdEntityIds?.some(created => created.id === id);
return Object.values(this.data.update?.descriptor.installState ?? {})
.some(state => state?.entityOutput?.id === id && state.resolution !== 'use-existing');
}
/** The keys the install state may keep a step under: its id, or else its name, as written or resolved. */

Loading…
Cancel
Save