Browse Source

Merge pull request #16232 from MazurenkoNick/gw-template-resolve-port-automatically

Resolve gateway MQTT port from connectivity settings in agent app template merge
pull/16247/head
Viacheslav Klimov 2 months ago
committed by GitHub
parent
commit
6efb0fc044
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 7
      application/src/main/java/org/thingsboard/server/service/agent/config/MergeHostValuesRule.java
  2. 13
      application/src/test/java/org/thingsboard/server/service/agent/config/MergeHostValuesRuleTest.java
  3. 2
      common/dao-api/src/main/java/org/thingsboard/server/dao/device/DeviceConnectivityService.java
  4. 12
      dao/src/main/java/org/thingsboard/server/dao/device/DeviceConnectivityServiceImpl.java

7
application/src/main/java/org/thingsboard/server/service/agent/config/MergeHostValuesRule.java

@ -96,11 +96,16 @@ public class MergeHostValuesRule implements AppConfigMergeRule {
log.warn("Skipping gateway host merge: resolveGatewayHost returned blank for baseUrl [{}]", baseUrl); log.warn("Skipping gateway host merge: resolveGatewayHost returned blank for baseUrl [{}]", baseUrl);
return; return;
} }
String port = deviceConnectivityService.resolveGatewayPort();
if (StringUtils.isBlank(port)) {
log.debug("Gateway MQTT port is not configured in connectivity settings, using default [{}]", GATEWAY_DEFAULT_PORT);
port = GATEWAY_DEFAULT_PORT;
}
var imagePattern = AgentApplicationType.GATEWAY.getMainImagePattern(); var imagePattern = AgentApplicationType.GATEWAY.getMainImagePattern();
GatewayEnvSchema schema = GatewayEnvSchema.detect(compose, imagePattern); GatewayEnvSchema schema = GatewayEnvSchema.detect(compose, imagePattern);
Map<String, String> envVars = Map.of( Map<String, String> envVars = Map.of(
schema.getHostKey(), host, schema.getHostKey(), host,
schema.getPortKey(), GATEWAY_DEFAULT_PORT schema.getPortKey(), port
); );
DockerComposeUtils.setEnvVariables(compose, imagePattern, envVars); DockerComposeUtils.setEnvVariables(compose, imagePattern, envVars);
} }

13
application/src/test/java/org/thingsboard/server/service/agent/config/MergeHostValuesRuleTest.java

@ -116,20 +116,31 @@ class MergeHostValuesRuleTest {
@Test @Test
void apply_setsGatewayHostAndPort() { void apply_setsGatewayHostAndPort() {
when(deviceConnectivityService.resolveGatewayHost("http://demo.example.com:8080")).thenReturn("demo.example.com"); when(deviceConnectivityService.resolveGatewayHost("http://demo.example.com:8080")).thenReturn("demo.example.com");
when(deviceConnectivityService.resolveGatewayPort()).thenReturn("8883");
AgentApplication app = gatewayApp(); AgentApplication app = gatewayApp();
rule.apply(app, ctx(true, "http://demo.example.com:8080")); rule.apply(app, ctx(true, "http://demo.example.com:8080"));
assertEquals("demo.example.com", env(app, "TB_GW_HOST")); assertEquals("demo.example.com", env(app, "TB_GW_HOST"));
assertEquals("8883", env(app, "TB_GW_PORT"));
}
@Test
void apply_fallsBackToDefaultGatewayPort_whenPortNotConfigured() {
when(deviceConnectivityService.resolveGatewayHost("http://demo.example.com:8080")).thenReturn("demo.example.com");
when(deviceConnectivityService.resolveGatewayPort()).thenReturn(null);
AgentApplication app = gatewayApp();
rule.apply(app, ctx(true, "http://demo.example.com:8080"));
assertEquals("1883", env(app, "TB_GW_PORT")); assertEquals("1883", env(app, "TB_GW_PORT"));
} }
@Test @Test
void apply_setsLegacyGatewayHostAndPort_whenComposeUsesLegacyEnvNames() { void apply_setsLegacyGatewayHostAndPort_whenComposeUsesLegacyEnvNames() {
when(deviceConnectivityService.resolveGatewayHost("http://demo.example.com:8080")).thenReturn("demo.example.com"); when(deviceConnectivityService.resolveGatewayHost("http://demo.example.com:8080")).thenReturn("demo.example.com");
when(deviceConnectivityService.resolveGatewayPort()).thenReturn("1884");
AgentApplication app = app(AgentApplicationType.GATEWAY, GATEWAY_IMAGE); AgentApplication app = app(AgentApplicationType.GATEWAY, GATEWAY_IMAGE);
addEnv(app, "host", "port", "accessToken"); addEnv(app, "host", "port", "accessToken");
rule.apply(app, ctx(true, "http://demo.example.com:8080")); rule.apply(app, ctx(true, "http://demo.example.com:8080"));
assertEquals("demo.example.com", env(app, "host")); assertEquals("demo.example.com", env(app, "host"));
assertEquals("1883", env(app, "port")); assertEquals("1884", env(app, "port"));
assertFalse(compose(app).get("services").get("main").get("environment").has("TB_GW_HOST")); assertFalse(compose(app).get("services").get("main").get("environment").has("TB_GW_HOST"));
} }

2
common/dao-api/src/main/java/org/thingsboard/server/dao/device/DeviceConnectivityService.java

@ -22,4 +22,6 @@ public interface DeviceConnectivityService {
Resource createGatewayDockerComposeFile(String baseUrl, Device device, DockerComposeParams params) throws URISyntaxException; Resource createGatewayDockerComposeFile(String baseUrl, Device device, DockerComposeParams params) throws URISyntaxException;
String resolveGatewayHost(String baseUrl); String resolveGatewayHost(String baseUrl);
String resolveGatewayPort();
} }

12
dao/src/main/java/org/thingsboard/server/dao/device/DeviceConnectivityServiceImpl.java

@ -164,7 +164,7 @@ public class DeviceConnectivityServiceImpl implements DeviceConnectivityService
return null; return null;
} }
try { try {
String mqttType = isEnabled(MQTTS) ? MQTTS : MQTT; String mqttType = resolveGatewayMqttType();
DeviceConnectivityInfo properties = getConnectivity(mqttType); DeviceConnectivityInfo properties = getConnectivity(mqttType);
String host = DeviceConnectivityUtil.getHost(baseUrl, properties, mqttType); String host = DeviceConnectivityUtil.getHost(baseUrl, properties, mqttType);
return DeviceConnectivityUtil.isLocalhost(host) ? DeviceConnectivityUtil.HOST_DOCKER_INTERNAL : host; return DeviceConnectivityUtil.isLocalhost(host) ? DeviceConnectivityUtil.HOST_DOCKER_INTERNAL : host;
@ -174,6 +174,16 @@ public class DeviceConnectivityServiceImpl implements DeviceConnectivityService
} }
} }
@Override
public String resolveGatewayPort() {
DeviceConnectivityInfo properties = getConnectivity(resolveGatewayMqttType());
return properties == null ? null : StringUtils.trimToNull(getPort(properties));
}
private String resolveGatewayMqttType() {
return isEnabled(MQTTS) ? MQTTS : MQTT;
}
private DeviceConnectivityInfo getConnectivity(String protocol) { private DeviceConnectivityInfo getConnectivity(String protocol) {
AdminSettings connectivitySettings = adminSettingsService.findAdminSettingsByKey(TenantId.SYS_TENANT_ID, "connectivity"); AdminSettings connectivitySettings = adminSettingsService.findAdminSettingsByKey(TenantId.SYS_TENANT_ID, "connectivity");
JsonNode connectivity; JsonNode connectivity;

Loading…
Cancel
Save