Commit f6071f4f by lepdou

Merge branch 'master' of github.com:ctripcorp/apollo

parents 7e1355d1 29f2ad94
...@@ -11,7 +11,9 @@ import com.ctrip.framework.apollo.common.entity.AppNamespace; ...@@ -11,7 +11,9 @@ import com.ctrip.framework.apollo.common.entity.AppNamespace;
import com.ctrip.framework.apollo.biz.service.AppNamespaceService; import com.ctrip.framework.apollo.biz.service.AppNamespaceService;
import com.ctrip.framework.apollo.common.utils.BeanUtils; import com.ctrip.framework.apollo.common.utils.BeanUtils;
import com.ctrip.framework.apollo.core.dto.AppNamespaceDTO; import com.ctrip.framework.apollo.core.dto.AppNamespaceDTO;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import com.ctrip.framework.apollo.core.exception.BadRequestException; import com.ctrip.framework.apollo.core.exception.BadRequestException;
import com.ctrip.framework.apollo.core.utils.StringUtils;
import java.util.List; import java.util.List;
...@@ -31,7 +33,11 @@ public class AppNamespaceController { ...@@ -31,7 +33,11 @@ public class AppNamespaceController {
throw new BadRequestException("app namespaces already exist."); throw new BadRequestException("app namespaces already exist.");
} }
entity = appNamespaceService.createAppNamespace(entity, entity.getDataChangeCreatedBy()); if (StringUtils.isEmpty(appNamespace.getFormat())){
appNamespace.setFormat(ConfigFileFormat.Properties.getValue());
}
entity = appNamespaceService.createAppNamespace(entity);
return BeanUtils.transfrom(AppNamespaceDTO.class, entity); return BeanUtils.transfrom(AppNamespaceDTO.class, entity);
......
...@@ -74,16 +74,18 @@ public class ItemController { ...@@ -74,16 +74,18 @@ public class ItemController {
} }
@PreAcquireNamespaceLock @PreAcquireNamespaceLock
@RequestMapping(path = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items", method = RequestMethod.PUT) @RequestMapping(path = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items/{itemId}", method = RequestMethod.PUT)
public ItemDTO update(@PathVariable("appId") String appId, public ItemDTO update(@PathVariable("appId") String appId,
@PathVariable("clusterName") String clusterName, @PathVariable("clusterName") String clusterName,
@PathVariable("namespaceName") String namespaceName, @RequestBody ItemDTO itemDTO) { @PathVariable("namespaceName") String namespaceName,
@PathVariable("itemId") long itemId,
@RequestBody ItemDTO itemDTO) {
Item entity = BeanUtils.transfrom(Item.class, itemDTO); Item entity = BeanUtils.transfrom(Item.class, itemDTO);
ConfigChangeContentBuilder builder = new ConfigChangeContentBuilder(); ConfigChangeContentBuilder builder = new ConfigChangeContentBuilder();
Item managedEntity = itemService.findOne(appId, clusterName, namespaceName, entity.getKey()); Item managedEntity = itemService.findOne(itemId);
if (managedEntity == null) { if (managedEntity == null) {
throw new BadRequestException("item not exist"); throw new BadRequestException("item not exist");
} }
......
...@@ -71,7 +71,8 @@ public class AppNamespaceService { ...@@ -71,7 +71,8 @@ public class AppNamespaceService {
} }
@Transactional @Transactional
public AppNamespace createAppNamespace(AppNamespace appNamespace, String createBy){ public AppNamespace createAppNamespace(AppNamespace appNamespace){
String createBy = appNamespace.getDataChangeCreatedBy();
if (!isAppNamespaceNameUnique(appNamespace.getAppId(), appNamespace.getName())) { if (!isAppNamespaceNameUnique(appNamespace.getAppId(), appNamespace.getName())) {
throw new ServiceException("appnamespace not unique"); throw new ServiceException("appnamespace not unique");
} }
......
...@@ -101,13 +101,19 @@ public class AdminServiceAPI { ...@@ -101,13 +101,19 @@ public class AdminServiceAPI {
return Arrays.asList(itemDTOs); return Arrays.asList(itemDTOs);
} }
public void updateItems(String appId, Env env, String clusterName, String namespace, public void updateItemsByChangeSet(String appId, Env env, String clusterName, String namespace,
ItemChangeSets changeSets) { ItemChangeSets changeSets) {
restTemplate.postForEntity("{host}/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/itemset", restTemplate.postForEntity("{host}/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/itemset",
changeSets, Void.class, getAdminServiceHost(env), appId, clusterName, namespace); changeSets, Void.class, getAdminServiceHost(env), appId, clusterName, namespace);
} }
public ItemDTO createOrUpdateItem(String appId, Env env, String clusterName, String namespace, ItemDTO item) { public void updateItem(String appId, Env env, String clusterName, String namespace, long itemId, ItemDTO item) {
restTemplate.put("{host}/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items/{itemId}",
item, getAdminServiceHost(env), appId, clusterName, namespace, itemId);
}
public ItemDTO createItem(String appId, Env env, String clusterName, String namespace, ItemDTO item) {
return restTemplate.postForEntity("{host}/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items", return restTemplate.postForEntity("{host}/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items",
item, ItemDTO.class, getAdminServiceHost(env), appId, clusterName, namespace) item, ItemDTO.class, getAdminServiceHost(env), appId, clusterName, namespace)
.getBody(); .getBody();
......
...@@ -61,17 +61,17 @@ public class ConfigController { ...@@ -61,17 +61,17 @@ public class ConfigController {
@RequestBody ItemDTO item){ @RequestBody ItemDTO item){
checkModel(isValidItem(item)); checkModel(isValidItem(item));
return configService.createOrUpdateItem(appId, Env.valueOf(env), clusterName, namespaceName, item); return configService.createItem(appId, Env.valueOf(env), clusterName, namespaceName, item);
} }
@PreAuthorize(value = "@permissionValidator.hasModifyNamespacePermission(#appId, #namespaceName)") @PreAuthorize(value = "@permissionValidator.hasModifyNamespacePermission(#appId, #namespaceName)")
@RequestMapping(value = "/apps/{appId}/envs/{env}/clusters/{clusterName}/namespaces/{namespaceName}/item", method = RequestMethod.PUT) @RequestMapping(value = "/apps/{appId}/envs/{env}/clusters/{clusterName}/namespaces/{namespaceName}/item", method = RequestMethod.PUT)
public ItemDTO updateItem(@PathVariable String appId, @PathVariable String env, public void updateItem(@PathVariable String appId, @PathVariable String env,
@PathVariable String clusterName, @PathVariable String namespaceName, @PathVariable String clusterName, @PathVariable String namespaceName,
@RequestBody ItemDTO item){ @RequestBody ItemDTO item){
checkModel(isValidItem(item)); checkModel(isValidItem(item));
return configService.createOrUpdateItem(appId, Env.valueOf(env), clusterName, namespaceName, item); configService.updateItem(appId, Env.valueOf(env), clusterName, namespaceName, item);
} }
......
...@@ -68,7 +68,9 @@ public class ConfigService { ...@@ -68,7 +68,9 @@ public class ConfigService {
long namespaceId = model.getNamespaceId(); long namespaceId = model.getNamespaceId();
String configText = model.getConfigText(); String configText = model.getConfigText();
ConfigTextResolver resolver = model.getFormat() == ConfigFileFormat.Properties ? propertyResolver : fileTextResolver; ConfigTextResolver resolver =
model.getFormat() == ConfigFileFormat.Properties ? propertyResolver : fileTextResolver;
ItemChangeSets changeSets = resolver.resolve(namespaceId, configText, ItemChangeSets changeSets = resolver.resolve(namespaceId, configText,
itemAPI.findItems(appId, env, clusterName, namespaceName)); itemAPI.findItems(appId, env, clusterName, namespaceName));
if (changeSets.isEmpty()) { if (changeSets.isEmpty()) {
...@@ -76,23 +78,29 @@ public class ConfigService { ...@@ -76,23 +78,29 @@ public class ConfigService {
} }
changeSets.setDataChangeLastModifiedBy(userInfoHolder.getUser().getUserId()); changeSets.setDataChangeLastModifiedBy(userInfoHolder.getUser().getUserId());
itemAPI.updateItems(appId, env, clusterName, namespaceName, changeSets); itemAPI.updateItemsByChangeSet(appId, env, clusterName, namespaceName, changeSets);
} }
public ItemDTO createOrUpdateItem(String appId, Env env, String clusterName, String namespaceName, ItemDTO item) { public ItemDTO createItem(String appId, Env env, String clusterName, String namespaceName, ItemDTO item) {
NamespaceDTO namespace = namespaceAPI.loadNamespace(appId, env, clusterName, namespaceName); NamespaceDTO namespace = namespaceAPI.loadNamespace(appId, env, clusterName, namespaceName);
if (namespace == null) { if (namespace == null) {
throw new BadRequestException( throw new BadRequestException(
"namespace:" + namespaceName + " not exist in env:" + env + ", cluster:" + clusterName); "namespace:" + namespaceName + " not exist in env:" + env + ", cluster:" + clusterName);
} }
item.setNamespaceId(namespace.getId());
String username = userInfoHolder.getUser().getUserId(); String username = userInfoHolder.getUser().getUserId();
if (StringUtils.isEmpty(item.getDataChangeCreatedBy())) { item.setDataChangeCreatedBy(username);
item.setDataChangeCreatedBy(username);
}
item.setDataChangeLastModifiedBy(username); item.setDataChangeLastModifiedBy(username);
item.setNamespaceId(namespace.getId());
return itemAPI.createOrUpdateItem(appId, env, clusterName, namespaceName, item); return itemAPI.createItem(appId, env, clusterName, namespaceName, item);
}
public void updateItem(String appId, Env env, String clusterName, String namespaceName, ItemDTO item) {
String username = userInfoHolder.getUser().getUserId();
item.setDataChangeLastModifiedBy(username);
itemAPI.updateItem(appId, env, clusterName, namespaceName, item.getId(), item);
} }
public void deleteItem(Env env, long itemId) { public void deleteItem(Env env, long itemId) {
...@@ -120,9 +128,9 @@ public class ConfigService { ...@@ -120,9 +128,9 @@ public class ConfigService {
changeSets.setDataChangeLastModifiedBy(userInfoHolder.getUser().getUserId()); changeSets.setDataChangeLastModifiedBy(userInfoHolder.getUser().getUserId());
try { try {
itemAPI itemAPI
.updateItems(namespaceIdentifer.getAppId(), namespaceIdentifer.getEnv(), .updateItemsByChangeSet(namespaceIdentifer.getAppId(), namespaceIdentifer.getEnv(),
namespaceIdentifer.getClusterName(), namespaceIdentifer.getClusterName(),
namespaceIdentifer.getNamespaceName(), changeSets); namespaceIdentifer.getNamespaceName(), changeSets);
} catch (HttpClientErrorException e) { } catch (HttpClientErrorException e) {
logger.error("sync items error. namespace:{}", namespaceIdentifer); logger.error("sync items error. namespace:{}", namespaceIdentifer);
throw new ServiceException(String.format("sync item error. env:%s, clusterName:%s", namespaceIdentifer.getEnv(), throw new ServiceException(String.format("sync item error. env:%s, clusterName:%s", namespaceIdentifer.getEnv(),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment