Skip to content

Commit

Permalink
Merge pull request #770 from hashmapinc/Tempus-729-Tempus-Gateway
Browse files Browse the repository at this point in the history
Tempus-729-Tempus Gateway
  • Loading branch information
shgupta22 committed Oct 12, 2018
2 parents eaca999 + 3715836 commit bc94588
Show file tree
Hide file tree
Showing 51 changed files with 2,181 additions and 42 deletions.
4 changes: 4 additions & 0 deletions application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,10 @@
<artifactId>spring-cloud-starter-contract-stub-runner</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.kubernetes</groupId>
<artifactId>client-java</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
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;
}
}
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 {
}
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 {
}
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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import com.hashmapinc.server.dao.device.DeviceService;
import com.hashmapinc.server.dao.exception.DataValidationException;
import com.hashmapinc.server.dao.exception.IncorrectParameterException;
import com.hashmapinc.server.dao.gatewayconfiguration.TempusGatewayConfigurationService;
import com.hashmapinc.server.dao.metadataingestion.MetadataIngestionService;
import com.hashmapinc.server.dao.model.ModelConstants;
import com.hashmapinc.server.dao.plugin.PluginService;
Expand All @@ -61,6 +62,7 @@
import com.hashmapinc.server.exception.TempusErrorResponseHandler;
import com.hashmapinc.server.exception.TempusException;
import com.hashmapinc.server.service.component.ComponentDiscoveryService;
import com.hashmapinc.server.service.kubernetes.gateway.TempusGatewayKubernetesService;
import com.hashmapinc.server.service.metadataingestion.MetadataConfigService;
import com.hashmapinc.server.service.metadataingestion.MetadataQueryService;
import com.hashmapinc.server.service.security.auth.AttributeBasedPermissionEvaluator;
Expand Down Expand Up @@ -168,6 +170,12 @@ public abstract class BaseController {
@Autowired
protected CustomerGroupService customerGroupService;

@Autowired
protected TempusGatewayConfigurationService tempusGatewayConfigurationService;

@Autowired
protected TempusGatewayKubernetesService tempusGatewayKubernetesService;

@Autowired
protected AttributeBasedPermissionEvaluator evaluator;

Expand Down Expand Up @@ -313,6 +321,22 @@ CustomerGroup checkCustomerGroupId(CustomerGroupId customerGroupId) throws Tempu
}
}

TempusGatewayConfiguration checkTempusGatewayConfigurationId(TempusGatewayConfigurationId tempusGatewayConfigurationId) throws TempusException {
try {
validateId(tempusGatewayConfigurationId, "Incorrect tempusGatewayConfigurationId " + tempusGatewayConfigurationId);
SecurityUser authUser = getCurrentUser();
TempusGatewayConfiguration tempusGatewayConfiguration = tempusGatewayConfigurationService.findTempusGatewayConfigurationById(tempusGatewayConfigurationId);
checkTempusGatewayConfiguration(tempusGatewayConfiguration);
if (!tempusGatewayConfiguration.getTenantId().getId().equals(authUser.getTenantId().getId())) {
throw new TempusException(YOU_DON_T_HAVE_PERMISSION_TO_PERFORM_THIS_OPERATION,
TempusErrorCode.PERMISSION_DENIED);
}
return tempusGatewayConfiguration;
} catch (Exception e) {
throw handleException(e, false);
}
}

Long checkLong(String value, String paramName) {
try {
return Long.parseLong(value);
Expand Down Expand Up @@ -341,6 +365,11 @@ private void checkCustomerGroup(CustomerGroup customerGroup) throws TempusExcept
checkCustomerId(customerGroup.getCustomerId());
}

private void checkTempusGatewayConfiguration(TempusGatewayConfiguration tempusGatewayConfiguration) throws TempusException {
checkNotNull(tempusGatewayConfiguration);
checkTenantId(tempusGatewayConfiguration.getTenantId());
}

User checkUserId(UserId userId) throws TempusException {
try {
validateId(userId, "Incorrect userId " + userId);
Expand Down
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;
}
}
}

0 comments on commit bc94588

Please sign in to comment.