Search in sources :

Example 16 with BadRequestException

use of com.ctrip.framework.apollo.common.exception.BadRequestException in project apollo by ctripcorp.

the class ReleaseController method createRelease.

@PreAuthorize(value = "@permissionValidator.hasReleaseNamespacePermission(#appId, #namespaceName)")
@RequestMapping(value = "/apps/{appId}/envs/{env}/clusters/{clusterName}/namespaces/{namespaceName}/releases", method = RequestMethod.POST)
public ReleaseDTO createRelease(@PathVariable String appId, @PathVariable String env, @PathVariable String clusterName, @PathVariable String namespaceName, @RequestBody NamespaceReleaseModel model) {
    checkModel(Objects.nonNull(model));
    model.setAppId(appId);
    model.setEnv(env);
    model.setClusterName(clusterName);
    model.setNamespaceName(namespaceName);
    if (model.isEmergencyPublish() && !portalConfig.isEmergencyPublishAllowed(Env.valueOf(env))) {
        throw new BadRequestException(String.format("Env: %s is not supported emergency publish now", env));
    }
    ReleaseDTO createdRelease = releaseService.publish(model);
    ConfigPublishEvent event = ConfigPublishEvent.instance();
    event.withAppId(appId).withCluster(clusterName).withNamespace(namespaceName).withReleaseId(createdRelease.getId()).setNormalPublishEvent(true).setEnv(Env.valueOf(env));
    publisher.publishEvent(event);
    return createdRelease;
}
Also used : BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) ConfigPublishEvent(com.ctrip.framework.apollo.portal.listener.ConfigPublishEvent) ReleaseDTO(com.ctrip.framework.apollo.common.dto.ReleaseDTO) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 17 with BadRequestException

use of com.ctrip.framework.apollo.common.exception.BadRequestException in project apollo by ctripcorp.

the class PropertyResolver method resolve.

@Override
public ItemChangeSets resolve(long namespaceId, String configText, List<ItemDTO> baseItems) {
    Map<Integer, ItemDTO> oldLineNumMapItem = BeanUtils.mapByKey("lineNum", baseItems);
    Map<String, ItemDTO> oldKeyMapItem = BeanUtils.mapByKey("key", baseItems);
    // remove comment and blank item map.
    oldKeyMapItem.remove("");
    String[] newItems = configText.split(ITEM_SEPARATOR);
    if (isHasRepeatKey(newItems)) {
        throw new BadRequestException("config text has repeat key please check.");
    }
    ItemChangeSets changeSets = new ItemChangeSets();
    // use for delete blank and comment item
    Map<Integer, String> newLineNumMapItem = new HashMap<Integer, String>();
    int lineCounter = 1;
    for (String newItem : newItems) {
        newItem = newItem.trim();
        newLineNumMapItem.put(lineCounter, newItem);
        ItemDTO oldItemByLine = oldLineNumMapItem.get(lineCounter);
        // comment item
        if (isCommentItem(newItem)) {
            handleCommentLine(namespaceId, oldItemByLine, newItem, lineCounter, changeSets);
        // blank item
        } else if (isBlankItem(newItem)) {
            handleBlankLine(namespaceId, oldItemByLine, lineCounter, changeSets);
        // normal item
        } else {
            handleNormalLine(namespaceId, oldKeyMapItem, newItem, lineCounter, changeSets);
        }
        lineCounter++;
    }
    deleteCommentAndBlankItem(oldLineNumMapItem, newLineNumMapItem, changeSets);
    deleteNormalKVItem(oldKeyMapItem, changeSets);
    return changeSets;
}
Also used : HashMap(java.util.HashMap) ItemDTO(com.ctrip.framework.apollo.common.dto.ItemDTO) ItemChangeSets(com.ctrip.framework.apollo.common.dto.ItemChangeSets) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException)

Example 18 with BadRequestException

use of com.ctrip.framework.apollo.common.exception.BadRequestException in project apollo by ctripcorp.

the class PropertyResolver method handleNormalLine.

private void handleNormalLine(Long namespaceId, Map<String, ItemDTO> keyMapOldItem, String newItem, int lineCounter, ItemChangeSets changeSets) {
    String[] kv = parseKeyValueFromItem(newItem);
    if (kv == null) {
        throw new BadRequestException("line:" + lineCounter + " key value must separate by '='");
    }
    String newKey = kv[0];
    // handle user input \n
    String newValue = kv[1].replace("\\n", "\n");
    ItemDTO oldItem = keyMapOldItem.get(newKey);
    if (oldItem == null) {
        // new item
        changeSets.addCreateItem(buildNormalItem(0l, namespaceId, newKey, newValue, "", lineCounter));
    } else if (!newValue.equals(oldItem.getValue()) || lineCounter != oldItem.getLineNum()) {
        // update item
        changeSets.addUpdateItem(buildNormalItem(oldItem.getId(), namespaceId, newKey, newValue, oldItem.getComment(), lineCounter));
    }
    keyMapOldItem.remove(newKey);
}
Also used : ItemDTO(com.ctrip.framework.apollo.common.dto.ItemDTO) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException)

Example 19 with BadRequestException

use of com.ctrip.framework.apollo.common.exception.BadRequestException in project apollo by ctripcorp.

the class AppController method update.

@PreAuthorize(value = "@permissionValidator.isAppAdmin(#appId)")
@RequestMapping(value = "/{appId:.+}", method = RequestMethod.PUT)
public void update(@PathVariable String appId, @RequestBody AppModel appModel) {
    if (!Objects.equals(appId, appModel.getAppId())) {
        throw new BadRequestException("The App Id of path variable and request body is different");
    }
    App app = transformToApp(appModel);
    App updatedApp = appService.updateAppInLocal(app);
    publisher.publishEvent(new AppInfoChangedEvent(updatedApp));
}
Also used : App(com.ctrip.framework.apollo.common.entity.App) AppInfoChangedEvent(com.ctrip.framework.apollo.portal.listener.AppInfoChangedEvent) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 20 with BadRequestException

use of com.ctrip.framework.apollo.common.exception.BadRequestException in project apollo by ctripcorp.

the class NamespaceBranchController method merge.

@PreAuthorize(value = "@permissionValidator.hasReleaseNamespacePermission(#appId, #namespaceName)")
@RequestMapping(value = "/apps/{appId}/envs/{env}/clusters/{clusterName}/namespaces/{namespaceName}/branches/{branchName}/merge", method = RequestMethod.POST)
public ReleaseDTO merge(@PathVariable String appId, @PathVariable String env, @PathVariable String clusterName, @PathVariable String namespaceName, @PathVariable String branchName, @RequestParam(value = "deleteBranch", defaultValue = "true") boolean deleteBranch, @RequestBody NamespaceReleaseModel model) {
    if (model.isEmergencyPublish() && !portalConfig.isEmergencyPublishAllowed(Env.fromString(env))) {
        throw new BadRequestException(String.format("Env: %s is not supported emergency publish now", env));
    }
    ReleaseDTO createdRelease = namespaceBranchService.merge(appId, Env.valueOf(env), clusterName, namespaceName, branchName, model.getReleaseTitle(), model.getReleaseComment(), model.isEmergencyPublish(), deleteBranch);
    ConfigPublishEvent event = ConfigPublishEvent.instance();
    event.withAppId(appId).withCluster(clusterName).withNamespace(namespaceName).withReleaseId(createdRelease.getId()).setMergeEvent(true).setEnv(Env.valueOf(env));
    publisher.publishEvent(event);
    return createdRelease;
}
Also used : BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) ConfigPublishEvent(com.ctrip.framework.apollo.portal.listener.ConfigPublishEvent) ReleaseDTO(com.ctrip.framework.apollo.common.dto.ReleaseDTO) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

BadRequestException (com.ctrip.framework.apollo.common.exception.BadRequestException)46 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)17 Transactional (org.springframework.transaction.annotation.Transactional)12 ItemDTO (com.ctrip.framework.apollo.common.dto.ItemDTO)9 AppNamespace (com.ctrip.framework.apollo.common.entity.AppNamespace)9 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)9 Namespace (com.ctrip.framework.apollo.biz.entity.Namespace)7 App (com.ctrip.framework.apollo.common.entity.App)6 Cluster (com.ctrip.framework.apollo.biz.entity.Cluster)5 NamespaceDTO (com.ctrip.framework.apollo.common.dto.NamespaceDTO)5 Item (com.ctrip.framework.apollo.biz.entity.Item)4 UserInfo (com.ctrip.framework.apollo.portal.entity.bo.UserInfo)4 ItemChangeSets (com.ctrip.framework.apollo.common.dto.ItemChangeSets)3 ReleaseDTO (com.ctrip.framework.apollo.common.dto.ReleaseDTO)3 OpenItemDTO (com.ctrip.framework.apollo.openapi.dto.OpenItemDTO)3 ConfigPublishEvent (com.ctrip.framework.apollo.portal.listener.ConfigPublishEvent)3 PreAcquireNamespaceLock (com.ctrip.framework.apollo.adminservice.aop.PreAcquireNamespaceLock)2 Commit (com.ctrip.framework.apollo.biz.entity.Commit)2 NamespaceLock (com.ctrip.framework.apollo.biz.entity.NamespaceLock)2 Release (com.ctrip.framework.apollo.biz.entity.Release)2