Skip to content

Commit 5821922

Browse files
authored
[issue-87]: Added a new feature to get instance count by namespace. (#103)
* feat:#87 Get instance count * feat:#87 Get instance count Test * feat:#87 update CHANGES.md
1 parent 27fc0a5 commit 5821922

File tree

6 files changed

+175
-0
lines changed

6 files changed

+175
-0
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Apollo Java 2.5.0
77
------------------
88

99
* [Feature Provide a new open APl to return the organization list](https://github.com/apolloconfig/apollo-java/pull/102)
10+
* [Feature Added a new feature to get instance count by namespace.](https://github.com/apolloconfig/apollo-java/pull/103)
1011

1112
------------------
1213
All issues and pull requests are [here](https://github.com/apolloconfig/apollo-java/milestone/5?closed=1)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2024 Apollo Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
package com.ctrip.framework.apollo.openapi.api;
18+
19+
public interface InstanceOpenApiService {
20+
21+
/**
22+
* Return instance count by namespace
23+
* @since 2.5.0
24+
*/
25+
int getInstanceCountByNamespace(String appId, String env, String clusterName, String namespaceName);
26+
}

apollo-openapi/src/main/java/com/ctrip/framework/apollo/openapi/client/ApolloOpenApiClient.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.ctrip.framework.apollo.openapi.client.service.ItemOpenApiService;
2323
import com.ctrip.framework.apollo.openapi.client.service.NamespaceOpenApiService;
2424
import com.ctrip.framework.apollo.openapi.client.service.ReleaseOpenApiService;
25+
import com.ctrip.framework.apollo.openapi.client.service.InstanceOpenApiService;
2526
import com.ctrip.framework.apollo.openapi.client.service.OrganizationOpenApiService;
2627
import com.ctrip.framework.apollo.openapi.dto.*;
2728
import com.google.common.base.Preconditions;
@@ -51,6 +52,7 @@ public class ApolloOpenApiClient {
5152
private final ReleaseOpenApiService releaseService;
5253
private final NamespaceOpenApiService namespaceService;
5354
private final ClusterOpenApiService clusterService;
55+
private final InstanceOpenApiService instanceService;
5456
private static final Gson GSON = new GsonBuilder().setDateFormat(ApolloOpenApiConstants.JSON_DATE_FORMAT).create();
5557

5658
private ApolloOpenApiClient(String portalUrl, String token, RequestConfig requestConfig) {
@@ -66,6 +68,7 @@ private ApolloOpenApiClient(String portalUrl, String token, RequestConfig reques
6668
itemService = new ItemOpenApiService(client, baseUrl, GSON);
6769
releaseService = new ReleaseOpenApiService(client, baseUrl, GSON);
6870
organizationOpenService = new OrganizationOpenApiService(client, baseUrl, GSON);
71+
instanceService = new InstanceOpenApiService(client, baseUrl, GSON);
6972
}
7073

7174
public void createApp(OpenCreateAppDTO req) {
@@ -243,6 +246,14 @@ public void rollbackRelease(String env, long releaseId, String operator) {
243246
releaseService.rollbackRelease(env, releaseId, operator);
244247
}
245248

249+
/**
250+
* Get instance count by namespace
251+
* @since 2.5.0
252+
*/
253+
public int getInstanceCountByNamespace(String appId, String env, String clusterName, String namespaceName) {
254+
return instanceService.getInstanceCountByNamespace(appId, env, clusterName, namespaceName);
255+
}
256+
246257

247258
public String getPortalUrl() {
248259
return portalUrl;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2024 Apollo Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
package com.ctrip.framework.apollo.openapi.client.service;
18+
19+
import com.ctrip.framework.apollo.core.ConfigConsts;
20+
import com.ctrip.framework.apollo.openapi.client.url.OpenApiPathBuilder;
21+
import com.google.common.base.Strings;
22+
import com.google.gson.Gson;
23+
import org.apache.http.client.methods.CloseableHttpResponse;
24+
import org.apache.http.impl.client.CloseableHttpClient;
25+
import org.apache.http.util.EntityUtils;
26+
27+
public class InstanceOpenApiService extends AbstractOpenApiService implements
28+
com.ctrip.framework.apollo.openapi.api.InstanceOpenApiService {
29+
30+
public InstanceOpenApiService(CloseableHttpClient client, String baseUrl, Gson gson) {
31+
super(client, baseUrl, gson);
32+
}
33+
34+
@Override
35+
public int getInstanceCountByNamespace(String appId, String env, String clusterName, String namespaceName) {
36+
if (Strings.isNullOrEmpty(clusterName)) {
37+
clusterName = ConfigConsts.CLUSTER_NAME_DEFAULT;
38+
}
39+
if (Strings.isNullOrEmpty(namespaceName)) {
40+
namespaceName = ConfigConsts.NAMESPACE_APPLICATION;
41+
}
42+
43+
checkNotEmpty(appId, "App id");
44+
checkNotEmpty(env, "Env");
45+
46+
OpenApiPathBuilder pathBuilder = OpenApiPathBuilder.newBuilder()
47+
.envsPathVal(env)
48+
.appsPathVal(appId)
49+
.clustersPathVal(clusterName)
50+
.namespacesPathVal(namespaceName)
51+
.customResource("instances");
52+
53+
try (CloseableHttpResponse response = get(pathBuilder)) {
54+
return gson.fromJson(EntityUtils.toString(response.getEntity()), Integer.class);
55+
} catch (Throwable ex) {
56+
throw new RuntimeException(String.format("Get instance count: appId: %s, cluster: %s, namespace: %s in env: %s failed",
57+
appId, clusterName, namespaceName, env), ex);
58+
}
59+
}
60+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2022 Apollo Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
package com.ctrip.framework.apollo.openapi.client.service;
18+
19+
import org.apache.http.client.methods.HttpGet;
20+
import org.apache.http.entity.StringEntity;
21+
import org.junit.Before;
22+
import org.junit.Test;
23+
import org.mockito.ArgumentCaptor;
24+
25+
import static org.junit.Assert.assertEquals;
26+
import static org.mockito.Mockito.*;
27+
28+
public class InstanceOpenApiServiceTest extends AbstractOpenApiServiceTest {
29+
30+
private InstanceOpenApiService instanceOpenApiService;
31+
32+
private String someAppId;
33+
private String someEnv;
34+
private String someCluster;
35+
private String someNamespace;
36+
37+
@Before
38+
public void setUp() throws Exception {
39+
super.setUp();
40+
someAppId = "someAppId";
41+
someEnv = "someEnv";
42+
someCluster = "someCluster";
43+
someNamespace = "someNamespace";
44+
instanceOpenApiService = new InstanceOpenApiService(httpClient, someBaseUrl, gson);
45+
}
46+
47+
@Test
48+
public void testGetInstanceCountByNamespace() throws Exception {
49+
final ArgumentCaptor<HttpGet> request = ArgumentCaptor.forClass(HttpGet.class);
50+
51+
StringEntity responseEntity = new StringEntity("1");
52+
when(someHttpResponse.getEntity()).thenReturn(responseEntity);
53+
54+
instanceOpenApiService.getInstanceCountByNamespace(someAppId, someEnv, someCluster, someNamespace);
55+
56+
verify(httpClient, times(1)).execute(request.capture());
57+
58+
HttpGet get = request.getValue();
59+
60+
assertEquals(String.format("%s/envs/%s/apps/%s/clusters/%s/namespaces/%s/instances",
61+
someBaseUrl, someEnv, someAppId, someCluster, someNamespace), get.getURI().toString());
62+
}
63+
}

apollo-openapi/src/test/java/com/ctrip/framework/apollo/openapi/client/url/OpenApiPathBuilderTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,20 @@ public void testBuildPath() {
224224
.addParam("operator", operator)
225225
.buildPath(baseURL);
226226
assertEquals(expected, actual);
227+
228+
// InstanceOpenApiService path check
229+
path = String.format("envs/%s/apps/%s/clusters/%s/namespaces/%s/instances",
230+
tools.escapePath(env), tools.escapePath(appId), tools.escapePath(clusterName),
231+
tools.escapePath(namespaceName));
232+
expected = String.format("%s/%s", baseURL, path);
233+
actual = OpenApiPathBuilder.newBuilder()
234+
.envsPathVal(env)
235+
.appsPathVal(appId)
236+
.clustersPathVal(clusterName)
237+
.namespacesPathVal(namespaceName)
238+
.customResource("instances")
239+
.buildPath(baseURL);
240+
assertEquals(expected, actual);
227241
}
228242

229243
@Test(expected = IllegalArgumentException.class)

0 commit comments

Comments
 (0)