Coverage Report - com.ctrip.framework.apollo.portal.controller.NamespaceController
 
Classes in this File Line Coverage Branch Coverage Complexity
NamespaceController
9%
2/22
0%
0/6
1.75
 
 1  
 package com.ctrip.framework.apollo.portal.controller;
 2  
 
 3  
 import com.ctrip.framework.apollo.common.entity.AppNamespace;
 4  
 import com.ctrip.framework.apollo.core.dto.NamespaceDTO;
 5  
 import com.ctrip.framework.apollo.core.enums.Env;
 6  
 import com.ctrip.framework.apollo.core.utils.StringUtils;
 7  
 import com.ctrip.framework.apollo.portal.auth.UserInfoHolder;
 8  
 import com.ctrip.framework.apollo.portal.entity.form.NamespaceCreationModel;
 9  
 import com.ctrip.framework.apollo.portal.entity.vo.NamespaceVO;
 10  
 import com.ctrip.framework.apollo.portal.listener.AppNamespaceCreationEvent;
 11  
 import com.ctrip.framework.apollo.portal.service.NamespaceService;
 12  
 
 13  
 import org.slf4j.Logger;
 14  
 import org.slf4j.LoggerFactory;
 15  
 import org.springframework.beans.factory.annotation.Autowired;
 16  
 import org.springframework.context.ApplicationEventPublisher;
 17  
 import org.springframework.http.ResponseEntity;
 18  
 import org.springframework.security.access.prepost.PreAuthorize;
 19  
 import org.springframework.util.CollectionUtils;
 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.checkArgument;
 29  
 import static com.ctrip.framework.apollo.portal.util.RequestPrecondition.checkModel;
 30  
 
 31  
 @RestController
 32  1
 public class NamespaceController {
 33  
 
 34  1
   Logger logger = LoggerFactory.getLogger(NamespaceController.class);
 35  
 
 36  
   @Autowired
 37  
   private ApplicationEventPublisher publisher;
 38  
   @Autowired
 39  
   private UserInfoHolder userInfoHolder;
 40  
   @Autowired
 41  
   private NamespaceService namespaceService;
 42  
 
 43  
   @RequestMapping("/appnamespaces/public")
 44  
   public List<AppNamespace> findPublicAppNamespaces() {
 45  0
     return namespaceService.findPublicAppNamespaces();
 46  
   }
 47  
 
 48  
   @PreAuthorize(value = "@permissionValidator.hasCreateNamespacePermission(#appId)")
 49  
   @RequestMapping(value = "/apps/{appId}/namespaces", method = RequestMethod.POST)
 50  
   public ResponseEntity<Void> createNamespace(@PathVariable String appId, @RequestBody List<NamespaceCreationModel> models) {
 51  
 
 52  0
     checkModel(!CollectionUtils.isEmpty(models));
 53  
 
 54  0
     for (NamespaceCreationModel model : models) {
 55  0
       NamespaceDTO namespace = model.getNamespace();
 56  
 
 57  0
       checkArgument(model.getEnv(), namespace.getAppId(), namespace.getClusterName(), namespace.getNamespaceName());
 58  
 
 59  
       try {
 60  
         // TODO: 16/6/17 某些环境创建失败,统一处理这种场景
 61  0
         namespaceService.createNamespace(Env.valueOf(model.getEnv()), namespace);
 62  0
       } catch (Exception e) {
 63  0
         logger.error("create namespace error.", e);
 64  0
       }
 65  0
     }
 66  0
     return ResponseEntity.ok().build();
 67  
   }
 68  
 
 69  
   @RequestMapping(value = "/apps/{appId}/appnamespaces", method = RequestMethod.POST)
 70  
   public void createAppNamespace(@PathVariable String appId, @RequestBody AppNamespace appNamespace) {
 71  
 
 72  0
     checkArgument(appNamespace.getAppId(), appNamespace.getName());
 73  
 
 74  0
     String operator = userInfoHolder.getUser().getUserId();
 75  0
     if (StringUtils.isEmpty(appNamespace.getDataChangeCreatedBy())) {
 76  0
       appNamespace.setDataChangeCreatedBy(operator);
 77  
     }
 78  0
     appNamespace.setDataChangeLastModifiedBy(operator);
 79  0
     AppNamespace createdAppNamespace = namespaceService.createAppNamespaceInLocal(appNamespace);
 80  
 
 81  0
     publisher.publishEvent(new AppNamespaceCreationEvent(createdAppNamespace));
 82  
 
 83  0
   }
 84  
 
 85  
   @RequestMapping("/apps/{appId}/envs/{env}/clusters/{clusterName}/namespaces")
 86  
   public List<NamespaceVO> findNamespaces(@PathVariable String appId, @PathVariable String env,
 87  
                                           @PathVariable String clusterName) {
 88  
 
 89  0
     return namespaceService.findNampspaces(appId, Env.valueOf(env), clusterName);
 90  
   }
 91  
 
 92  
 }