Commit 08c4768c by Jason Song Committed by GitHub

Merge pull request #270 from lepdou/0613_17

create app namespace
parents 0db06584 38f02745
......@@ -71,13 +71,6 @@ public class AdminServiceAPI {
return dto;
}
public List<AppNamespaceDTO> findPublicAppNamespaces(Env env) {
AppNamespaceDTO[]
appNamespaceDTOs =
restTemplate.getForObject("{host}/appnamespaces/public", AppNamespaceDTO[].class
, getAdminServiceHost(env));
return Arrays.asList(appNamespaceDTOs);
}
public NamespaceDTO createNamespace(Env env, NamespaceDTO namespace) {
return restTemplate
......@@ -85,7 +78,7 @@ public class AdminServiceAPI {
getAdminServiceHost(env), namespace.getAppId(), namespace.getClusterName()).getBody();
}
public AppNamespaceDTO createOrUpdate(Env env, AppNamespaceDTO appNamespace) {
public AppNamespaceDTO createOrUpdateAppNamespace(Env env, AppNamespaceDTO appNamespace) {
return restTemplate.postForEntity("{host}/apps/{appId}/appnamespaces", appNamespace, AppNamespaceDTO.class,
getAdminServiceHost(env), appNamespace.getAppId()).getBody();
}
......
package com.ctrip.framework.apollo.portal.controller;
import com.ctrip.framework.apollo.common.entity.AppNamespace;
import com.ctrip.framework.apollo.core.dto.AppNamespaceDTO;
import com.ctrip.framework.apollo.core.dto.NamespaceDTO;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.core.utils.StringUtils;
import com.ctrip.framework.apollo.portal.auth.UserInfoHolder;
import com.ctrip.framework.apollo.portal.entity.vo.NamespaceVO;
import com.ctrip.framework.apollo.portal.listener.AppNamespaceCreationEvent;
import com.ctrip.framework.apollo.portal.service.NamespaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
......@@ -23,6 +26,10 @@ import static com.ctrip.framework.apollo.portal.util.RequestPrecondition.checkAr
public class NamespaceController {
@Autowired
private ApplicationEventPublisher publisher;
@Autowired
private UserInfoHolder userInfoHolder;
@Autowired
private NamespaceService namespaceService;
@RequestMapping("/appnamespaces/public")
......@@ -30,7 +37,7 @@ public class NamespaceController {
return namespaceService.findPublicAppNamespaces();
}
@PreAuthorize(value = "@permissionValidator.hasCreateNamespacePermission(#appId)")
@PreAuthorize(value = "@permissionValidator.hasCreateNamespacePermission(#namespace.appId)")
@RequestMapping(value = "/apps/{appId}/envs/{env}/clusters/{clusterName}/namespaces", method = RequestMethod.POST)
public NamespaceDTO createNamespace(@PathVariable String env, @RequestBody NamespaceDTO namespace) {
......@@ -40,11 +47,19 @@ public class NamespaceController {
}
@RequestMapping(value = "/apps/{appId}/appnamespaces", method = RequestMethod.POST)
public void createAppNamespace(@PathVariable String appId, @RequestBody AppNamespaceDTO appNamespace) {
public void createAppNamespace(@PathVariable String appId, @RequestBody AppNamespace appNamespace) {
checkArgument(appNamespace.getAppId(), appNamespace.getName());
namespaceService.createRemoteAppNamespace(appNamespace);
String operator = userInfoHolder.getUser().getUserId();
if (StringUtils.isEmpty(appNamespace.getDataChangeCreatedBy())) {
appNamespace.setDataChangeCreatedBy(operator);
}
appNamespace.setDataChangeLastModifiedBy(operator);
AppNamespace createdAppNamespace = namespaceService.createAppNamespaceInLocal(appNamespace);
publisher.publishEvent(new AppNamespaceCreationEvent(createdAppNamespace));
}
@RequestMapping("/apps/{appId}/envs/{env}/clusters/{clusterName}/namespaces")
......
......@@ -8,11 +8,6 @@ import org.springframework.context.ApplicationEvent;
public class AppCreationEvent extends ApplicationEvent {
/**
* Create a new ApplicationEvent.
*
* @param source the object on which the event initially occurred (never {@code null})
*/
public AppCreationEvent(Object source) {
super(source);
}
......
package com.ctrip.framework.apollo.portal.listener;
import com.google.common.base.Preconditions;
import com.ctrip.framework.apollo.common.entity.AppNamespace;
import org.springframework.context.ApplicationEvent;
public class AppNamespaceCreationEvent extends ApplicationEvent {
public AppNamespaceCreationEvent(Object source) {
super(source);
}
public AppNamespace getAppNamespace() {
Preconditions.checkState(source != null);
return (AppNamespace) this.source;
}
}
......@@ -3,6 +3,7 @@ package com.ctrip.framework.apollo.portal.listener;
import com.ctrip.framework.apollo.common.utils.BeanUtils;
import com.ctrip.framework.apollo.common.utils.ExceptionUtils;
import com.ctrip.framework.apollo.core.dto.AppDTO;
import com.ctrip.framework.apollo.core.dto.AppNamespaceDTO;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.PortalSettings;
import com.ctrip.framework.apollo.portal.api.AdminServiceAPI;
......@@ -25,6 +26,8 @@ public class CreationListener {
private PortalSettings portalSettings;
@Autowired
private AdminServiceAPI.AppAPI appAPI;
@Autowired
private AdminServiceAPI.NamespaceAPI namespaceAPI;
@EventListener
public void onAppCreationEvent(AppCreationEvent event) {
......@@ -34,7 +37,20 @@ public class CreationListener {
try {
appAPI.createApp(env, appDTO);
} catch (HttpStatusCodeException e) {
logger.error(ExceptionUtils.toString(e));
logger.error("call appAPI.createApp error.[{app}, {env}]", appDTO.getAppId(), env, e);
}
}
}
@EventListener
public void onAppNamespaceCreationEvent(AppNamespaceCreationEvent event){
AppNamespaceDTO dto = BeanUtils.transfrom(AppNamespaceDTO.class, event.getAppNamespace());
List<Env> envs = portalSettings.getActiveEnvs();
for (Env env : envs) {
try {
namespaceAPI.createOrUpdateAppNamespace(env, dto);
} catch (HttpStatusCodeException e) {
logger.error("call namespaceAPI.createOrUpdateAppNamespace error. [{app}, {env}]", dto.getAppId(), env, e);
}
}
}
......
......@@ -95,19 +95,15 @@ public class NamespaceService {
return Objects.isNull(appNamespaceRepository.findByAppIdAndName(appId, namespaceName));
}
public void createRemoteAppNamespace(AppNamespaceDTO appNamespace) {
String operator = userInfoHolder.getUser().getUserId();
if (StringUtils.isEmpty(appNamespace.getDataChangeCreatedBy())) {
appNamespace.setDataChangeCreatedBy(operator);
}
appNamespace.setDataChangeLastModifiedBy(operator);
for (Env env : portalSettings.getActiveEnvs()) {
try {
namespaceAPI.createOrUpdate(env, appNamespace);
} catch (HttpStatusCodeException e) {
logger.error(ExceptionUtils.toString(e));
throw e;
}
@Transactional
public AppNamespace createAppNamespaceInLocal(AppNamespace appNamespace) {
AppNamespace managedAppNamespace = appNamespaceRepository.findByAppIdAndName(appNamespace.getAppId(), appNamespace.getName());
//update
if (managedAppNamespace != null){
BeanUtils.copyEntityProperties(appNamespace, managedAppNamespace);
return appNamespaceRepository.save(managedAppNamespace);
}else {
return appNamespaceRepository.save(appNamespace);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment