| 1 | |
package com.ctrip.framework.apollo.portal.controller; |
| 2 | |
|
| 3 | |
import org.springframework.beans.factory.annotation.Autowired; |
| 4 | |
import org.springframework.http.HttpStatus; |
| 5 | |
import org.springframework.http.ResponseEntity; |
| 6 | |
import org.springframework.web.bind.annotation.PathVariable; |
| 7 | |
import org.springframework.web.bind.annotation.RequestBody; |
| 8 | |
import org.springframework.web.bind.annotation.RequestMapping; |
| 9 | |
import org.springframework.web.bind.annotation.RequestMethod; |
| 10 | |
import org.springframework.web.bind.annotation.RestController; |
| 11 | |
import org.springframework.web.client.HttpClientErrorException; |
| 12 | |
|
| 13 | |
import com.ctrip.framework.apollo.common.http.MultiResponseEntity; |
| 14 | |
import com.ctrip.framework.apollo.common.http.RichResponseEntity; |
| 15 | |
import com.ctrip.framework.apollo.core.dto.AppDTO; |
| 16 | |
import com.ctrip.framework.apollo.core.enums.Env; |
| 17 | |
import com.ctrip.framework.apollo.core.exception.BadRequestException; |
| 18 | |
import com.ctrip.framework.apollo.core.utils.StringUtils; |
| 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.service.PortalAppService; |
| 22 | |
|
| 23 | |
import java.util.List; |
| 24 | |
|
| 25 | |
@RestController |
| 26 | |
@RequestMapping("/apps") |
| 27 | 1 | public class PortalAppController { |
| 28 | |
|
| 29 | |
@Autowired |
| 30 | |
private PortalAppService appService; |
| 31 | |
|
| 32 | |
@Autowired |
| 33 | |
private PortalSettings portalSettings; |
| 34 | |
|
| 35 | |
@RequestMapping("/envs/{env}") |
| 36 | |
public List<AppDTO> findAllApp(@PathVariable String env){ |
| 37 | |
|
| 38 | 0 | if (StringUtils.isEmpty(env)){ |
| 39 | 0 | throw new BadRequestException("env can not be empty"); |
| 40 | |
} |
| 41 | 0 | return appService.findAll(Env.valueOf(env)); |
| 42 | |
} |
| 43 | |
|
| 44 | |
@RequestMapping("/{appId}/navtree") |
| 45 | |
public MultiResponseEntity<EnvClusterInfo> nav(@PathVariable String appId) { |
| 46 | |
|
| 47 | 0 | if (StringUtils.isEmpty(appId)) { |
| 48 | 0 | throw new BadRequestException("app id can not be empty."); |
| 49 | |
} |
| 50 | 0 | MultiResponseEntity<EnvClusterInfo> response = MultiResponseEntity.ok(); |
| 51 | 0 | List<Env> envs = portalSettings.getActiveEnvs(); |
| 52 | 0 | for (Env env : envs) { |
| 53 | |
try { |
| 54 | 0 | response.addResponseEntity(RichResponseEntity.ok(appService.createEnvNavNode(env, appId))); |
| 55 | 0 | } catch (Exception e) { |
| 56 | 0 | response.addResponseEntity(RichResponseEntity.error(HttpStatus.INTERNAL_SERVER_ERROR, |
| 57 | 0 | "load env:" + env.name() + " cluster error." + e |
| 58 | 0 | .getMessage())); |
| 59 | 0 | } |
| 60 | 0 | } |
| 61 | 0 | return response; |
| 62 | |
} |
| 63 | |
|
| 64 | |
@RequestMapping(value = "/envs/{env}", method = RequestMethod.POST, consumes = {"application/json"}) |
| 65 | |
public ResponseEntity<Void> create(@PathVariable String env, @RequestBody AppDTO app) { |
| 66 | 1 | if (isInvalidApp(app)){ |
| 67 | 0 | throw new BadRequestException("request payload contains empty"); |
| 68 | |
} |
| 69 | 1 | if ("ALL".equals(env)){ |
| 70 | 1 | appService.createAppInAllEnvs(app); |
| 71 | |
} else { |
| 72 | 0 | appService.createApp(Env.valueOf(env), app); |
| 73 | |
} |
| 74 | 1 | return ResponseEntity.ok().build(); |
| 75 | |
} |
| 76 | |
|
| 77 | |
@RequestMapping(value = "/{appId}", method = RequestMethod.GET) |
| 78 | |
public AppDTO load(@PathVariable String appId){ |
| 79 | 0 | if (StringUtils.isEmpty(appId)){ |
| 80 | 0 | throw new BadRequestException("app id can not be empty."); |
| 81 | |
} |
| 82 | 0 | return appService.load(appId); |
| 83 | |
} |
| 84 | |
|
| 85 | |
@RequestMapping(value = "/{appId}/miss_envs") |
| 86 | |
public MultiResponseEntity<Env> findMissEnvs(@PathVariable String appId) { |
| 87 | 0 | MultiResponseEntity<Env> response = MultiResponseEntity.ok(); |
| 88 | 0 | for (Env env : portalSettings.getActiveEnvs()) { |
| 89 | |
try { |
| 90 | 0 | appService.load(env, appId); |
| 91 | 0 | } catch (Exception e) { |
| 92 | 0 | if (e instanceof HttpClientErrorException && |
| 93 | 0 | ((HttpClientErrorException)e).getStatusCode() == HttpStatus.NOT_FOUND){ |
| 94 | 0 | response.addResponseEntity(RichResponseEntity.ok(env)); |
| 95 | |
} else { |
| 96 | 0 | response.addResponseEntity(RichResponseEntity.error(HttpStatus.INTERNAL_SERVER_ERROR, |
| 97 | |
String |
| 98 | 0 | .format("load appId:%s from env %s error.", appId, |
| 99 | |
env) |
| 100 | 0 | + e.getMessage())); |
| 101 | |
} |
| 102 | 0 | } |
| 103 | |
|
| 104 | 0 | } |
| 105 | |
|
| 106 | 0 | return response; |
| 107 | |
|
| 108 | |
} |
| 109 | |
|
| 110 | |
private boolean isInvalidApp(AppDTO app) { |
| 111 | 1 | return StringUtils.isContainEmpty(app.getName(), app.getAppId(), app.getOwnerEmail(), app.getOwnerName()); |
| 112 | |
} |
| 113 | |
} |
| 114 | |
|