use of com.ctrip.framework.apollo.common.dto.NamespaceDTO in project apollo by ctripcorp.
the class NamespaceService method deleteNamespace.
@Transactional
public void deleteNamespace(String appId, Env env, String clusterName, String namespaceName) {
AppNamespace appNamespace = appNamespaceService.findByAppIdAndName(appId, namespaceName);
// 1. check parent namespace has not instances
if (namespaceHasInstances(appId, env, clusterName, namespaceName)) {
throw new BadRequestException("Can not delete namespace because namespace has active instances");
}
// 2. check child namespace has not instances
NamespaceDTO childNamespace = branchService.findBranchBaseInfo(appId, env, clusterName, namespaceName);
if (childNamespace != null && namespaceHasInstances(appId, env, childNamespace.getClusterName(), namespaceName)) {
throw new BadRequestException("Can not delete namespace because namespace's branch has active instances");
}
// 3. check public namespace has not associated namespace
if (appNamespace != null && appNamespace.isPublic() && publicAppNamespaceHasAssociatedNamespace(namespaceName, env)) {
throw new BadRequestException("Can not delete public namespace which has associated namespaces");
}
String operator = userInfoHolder.getUser().getUserId();
namespaceAPI.deleteNamespace(env, appId, clusterName, namespaceName, operator);
}
use of com.ctrip.framework.apollo.common.dto.NamespaceDTO in project apollo by ctripcorp.
the class NamespaceService method fillAppNamespaceProperties.
private void fillAppNamespaceProperties(NamespaceBO namespace) {
final NamespaceDTO namespaceDTO = namespace.getBaseInfo();
final String appId = namespaceDTO.getAppId();
final String clusterName = namespaceDTO.getClusterName();
final String namespaceName = namespaceDTO.getNamespaceName();
// 先从当前appId下面找,包含私有的和公共的
AppNamespace appNamespace = appNamespaceService.findByAppIdAndName(appId, namespaceName);
// 再从公共的app namespace里面找
if (appNamespace == null) {
appNamespace = appNamespaceService.findPublicAppNamespace(namespaceName);
}
final String format;
final boolean isPublic;
if (appNamespace == null) {
// dirty data
LOGGER.warn("Dirty data, cannot find appNamespace by namespaceName [{}], appId = {}, cluster = {}, set it format to {}, make public", namespaceName, appId, clusterName, ConfigFileFormat.Properties.getValue());
format = ConfigFileFormat.Properties.getValue();
// set to true, because public namespace allowed to delete by user
isPublic = true;
} else {
format = appNamespace.getFormat();
isPublic = appNamespace.isPublic();
namespace.setParentAppId(appNamespace.getAppId());
namespace.setComment(appNamespace.getComment());
}
namespace.setFormat(format);
namespace.setPublic(isPublic);
}
use of com.ctrip.framework.apollo.common.dto.NamespaceDTO in project apollo by ctripcorp.
the class ItemService method revokeItem.
public void revokeItem(String appId, Env env, String clusterName, String namespaceName) {
NamespaceDTO namespace = namespaceAPI.loadNamespace(appId, env, clusterName, namespaceName);
if (namespace == null) {
throw new BadRequestException("namespace:" + namespaceName + " not exist in env:" + env + ", cluster:" + clusterName);
}
long namespaceId = namespace.getId();
Map<String, String> releaseItemDTOs = new HashMap<>();
ReleaseDTO latestRelease = releaseAPI.loadLatestRelease(appId, env, clusterName, namespaceName);
if (latestRelease != null) {
releaseItemDTOs = GSON.fromJson(latestRelease.getConfigurations(), GsonType.CONFIG);
}
List<ItemDTO> baseItems = itemAPI.findItems(appId, env, clusterName, namespaceName);
Map<String, ItemDTO> oldKeyMapItem = BeanUtils.mapByKey("key", baseItems);
Map<String, ItemDTO> deletedItemDTOs = new HashMap<>();
// deleted items for comment
findDeletedItems(appId, env, clusterName, namespaceName).forEach(item -> {
deletedItemDTOs.put(item.getKey(), item);
});
ItemChangeSets changeSets = new ItemChangeSets();
AtomicInteger lineNum = new AtomicInteger(1);
releaseItemDTOs.forEach((key, value) -> {
ItemDTO oldItem = oldKeyMapItem.get(key);
if (oldItem == null) {
ItemDTO deletedItemDto = deletedItemDTOs.computeIfAbsent(key, k -> new ItemDTO());
changeSets.addCreateItem(buildNormalItem(0L, namespaceId, key, value, deletedItemDto.getComment(), lineNum.get()));
} else if (!oldItem.getValue().equals(value) || lineNum.get() != oldItem.getLineNum()) {
changeSets.addUpdateItem(buildNormalItem(oldItem.getId(), namespaceId, key, value, oldItem.getComment(), lineNum.get()));
}
oldKeyMapItem.remove(key);
lineNum.set(lineNum.get() + 1);
});
oldKeyMapItem.forEach((key, value) -> changeSets.addDeleteItem(oldKeyMapItem.get(key)));
changeSets.setDataChangeLastModifiedBy(userInfoHolder.getUser().getUserId());
updateItems(appId, env, clusterName, namespaceName, changeSets);
String formatStr = String.format("%s+%s+%s+%s", appId, env, clusterName, namespaceName);
Tracer.logEvent(TracerEventType.MODIFY_NAMESPACE_BY_TEXT, formatStr);
Tracer.logEvent(TracerEventType.MODIFY_NAMESPACE, formatStr);
}
use of com.ctrip.framework.apollo.common.dto.NamespaceDTO in project apollo by ctripcorp.
the class ItemService method createCommentItem.
public ItemDTO createCommentItem(String appId, Env env, String clusterName, String namespaceName, ItemDTO item) {
NamespaceDTO namespace = namespaceAPI.loadNamespace(appId, env, clusterName, namespaceName);
if (namespace == null) {
throw new BadRequestException("namespace:" + namespaceName + " not exist in env:" + env + ", cluster:" + clusterName);
}
item.setNamespaceId(namespace.getId());
return itemAPI.createCommentItem(appId, env, clusterName, namespaceName, item);
}
use of com.ctrip.framework.apollo.common.dto.NamespaceDTO in project apollo by ctripcorp.
the class SearchController method searchByItem.
private PageDTO<App> searchByItem(String itemKey, Pageable pageable) {
List<App> result = Lists.newLinkedList();
if (StringUtils.isEmpty(itemKey)) {
return new PageDTO<>(result, pageable, 0);
}
// use the env witch has the most namespace as page index.
final AtomicLong maxTotal = new AtomicLong(0);
List<Env> activeEnvs = portalSettings.getActiveEnvs();
activeEnvs.forEach(env -> {
PageDTO<NamespaceDTO> namespacePage = namespaceService.findNamespacesByItem(env, itemKey, pageable);
if (!namespacePage.hasContent()) {
return;
}
long currentEnvNSTotal = namespacePage.getTotal();
if (currentEnvNSTotal > maxTotal.get()) {
maxTotal.set(namespacePage.getTotal());
}
List<NamespaceDTO> namespaceDTOS = namespacePage.getContent();
namespaceDTOS.forEach(namespaceDTO -> {
String cluster = namespaceDTO.getClusterName();
String namespaceName = namespaceDTO.getNamespaceName();
App app = new App();
app.setAppId(namespaceDTO.getAppId());
app.setName(env.getName() + " / " + cluster + " / " + namespaceName);
app.setOrgId(env.getName() + "+" + cluster + "+" + namespaceName);
app.setOrgName("SearchByItem" + "+" + itemKey);
result.add(app);
});
});
return new PageDTO<>(result, pageable, maxTotal.get());
}
Aggregations