Skip to content

Commit

Permalink
Portal-UI adds serverconfig configuration management of ApolloConfigDB (
Browse files Browse the repository at this point in the history
#4680)

* add tech-support-qq-4.png

* Update README.md

* Enhance the user experience in the scenario of submitting duplicate keys

* Modify the key-value conflict exception prompt, adjust the code style

* feat(apollo-portal): Added serverconfig configuration management for ApolloConfigDB

* doc(CHANGES.md): update CHANGES.md

* Fix(CI): Fix CI check license failures

* feat(apollo-biz): Additional test cases

Co-authored-by: Jason Song <nobodyiam@gmail.com>
  • Loading branch information
klboke and nobodyiam committed Dec 15, 2022
1 parent 7df79bf commit d262471
Show file tree
Hide file tree
Showing 25 changed files with 984 additions and 588 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ Apollo 2.1.0
* [feat: use can change spring.profiles.active's value without rebuild project](https://github.com/apolloconfig/apollo/pull/4616)
* [refactor: remove app.properties and move some config file's location](https://github.com/apolloconfig/apollo/pull/4637)
* [Fix the problem of deleting blank items appear at the end](https://github.com/apolloconfig/apollo/pull/4662)
* [Portal-UI adds serverConfig configuration management of ApolloConfigDB](https://github.com/apolloconfig/apollo/pull/4680)
* [Enable login authentication for eureka](https://github.com/apolloconfig/apollo/pull/4663)


------------------
All issues and pull requests are [here](https://github.com/apolloconfig/apollo/milestone/11?closed=1)
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2022 Apollo Authors
*
* 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.ctrip.framework.apollo.adminservice.controller;

import com.ctrip.framework.apollo.biz.entity.ServerConfig;
import com.ctrip.framework.apollo.biz.service.ServerConfigService;
import java.util.List;
import javax.validation.Valid;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
* @author kl (http://kailing.pub)
* @since 2022/12/13
*/
@RestController
public class ServerConfigController {
private final ServerConfigService serverConfigService;
public ServerConfigController(ServerConfigService serverConfigService) {
this.serverConfigService = serverConfigService;
}
@GetMapping("/server/config/find-all-config")
public List<ServerConfig> findAllServerConfig() {
return serverConfigService.findAll();
}

@PostMapping("/server/config")
public ServerConfig createOrUpdatePortalDBConfig(@Valid @RequestBody ServerConfig serverConfig) {
return serverConfigService.createOrUpdateConfig(serverConfig);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2022 Apollo Authors
*
* 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.ctrip.framework.apollo.adminservice.controller;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import com.ctrip.framework.apollo.biz.entity.ServerConfig;
import org.junit.jupiter.api.Test;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.jdbc.Sql.ExecutionPhase;

/**
* @author kl (http://kailing.pub)
* @since 2022/12/14
*/
class ServerConfigControllerTest extends AbstractControllerTest {

@Test
@Sql(scripts = "/controller/test-server-config.sql", executionPhase = ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/controller/cleanup.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD)
void findAllServerConfig() {
ServerConfig[] serverConfigs = restTemplate.getForObject(url("/server/config/find-all-config"), ServerConfig[].class);
assertNotNull(serverConfigs);
assertEquals(1, serverConfigs.length);
assertEquals("name", serverConfigs[0].getKey());
assertEquals("kl", serverConfigs[0].getValue());
}

@Test
@Sql(scripts = "/controller/test-server-config.sql", executionPhase = ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/controller/cleanup.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD)
void createOrUpdatePortalDBConfig() {
ServerConfig serverConfig = new ServerConfig();
serverConfig.setKey("name");
serverConfig.setValue("ckl");
ServerConfig response = restTemplate.postForObject(url("/server/config"), serverConfig, ServerConfig.class);
assertNotNull(response);

ServerConfig[] serverConfigs = restTemplate.getForObject(url("/server/config/find-all-config"), ServerConfig[].class);
assertNotNull(serverConfigs);
assertEquals(1, serverConfigs.length);
assertEquals("name", serverConfigs[0].getKey());
assertEquals("ckl", serverConfigs[0].getValue());

serverConfig = new ServerConfig();
serverConfig.setKey("age");
serverConfig.setValue("30");
response = restTemplate.postForObject(url("/server/config"), serverConfig, ServerConfig.class);
assertNotNull(response);

serverConfigs = restTemplate.getForObject(url("/server/config/find-all-config"), ServerConfig[].class);
assertNotNull(serverConfigs);
assertEquals(2, serverConfigs.length);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--
-- Copyright 2022 Apollo Authors
--
-- 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.
--
INSERT INTO `ServerConfig` (`Key`, `Cluster`, `Value`)
VALUES
('name', 'default', 'kl');
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@
*/
public interface ServerConfigRepository extends PagingAndSortingRepository<ServerConfig, Long> {
ServerConfig findTopByKeyAndCluster(String key, String cluster);

ServerConfig findByKey(String key);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2022 Apollo Authors
*
* 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.ctrip.framework.apollo.biz.service;

import com.ctrip.framework.apollo.biz.entity.ServerConfig;
import com.ctrip.framework.apollo.biz.repository.ServerConfigRepository;
import com.google.common.collect.Lists;
import java.util.List;
import java.util.Objects;
import javax.transaction.Transactional;
import org.springframework.stereotype.Service;

/**
* @author kl (http://kailing.pub)
* @since 2022/12/13
*/
@Service
public class ServerConfigService {

private final ServerConfigRepository serverConfigRepository;

public ServerConfigService(ServerConfigRepository serverConfigRepository) {
this.serverConfigRepository = serverConfigRepository;
}

public List<ServerConfig> findAll() {
Iterable<ServerConfig> serverConfigs = serverConfigRepository.findAll();
return Lists.newArrayList(serverConfigs);
}

@Transactional
public ServerConfig createOrUpdateConfig(ServerConfig serverConfig) {

ServerConfig storedConfig = serverConfigRepository.findByKey(serverConfig.getKey());

if (Objects.isNull(storedConfig)) {//create
serverConfig.setId(0L);//为空,设置ID 为0,jpa执行新增操作
if(Objects.isNull(serverConfig.getCluster())){
serverConfig.setCluster("default");
}
return serverConfigRepository.save(serverConfig);
}

//update
storedConfig.setComment(serverConfig.getComment());
storedConfig.setDataChangeLastModifiedBy(serverConfig.getDataChangeLastModifiedBy());
storedConfig.setValue(serverConfig.getValue());

return serverConfigRepository.save(storedConfig);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright 2022 Apollo Authors
*
* 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.ctrip.framework.apollo.biz.service;

import com.ctrip.framework.apollo.biz.AbstractIntegrationTest;
import com.ctrip.framework.apollo.biz.entity.ServerConfig;
import java.util.List;

import static org.assertj.core.api.Assertions.*;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

/**
* @author kl (http://kailing.pub)
* @since 2022/12/14
*/
public class ServerConfigServiceTest extends AbstractIntegrationTest {

@Autowired
private ServerConfigService serverConfigService;

@Test
public void findAll() {
List<ServerConfig> serverConfigs = serverConfigService.findAll();
assertThat(serverConfigs).isNotNull();
assertThat(serverConfigs.size()).isGreaterThanOrEqualTo(0);
}

@Test
public void createOrUpdateConfig() {
ServerConfig serverConfig = new ServerConfig();
serverConfig.setKey("name");
serverConfig.setValue("kl");
serverConfigService.createOrUpdateConfig(serverConfig);

List<ServerConfig> serverConfigs = serverConfigService.findAll();
assertThat(serverConfigs).isNotNull();
assertThat(serverConfigs.get(0).getValue()).isEqualTo("kl");
assertThat(serverConfigs.get(0).getCluster()).isEqualTo("default");
assertThat(serverConfigs.get(0).getKey()).isEqualTo("name");


serverConfig.setValue("kl2");
serverConfigService.createOrUpdateConfig(serverConfig);

serverConfigs = serverConfigService.findAll();
assertThat(serverConfigs).isNotNull();
assertThat(serverConfigs.size()).isEqualTo(1);
assertThat(serverConfigs.get(0).getValue()).isEqualTo("kl2");
assertThat(serverConfigs.get(0).getKey()).isEqualTo("name");

serverConfig = new ServerConfig();
serverConfig.setKey("name2");
serverConfig.setValue("kl2");
serverConfigService.createOrUpdateConfig(serverConfig);

serverConfigs = serverConfigService.findAll();
assertThat(serverConfigs).isNotNull();
assertThat(serverConfigs.size()).isEqualTo(2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.ctrip.framework.apollo.common.dto.*;
import com.ctrip.framework.apollo.openapi.dto.OpenItemDTO;
import com.ctrip.framework.apollo.portal.entity.po.ServerConfig;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.google.common.base.Joiner;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -574,5 +575,16 @@ public PageDTO<ReleaseHistoryDTO> findByPreviousReleaseIdAndOperation(Env env, l
}

}
@Service
public static class ServerConfigAPI extends API {
public List<ServerConfig> findAllConfigDBConfig(Env env){
return restTemplate.get(env, "/server/config/find-all-config", new ParameterizedTypeReference<List<ServerConfig>>() {
}).getBody();
}

public ServerConfig createOrUpdateConfigDBConfig(Env env, ServerConfig serverConfig){
return restTemplate.post(env, "/server/config", serverConfig, ServerConfig.class);
}
}

}

0 comments on commit d262471

Please sign in to comment.