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