Skip to content

Commit

Permalink
Merge pull request #47 from swordqiu/hotfix/qj-endpoint-type-apigateway
Browse files Browse the repository at this point in the history
fix: support apigateway type of endpoint
  • Loading branch information
swordqiu authored Nov 10, 2023
2 parents 8a1f956 + 349cecd commit 5fd68e9
Show file tree
Hide file tree
Showing 34 changed files with 155 additions and 57 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.yunionyun.mcp</groupId>
<artifactId>mcclient</artifactId>
<version>3.2.5</version>
<version>3.2.6</version>
<packaging>jar</packaging>

<name>${project.groupId}:${project.artifactId}</name>
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/yunionyun/mcp/mcclient/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -430,4 +430,8 @@ public String joinUrl(String endpoint, String path) {
}
return String.format("%s%s", endpoint, path);
}

public String getAuthUrl() {
return this.authUrl;
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/yunionyun/mcp/mcclient/EndpointType.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ public class EndpointType extends BaseEnumConstants {
public static final EndpointType InternalURL;
public static final EndpointType PublicURL;
public static final EndpointType AdminURL;
public static final EndpointType ApigatewayURL;

static {
InternalURL = new EndpointType("internalURL");
PublicURL = new EndpointType("PublicURL");
AdminURL = new EndpointType("AdminURL");
ApigatewayURL = new EndpointType("apigateway");
}

private EndpointType(String rep) {
Expand Down
96 changes: 78 additions & 18 deletions src/main/java/com/yunionyun/mcp/mcclient/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,15 @@ public String getHeader(String key) {
return this.headers.get(key);
}

private String getServiceType(String service, String apiVersion) {
private String getApiVersionByServiceType(String service) {
if (service.equals("compute")) {
return "v2";
}
return null;
}

private String getServiceType(String service) {
String apiVersion = this.getApiVersionByServiceType(service);
if (apiVersion != null
&& apiVersion.length() > 0
&& !apiVersion.equals(DEFAULT_API_VERSION)) {
Expand All @@ -58,36 +66,89 @@ private String getServiceType(String service, String apiVersion) {
return service;
}

public String getServiceUrl(String service, String apiVersion, EndpointType endpointType)
public String getServiceUrl(String service, EndpointType endpointType)
throws McClientJavaBizException {
if (this.endpointType != null) {
endpointType = this.endpointType;
String[] candidates = this.getServiceUrls(service, endpointType);
int randidx = (int) (Math.random() * candidates.length);
if (candidates.length != 0) {
return candidates[randidx];
} else {
// 此种情况是服务端目录列表中没有对应的url与配置文件中传入的url进行匹配
throw new McClientJavaBizException("服务端目录列表中没有" + service + "对应的url");
}
// logger.debug("getServiceUrl " + service + " epType " + endpointType);
return this.token.getServiceUrl(
this.getServiceType(service, apiVersion), this.region, this.zone, endpointType);
}

public String[] getServiceUrls(String service, String apiVersion, EndpointType endpointType)
public String[] getApigatewayServiceUrls(String service, String region, String zone, EndpointType endpointType)
throws McClientJavaBizException {
String[] urls = this.getServiceVersionUrls(service, region, zone, EndpointType.InternalURL);
if (urls.length == 0) {
// 此种情况是服务端目录列表中没有对应的url与配置文件中传入的url进行匹配
throw new McClientJavaBizException("服务端目录列表中没有" + service + "对应的url");
}

// replace URLs with authUrl prefix
// find the common prefix
String prefix = this.client.getAuthUrl();
int lastSlashPos = prefix.lastIndexOf("/api/s/identity");
if (lastSlashPos <= 0) {
throw new McClientJavaBizException("invalue auth_url " + this.client.getAuthUrl() + ", should be url of apigateway endpoint, e.g. https://<apigateway-host>/api/s/identity/v3");
}
String hostPart = prefix.substring(0, lastSlashPos);
while (hostPart.endsWith("/")) {
hostPart = hostPart.substring(0, hostPart.length()-1);
}
prefix = hostPart + "/api/s/" + service;
if (region != null && region.length() > 0) {
prefix += "/r/" + region;
if (zone != null && zone.length() > 0) {
prefix += "/z/" + zone;
}
}
String[] rets = new String[urls.length];
for (int i = 0; i < urls.length; i++) {
String url = urls[i];
if (url == null || url.length() < 9) {
continue;
}
int slashPos = url.indexOf("/", 9);
if (slashPos > 0) {
rets[i] = prefix + url.substring(slashPos);
} else {
rets[i] = prefix;
}
}
return rets;
}

public String[] getServiceVersionUrls(String service, String region, String zone, EndpointType endpointType)
throws McClientJavaBizException {
return this.token.getServiceUrls(service, region, zone, endpointType);
}

public String[] getServiceUrls(String service, EndpointType endpointType)
throws McClientJavaBizException {
if (this.endpointType != null) {
endpointType = this.endpointType;
}
// logger.debug("getServiceUrl " + service + " epType " + endpointType);
return this.token.getServiceUrls(
this.getServiceType(service, apiVersion), this.region, this.zone, endpointType);
service = this.getServiceType(service);
if (endpointType.equals(EndpointType.ApigatewayURL)) {
System.out.println("getApigatewayServiceUrls");
return this.getApigatewayServiceUrls(service, this.region, this.zone, endpointType);
} else {
System.out.println("getServiceVersionUrls");
return this.getServiceVersionUrls(service, this.region, this.zone, endpointType);
}
}

public JSONObject jsonRequest(
String service,
String apiVersion,
EndpointType endpointType,
String method,
String url,
HttpHeaders headers,
JSONObject jsonBody)
throws McClientJavaBizException, IOException, JSONClientException {
String baseurl = Utils.stripURLVersion(this.getServiceUrl(service, apiVersion, endpointType));
String baseurl = Utils.stripURLVersion(this.getServiceUrl(service, endpointType));
logger.debug("jsonRequest " + baseurl + " token " + this.token.getToken());
HttpHeaders tmpHdr = new HttpHeaders();
if (headers != null) {
Expand All @@ -97,8 +158,8 @@ public JSONObject jsonRequest(
return this.client.jsonRequest(baseurl, this.token.getToken(), method, url, tmpHdr, jsonBody);
}

public JSONArray jsonRequestArray(String service, String apiVersion, EndpointType endpointType, String method, String url, HttpHeaders headers, JSONObject jsonBody) throws Exception {
String baseurl = Utils.stripURLVersion(this.getServiceUrl(service, apiVersion, endpointType));
public JSONArray jsonRequestArray(String service, EndpointType endpointType, String method, String url, HttpHeaders headers, JSONObject jsonBody) throws Exception {
String baseurl = Utils.stripURLVersion(this.getServiceUrl(service, endpointType));
logger.debug("jsonRequestArray " + baseurl + " token " + this.token.getToken());
HttpHeaders tmpHdr = new HttpHeaders();
if (headers != null) {
Expand Down Expand Up @@ -129,17 +190,16 @@ public void clearTaskNotifyUrl() {
this.removeHeader(TASK_NOTIFY_URL_HEAD);
}

public JSONObject rowRequest(
public JSONObject rawRequest(
String service,
String apiVersion,
EndpointType endpointType,
String method,
String url,
HttpHeaders headers,
InputStream body)
throws NoSuchAlgorithmException, McClientJavaBizException, IOException,
KeyManagementException, JSONClientException {
String baseurl = Utils.stripURLVersion(this.getServiceUrl(service, apiVersion, endpointType));
String baseurl = Utils.stripURLVersion(this.getServiceUrl(service, endpointType));
if (this.headers != null) {
headers.update(this.headers);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public String[] getServiceUrls(String region, String zone, EndpointType endpoint
}
}

public String getServiceUrl(String region, String zone, EndpointType endpointType)
/*public String getServiceUrl(String region, String zone, EndpointType endpointType)
throws McClientJavaBizException {
String[] candidates = this.getServiceUrls(region, zone, endpointType);
int randidx = (int) (Math.random() * candidates.length);
Expand All @@ -82,5 +82,5 @@ public String getServiceUrl(String region, String zone, EndpointType endpointTyp
// 此种情况是服务端目录列表中没有对应的url与配置文件中传入的url进行匹配
throw new McClientJavaBizException("服务端目录列表中没有" + this.type + "对应的url");
}
}
}*/
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public String[] getServiceUrls(
+ Endpoint.RegionID(region, zone));
}

public String getServiceUrl(String service, String region, String zone, EndpointType endpointType)
/*public String getServiceUrl(String service, String region, String zone, EndpointType endpointType)
throws McClientJavaBizException {
for (int i = 0; i < this.services.length; ++i) {
if (this.services[i].getType().equals(service)) {
Expand All @@ -153,7 +153,7 @@ public String getServiceUrl(String service, String region, String zone, Endpoint
+ endpointType
+ " "
+ Endpoint.RegionID(region, zone));
}
}*/

public boolean isAdmin() {
String[] roles = this.getRoles();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public AnsibleManager(
EndpointType endpointType,
String[] columns,
String[] adminColumns) {
super("ansible", null, endpointType, null, columns, adminColumns, keyword, keywordPlural, null);
super("ansible", endpointType, null, columns, adminColumns, keyword, keywordPlural, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ public BaseAutoUpdateManager(
EndpointType endpointType,
String[] columns,
String[] adminColumns) {
super("autoupdate", null, endpointType, null, columns, adminColumns, keyword, keywordPlural, null);
super("autoupdate", endpointType, null, columns, adminColumns, keyword, keywordPlural, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ public BaseCloudIdManager(
EndpointType endpointType,
String[] columns,
String[] adminColumns) {
super("cloudid", null, endpointType, null, columns, adminColumns, keyword, keywordPlural, null);
super("cloudid", endpointType, null, columns, adminColumns, keyword, keywordPlural, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ public BaseCloudeventManager(
EndpointType endpointType,
String[] columns,
String[] adminColumns) {
super("cloudevent", null, endpointType, null, columns, adminColumns, keyword, keywordPlural, null);
super("cloudevent", endpointType, null, columns, adminColumns, keyword, keywordPlural, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ public BaseDevtoolManager(
EndpointType endpointType,
String[] columns,
String[] adminColumns) {
super("devtool", null, endpointType, null, columns, adminColumns, keyword, keywordPlural, null);
super("devtool", endpointType, null, columns, adminColumns, keyword, keywordPlural, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ public BaseITSMManager(
EndpointType endpointType,
String[] columns,
String[] adminColumns) {
super("itsm", null, endpointType, null, columns, adminColumns, keyword, keywordPlural, null);
super("itsm", endpointType, null, columns, adminColumns, keyword, keywordPlural, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ public BaseLogActionManager(
EndpointType endpointType,
String[] columns,
String[] adminColumns) {
super("log", null, endpointType, "v1", columns, adminColumns, keyword, keywordPlural, null);
super("log", endpointType, "v1", columns, adminColumns, keyword, keywordPlural, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,15 @@ public class BaseManager {
String[] colums;
String[] adminColums;
String[] allColumns;
String apiVersion;

public BaseManager(
String serviceType,
String apiVersion,
EndpointType endpointType,
String version,
String[] columns,
String[] adminColumns) {
this.serviceType = serviceType;
this.endpointType = endpointType;
this.apiVersion = apiVersion;
this.version = version;
this.colums = columns;
this.adminColums = adminColumns;
Expand Down Expand Up @@ -73,7 +70,7 @@ public String getUrl(String url) {
protected JSONObject jsonRequest(
Session s, String method, String url, HttpHeaders headers, JSONObject body)
throws McClientJavaBizException, IOException, JSONClientException {
return s.jsonRequest(this.serviceType, this.apiVersion, this.endpointType, method, this.getUrl(url), headers, body);
return s.jsonRequest(this.serviceType, this.endpointType, method, this.getUrl(url), headers, body);
}

public ListResult _list(Session s, String url, String respKey)
Expand All @@ -85,7 +82,7 @@ public ListResult _list(Session s, String url, String respKey)
}

public JSONArray _listRaw(Session s, String url) throws Exception {
return s.jsonRequestArray(this.serviceType, this.apiVersion, this.endpointType, "GET", url, (HttpHeaders) null, (JSONObject) null);
return s.jsonRequestArray(this.serviceType, this.endpointType, "GET", url, (HttpHeaders) null, (JSONObject) null);
}

public JSONObject _get(Session s, String url, String respKey)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ public BaseMeterAlertManager(
EndpointType endpointType,
String[] columns,
String[] adminColumns) {
super("meteralert", null, endpointType, null, columns, adminColumns, keyword, keywordPlural, null);
super("meteralert", endpointType, null, columns, adminColumns, keyword, keywordPlural, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ public BaseNotifyManager(
EndpointType endpointType,
String[] columns,
String[] adminColumns) {
super("notify", null, endpointType, "v2", columns, adminColumns, keyword, keywordPlural, null);
super("notify", endpointType, "v2", columns, adminColumns, keyword, keywordPlural, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ public BaseSchedulerManager(
EndpointType endpointType,
String[] columns,
String[] adminColumns) {
super("scheduler", null, endpointType, null, columns, adminColumns, keyword, keywordPlural, null);
super("scheduler", endpointType, null, columns, adminColumns, keyword, keywordPlural, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ public BaseVNCProxyManager(
EndpointType endpointType,
String[] columns,
String[] adminColumns) {
super("vncproxy", null, endpointType, null, columns, adminColumns, keyword, keywordPlural, null);
super("vncproxy", endpointType, null, columns, adminColumns, keyword, keywordPlural, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ public BaseWebConsoleManager(
EndpointType endpointType,
String[] columns,
String[] adminColumns) {
super("webconsole", null, endpointType, null, columns, adminColumns, keyword, keywordPlural, null);
super("webconsole", endpointType, null, columns, adminColumns, keyword, keywordPlural, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ public BaseWebsocketManager(
EndpointType endpointType,
String[] columns,
String[] adminColumns) {
super("websocket", null, endpointType, "v1", columns, adminColumns, keyword, keywordPlural, null);
super("websocket", endpointType, "v1", columns, adminColumns, keyword, keywordPlural, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ public BastionhostManager(
EndpointType endpointType,
String[] columns,
String[] adminColumns) {
super("bastionhost", null, endpointType, null, columns, adminColumns, keyword, keywordPlural, null);
super("bastionhost", endpointType, null, columns, adminColumns, keyword, keywordPlural, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public CloudIdJointManager(
BaseCloudIdManager slave) {
super(
"cloudid",
null,
endpointType,
null,
columns,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public ComputeJointManager(
ComputeManager slave) {
super(
"compute",
"v2",
endpointType,
null,
columns,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ public ComputeManager(
EndpointType endpointType,
String[] columns,
String[] adminColumns) {
super("compute", "v2", endpointType, null, columns, adminColumns, keyword, keywordPlural, null);
super("compute", endpointType, null, columns, adminColumns, keyword, keywordPlural, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ public ConfigManager(
EndpointType endpointType,
String[] columns,
String[] adminColumns) {
super("yunionconf", null, endpointType, null, columns, adminColumns, keyword, keywordPlural, null);
super("yunionconf", endpointType, null, columns, adminColumns, keyword, keywordPlural, null);
}
}
Loading

0 comments on commit 5fd68e9

Please sign in to comment.