##### Coverage Report - com.ctrip.apollo.portal.api.AdminServiceAPI

Classes in this File | Line Coverage | Branch Coverage | Complexity
AdminServiceAPI |
100% | 2/2
N/A | N/A
| 1.0;1
AdminServiceAPI$AppAPI |
25% | 2/8
N/A | N/A
| 1.0;1
AdminServiceAPI$ClusterAPI |
25% | 1/4
N/A | N/A
| 1.0;1
AdminServiceAPI$HealthAPI |
100% | 2/2
N/A | N/A
| 1.0;1
AdminServiceAPI$ItemAPI |
12% | 1/8
N/A | N/A
| 1.0;1
AdminServiceAPI$NamespaceAPI |
5% | 1/17
N/A | N/A
| 1.0;1
AdminServiceAPI$ReleaseAPI |
7% | 1/14
N/A | N/A
| 1.0;1

1 |  |
     package com.ctrip.apollo.portal.api;
2 |  |
3 |  |
4 |  |
     import com.ctrip.apollo.core.dto.AppNamespaceDTO;
5 |  |
     import com.ctrip.apollo.core.enums.Env;
6 |  |
     import com.ctrip.apollo.core.dto.AppDTO;
7 |  |
     import com.ctrip.apollo.core.dto.ClusterDTO;
8 |  |
     import com.ctrip.apollo.core.dto.ItemChangeSets;
9 |  |
     import com.ctrip.apollo.core.dto.ItemDTO;
10 |  |
     import com.ctrip.apollo.core.dto.NamespaceDTO;
11 |  |
     import com.ctrip.apollo.core.dto.ReleaseDTO;
12 |  |
13 |  |
     import org.springframework.boot.actuate.health.Health;
14 |  |
     import org.springframework.http.HttpEntity;
15 |  |
     import org.springframework.http.HttpHeaders;
16 |  |
     import org.springframework.http.MediaType;
17 |  |
     import org.springframework.http.ResponseEntity;
18 |  |
     import org.springframework.stereotype.Service;
19 |  |
     import org.springframework.util.LinkedMultiValueMap;
20 |  |
     import org.springframework.util.MultiValueMap;
21 |  |
22 |  |
     import java.util.Arrays;
23 |  |
     import java.util.List;
24 |  |
25 |  |
26 |  |
     @Service
27 | 1 |
     public class AdminServiceAPI {
28 |  |
29 |  |
       @Service
30 | 1 |
       public static class HealthAPI extends API{
31 |  |
32 |  |
         public Health health(Env env){
33 | 2 |
           return restTemplate.getForObject(getAdminServiceHost(env) + "/health", Health.class);
34 |  |
         }
35 |  |
       }
36 |  |
37 |  |
       @Service
38 | 1 |
       public static class AppAPI extends API {
39 |  |
40 | 1 |
         public static String APP_API = "/apps";
41 |  |
42 |  |
         public List<AppDTO> findApps(Env env) {
43 | 0 |
           AppDTO[] appDTOs =
44 | 0 |
               restTemplate.getForObject(getAdminServiceHost(env) + APP_API, AppDTO[].class);
45 | 0 |
           return Arrays.asList(appDTOs);
46 |  |
         }
47 |  |
48 |  |
         public AppDTO loadApp(Env env, String appId){
49 | 0 |
           return restTemplate.getForObject(getAdminServiceHost(env) + APP_API + "/" + appId, AppDTO.class);
50 |  |
         }
51 |  |
52 |  |
         public AppDTO createApp(Env env, AppDTO app) {
53 | 0 |
           return restTemplate.postForEntity(getAdminServiceHost(env) + APP_API, app, AppDTO.class)
54 | 0 |
               .getBody();
55 |  |
         }
56 |  |
       }
57 |  |
58 |  |
59 |  |
       @Service
60 | 1 |
       public static class NamespaceAPI extends API {
61 |  |
62 |  |
         public List<NamespaceDTO> findNamespaceByCluster(String appId, Env env, String clusterName) {
63 | 0 |
           NamespaceDTO[] namespaceDTOs = restTemplate.getForObject(
64 | 0 |
               getAdminServiceHost(env)
65 | 0 |
                   + String.format("apps/%s/clusters/%s/namespaces", appId, clusterName),
66 |  |
               NamespaceDTO[].class);
67 | 0 |
           return Arrays.asList(namespaceDTOs);
68 |  |
         }
69 |  |
70 |  |
         public NamespaceDTO loadNamespace(String appId, Env env, String clusterName,
71 |  |
             String namespaceName) {
72 | 0 |
           return restTemplate.getForObject(getAdminServiceHost(env)
73 | 0 |
               + String.format("apps/%s/clusters/%s/namespaces/%s", appId, clusterName, namespaceName),
74 |  |
               NamespaceDTO.class);
75 |  |
         }
76 |  |
77 |  |
         public List<AppNamespaceDTO> findPublicAppNamespaces(Env env){
78 | 0 |
           AppNamespaceDTO[] appNamespaceDTOs = restTemplate.getForObject(
79 | 0 |
               getAdminServiceHost(env)+ "appnamespaces/public",
80 |  |
               AppNamespaceDTO[].class);
81 | 0 |
           return Arrays.asList(appNamespaceDTOs);
82 |  |
         }
83 |  |
84 |  |
         public NamespaceDTO createNamespace(Env env, NamespaceDTO namespace) {
85 | 0 |
           return restTemplate.postForEntity(getAdminServiceHost(env) +
86 | 0 |
                                             String.format("/apps/%s/clusters/%s/namespaces", namespace.getAppId(),
87 | 0 |
                                                           namespace.getClusterName()), namespace, NamespaceDTO.class)
88 | 0 |
               .getBody();
89 |  |
         }
90 |  |
91 |  |
         public AppNamespaceDTO createAppNamespace(Env env, AppNamespaceDTO appNamespace) {
92 | 0 |
           return restTemplate.postForEntity(getAdminServiceHost(env) +
93 | 0 |
                                             String.format("/apps/%s/appnamespaces", appNamespace.getAppId()), appNamespace, AppNamespaceDTO.class)
94 | 0 |
               .getBody();
95 |  |
         }
96 |  |
97 |  |
       }
98 |  |
99 |  |
       @Service
100 | 1 |
       public static class ItemAPI extends API {
101 |  |
102 |  |
         public List<ItemDTO> findItems(String appId, Env env, String clusterName, String namespace) {
103 | 0 |
           ItemDTO[] itemDTOs =
104 |  |
               restTemplate
105 | 0 |
                   .getForObject(
106 | 0 |
                       getAdminServiceHost(env) + String.format(
107 |  |
                           "apps/%s/clusters/%s/namespaces/%s/items", appId, clusterName, namespace),
108 |  |
                       ItemDTO[].class);
109 | 0 |
           return Arrays.asList(itemDTOs);
110 |  |
         }
111 |  |
112 |  |
         public void updateItems(String appId, Env env, String clusterName, String namespace,
113 |  |
             ItemChangeSets changeSets) {
114 | 0 |
           restTemplate.postForEntity(getAdminServiceHost(env) + String
115 | 0 |
               .format("apps/%s/clusters/%s/namespaces/%s/itemset", appId, clusterName, namespace),
116 |  |
               changeSets, Void.class);
117 | 0 |
         }
118 |  |
       }
119 |  |
120 |  |
       @Service
121 | 1 |
       public static class ClusterAPI extends API {
122 |  |
123 |  |
         public List<ClusterDTO> findClustersByApp(String appId, Env env) {
124 | 0 |
           ClusterDTO[] clusterDTOs = restTemplate.getForObject(
125 | 0 |
               getAdminServiceHost(env) + String.format("apps/%s/clusters", appId), ClusterDTO[].class);
126 | 0 |
           return Arrays.asList(clusterDTOs);
127 |  |
         }
128 |  |
       }
129 |  |
130 | 1 |
       @Service
131 | 1 |
       public static class ReleaseAPI extends API {
132 |  |
133 |  |
         public ReleaseDTO loadLatestRelease(String appId, Env env, String clusterName,
134 |  |
             String namespace) {
135 | 0 |
           ReleaseDTO releaseDTO = restTemplate.getForObject(
136 | 0 |
               getAdminServiceHost(env) + String.format(
137 |  |
                   "apps/%s/clusters/%s/namespaces/%s/releases/latest", appId, clusterName, namespace),
138 |  |
               ReleaseDTO.class);
139 | 0 |
           return releaseDTO;
140 |  |
         }
141 |  |
142 |  |
         public ReleaseDTO release(String appId, Env env, String clusterName, String namespace,
143 |  |
             String releaseBy, String comment) {
144 | 0 |
           HttpHeaders headers = new HttpHeaders();
145 | 0 |
           headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
146 | 0 |
           MultiValueMap<String, String> parameters = new LinkedMultiValueMap<String, String>();
147 | 0 |
           parameters.add("name", releaseBy);
148 | 0 |
           parameters.add("comment", comment);
149 | 0 |
           HttpEntity<MultiValueMap<String, String>> entity =
150 |  |
               new HttpEntity<MultiValueMap<String, String>>(parameters, headers);
151 | 0 |
           ResponseEntity<ReleaseDTO> response =
152 |  |
               restTemplate
153 | 0 |
                   .postForEntity(
154 | 0 |
                       getAdminServiceHost(env) + String.format(
155 |  |
                           "apps/%s/clusters/%s/namespaces/%s/releases", appId, clusterName, namespace),
156 |  |
                       entity, ReleaseDTO.class);
157 | 0 |
           return response.getBody();
158 |  |
         }
159 |  |
       }
160 |  |
     }