| 1 | |
package com.ctrip.framework.apollo.portal.controller; |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
import com.ctrip.framework.apollo.core.dto.ItemDTO; |
| 6 | |
import com.ctrip.framework.apollo.core.enums.Env; |
| 7 | |
import com.ctrip.framework.apollo.core.dto.ReleaseDTO; |
| 8 | |
import com.ctrip.framework.apollo.core.exception.BadRequestException; |
| 9 | |
import com.ctrip.framework.apollo.core.utils.StringUtils; |
| 10 | |
import com.ctrip.framework.apollo.portal.entity.vo.ItemDiffs; |
| 11 | |
import com.ctrip.framework.apollo.portal.entity.form.NamespaceSyncModel; |
| 12 | |
import com.ctrip.framework.apollo.portal.entity.form.NamespaceTextModel; |
| 13 | |
import com.ctrip.framework.apollo.portal.entity.form.NamespaceReleaseModel; |
| 14 | |
import com.ctrip.framework.apollo.portal.service.ConfigService; |
| 15 | |
|
| 16 | |
import org.springframework.beans.factory.annotation.Autowired; |
| 17 | |
import org.springframework.http.HttpStatus; |
| 18 | |
import org.springframework.http.ResponseEntity; |
| 19 | |
import org.springframework.security.access.prepost.PreAuthorize; |
| 20 | |
import org.springframework.web.bind.annotation.PathVariable; |
| 21 | |
import org.springframework.web.bind.annotation.RequestBody; |
| 22 | |
import org.springframework.web.bind.annotation.RequestMapping; |
| 23 | |
import org.springframework.web.bind.annotation.RequestMethod; |
| 24 | |
import org.springframework.web.bind.annotation.RestController; |
| 25 | |
|
| 26 | |
import java.util.List; |
| 27 | |
|
| 28 | |
import static com.ctrip.framework.apollo.portal.util.RequestPrecondition.checkModel; |
| 29 | |
|
| 30 | |
@RestController |
| 31 | |
@RequestMapping("") |
| 32 | 1 | public class ConfigController { |
| 33 | |
|
| 34 | |
@Autowired |
| 35 | |
private ConfigService configService; |
| 36 | |
|
| 37 | |
@PreAuthorize(value = "@permissionValidator.hasModifyNamespacePermission(#appId, #namespaceName)") |
| 38 | |
@RequestMapping(value = "/apps/{appId}/envs/{env}/clusters/{clusterName}/namespaces/{namespaceName}/items", method = RequestMethod.PUT, consumes = { |
| 39 | |
"application/json"}) |
| 40 | |
public void modifyItems(@PathVariable String appId, @PathVariable String env, |
| 41 | |
@PathVariable String clusterName, @PathVariable String namespaceName, |
| 42 | |
@RequestBody NamespaceTextModel model) { |
| 43 | |
|
| 44 | 0 | checkModel(model != null); |
| 45 | |
|
| 46 | 0 | model.setAppId(appId); |
| 47 | 0 | model.setClusterName(clusterName); |
| 48 | 0 | model.setEnv(env); |
| 49 | 0 | model.setNamespaceName(namespaceName); |
| 50 | |
|
| 51 | |
|
| 52 | 0 | configService.updateConfigItemByText(model); |
| 53 | 0 | } |
| 54 | |
|
| 55 | |
@PreAuthorize(value = "@permissionValidator.hasModifyNamespacePermission(#appId, #namespaceName)") |
| 56 | |
@RequestMapping(value = "/apps/{appId}/envs/{env}/clusters/{clusterName}/namespaces/{namespaceName}/item", method = RequestMethod.POST) |
| 57 | |
public ItemDTO createItem(@PathVariable String appId, @PathVariable String env, |
| 58 | |
@PathVariable String clusterName, @PathVariable String namespaceName, |
| 59 | |
@RequestBody ItemDTO item){ |
| 60 | 0 | checkModel(isValidItem(item)); |
| 61 | |
|
| 62 | 0 | return configService.createOrUpdateItem(appId, Env.valueOf(env), clusterName, namespaceName, item); |
| 63 | |
} |
| 64 | |
|
| 65 | |
@PreAuthorize(value = "@permissionValidator.hasModifyNamespacePermission(#appId, #namespaceName)") |
| 66 | |
@RequestMapping(value = "/apps/{appId}/envs/{env}/clusters/{clusterName}/namespaces/{namespaceName}/item", method = RequestMethod.PUT) |
| 67 | |
public ItemDTO updateItem(@PathVariable String appId, @PathVariable String env, |
| 68 | |
@PathVariable String clusterName, @PathVariable String namespaceName, |
| 69 | |
@RequestBody ItemDTO item){ |
| 70 | 0 | checkModel(isValidItem(item)); |
| 71 | |
|
| 72 | 0 | return configService.createOrUpdateItem(appId, Env.valueOf(env), clusterName, namespaceName, item); |
| 73 | |
} |
| 74 | |
|
| 75 | |
|
| 76 | |
@PreAuthorize(value = "@permissionValidator.hasModifyNamespacePermission(#appId, #namespaceName)") |
| 77 | |
@RequestMapping(value = "/apps/{appId}/envs/{env}/clusters/{clusterName}/namespaces/{namespaceName}/items/{itemId}", method = RequestMethod.DELETE) |
| 78 | |
public void deleteItem(@PathVariable String appId, @PathVariable String env, |
| 79 | |
@PathVariable String clusterName, @PathVariable String namespaceName, |
| 80 | |
@PathVariable long itemId){ |
| 81 | 0 | if (itemId <= 0){ |
| 82 | 0 | throw new BadRequestException("item id invalid"); |
| 83 | |
} |
| 84 | 0 | configService.deleteItem(Env.valueOf(env), itemId); |
| 85 | 0 | } |
| 86 | |
|
| 87 | |
@PreAuthorize(value = "@permissionValidator.hasReleaseNamespacePermission(#appId, #namespaceName)") |
| 88 | |
@RequestMapping(value = "/apps/{appId}/envs/{env}/clusters/{clusterName}/namespaces/{namespaceName}/release", method = RequestMethod.POST, consumes = { |
| 89 | |
"application/json"}) |
| 90 | |
public ReleaseDTO createRelease(@PathVariable String appId, |
| 91 | |
@PathVariable String env, @PathVariable String clusterName, |
| 92 | |
@PathVariable String namespaceName, @RequestBody NamespaceReleaseModel model) { |
| 93 | |
|
| 94 | 0 | checkModel(model != null); |
| 95 | 0 | model.setAppId(appId); |
| 96 | 0 | model.setEnv(env); |
| 97 | 0 | model.setClusterName(clusterName); |
| 98 | 0 | model.setNamespaceName(namespaceName); |
| 99 | |
|
| 100 | 0 | return configService.createRelease(model); |
| 101 | |
|
| 102 | |
} |
| 103 | |
|
| 104 | |
@RequestMapping(value = "/apps/{appId}/envs/{env}/clusters/{clusterName}/namespaces/{namespaceName}/items") |
| 105 | |
public List<ItemDTO> findItems(@PathVariable String appId, @PathVariable String env, |
| 106 | |
@PathVariable String clusterName, @PathVariable String namespaceName){ |
| 107 | |
|
| 108 | 0 | return configService.findItems(appId, Env.valueOf(env), clusterName, namespaceName); |
| 109 | |
} |
| 110 | |
|
| 111 | |
@RequestMapping(value = "/namespaces/{namespaceName}/diff", method = RequestMethod.POST, consumes = { |
| 112 | |
"application/json"}) |
| 113 | |
public List<ItemDiffs> diff(@RequestBody NamespaceSyncModel model){ |
| 114 | 0 | checkModel(model != null && !model.isInvalid()); |
| 115 | |
|
| 116 | 0 | return configService.compare(model.getSyncToNamespaces(), model.getSyncItems()); |
| 117 | |
} |
| 118 | |
|
| 119 | |
@PreAuthorize(value = "@permissionValidator.hasModifyNamespacePermission(#appId, #namespaceName)") |
| 120 | |
@RequestMapping(value = "/apps/{appId}/namespaces/{namespaceName}/items", method = RequestMethod.PUT, consumes = { |
| 121 | |
"application/json"}) |
| 122 | |
public ResponseEntity<Void> update(@PathVariable String appId, @PathVariable String namespaceName, |
| 123 | |
@RequestBody NamespaceSyncModel model){ |
| 124 | 0 | checkModel(model != null && !model.isInvalid()); |
| 125 | |
|
| 126 | 0 | configService.syncItems(model.getSyncToNamespaces(), model.getSyncItems()); |
| 127 | 0 | return ResponseEntity.status(HttpStatus.OK).build(); |
| 128 | |
} |
| 129 | |
|
| 130 | |
private boolean isValidItem(ItemDTO item){ |
| 131 | 0 | return item != null && !StringUtils.isContainEmpty(item.getKey(), item.getValue()); |
| 132 | |
} |
| 133 | |
|
| 134 | |
} |