Skip to content

Commit

Permalink
Add delete function
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruce.wu committed Dec 16, 2024
1 parent cbf56ff commit 888a167
Show file tree
Hide file tree
Showing 14 changed files with 687 additions and 178 deletions.
123 changes: 123 additions & 0 deletions example/pop.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>弹窗示例</title>
<style>
/* 页面背景半透明遮罩 */
.modal-overlay {
display: none; /* 默认隐藏 */
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5); /* 半透明背景 */
z-index: 10;
}

/* 弹窗容器 */
.modal {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 300px;
padding: 20px;
background-color: white;
border-radius: 10px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
text-align: center;
z-index: 11;
}

/* 关闭按钮 */
.modal-close {
position: absolute;
top: 10px;
right: 10px;
background-color: transparent;
border: none;
font-size: 18px;
font-weight: bold;
cursor: pointer;
color: #555;
}

.modal-close:hover {
color: red;
}

/* 显示弹窗 */
.modal-overlay.active {
display: block;
}

/* 弹窗标题 */
.modal-title {
font-size: 18px;
margin-bottom: 10px;
font-weight: bold;
}

/* 弹窗内容 */
.modal-content {
font-size: 14px;
color: #333;
margin-bottom: 20px;
}

/* 弹窗按钮 */
.modal-button {
padding: 10px 20px;
background-color: #007bff;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}

.modal-button:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<!-- 打开弹窗按钮 -->
<button id="openModal" class="modal-button">打开弹窗</button>

<!-- 弹窗 -->
<div id="modalOverlay" class="modal-overlay">
<div class="modal">
<button class="modal-close" id="closeModal">&times;</button>
<div class="modal-title">弹窗标题</div>
<div class="modal-content">这是弹窗的内容,可以自由定制。</div>
</div>
</div>

<script>
// 获取元素
const openModalButton = document.getElementById('openModal');
const closeModalButton = document.getElementById('closeModal');
const modalOverlay = document.getElementById('modalOverlay');

// 打开弹窗
openModalButton.addEventListener('click', function () {
modalOverlay.classList.add('active');
});

// 关闭弹窗
closeModalButton.addEventListener('click', function () {
modalOverlay.classList.remove('active');
});

// 点击遮罩关闭弹窗
modalOverlay.addEventListener('click', function (e) {
if (e.target === modalOverlay) {
modalOverlay.classList.remove('active');
}
});
</script>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
import io.jenkins.plugins.blueking.model.dto.FileHost;
import io.jenkins.plugins.blueking.model.dto.PageData;
import io.jenkins.plugins.blueking.model.dto.PropertyFilter;
import io.jenkins.plugins.blueking.model.dto.SelectedHost;
import io.jenkins.plugins.blueking.model.req.ListBizHostsRequest;
import io.jenkins.plugins.blueking.model.req.SearchBizInstTopoRequest;
import io.jenkins.plugins.blueking.utils.BluekingException;
import io.jenkins.plugins.blueking.utils.Constants;
import io.jenkins.plugins.blueking.utils.FileHostFilter;
import io.jenkins.plugins.blueking.utils.SelectedHostFilter;
import io.jenkins.plugins.blueking.utils.Tuple;
import io.jenkins.plugins.blueking.utils.Utils;
import java.io.IOException;
Expand Down Expand Up @@ -91,15 +93,17 @@ public class BkHostsChoiceParameterDefinition extends ParameterDefinition {
/**
* 外网IP列表,逗号分隔
*/
private String outerEnv = "OUTER_IPS";
private String outerEnv;
/**
* 内网IP列表,逗号分隔
*/
private String innerEnv = "INNER_IPS";
private String innerEnv;

@DataBoundConstructor
public BkHostsChoiceParameterDefinition(String name) {
public BkHostsChoiceParameterDefinition(String name, String outerEnv, String innerEnv) {
super(Util.fixNull(Util.fixEmpty(name), "DEPLOY_IPS"));
this.outerEnv = Util.fixNull(Util.fixEmpty(outerEnv), "OUTER_IPS");
this.innerEnv = Util.fixNull(Util.fixEmpty(innerEnv), "INNER_IPS");
}

@DataBoundSetter
Expand Down Expand Up @@ -172,8 +176,7 @@ public List<BkInstObj> getBkInstTopo() {
SearchBizInstTopoRequest request = new SearchBizInstTopoRequest();
request.setLevel(-1);
request.setBkBizId(bkBiz.getBkBizId());
List<BkInstObj> topo = client.searchBizInstTopo(request);
return topo;
return client.searchBizInstTopo(request);
}

public String getParams() {
Expand All @@ -196,7 +199,7 @@ public String getParams() {
return obj.toString();
}

private Tuple<Set<String>, Set<String>> getIps(String json) {
private Tuple<Set<String>, Set<String>> getIps(Object json) {
Set<String> innerIps = new LinkedHashSet<>();
Set<String> outerIps = new LinkedHashSet<>();
JSONObject ob = JSONObject.fromObject(json);
Expand All @@ -216,8 +219,8 @@ private Tuple<Set<String>, Set<String>> getIps(String json) {
public ParameterValue createValue(StaplerRequest req, JSONObject jo) {
String name = jo.getString("name");
JSONObject value = jo.getJSONObject("value");
String selectedBkHostsJson = value.getString("selectedBkHosts");
String selectedFileHostsJson = value.getString("selectedFileHosts");
Object selectedBkHostsJson = value.get("selectedBkHosts");
Object selectedFileHostsJson = value.get("selectedFileHosts");

Tuple<Set<String>, Set<String>> bkTuple = getIps(selectedBkHostsJson);
Tuple<Set<String>, Set<String>> fileTuple = getIps(selectedFileHostsJson);
Expand Down Expand Up @@ -304,11 +307,14 @@ public PageData<BkHost> doSearchBkHosts(
listBizHostsRequest.setBkBizId(Integer.parseInt(bizId));

if (Utils.isNotEmpty(keyword)) {
PropertyFilter fieldInnerIp = PropertyFilter.createField("bk_host_innerip", "equal", keyword);
PropertyFilter fieldOuterIp = PropertyFilter.createField("bk_host_outerip", "equal", keyword);
String[] parts = keyword.split("[,\\s]+");
PropertyFilter fieldInnerIp = PropertyFilter.createField("bk_host_innerip", Constants.IN, parts);
PropertyFilter fieldOuterIp = PropertyFilter.createField("bk_host_outerip", Constants.IN, parts);
PropertyFilter fieldHomeName = PropertyFilter.createField("host_name", Constants.IN, parts);
List<PropertyFilter> rules = new ArrayList<>(2);
rules.add(fieldInnerIp);
rules.add(fieldOuterIp);
rules.add(fieldHomeName);
PropertyFilter hostPropertyFilter = PropertyFilter.createCondition(Constants.OR, rules);
listBizHostsRequest.setHostPropertyFilter(hostPropertyFilter);
}
Expand Down Expand Up @@ -389,19 +395,60 @@ public PageData<FileHost> doSearchFileHosts(
int endIdx = Math.min(fileHosts.size(), (pageNum + 1) * limitNum);
pageData.setInfo(fileHosts.subList(pageNum * limitNum, endIdx));
}
pageData.setInfo(fileHosts);
}
Set<String> selectedHostIds = getSelectedHostIds(selectedHosts);
pageData.getInfo().forEach(e -> e.setSelected(selectedHostIds.contains(e.getHostId())));
return pageData;
}

@JavaScriptMethod(name = "getSelectedHosts")
public PageData<SelectedHost> doGetSelectedHosts(
@QueryParameter("selectedHosts") String selectedHosts,
@QueryParameter("keyword") String keyword,
@QueryParameter("page") String page,
@QueryParameter("limit") String limit) {

PageData<SelectedHost> pageData = new PageData<>();

List<SelectedHost> result = new ArrayList<>();
SelectedHostFilter filter = new SelectedHostFilter(keyword);
JSONObject selectedHostsObj = JSONObject.fromObject(selectedHosts);
if (selectedHostsObj.has("data")) {
JSONArray arr = JSONArray.fromObject(selectedHostsObj.get("data"));
for (int i = 0; i < arr.size(); i++) {
String innerIp = arr.getJSONArray(i).getString(0);
String outerIp = arr.getJSONArray(i).getString(1);
String name = arr.getJSONArray(i).getString(2);
String id = arr.getJSONArray(i).getString(3);
SelectedHost e = new SelectedHost(id, innerIp, outerIp, name);
if (filter.test(e)) {
result.add(e);
}
}
}

if (result.isEmpty()) {
pageData.setCount(0);
pageData.setInfo(Collections.emptyList());
} else {
int pageNum = NumberUtil.parseInt(page);
int limitNum = NumberUtil.parseInt(limit);
if (pageNum * limitNum >= result.size()) {
pageData.setInfo(Collections.emptyList());
} else {
int endIdx = Math.min(result.size(), (pageNum + 1) * limitNum);
pageData.setInfo(result.subList(pageNum * limitNum, endIdx));
}
pageData.setCount(result.size());
}
return pageData;
}

private Set<String> getSelectedHostIds(String selectedHosts) {
Set<String> selectedHostIds = new HashSet<>();
JSONObject selectedHostsObj = JSONObject.fromObject(selectedHosts);
if (selectedHostsObj.has("data")) {
String hostsJson = selectedHostsObj.getString("data");
JSONArray arr = JSONArray.fromObject(hostsJson);
JSONArray arr = JSONArray.fromObject(selectedHostsObj.get("data"));
for (int i = 0; i < arr.size(); i++) {
selectedHostIds.add(arr.getJSONArray(i).getString(3));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.jenkins.plugins.blueking.model.dto;

import java.io.Serializable;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Setter
@Getter
@ToString
public class SelectedHost implements Serializable {

private static final long serialVersionUID = 3149159959212400924L;

private boolean selected;

private String id;

private String innerip;

private String outerip;

private String name;

public SelectedHost() {}

public SelectedHost(String id, String innerip, String outerip, String name) {
this.id = id;
this.innerip = innerip;
this.outerip = outerip;
this.name = name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ public interface Constants {

String AND = "AND";
String OR = "OR";

String EQ = "$eq";
String EQUAL = "equal";

String IN = "in";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.jenkins.plugins.blueking.utils;

import io.jenkins.plugins.blueking.model.dto.SelectedHost;
import java.util.function.Predicate;

public class SelectedHostFilter implements Predicate<SelectedHost> {

private final String keyword;

public SelectedHostFilter(String keyword) {
this.keyword = keyword;
}

@Override
public boolean test(SelectedHost e) {
if (Utils.isNullOrEmpty(keyword)) {
return true;
}

if (Utils.isNotEmpty(e.getInnerip()) && e.getInnerip().contains(keyword)) {
return true;
}
if (Utils.isNotEmpty(e.getOuterip()) && e.getOuterip().contains(keyword)) {
return true;
}
return Utils.isNotEmpty(e.getName()) && e.getName().contains(keyword);
}
}
8 changes: 4 additions & 4 deletions src/main/resources/host_template.csv
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
内网IP,外网IP(必填),主机名,模块
INNER IP,OUTER IP,HOSTNAME,MODULE
172.31.0.29,49.70.30.22,HOST-USER-01,user
172.31.0.20,49.70.30.23,HOST-USER-02,user
192.168.10.20,18.30.11.82,HOST-USER-03,user
172.31.0.29,49.70.30.22,HOST-BILL-01,bill
172.31.0.20,49.70.30.23,HOST-BILL-02,bill
192.168.10.20,18.30.11.82,HOST-BILL-03,bill
172.245.233.175,49.70.30.28,HOST-BILL-01,bill
172.245.255.176,49.71.30.66,HOST-BILL-02,bill
192.168.10.21,100.200.112.128,HOST-BILL-03,bill
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<div>
Additional host information file. <br>
Stored in File Credentials, the file must be in CSV format and must follow this template.
Stored in File Credentials, <a
href="https://github.com/dumasd/jenkins-blueking-plugin/blob/main/src/main/resources/host_template.csv">the file
must be in CSV format and must follow this template.</a>
</div>
Loading

0 comments on commit 888a167

Please sign in to comment.