-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #770 from hashmapinc/Tempus-729-Tempus-Gateway
Tempus-729-Tempus Gateway
- Loading branch information
Showing
51 changed files
with
2,181 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
...t/java/com/hashmapinc/server/controller/BaseTempusGatewayConfigurationControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/** | ||
* Copyright © 2016-2018 The Thingsboard Authors | ||
* Modifications © 2017-2018 Hashmap, Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.hashmapinc.server.controller; | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.hashmapinc.server.common.data.TempusGatewayConfiguration; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.util.Optional; | ||
|
||
public class BaseTempusGatewayConfigurationControllerTest extends AbstractControllerTest { | ||
@Before | ||
public void beforeTest() throws Exception { | ||
loginTenantAdmin(); | ||
} | ||
|
||
@Test | ||
public void testSaveAndUpdateTempusGatewayConfiguration() throws Exception { | ||
final TempusGatewayConfiguration saveTempusGatewayConfiguration = createTempusGatewayConfiguration(); | ||
|
||
saveTempusGatewayConfiguration.setGatewayToken("gateway-config-token-new"); | ||
final TempusGatewayConfiguration updatedTempusGatewayConfiguration = | ||
doPost("/api/configuration/tempusGateway", saveTempusGatewayConfiguration, TempusGatewayConfiguration.class); | ||
Assert.assertNotNull(updatedTempusGatewayConfiguration); | ||
Assert.assertEquals(saveTempusGatewayConfiguration.getGatewayToken(), updatedTempusGatewayConfiguration.getGatewayToken()); | ||
} | ||
|
||
@Test | ||
public void testFindTempusGatewayConfigurationByIdAndByTenantId() throws Exception { | ||
final TempusGatewayConfiguration saveTempusGatewayConfiguration = createTempusGatewayConfiguration(); | ||
final TempusGatewayConfiguration tempusGatewayConfigurationById = | ||
doGet("/api/configuration/tempusGateway/"+saveTempusGatewayConfiguration.getId().getId().toString(), TempusGatewayConfiguration.class); | ||
Assert.assertNotNull(tempusGatewayConfigurationById); | ||
Assert.assertEquals(saveTempusGatewayConfiguration.getId(), tempusGatewayConfigurationById.getId()); | ||
final TempusGatewayConfiguration tempusGatewayConfigurationByTenantId = | ||
doGet("/api/configuration/tempusGateway", TempusGatewayConfiguration.class); | ||
Assert.assertNotNull(tempusGatewayConfigurationByTenantId); | ||
Assert.assertEquals(saveTempusGatewayConfiguration.getId(), tempusGatewayConfigurationByTenantId.getId()); | ||
|
||
} | ||
|
||
private TempusGatewayConfiguration createTempusGatewayConfiguration() throws Exception { | ||
TempusGatewayConfiguration tempusGatewayConfiguration = new TempusGatewayConfiguration(); | ||
tempusGatewayConfiguration.setReplicas(1); | ||
tempusGatewayConfiguration.setGatewayToken("token"); | ||
tempusGatewayConfiguration.setTenantId(tenantId); | ||
TempusGatewayConfiguration saveTempusGatewayConfiguration = | ||
doPost("/api/configuration/tempusGateway", tempusGatewayConfiguration, TempusGatewayConfiguration.class); | ||
|
||
Assert.assertNotNull(saveTempusGatewayConfiguration); | ||
Assert.assertNotNull(saveTempusGatewayConfiguration.getId()); | ||
Assert.assertTrue(saveTempusGatewayConfiguration.getCreatedTime() > 0); | ||
Assert.assertEquals(tempusGatewayConfiguration.getTenantId(), saveTempusGatewayConfiguration.getTenantId()); | ||
Assert.assertEquals(tempusGatewayConfiguration.getGatewayToken(), saveTempusGatewayConfiguration.getGatewayToken()); | ||
return saveTempusGatewayConfiguration; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...com/hashmapinc/server/controller/nosql/TempusGatewayConfigurationControllerNoSqlTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* Copyright © 2016-2018 The Thingsboard Authors | ||
* Modifications © 2017-2018 Hashmap, Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.hashmapinc.server.controller.nosql; | ||
|
||
import com.hashmapinc.server.controller.BaseTempusGatewayConfigurationControllerTest; | ||
import com.hashmapinc.server.dao.service.DaoNoSqlTest; | ||
|
||
@DaoNoSqlTest | ||
public class TempusGatewayConfigurationControllerNoSqlTest extends BaseTempusGatewayConfigurationControllerTest { | ||
} |
24 changes: 24 additions & 0 deletions
24
...ava/com/hashmapinc/server/controller/sql/TempusGatewayConfigurationControllerSqlTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* Copyright © 2016-2018 The Thingsboard Authors | ||
* Modifications © 2017-2018 Hashmap, Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.hashmapinc.server.controller.sql; | ||
|
||
import com.hashmapinc.server.controller.BaseTempusGatewayConfigurationControllerTest; | ||
import com.hashmapinc.server.dao.service.DaoSqlTest; | ||
|
||
@DaoSqlTest | ||
public class TempusGatewayConfigurationControllerSqlTest extends BaseTempusGatewayConfigurationControllerTest { | ||
} |
71 changes: 71 additions & 0 deletions
71
application/src/main/java/com/hashmapinc/server/config/TempusGatewayProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** | ||
* Copyright © 2016-2018 The Thingsboard Authors | ||
* Modifications © 2017-2018 Hashmap, Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.hashmapinc.server.config; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import java.util.List; | ||
|
||
@Configuration | ||
@EnableConfigurationProperties(TempusGatewayProperties.class) | ||
@ConfigurationProperties(prefix = "gateway") | ||
public class TempusGatewayProperties { | ||
|
||
private String image; | ||
private String host; | ||
private int port; | ||
private List<String> commands; | ||
|
||
public TempusGatewayProperties() { | ||
super(); | ||
} | ||
|
||
public String getImage() { | ||
return image; | ||
} | ||
|
||
public void setImage(String image) { | ||
this.image = image; | ||
} | ||
|
||
public String getHost() { | ||
return host; | ||
} | ||
|
||
public void setHost(String host) { | ||
this.host = host; | ||
} | ||
|
||
public int getPort() { | ||
return port; | ||
} | ||
|
||
public void setPort(int port) { | ||
this.port = port; | ||
} | ||
|
||
public List<String> getCommands() { | ||
return commands; | ||
} | ||
|
||
public void setCommands(List<String> commands) { | ||
this.commands = commands; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
.../src/main/java/com/hashmapinc/server/controller/TempusGatewayConfigurationController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/** | ||
* Copyright © 2016-2018 The Thingsboard Authors | ||
* Modifications © 2017-2018 Hashmap, Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.hashmapinc.server.controller; | ||
|
||
import com.hashmapinc.server.common.data.TempusGatewayConfiguration; | ||
import com.hashmapinc.server.common.data.id.TempusGatewayConfigurationId; | ||
import com.hashmapinc.server.common.data.id.TenantId; | ||
import com.hashmapinc.server.common.data.kubernetes.ReplicaSetStatus; | ||
import com.hashmapinc.server.exception.TempusException; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.Optional; | ||
|
||
@RestController | ||
@RequestMapping("/api/configuration/") | ||
@Slf4j | ||
public class TempusGatewayConfigurationController extends BaseController { | ||
public static final String TEMPUS_GATEWAY_CONFIGURATION_ID = "tempusGatewayConfigurationId"; | ||
|
||
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')") | ||
@GetMapping(value = "tempusGateway/{tempusGatewayConfigurationId}") | ||
@ResponseBody | ||
public TempusGatewayConfiguration getTempusGatewayConfigurationById(@PathVariable(TEMPUS_GATEWAY_CONFIGURATION_ID) String strTempusGatewayConfigurationId) | ||
throws TempusException { | ||
checkParameter(TEMPUS_GATEWAY_CONFIGURATION_ID, strTempusGatewayConfigurationId); | ||
try { | ||
TempusGatewayConfigurationId tempusGatewayConfigurationId = new TempusGatewayConfigurationId(toUUID(strTempusGatewayConfigurationId)); | ||
return checkTempusGatewayConfigurationId(tempusGatewayConfigurationId); | ||
} catch (Exception e) { | ||
throw handleException(e); | ||
} | ||
} | ||
|
||
@PreAuthorize("hasAuthority('TENANT_ADMIN')") | ||
@PostMapping(value = "/tempusGateway") | ||
@ResponseBody | ||
public TempusGatewayConfiguration saveTempusGatewayConfiguration(@RequestBody TempusGatewayConfiguration tempusGatewayConfiguration) throws TempusException { | ||
try { | ||
tempusGatewayConfiguration.setTenantId(getCurrentUser().getTenantId()); | ||
TempusGatewayConfiguration savedTempusGatewayConfiguration = | ||
checkNotNull(tempusGatewayConfigurationService.saveTempusGatewayConfiguration(tempusGatewayConfiguration)); | ||
|
||
log.debug("savedTempusGatewayConfiguration : [{}]", savedTempusGatewayConfiguration); | ||
return savedTempusGatewayConfiguration; | ||
} catch (Exception e) { | ||
throw handleException(e); | ||
} | ||
} | ||
|
||
@PreAuthorize("hasAuthority('TENANT_ADMIN')") | ||
@GetMapping(value = "/tempusGateway") | ||
@ResponseBody | ||
public TempusGatewayConfiguration getTempusGatewayConfiguration() throws TempusException { | ||
try { | ||
TenantId tenantId = getCurrentUser().getTenantId(); | ||
return tempusGatewayConfigurationService.findTempusGatewayConfigurationByTenantId(tenantId).orElse(new TempusGatewayConfiguration()); | ||
} catch (Exception e) { | ||
throw handleException(e); | ||
} | ||
} | ||
|
||
@PreAuthorize("hasAuthority('TENANT_ADMIN')") | ||
@GetMapping(value = "/tempusGateway/deploy") | ||
@ResponseBody | ||
public Boolean deployTempusGateway() throws TempusException { | ||
try { | ||
TenantId tenantId = getCurrentUser().getTenantId(); | ||
final Optional<TempusGatewayConfiguration> tempusGatewayConfigurationByTenantId = | ||
tempusGatewayConfigurationService.findTempusGatewayConfigurationByTenantId(tenantId); | ||
|
||
if(!tempusGatewayConfigurationByTenantId.isPresent()){ | ||
return false; | ||
} | ||
tempusGatewayConfigurationByTenantId.ifPresent(tempusGatewayConfiguration -> | ||
tempusGatewayKubernetesService.deployTempusGateway(tempusGatewayConfiguration)); | ||
return true; | ||
} catch (Exception e) { | ||
log.debug("Exception [{}]", e); | ||
return false; | ||
} | ||
} | ||
|
||
@PreAuthorize("hasAuthority('TENANT_ADMIN')") | ||
@GetMapping(value = "/tempusGateway/status") | ||
@ResponseBody | ||
public ReplicaSetStatus getTempusGatewayPodsStatus() throws TempusException { | ||
try { | ||
TenantId tenantId = getCurrentUser().getTenantId(); | ||
final Optional<TempusGatewayConfiguration> tempusGatewayConfiguration = | ||
tempusGatewayConfigurationService.findTempusGatewayConfigurationByTenantId(tenantId); | ||
|
||
if(!tempusGatewayConfiguration.isPresent()){ | ||
return null; | ||
} | ||
return tempusGatewayKubernetesService.getTempusGatewayReplicaSetStatus(tenantId); | ||
|
||
} catch (Exception e) { | ||
log.debug("Exception [{}]", e); | ||
return null; | ||
} | ||
} | ||
} |
Oops, something went wrong.