Skip to content

Support HTTP POST request for update operations in SentinelApiClient of Sentinel dashboard #620

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,32 @@
* @since 0.2.1
*/
public class SentinelVersion {

private int majorVersion;
private int minorVersion;
private int fixVersion;
private String postfix;

public SentinelVersion() {
this(0, 0, 0);
}

public SentinelVersion(int major, int minor, int fix) {
this(major, minor, fix, null);
}

public SentinelVersion(int major, int minor, int fix, String postfix) {
this.majorVersion = major;
this.minorVersion = minor;
this.fixVersion = fix;
this.postfix = postfix;
}

/**
* 000, 000, 000
*/
public int getFullVersion() {
return majorVersion * 1000000 + minorVersion * 1000 + fixVersion;
}

public int getMajorVersion() {
return majorVersion;
Expand Down Expand Up @@ -66,18 +87,14 @@ public boolean greaterThan(SentinelVersion version) {
if (version == null) {
return true;
}
return this.majorVersion > version.majorVersion
|| this.minorVersion > version.minorVersion
|| this.fixVersion > version.fixVersion;
return getFullVersion() > version.getFullVersion();
}

public boolean greaterOrEqual(SentinelVersion version) {
if (version == null) {
return true;
}
return this.majorVersion >= version.majorVersion
|| this.minorVersion >= version.minorVersion
|| this.fixVersion >= version.fixVersion;
return getFullVersion() >= version.getFullVersion();
}

@Override
Expand All @@ -87,9 +104,7 @@ public boolean equals(Object o) {

SentinelVersion that = (SentinelVersion)o;

if (majorVersion != that.majorVersion) { return false; }
if (minorVersion != that.minorVersion) { return false; }
if (fixVersion != that.fixVersion) { return false; }
if (getFullVersion() != that.getFullVersion()) { return false; }
return postfix != null ? postfix.equals(that.postfix) : that.postfix == null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.alibaba.csp.sentinel.dashboard.datasource.entity.rule;

import com.alibaba.csp.sentinel.slots.block.Rule;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule;
import com.alibaba.csp.sentinel.util.AssertUtil;

Expand Down Expand Up @@ -55,4 +56,9 @@ public String getResource() {
public int getStrategy() {
return rule.getStrategy();
}

@Override
public Rule toRule() {
return rule;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ public void setGmtModified(Date gmtModified) {
this.gmtModified = gmtModified;
}

public DegradeRule toDegradeRule() {
@Override
public DegradeRule toRule() {
DegradeRule rule = new DegradeRule();
rule.setResource(resource);
rule.setLimitApp(limitApp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@ public void setGmtModified(Date gmtModified) {
this.gmtModified = gmtModified;
}

public FlowRule toFlowRule() {
@Override
public FlowRule toRule() {
FlowRule flowRule = new FlowRule();
flowRule.setCount(this.count);
flowRule.setGrade(this.grade);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.util.List;

import com.alibaba.csp.sentinel.slots.block.Rule;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowClusterConfig;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowItem;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule;
Expand Down Expand Up @@ -84,4 +85,9 @@ public boolean isClusterMode() {
public ParamFlowClusterConfig getClusterConfig() {
return rule.getClusterConfig();
}

@Override
public Rule toRule() {
return rule;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import java.util.Date;

import com.alibaba.csp.sentinel.slots.block.Rule;

/**
* @author leyou
*/
Expand All @@ -33,4 +35,6 @@ public interface RuleEntity {
Integer getPort();

Date getGmtCreate();

Rule toRule();
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ public void setGmtModified(Date gmtModified) {
this.gmtModified = gmtModified;
}

public SystemRule toSystemRule() {
@Override
public SystemRule toRule() {
SystemRule rule = new SystemRule();
rule.setHighestSystemLoad(avgLoad);
rule.setAvgRt(avgRt);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public final class VersionUtils {
/**
* Parse version of Sentinel from raw string.
*
* @param s version string
* @param versionFull version string
* @return parsed {@link SentinelVersion} if the version is valid; empty if
* there is something wrong with the format
*/
Expand All @@ -41,25 +41,50 @@ public static Optional<SentinelVersion> parseVersion(String s) {
return Optional.empty();
}
try {
String versionFull = s;
SentinelVersion version = new SentinelVersion();
String[] postArr = s.split("-");
if (postArr.length > 1) {
version.setPostfix(postArr[1]);

// postfix
int index = versionFull.indexOf("-");
if (index == 0) {
// Start with "-"
return Optional.empty();
}
String[] arr = postArr[0].split("\\.");
if (arr.length == 2) {
version.setMajorVersion(Integer.valueOf(arr[0]))
.setMinorVersion(Integer.valueOf(arr[1]))
.setFixVersion(0);
} else if (arr.length == 3) {
version.setMajorVersion(Integer.valueOf(arr[0]))
.setMinorVersion(Integer.valueOf(arr[1]))
.setFixVersion(Integer.valueOf(arr[2]));
} else {
if (index == versionFull.length() - 1) {
// End with "-"
} else if (index > 0) {
version.setPostfix(versionFull.substring(index + 1));
}

if (index >= 0) {
versionFull = versionFull.substring(0, index);
}

// x.x.x
int segment = 0;
int[] ver = new int[3];
while (segment < ver.length) {
index = versionFull.indexOf('.');
if (index < 0) {
if (versionFull.length() > 0) {
ver[segment] = Integer.valueOf(versionFull);
}
break;
}
ver[segment] = Integer.valueOf(versionFull.substring(0, index));
versionFull = versionFull.substring(index + 1);
segment ++;
}

if (ver[0] < 1) {
// Wrong format, return empty.
return Optional.empty();
} else {
return Optional.of(version
.setMajorVersion(ver[0])
.setMinorVersion(ver[1])
.setFixVersion(ver[2]));
}
return Optional.of(version);
} catch (Exception ex) {
// Parse fail, return empty.
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.alibaba.csp.sentinel.dashboard.datasource.entity;

import static org.junit.Assert.*;

import org.junit.Test;

public class SentinelVersionTest {
@Test
public void testEqual() {
assertEquals(new SentinelVersion(1, 0, 0), new SentinelVersion(1, 0, 0));
assertNotEquals(new SentinelVersion(1, 0, 0), new SentinelVersion(1, 2, 3));
assertNotEquals(new SentinelVersion(1, 0, 0), new SentinelVersion(1, 0, 0, ""));
assertEquals(new SentinelVersion(1, 0, 0, ""), new SentinelVersion(1, 0, 0, ""));
assertNotEquals(new SentinelVersion(1, 0, 0, ""), new SentinelVersion(1, 0, 0, null));
assertEquals(new SentinelVersion(1, 0, 0, null), new SentinelVersion(1, 0, 0, null));
}

@Test
public void testGreater() {
assertTrue(new SentinelVersion(2, 0, 0).greaterThan(new SentinelVersion(1, 0, 0)));
assertTrue(new SentinelVersion(1, 1, 0).greaterThan(new SentinelVersion(1, 0, 0)));
assertTrue(new SentinelVersion(1, 1, 2).greaterThan(new SentinelVersion(1, 1, 0)));
assertTrue(new SentinelVersion(1, 1, 4).greaterThan(new SentinelVersion(1, 1, 3)));
assertFalse(new SentinelVersion(1, 0, 0).greaterThan(new SentinelVersion(1, 0, 0)));
assertFalse(new SentinelVersion(1, 0, 0).greaterThan(new SentinelVersion(1, 1, 0)));
assertFalse(new SentinelVersion(1, 1, 3).greaterThan(new SentinelVersion(1, 1, 3)));
assertFalse(new SentinelVersion(1, 1, 2).greaterThan(new SentinelVersion(1, 1, 3)));
assertFalse(new SentinelVersion(1, 0, 0, "").greaterThan(new SentinelVersion(1, 0, 0)));
assertTrue(new SentinelVersion(1, 0, 1).greaterThan(new SentinelVersion(1, 0, 0)));
assertTrue(new SentinelVersion(1, 0, 1, "a").greaterThan(new SentinelVersion(1, 0, 0, "b")));
assertFalse(new SentinelVersion(1, 0, 0, "b").greaterThan(new SentinelVersion(1, 0, 0, "a")));
}
}
Loading