未验证 提交 0d541123 编写于 作者: J Jason Song 提交者: GitHub

Merge pull request #1704 from nobodyiam/open-api-enhancement

Enhance open api capabilities
......@@ -86,6 +86,17 @@ public class ApolloOpenApiClient {
return namespaceService.getNamespaceLock(appId, env, clusterName, namespaceName);
}
/**
* Get config
*
* @return the item or null if not exists
*
* @since 1.2.0
*/
public OpenItemDTO getItem(String appId, String env, String clusterName, String namespaceName, String key) {
return itemService.getItem(appId, env, clusterName, namespaceName, key);
}
/**
* Add config
* @return the created config
......
......@@ -2,8 +2,15 @@ package com.ctrip.framework.apollo.openapi.client.exception;
public class ApolloOpenApiException extends RuntimeException {
private int status;
public ApolloOpenApiException(int status, String reason, String message) {
super(String.format("Request to apollo open api failed, status code: %d, reason: %s, message: %s", status, reason,
message));
this.status = status;
}
public int getStatus() {
return status;
}
}
package com.ctrip.framework.apollo.openapi.client.service;
import com.ctrip.framework.apollo.core.ConfigConsts;
import com.ctrip.framework.apollo.openapi.client.exception.ApolloOpenApiException;
import com.ctrip.framework.apollo.openapi.dto.OpenItemDTO;
import com.google.common.base.Strings;
import com.google.gson.Gson;
......@@ -14,6 +15,34 @@ public class ItemOpenApiService extends AbstractOpenApiService {
super(client, baseUrl, gson);
}
public OpenItemDTO getItem(String appId, String env, String clusterName, String namespaceName, String key) {
if (Strings.isNullOrEmpty(clusterName)) {
clusterName = ConfigConsts.CLUSTER_NAME_DEFAULT;
}
if (Strings.isNullOrEmpty(namespaceName)) {
namespaceName = ConfigConsts.NAMESPACE_APPLICATION;
}
checkNotEmpty(appId, "App id");
checkNotEmpty(env, "Env");
checkNotEmpty(key, "Item key");
String path = String.format("envs/%s/apps/%s/clusters/%s/namespaces/%s/items/%s",
escapePath(env), escapePath(appId), escapePath(clusterName), escapePath(namespaceName), escapePath(key));
try (CloseableHttpResponse response = get(path)) {
return gson.fromJson(EntityUtils.toString(response.getEntity()), OpenItemDTO.class);
} catch (Throwable ex) {
// return null if item doesn't exist
if (ex instanceof ApolloOpenApiException && ((ApolloOpenApiException)ex).getStatus() == 404) {
return null;
}
throw new RuntimeException(String
.format("Get item: %s for appId: %s, cluster: %s, namespace: %s in env: %s failed", key, appId, clusterName,
namespaceName, env), ex);
}
}
public OpenItemDTO createItem(String appId, String env, String clusterName, String namespaceName, OpenItemDTO itemDTO) {
if (Strings.isNullOrEmpty(clusterName)) {
clusterName = ConfigConsts.CLUSTER_NAME_DEFAULT;
......
......@@ -10,6 +10,9 @@ public class OpenAppNamespaceDTO extends BaseDTO {
private boolean isPublic;
// whether to append namespace prefix for public namespace name
private boolean appendNamespacePrefix = true;
private String comment;
public String getName() {
......@@ -44,6 +47,14 @@ public class OpenAppNamespaceDTO extends BaseDTO {
isPublic = aPublic;
}
public boolean isAppendNamespacePrefix() {
return appendNamespacePrefix;
}
public void setAppendNamespacePrefix(boolean appendNamespacePrefix) {
this.appendNamespacePrefix = appendNamespacePrefix;
}
public String getComment() {
return comment;
}
......
package com.ctrip.framework.apollo.openapi.client.service;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import com.ctrip.framework.apollo.openapi.dto.OpenItemDTO;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.ContentType;
......@@ -40,6 +42,32 @@ public class ItemOpenApiServiceTest extends AbstractOpenApiServiceTest {
itemOpenApiService = new ItemOpenApiService(httpClient, someBaseUrl, gson);
}
@Test
public void testGetItem() throws Exception {
String someKey = "someKey";
final ArgumentCaptor<HttpGet> request = ArgumentCaptor.forClass(HttpGet.class);
itemOpenApiService.getItem(someAppId, someEnv, someCluster, someNamespace, someKey);
verify(httpClient, times(1)).execute(request.capture());
HttpGet get = request.getValue();
assertEquals(String
.format("%s/envs/%s/apps/%s/clusters/%s/namespaces/%s/items/%s", someBaseUrl, someEnv,
someAppId, someCluster, someNamespace, someKey), get.getURI().toString());
}
@Test
public void testGetNotExistedItem() throws Exception {
String someKey = "someKey";
when(statusLine.getStatusCode()).thenReturn(404);
assertNull(itemOpenApiService.getItem(someAppId, someEnv, someCluster, someNamespace, someKey));
}
@Test
public void testCreateItem() throws Exception {
String someKey = "someKey";
......
......@@ -33,6 +33,14 @@ public class ItemController {
@Autowired
private UserService userService;
@RequestMapping(value = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items/{key:.+}", method = RequestMethod.GET)
public OpenItemDTO getItem(@PathVariable String appId, @PathVariable String env, @PathVariable String clusterName,
@PathVariable String namespaceName, @PathVariable String key) {
ItemDTO itemDTO = itemService.loadItem(Env.fromString(env), appId, clusterName, namespaceName, key);
return itemDTO == null ? null : OpenApiBeanUtils.transformFromItemDTO(itemDTO);
}
@PreAuthorize(value = "@consumerPermissionValidator.hasModifyNamespacePermission(#request, #appId, #namespaceName, #env)")
@RequestMapping(value = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items", method = RequestMethod.POST)
......
......@@ -77,7 +77,7 @@ public class NamespaceController {
}
AppNamespace appNamespace = OpenApiBeanUtils.transformToAppNamespace(appNamespaceDTO);
AppNamespace createdAppNamespace = appNamespaceService.createAppNamespaceInLocal(appNamespace);
AppNamespace createdAppNamespace = appNamespaceService.createAppNamespaceInLocal(appNamespace, appNamespaceDTO.isAppendNamespacePrefix());
publisher.publishEvent(new AppNamespaceCreationEvent(createdAppNamespace));
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册