| 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.Cluster; |
| 14 | |
import com.ctrip.apollo.biz.service.ClusterService; |
| 15 | |
import com.ctrip.apollo.common.auth.ActiveUser; |
| 16 | |
import com.ctrip.apollo.common.utils.BeanUtils; |
| 17 | |
import com.ctrip.apollo.core.dto.ClusterDTO; |
| 18 | |
import com.ctrip.apollo.core.exception.NotFoundException; |
| 19 | |
|
| 20 | |
@RestController |
| 21 | 1 | public class ClusterController { |
| 22 | |
|
| 23 | |
@Autowired |
| 24 | |
private ClusterService clusterService; |
| 25 | |
|
| 26 | |
@RequestMapping(path = "/apps/{appId}/clusters", method = RequestMethod.POST) |
| 27 | |
public ClusterDTO createOrUpdate(@PathVariable("appId") String appId, @RequestBody ClusterDTO dto, |
| 28 | |
@ActiveUser UserDetails user) { |
| 29 | 0 | Cluster entity = BeanUtils.transfrom(Cluster.class, dto); |
| 30 | 0 | Cluster managedEntity = clusterService.findOne(appId, entity.getName()); |
| 31 | 0 | if (managedEntity != null) { |
| 32 | 0 | managedEntity.setDataChangeLastModifiedBy(user.getUsername()); |
| 33 | 0 | BeanUtils.copyEntityProperties(entity, managedEntity); |
| 34 | 0 | entity = clusterService.update(managedEntity); |
| 35 | |
} else { |
| 36 | 0 | entity.setDataChangeCreatedBy(user.getUsername()); |
| 37 | 0 | entity = clusterService.save(entity); |
| 38 | |
} |
| 39 | |
|
| 40 | 0 | dto = BeanUtils.transfrom(ClusterDTO.class, entity); |
| 41 | 0 | return dto; |
| 42 | |
} |
| 43 | |
|
| 44 | |
@RequestMapping(path = "/apps/{appId}/clusters/{clusterName}", method = RequestMethod.DELETE) |
| 45 | |
public void delete(@PathVariable("appId") String appId, |
| 46 | |
@PathVariable("clusterName") String clusterName, @ActiveUser UserDetails user) { |
| 47 | 0 | Cluster entity = clusterService.findOne(appId, clusterName); |
| 48 | 0 | if (entity == null) |
| 49 | 0 | throw new NotFoundException("cluster not found for clusterName " + clusterName); |
| 50 | 0 | clusterService.delete(entity.getId(), user.getUsername()); |
| 51 | 0 | } |
| 52 | |
|
| 53 | |
@RequestMapping("/apps/{appId}/clusters") |
| 54 | |
public List<ClusterDTO> find(@PathVariable("appId") String appId) { |
| 55 | 0 | List<Cluster> clusters = clusterService.findClusters(appId); |
| 56 | 0 | return BeanUtils.batchTransform(ClusterDTO.class, clusters); |
| 57 | |
} |
| 58 | |
|
| 59 | |
@RequestMapping("/apps/{appId}/clusters/{clusterName}") |
| 60 | |
public ClusterDTO get(@PathVariable("appId") String appId, |
| 61 | |
@PathVariable("clusterName") String clusterName) { |
| 62 | 4 | Cluster cluster = clusterService.findOne(appId, clusterName); |
| 63 | 4 | if (cluster == null) throw new NotFoundException("cluster not found for name " + clusterName); |
| 64 | 4 | return BeanUtils.transfrom(ClusterDTO.class, cluster); |
| 65 | |
} |
| 66 | |
|
| 67 | |
@RequestMapping("/apps/{appId}/cluster/{clusterName}/unique") |
| 68 | |
public boolean isAppIdUnique(@PathVariable("appId") String appId, |
| 69 | |
@PathVariable("clusterName") String clusterName) { |
| 70 | 0 | return clusterService.isClusterNameUnique(appId, clusterName); |
| 71 | |
} |
| 72 | |
} |