Skip to content

Commit

Permalink
Merge pull request apolloconfig#612 from lepdou/openapi-createnamespace
Browse files Browse the repository at this point in the history
add create namespace open api
  • Loading branch information
lepdou authored May 3, 2017
2 parents aeb9230 + 8850633 commit c511583
Show file tree
Hide file tree
Showing 20 changed files with 387 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ public String getValue() {
return value;
}

public static ConfigFileFormat fromString(String value){
if (StringUtils.isEmpty(value)){
public static ConfigFileFormat fromString(String value) {
if (StringUtils.isEmpty(value)) {
throw new IllegalArgumentException("value can not be empty");
}
switch (value){
switch (value) {
case "properties":
return Properties;
case "xml":
Expand All @@ -36,4 +36,13 @@ public static ConfigFileFormat fromString(String value){
}
throw new IllegalArgumentException(value + " can not map enum");
}

public static boolean isValidFormat(String value) {
try {
fromString(value);
return true;
} catch (IllegalArgumentException e) {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ public class ConsumerPermissionValidator {
@Autowired
private ConsumerAuthUtil consumerAuthUtil;


public boolean hasModifyNamespacePermission(HttpServletRequest request, String appId, String
namespaceName) {

if (hasCreateNamespacePermission(request, appId)) {
return true;
}
return permissionService.consumerHasPermission(consumerAuthUtil.retrieveConsumerId(request),
PermissionType.MODIFY_NAMESPACE,
RoleUtils.buildNamespaceTargetId(appId, namespaceName));
Expand All @@ -28,10 +33,19 @@ public boolean hasModifyNamespacePermission(HttpServletRequest request, String a

public boolean hasReleaseNamespacePermission(HttpServletRequest request, String appId, String
namespaceName) {
if (hasCreateNamespacePermission(request, appId)) {
return true;
}
return permissionService.consumerHasPermission(consumerAuthUtil.retrieveConsumerId(request),
PermissionType.RELEASE_NAMESPACE,
RoleUtils.buildNamespaceTargetId(appId, namespaceName));

}

public boolean hasCreateNamespacePermission(HttpServletRequest request, String appId) {
return permissionService.consumerHasPermission(consumerAuthUtil.retrieveConsumerId(request),
PermissionType.CREATE_NAMESPACE,
appId);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.ctrip.framework.apollo.openapi.dto;


import com.ctrip.framework.apollo.common.dto.BaseDTO;

public class OpenAppNamespaceDTO extends BaseDTO {

private String name;

private String appId;

private String format;

private boolean isPublic;

private String comment;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAppId() {
return appId;
}

public void setAppId(String appId) {
this.appId = appId;
}

public String getFormat() {
return format;
}

public void setFormat(String format) {
this.format = format;
}

public boolean isPublic() {
return isPublic;
}

public void setPublic(boolean aPublic) {
isPublic = aPublic;
}

public String getComment() {
return comment;
}

public void setComment(String comment) {
this.comment = comment;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,9 @@ public List<ConsumerRole> assignNamespaceRoleToConsumer(String token, String app
long namespaceReleaseRoleId = namespaceReleaseRole.getId();

ConsumerRole managedModifyRole = consumerRoleRepository.findByConsumerIdAndRoleId(consumerId, namespaceModifyRoleId);
if (managedModifyRole != null) {
throw new BadRequestException("Namespace's role has assigned to consumer.");
ConsumerRole managedReleaseRole = consumerRoleRepository.findByConsumerIdAndRoleId(consumerId, namespaceReleaseRoleId);
if (managedModifyRole != null && managedReleaseRole != null) {
return Arrays.asList(managedModifyRole, managedReleaseRole);
}

String operator = userInfoHolder.getUser().getUserId();
Expand All @@ -147,6 +148,29 @@ public List<ConsumerRole> assignNamespaceRoleToConsumer(String token, String app
return Arrays.asList(createdModifyConsumerRole, createdReleaseConsumerRole);
}

@Transactional
public ConsumerRole assignAppRoleToConsumer(String token, String appId) {
Long consumerId = getConsumerIdByToken(token);
if (consumerId == null) {
throw new BadRequestException("Token is Illegal");
}

Role masterRole = rolePermissionService.findRoleByRoleName(RoleUtils.buildAppMasterRoleName(appId));
if (masterRole == null) {
throw new BadRequestException("App's role does not exist. Please check whether app has created.");
}

long roleId = masterRole.getId();
ConsumerRole managedModifyRole = consumerRoleRepository.findByConsumerIdAndRoleId(consumerId, roleId);
if (managedModifyRole != null) {
return managedModifyRole;
}

String operator = userInfoHolder.getUser().getUserId();
ConsumerRole consumerRole = createConsumerRole(consumerId, roleId, operator);
return consumerRoleRepository.save(consumerRole);
}

@Transactional
public void createConsumerAudits(Iterable<ConsumerAudit> consumerAudits) {
consumerAuditRepository.save(consumerAudits);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;

import com.ctrip.framework.apollo.common.dto.AppNamespaceDTO;
import com.ctrip.framework.apollo.common.dto.ItemDTO;
import com.ctrip.framework.apollo.common.dto.NamespaceLockDTO;
import com.ctrip.framework.apollo.common.dto.ReleaseDTO;
import com.ctrip.framework.apollo.common.entity.AppNamespace;
import com.ctrip.framework.apollo.common.utils.BeanUtils;
import com.ctrip.framework.apollo.openapi.dto.OpenAppNamespaceDTO;
import com.ctrip.framework.apollo.openapi.dto.OpenItemDTO;
import com.ctrip.framework.apollo.openapi.dto.OpenNamespaceDTO;
import com.ctrip.framework.apollo.openapi.dto.OpenNamespaceLockDTO;
Expand Down Expand Up @@ -40,6 +43,15 @@ public static ItemDTO transformToItemDTO(OpenItemDTO openItemDTO) {
return BeanUtils.transfrom(ItemDTO.class, openItemDTO);
}

public static OpenAppNamespaceDTO transformToOpenAppNamespaceDTO(AppNamespace appNamespace) {
Preconditions.checkArgument(appNamespace != null);
return BeanUtils.transfrom(OpenAppNamespaceDTO.class, appNamespace);
}

public static AppNamespace transformToAppNamespace(OpenAppNamespaceDTO openAppNamespaceDTO) {
Preconditions.checkArgument(openAppNamespaceDTO != null);
return BeanUtils.transfrom(AppNamespace.class, openAppNamespaceDTO);
}

public static OpenReleaseDTO transformFromReleaseDTO(ReleaseDTO release) {
Preconditions.checkArgument(release != null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,88 @@

import com.ctrip.framework.apollo.common.dto.NamespaceDTO;
import com.ctrip.framework.apollo.common.dto.NamespaceLockDTO;
import com.ctrip.framework.apollo.common.entity.AppNamespace;
import com.ctrip.framework.apollo.common.exception.BadRequestException;
import com.ctrip.framework.apollo.common.utils.InputValidator;
import com.ctrip.framework.apollo.common.utils.RequestPrecondition;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.openapi.dto.OpenAppNamespaceDTO;
import com.ctrip.framework.apollo.openapi.dto.OpenNamespaceDTO;
import com.ctrip.framework.apollo.openapi.dto.OpenNamespaceLockDTO;
import com.ctrip.framework.apollo.openapi.util.OpenApiBeanUtils;
import com.ctrip.framework.apollo.portal.entity.bo.NamespaceBO;
import com.ctrip.framework.apollo.portal.listener.AppNamespaceCreationEvent;
import com.ctrip.framework.apollo.portal.service.AppNamespaceService;
import com.ctrip.framework.apollo.portal.service.NamespaceLockService;
import com.ctrip.framework.apollo.portal.service.NamespaceService;
import com.ctrip.framework.apollo.portal.spi.UserService;

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;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

import javax.servlet.http.HttpServletRequest;

@RestController("openapiNamespaceController")
@RequestMapping("/openapi/v1/envs/{env}")
public class NamespaceController {

@Autowired
private NamespaceLockService namespaceLockService;
@Autowired
private NamespaceService namespaceService;
@Autowired
private AppNamespaceService appNamespaceService;
@Autowired
private ApplicationEventPublisher publisher;
@Autowired
private UserService userService;


@PreAuthorize(value = "@consumerPermissionValidator.hasCreateNamespacePermission(#request, #appId)")
@RequestMapping(value = "/openapi/v1/apps/{appId}/appnamespaces", method = RequestMethod.POST)
public OpenAppNamespaceDTO createNamespace(@PathVariable String appId, @RequestBody OpenAppNamespaceDTO appNamespaceDTO,
HttpServletRequest request) {

if (!Objects.equals(appId, appNamespaceDTO.getAppId())) {
throw new BadRequestException(String.format("AppId not equal. AppId in path = %s, AppId in payload = %s", appId,
appNamespaceDTO.getAppId()));
}
RequestPrecondition.checkArgumentsNotEmpty(appNamespaceDTO.getAppId(), appNamespaceDTO.getName(),
appNamespaceDTO.getFormat(), appNamespaceDTO.getDataChangeCreatedBy());

if (!InputValidator.isValidAppNamespace(appNamespaceDTO.getName())) {
throw new BadRequestException(String.format("Namespace格式错误: %s",
InputValidator.INVALID_CLUSTER_NAMESPACE_MESSAGE + " & "
+ InputValidator.INVALID_NAMESPACE_NAMESPACE_MESSAGE));
}

if (!ConfigFileFormat.isValidFormat(appNamespaceDTO.getFormat())) {
throw new BadRequestException(String.format("Invalid namespace format. format = %s", appNamespaceDTO.getFormat()));
}

String operator = appNamespaceDTO.getDataChangeCreatedBy();
if (userService.findByUserId(operator) == null) {
throw new BadRequestException(String.format("Illegal user. user = %s", operator));
}

AppNamespace appNamespace = OpenApiBeanUtils.transformToAppNamespace(appNamespaceDTO);
AppNamespace createdAppNamespace = appNamespaceService.createAppNamespaceInLocal(appNamespace);

publisher.publishEvent(new AppNamespaceCreationEvent(createdAppNamespace));

return OpenApiBeanUtils.transformToOpenAppNamespaceDTO(createdAppNamespace);
}

@RequestMapping(value = "/apps/{appId}/clusters/{clusterName}/namespaces", method = RequestMethod.GET)
@RequestMapping(value = "/openapi/v1/envs/{env}/apps/{appId}/clusters/{clusterName}/namespaces", method = RequestMethod.GET)
public List<OpenNamespaceDTO> findNamespaces(@PathVariable String appId, @PathVariable String env,
@PathVariable String clusterName) {

Expand All @@ -37,7 +93,7 @@ public List<OpenNamespaceDTO> findNamespaces(@PathVariable String appId, @PathVa
.fromString(env), clusterName));
}

@RequestMapping(value = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName:.+}", method = RequestMethod.GET)
@RequestMapping(value = "/openapi/v1/envs/{env}/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName:.+}", method = RequestMethod.GET)
public OpenNamespaceDTO loadNamespace(@PathVariable String appId, @PathVariable String env,
@PathVariable String clusterName, @PathVariable String
namespaceName) {
Expand All @@ -49,7 +105,7 @@ public OpenNamespaceDTO loadNamespace(@PathVariable String appId, @PathVariable
return OpenApiBeanUtils.transformFromNamespaceBO(namespaceBO);
}

@RequestMapping(value = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/lock", method = RequestMethod.GET)
@RequestMapping(value = "/openapi/v1/envs/{env}/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/lock", method = RequestMethod.GET)
public OpenNamespaceLockDTO getNamespaceLock(@PathVariable String appId, @PathVariable String env,
@PathVariable String clusterName, @PathVariable
String namespaceName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ public class ReleaseController {
@PreAuthorize(value = "@consumerPermissionValidator.hasReleaseNamespacePermission(#request, #appId, #namespaceName)")
@RequestMapping(value = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/releases", method = RequestMethod.POST)
public OpenReleaseDTO createRelease(@PathVariable String appId, @PathVariable String env,
@PathVariable String clusterName, @PathVariable String
namespaceName,
@PathVariable String clusterName,
@PathVariable String namespaceName,
@RequestBody NamespaceReleaseModel model,
HttpServletRequest request) {

checkModel(model != null);
RequestPrecondition.checkArguments(!StringUtils.isContainEmpty(model.getReleasedBy(), model
.getReleaseTitle()),
"releaseTitle and releaseBy can not be empty");
"Params(releaseTitle and releasedBy) can not be empty");

if (userService.findByUserId(model.getReleasedBy()) == null) {
throw new BadRequestException("user(releaseBy) not exists");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
import org.springframework.web.bind.annotation.RestController;

import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Objects;

/**
* @author Jason Song([email protected])
Expand Down Expand Up @@ -65,17 +67,27 @@ public ConsumerToken getConsumerTokenByAppId(@RequestParam String appId) {

@PreAuthorize(value = "@permissionValidator.isSuperAdmin()")
@RequestMapping(value = "/consumers/{token}/assign-role", method = RequestMethod.POST)
public List<ConsumerRole> assignRoleToConsumer(@PathVariable String token, @RequestBody NamespaceDTO namespace) {
public List<ConsumerRole> assignNamespaceRoleToConsumer(@PathVariable String token,
@RequestParam String type,
@RequestBody NamespaceDTO namespace) {

String appId = namespace.getAppId();
String namespaceName = namespace.getNamespaceName();

if (StringUtils.isContainEmpty(appId, namespaceName)) {
throw new BadRequestException("Params(AppId、NamespaceName) can not be empty.");
if (StringUtils.isEmpty(appId)) {
throw new BadRequestException("Params(AppId) can not be empty.");
}

return consumerService.assignNamespaceRoleToConsumer(token, appId, namespaceName);
if (Objects.equals("AppRole", type)) {
return Collections.singletonList(consumerService.assignAppRoleToConsumer(token, appId));
} else {
if (StringUtils.isEmpty(namespaceName)) {
throw new BadRequestException("Params(NamespaceName) can not be empty.");
}
return consumerService.assignNamespaceRoleToConsumer(token, appId, namespaceName);
}
}



}
Loading

0 comments on commit c511583

Please sign in to comment.