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+ }
0 commit comments