Skip to content

Commit

Permalink
[Improve] Refactor update setting interface
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzk1 committed Feb 26, 2024
1 parent caf4cd7 commit a13304b
Show file tree
Hide file tree
Showing 6 changed files with 243 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.streampark.console.core.bean;

import org.apache.streampark.console.core.entity.Setting;

import lombok.Data;

@Data
public class SettingAlertEmailConfigParams {
private Setting host;
private Setting port;
private Setting from;
private Setting username;
private Setting password;
private Setting ssl;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.streampark.console.core.bean;

import org.apache.streampark.console.core.entity.Setting;

import lombok.Data;

@Data
public class SettingDockerConfigParams {
private Setting username;
private Setting password;
private Setting address;
private Setting namespace;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import org.apache.streampark.common.util.HadoopUtils;
import org.apache.streampark.console.base.domain.RestResponse;
import org.apache.streampark.console.core.bean.SettingAlertEmailConfigParams;
import org.apache.streampark.console.core.bean.SettingDockerConfigParams;
import org.apache.streampark.console.core.entity.Setting;
import org.apache.streampark.console.core.service.SettingService;

Expand All @@ -31,9 +33,11 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@Tag(name = "SETTING_TAG")
Expand Down Expand Up @@ -77,6 +81,34 @@ public RestResponse update(Setting setting) {
return RestResponse.success(updated);
}

@Operation(summary = "Update docker setting")
@PostMapping("update/docker")
@RequiresPermissions("setting:update")
public RestResponse updateDocker(@RequestBody SettingDockerConfigParams params) {
List<Setting> settings =
Arrays.asList(
params.getAddress(), params.getNamespace(),
params.getUsername(), params.getPassword());
boolean updated = settingService.updateSettings(settings);
return RestResponse.success(updated);
}

@Operation(summary = "Update alert email")
@PostMapping("update/alert/email")
@RequiresPermissions("setting:update")
public RestResponse updateAlertEmail(@RequestBody SettingAlertEmailConfigParams params) {
List<Setting> settings =
Arrays.asList(
params.getHost(),
params.getPort(),
params.getFrom(),
params.getUsername(),
params.getPassword(),
params.getSsl());
boolean updated = settingService.updateSettings(settings);
return RestResponse.success(updated);
}

@Operation(summary = "Check hadoop status")
@PostMapping("checkHadoop")
public RestResponse checkHadoop() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import com.baomidou.mybatisplus.extension.service.IService;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

Expand Down Expand Up @@ -65,6 +66,13 @@ public interface SettingService extends IService<Setting> {
*/
boolean update(Setting setting);

/**
* * Updates the specified Settings.
*
* @param settings list of setting object to update
* @return true if the update is successful, false otherwise
*/
boolean updateSettings(List<Setting> settings);
/**
* Retrieves the Maven configuration settings.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import javax.annotation.PostConstruct;

import java.util.List;
import java.util.Optional;

@Slf4j
Expand Down Expand Up @@ -78,6 +79,16 @@ public boolean update(Setting setting) {
}
}

public boolean updateSettings(List<Setting> settings) {
for (Setting each : settings) {
boolean result = update(each);
if (!result) {
return false;
}
}
return true;
}

@Override
public MavenConfig getMavenConfig() {
return MavenConfig.fromSetting();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.streampark.console.core.service;

import org.apache.streampark.console.SpringUnitTestBase;
import org.apache.streampark.console.core.bean.SettingAlertEmailConfigParams;
import org.apache.streampark.console.core.bean.SettingDockerConfigParams;
import org.apache.streampark.console.core.entity.Setting;

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

import java.util.Arrays;
import java.util.List;

import static org.junit.Assert.assertEquals;

class SettingServiceTest extends SpringUnitTestBase {
private SettingDockerConfigParams dockerConfigParams = new SettingDockerConfigParams();
private SettingAlertEmailConfigParams alertEmailConfigParams =
new SettingAlertEmailConfigParams();

@Autowired SettingService settingService;

/*docker config*/
void initDockerConfigParams(SettingDockerConfigParams params) {
Setting address = settingService.get("docker.register.address");
address.setSettingValue("test-address-setting-value");
Setting username = settingService.get("docker.register.user");
username.setSettingValue("test-username-setting-value");
Setting password = settingService.get("docker.register.password");
password.setSettingValue("test-password-setting-value");
Setting namespace = settingService.get("docker.register.namespace");
namespace.setSettingValue("test-namespace-setting-value");
params.setAddress(address);
params.setUsername(username);
params.setPassword(password);
params.setNamespace(namespace);
}

@Test
void testUpdateDockerConfigTest() {
initDockerConfigParams(dockerConfigParams);
List<Setting> settings =
Arrays.asList(
dockerConfigParams.getAddress(),
dockerConfigParams.getNamespace(),
dockerConfigParams.getUsername(),
dockerConfigParams.getPassword());
settingService.updateSettings(settings);

assertEquals(
"test-address-setting-value",
settingService.get("docker.register.address").getSettingValue());
assertEquals(
"test-username-setting-value",
settingService.get("docker.register.user").getSettingValue());
assertEquals(
"test-password-setting-value",
settingService.get("docker.register.password").getSettingValue());
assertEquals(
"test-namespace-setting-value",
settingService.get("docker.register.namespace").getSettingValue());
}

/*alert email config*/
void initAlertEmailConfigParams(SettingAlertEmailConfigParams params) {
Setting host = settingService.get("alert.email.host");
host.setSettingValue("test-host-setting-value");
Setting port = settingService.get("alert.email.port");
port.setSettingValue("test-port-setting-value");
Setting from = settingService.get("alert.email.from");
from.setSettingValue("test-from-setting-value");
Setting username = settingService.get("alert.email.userName");
username.setSettingValue("test-username-setting-value");
Setting password = settingService.get("alert.email.password");
password.setSettingValue("test-password-setting-value");
Setting ssl = settingService.get("alert.email.ssl");
ssl.setSettingValue("test-ssl-setting-value");
params.setHost(host);
params.setPort(port);
params.setFrom(from);
params.setUsername(username);
params.setPassword(password);
params.setSsl(ssl);
}

@Test
void testUpdateAlertEmailConfigTest() {
initAlertEmailConfigParams(alertEmailConfigParams);
List<Setting> settings =
Arrays.asList(
alertEmailConfigParams.getHost(),
alertEmailConfigParams.getPort(),
alertEmailConfigParams.getFrom(),
alertEmailConfigParams.getUsername(),
alertEmailConfigParams.getPassword(),
alertEmailConfigParams.getSsl());
settingService.updateSettings(settings);

assertEquals(
"test-host-setting-value", settingService.get("alert.email.host").getSettingValue());
assertEquals(
"test-port-setting-value", settingService.get("alert.email.port").getSettingValue());
assertEquals(
"test-from-setting-value", settingService.get("alert.email.from").getSettingValue());
assertEquals(
"test-username-setting-value",
settingService.get("alert.email.userName").getSettingValue());
assertEquals(
"test-password-setting-value",
settingService.get("alert.email.password").getSettingValue());
assertEquals("test-ssl-setting-value", settingService.get("alert.email.ssl").getSettingValue());
}
}

0 comments on commit a13304b

Please sign in to comment.