Coverage Report - com.ctrip.framework.apollo.portal.controller.AppController
 
Classes in this File Line Coverage Branch Coverage Complexity
AppController
11%
4/35
0%
0/8
2
 
 1  
 package com.ctrip.framework.apollo.portal.controller;
 2  
 
 3  
 
 4  
 import org.springframework.beans.factory.annotation.Autowired;
 5  
 import org.springframework.context.ApplicationEventPublisher;
 6  
 import org.springframework.http.HttpStatus;
 7  
 import org.springframework.http.ResponseEntity;
 8  
 import org.springframework.web.bind.annotation.PathVariable;
 9  
 import org.springframework.web.bind.annotation.RequestBody;
 10  
 import org.springframework.web.bind.annotation.RequestMapping;
 11  
 import org.springframework.web.bind.annotation.RequestMethod;
 12  
 import org.springframework.web.bind.annotation.RestController;
 13  
 import org.springframework.web.client.HttpClientErrorException;
 14  
 
 15  
 import com.ctrip.framework.apollo.common.entity.App;
 16  
 import com.ctrip.framework.apollo.common.http.MultiResponseEntity;
 17  
 import com.ctrip.framework.apollo.common.http.RichResponseEntity;
 18  
 import com.ctrip.framework.apollo.core.enums.Env;
 19  
 import com.ctrip.framework.apollo.portal.PortalSettings;
 20  
 import com.ctrip.framework.apollo.portal.entity.vo.EnvClusterInfo;
 21  
 import com.ctrip.framework.apollo.portal.listener.AppCreationEvent;
 22  
 import com.ctrip.framework.apollo.portal.service.AppService;
 23  
 
 24  
 import java.util.List;
 25  
 
 26  
 import static com.ctrip.framework.apollo.portal.util.RequestPrecondition.checkArgument;
 27  
 
 28  
 @RestController
 29  
 @RequestMapping("/apps")
 30  1
 public class AppController {
 31  
 
 32  
   @Autowired
 33  
   private AppService appService;
 34  
 
 35  
   @Autowired
 36  
   private PortalSettings portalSettings;
 37  
 
 38  
   @Autowired
 39  
   private ApplicationEventPublisher publisher;
 40  
 
 41  
   @RequestMapping("")
 42  
   public List<App> findAllApp() {
 43  0
     return appService.findAll();
 44  
   }
 45  
 
 46  
   @RequestMapping("/{appId}/navtree")
 47  
   public MultiResponseEntity<EnvClusterInfo> nav(@PathVariable String appId) {
 48  
 
 49  0
     MultiResponseEntity<EnvClusterInfo> response = MultiResponseEntity.ok();
 50  0
     List<Env> envs = portalSettings.getActiveEnvs();
 51  0
     for (Env env : envs) {
 52  
       try {
 53  0
         response.addResponseEntity(RichResponseEntity.ok(appService.createEnvNavNode(env, appId)));
 54  0
       } catch (Exception e) {
 55  0
         response.addResponseEntity(RichResponseEntity.error(HttpStatus.INTERNAL_SERVER_ERROR,
 56  0
                                                             "load env:" + env.name() + " cluster error." + e
 57  0
                                                                 .getMessage()));
 58  0
       }
 59  0
     }
 60  0
     return response;
 61  
   }
 62  
 
 63  
   /**
 64  
    * 创建App流程: 1.先在portal db中创建 2.再保存到各个环境的apollo db中
 65  
    *
 66  
    * 只要第一步成功,就算这次创建app是成功操作,如果某个环境的apollo db创建失败,可通过portal db中的app信息再次创建.
 67  
    */
 68  
   @RequestMapping(value = "", method = RequestMethod.POST)
 69  
   public ResponseEntity<Void> create(@RequestBody App app) {
 70  
 
 71  1
     checkArgument(app.getName(), app.getAppId(), app.getOwnerEmail(), app.getOwnerName());
 72  
 
 73  1
     appService.enrichUserInfo(app);
 74  1
     App createdApp = appService.createOrUpdateAppInLocal(app);
 75  
 
 76  0
     publisher.publishEvent(new AppCreationEvent(createdApp));
 77  
 
 78  0
     return ResponseEntity.ok().build();
 79  
   }
 80  
 
 81  
   @RequestMapping(value = "/envs/{env}", method = RequestMethod.POST, consumes = {"application/json"})
 82  
   public ResponseEntity<Void> create(@PathVariable String env, @RequestBody App app) {
 83  
 
 84  0
     checkArgument(app.getName(), app.getAppId(), app.getOwnerEmail(), app.getOwnerName());
 85  
 
 86  0
     appService.createApp(Env.valueOf(env), app);
 87  
 
 88  0
     return ResponseEntity.ok().build();
 89  
   }
 90  
 
 91  
   @RequestMapping(value = "/{appId}", method = RequestMethod.GET)
 92  
   public App load(@PathVariable String appId) {
 93  
 
 94  0
     return appService.load(appId);
 95  
   }
 96  
 
 97  
   @RequestMapping(value = "/{appId}/miss_envs")
 98  
   public MultiResponseEntity<Env> findMissEnvs(@PathVariable String appId) {
 99  
 
 100  0
     MultiResponseEntity<Env> response = MultiResponseEntity.ok();
 101  0
     for (Env env : portalSettings.getActiveEnvs()) {
 102  
       try {
 103  0
         appService.load(env, appId);
 104  0
       } catch (Exception e) {
 105  0
         if (e instanceof HttpClientErrorException &&
 106  0
             ((HttpClientErrorException) e).getStatusCode() == HttpStatus.NOT_FOUND) {
 107  0
           response.addResponseEntity(RichResponseEntity.ok(env));
 108  
         } else {
 109  0
           response.addResponseEntity(RichResponseEntity.error(HttpStatus.INTERNAL_SERVER_ERROR,
 110  0
                                                               String.format("load appId:%s from env %s error.", appId, env)
 111  0
                                                               + e.getMessage()));
 112  
         }
 113  0
       }
 114  
 
 115  0
     }
 116  
 
 117  0
     return response;
 118  
 
 119  
   }
 120  
 
 121  
 }