| 1 | |
package com.ctrip.apollo.adminservice.controller; |
| 2 | |
|
| 3 | |
import java.util.List; |
| 4 | |
|
| 5 | |
import org.springframework.beans.factory.annotation.Autowired; |
| 6 | |
import org.springframework.security.core.userdetails.UserDetails; |
| 7 | |
import org.springframework.web.bind.annotation.PathVariable; |
| 8 | |
import org.springframework.web.bind.annotation.RequestBody; |
| 9 | |
import org.springframework.web.bind.annotation.RequestMapping; |
| 10 | |
import org.springframework.web.bind.annotation.RequestMethod; |
| 11 | |
import org.springframework.web.bind.annotation.RestController; |
| 12 | |
|
| 13 | |
import com.ctrip.apollo.biz.entity.Namespace; |
| 14 | |
import com.ctrip.apollo.biz.service.NamespaceService; |
| 15 | |
import com.ctrip.apollo.common.auth.ActiveUser; |
| 16 | |
import com.ctrip.apollo.common.utils.BeanUtils; |
| 17 | |
import com.ctrip.apollo.core.dto.NamespaceDTO; |
| 18 | |
import com.ctrip.apollo.core.exception.NotFoundException; |
| 19 | |
|
| 20 | |
@RestController |
| 21 | 1 | public class NamespaceController { |
| 22 | |
|
| 23 | |
@Autowired |
| 24 | |
private NamespaceService namespaceService; |
| 25 | |
|
| 26 | |
@RequestMapping(path = "/apps/{appId}/clusters/{clusterName}/namespaces", method = RequestMethod.POST) |
| 27 | |
public NamespaceDTO createOrUpdate(@PathVariable("appId") String appId, |
| 28 | |
@PathVariable("clusterName") String clusterName, @RequestBody NamespaceDTO dto, |
| 29 | |
@ActiveUser UserDetails user) { |
| 30 | 0 | Namespace entity = BeanUtils.transfrom(Namespace.class, dto); |
| 31 | 0 | Namespace managedEntity = namespaceService.findOne(appId, clusterName, entity.getNamespaceName()); |
| 32 | 0 | if (managedEntity != null) { |
| 33 | 0 | managedEntity.setDataChangeLastModifiedBy(user.getUsername()); |
| 34 | 0 | BeanUtils.copyEntityProperties(entity, managedEntity); |
| 35 | 0 | entity = namespaceService.update(managedEntity); |
| 36 | |
} else { |
| 37 | 0 | entity.setDataChangeCreatedBy(user.getUsername()); |
| 38 | 0 | entity = namespaceService.save(entity); |
| 39 | |
} |
| 40 | |
|
| 41 | 0 | dto = BeanUtils.transfrom(NamespaceDTO.class, entity); |
| 42 | 0 | return dto; |
| 43 | |
} |
| 44 | |
|
| 45 | |
@RequestMapping(path = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}", method = RequestMethod.DELETE) |
| 46 | |
public void delete(@PathVariable("appId") String appId, |
| 47 | |
@PathVariable("clusterName") String clusterName, |
| 48 | |
@PathVariable("namespaceName") String namespaceName, @ActiveUser UserDetails user) { |
| 49 | 0 | Namespace entity = namespaceService.findOne(appId, clusterName, namespaceName); |
| 50 | 0 | if (entity == null) throw new NotFoundException( |
| 51 | 0 | String.format("namespace not found for %s %s %s", appId, clusterName, namespaceName)); |
| 52 | 0 | namespaceService.delete(entity.getId(), user.getUsername()); |
| 53 | 0 | } |
| 54 | |
|
| 55 | |
@RequestMapping("/apps/{appId}/clusters/{clusterName}/namespaces") |
| 56 | |
public List<NamespaceDTO> find(@PathVariable("appId") String appId, |
| 57 | |
@PathVariable("clusterName") String clusterName) { |
| 58 | 0 | List<Namespace> groups = namespaceService.findNamespaces(appId, clusterName); |
| 59 | 0 | return BeanUtils.batchTransform(NamespaceDTO.class, groups); |
| 60 | |
} |
| 61 | |
|
| 62 | |
@RequestMapping("/namespaces/{namespaceId}") |
| 63 | |
public NamespaceDTO get(@PathVariable("namespaceId") Long namespaceId) { |
| 64 | 0 | Namespace namespace = namespaceService.findOne(namespaceId); |
| 65 | 0 | if (namespace == null) |
| 66 | 0 | throw new NotFoundException(String.format("namespace not found for %s", namespaceId)); |
| 67 | 0 | return BeanUtils.transfrom(NamespaceDTO.class, namespace); |
| 68 | |
} |
| 69 | |
|
| 70 | |
@RequestMapping("/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}") |
| 71 | |
public NamespaceDTO get(@PathVariable("appId") String appId, |
| 72 | |
@PathVariable("clusterName") String clusterName, |
| 73 | |
@PathVariable("namespaceName") String namespaceName) { |
| 74 | 4 | Namespace namespace = namespaceService.findOne(appId, clusterName, namespaceName); |
| 75 | 4 | if (namespace == null) throw new NotFoundException( |
| 76 | 0 | String.format("namespace not found for %s %s %s", appId, clusterName, namespaceName)); |
| 77 | 4 | return BeanUtils.transfrom(NamespaceDTO.class, namespace); |
| 78 | |
} |
| 79 | |
|
| 80 | |
} |