From 4a01e5da23a4fab498fdea95f36d4f6c28c3d6eb Mon Sep 17 00:00:00 2001 From: castor <58140421+castorqin@users.noreply.github.com> Date: Thu, 10 Aug 2023 16:58:53 +0800 Subject: [PATCH] [INLONG-8663][Manager] Add tenant related OpenAPI (#8665) Co-authored-by: castorqin --- .../manager/pojo/user/LoginUserUtils.java | 11 +++ .../controller/InlongClusterController.java | 6 +- .../openapi/OpenInLongClusterController.java | 49 ++++++++++ .../openapi/OpenInlongTenantController.java | 83 +++++++++++++++++ .../OpenInlongTenantRoleController.java | 92 +++++++++++++++++++ 5 files changed, 237 insertions(+), 4 deletions(-) create mode 100644 inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/controller/openapi/OpenInlongTenantController.java create mode 100644 inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/controller/openapi/OpenInlongTenantRoleController.java diff --git a/inlong-manager/manager-pojo/src/main/java/org/apache/inlong/manager/pojo/user/LoginUserUtils.java b/inlong-manager/manager-pojo/src/main/java/org/apache/inlong/manager/pojo/user/LoginUserUtils.java index 92bf9e994fc..689ee11d588 100644 --- a/inlong-manager/manager-pojo/src/main/java/org/apache/inlong/manager/pojo/user/LoginUserUtils.java +++ b/inlong-manager/manager-pojo/src/main/java/org/apache/inlong/manager/pojo/user/LoginUserUtils.java @@ -17,8 +17,13 @@ package org.apache.inlong.manager.pojo.user; +import org.apache.inlong.manager.common.enums.InlongUserTypeEnum; +import org.apache.inlong.manager.common.enums.TenantUserTypeEnum; + import lombok.extern.slf4j.Slf4j; +import java.util.Set; + /** * User info for login */ @@ -34,6 +39,12 @@ public static UserInfo getLoginUser() { return LOGIN_USER_DETAIL_TL.get(); } + public static boolean isInlongAdminOrTenantAdmin() { + Set roles = LOGIN_USER_DETAIL_TL.get().getRoles(); + return roles.contains(TenantUserTypeEnum.TENANT_ADMIN.name()) || roles.contains( + InlongUserTypeEnum.INLONG_ADMIN.name()); + } + public static void setUserLoginInfo(UserInfo userInfo) { LOGIN_USER_DETAIL_TL.set(userInfo); log.debug("user login: {}", userInfo.getName()); diff --git a/inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/controller/InlongClusterController.java b/inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/controller/InlongClusterController.java index 947b249d7c1..1ce4ee1dd0c 100644 --- a/inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/controller/InlongClusterController.java +++ b/inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/controller/InlongClusterController.java @@ -18,7 +18,6 @@ package org.apache.inlong.manager.web.controller; import org.apache.inlong.manager.common.enums.OperationType; -import org.apache.inlong.manager.common.enums.TenantUserTypeEnum; import org.apache.inlong.manager.common.validation.SaveValidation; import org.apache.inlong.manager.common.validation.UpdateByIdValidation; import org.apache.inlong.manager.common.validation.UpdateByKeyValidation; @@ -95,8 +94,7 @@ public Response getTag(@PathVariable Integer id) { @ApiOperation(value = "List cluster tags") public Response> listTag(@RequestBody ClusterTagPageRequest request) { request.setCurrentUser(LoginUserUtils.getLoginUser().getName()); - request.setIsAdminRole( - LoginUserUtils.getLoginUser().getRoles().contains(TenantUserTypeEnum.TENANT_ADMIN.name())); + request.setIsAdminRole(LoginUserUtils.isInlongAdminOrTenantAdmin()); return Response.success(clusterService.listTag(request)); } @@ -179,7 +177,7 @@ public Response get(@PathVariable Integer id) { public Response> list(@RequestBody ClusterPageRequest request) { request.setCurrentUser(LoginUserUtils.getLoginUser().getName()); request.setIsAdminRole( - LoginUserUtils.getLoginUser().getRoles().contains(TenantUserTypeEnum.TENANT_ADMIN.name())); + LoginUserUtils.isInlongAdminOrTenantAdmin()); return Response.success(clusterService.list(request)); } diff --git a/inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/controller/openapi/OpenInLongClusterController.java b/inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/controller/openapi/OpenInLongClusterController.java index e2ca806b203..f72e66a416a 100644 --- a/inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/controller/openapi/OpenInLongClusterController.java +++ b/inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/controller/openapi/OpenInLongClusterController.java @@ -19,6 +19,7 @@ import org.apache.inlong.manager.common.enums.ErrorCodeEnum; import org.apache.inlong.manager.common.enums.OperationType; +import org.apache.inlong.manager.common.enums.TenantUserTypeEnum; import org.apache.inlong.manager.common.util.Preconditions; import org.apache.inlong.manager.common.validation.SaveValidation; import org.apache.inlong.manager.common.validation.UpdateByIdValidation; @@ -32,8 +33,13 @@ import org.apache.inlong.manager.pojo.cluster.ClusterTagPageRequest; import org.apache.inlong.manager.pojo.cluster.ClusterTagRequest; import org.apache.inlong.manager.pojo.cluster.ClusterTagResponse; +import org.apache.inlong.manager.pojo.cluster.TenantClusterTagInfo; +import org.apache.inlong.manager.pojo.cluster.TenantClusterTagPageRequest; +import org.apache.inlong.manager.pojo.cluster.TenantClusterTagRequest; +import org.apache.inlong.manager.pojo.common.PageResult; import org.apache.inlong.manager.pojo.common.Response; import org.apache.inlong.manager.pojo.user.LoginUserUtils; +import org.apache.inlong.manager.pojo.user.UserRoleCode; import org.apache.inlong.manager.service.cluster.InlongClusterService; import org.apache.inlong.manager.service.operationlog.OperationLog; @@ -41,6 +47,7 @@ import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; +import org.apache.shiro.authz.annotation.RequiresRoles; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.DeleteMapping; @@ -80,6 +87,8 @@ public Response getTag(@PathVariable Integer id) { public Response> listTag(@RequestBody ClusterTagPageRequest request) { Preconditions.expectNotNull(request, ErrorCodeEnum.INVALID_PARAMETER, "request cannot be null"); Preconditions.expectNotNull(LoginUserUtils.getLoginUser(), ErrorCodeEnum.LOGIN_USER_EMPTY); + request.setIsAdminRole( + LoginUserUtils.getLoginUser().getRoles().contains(TenantUserTypeEnum.TENANT_ADMIN.name())); return Response.success(clusterService.listTag(request, LoginUserUtils.getLoginUser())); } @@ -125,6 +134,8 @@ public Response get(@PathVariable Integer id) { public Response> list(@RequestBody ClusterPageRequest request) { Preconditions.expectNotNull(request, ErrorCodeEnum.INVALID_PARAMETER, "request cannot be null"); Preconditions.expectNotNull(LoginUserUtils.getLoginUser(), ErrorCodeEnum.LOGIN_USER_EMPTY); + request.setIsAdminRole( + LoginUserUtils.getLoginUser().getRoles().contains(TenantUserTypeEnum.TENANT_ADMIN.name())); return Response.success(clusterService.list(request, LoginUserUtils.getLoginUser())); } @@ -227,4 +238,42 @@ public Response deleteNode(@PathVariable Integer id) { return Response.success(clusterService.deleteNode(id, LoginUserUtils.getLoginUser())); } + @PostMapping(value = "/cluster/tenant/tag/save") + @ApiOperation(value = "Save tenant cluster tag") + @OperationLog(operation = OperationType.CREATE) + @RequiresRoles(value = UserRoleCode.INLONG_ADMIN) + public Response saveTenantTag( + @Validated(SaveValidation.class) @RequestBody TenantClusterTagRequest request) { + String currentUser = LoginUserUtils.getLoginUser().getName(); + return Response.success(clusterService.saveTenantTag(request, currentUser)); + } + + @PostMapping(value = "/cluster/tenant/tag/list") + @ApiOperation(value = "List tenant cluster tags") + public Response> listTenantTag(@RequestBody TenantClusterTagPageRequest request) { + return Response.success(clusterService.listTenantTag(request)); + } + + @PostMapping(value = "/cluster/tag/listTagByTenantRole") + @ApiOperation(value = "List cluster tags by tenant condition") + public Response> listTagByTenantRole( + @RequestBody TenantClusterTagPageRequest request) { + return Response.success(clusterService.listTagByTenantRole(request)); + } + + @PostMapping(value = "/cluster/listByTenantRole") + @ApiOperation(value = "List cluster by tenant condition") + public Response> listByTenantRole( + @RequestBody ClusterPageRequest request) { + return Response.success(clusterService.listByTenantRole(request)); + } + + @DeleteMapping(value = "/cluster/tenant/tag/delete/{id}") + @ApiOperation(value = "Delete tenant cluster tag by id") + @OperationLog(operation = OperationType.DELETE) + @ApiImplicitParam(name = "id", value = "Cluster tag ID", dataTypeClass = Integer.class, required = true) + @RequiresRoles(value = UserRoleCode.INLONG_ADMIN) + public Response deleteTenantTag(@PathVariable Integer id) { + return Response.success(clusterService.deleteTenantTag(id, LoginUserUtils.getLoginUser().getName())); + } } diff --git a/inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/controller/openapi/OpenInlongTenantController.java b/inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/controller/openapi/OpenInlongTenantController.java new file mode 100644 index 00000000000..6f9bd2a79f1 --- /dev/null +++ b/inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/controller/openapi/OpenInlongTenantController.java @@ -0,0 +1,83 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.inlong.manager.web.controller.openapi; + +import org.apache.inlong.manager.common.enums.OperationType; +import org.apache.inlong.manager.common.validation.UpdateByIdValidation; +import org.apache.inlong.manager.pojo.common.PageResult; +import org.apache.inlong.manager.pojo.common.Response; +import org.apache.inlong.manager.pojo.tenant.InlongTenantInfo; +import org.apache.inlong.manager.pojo.tenant.InlongTenantPageRequest; +import org.apache.inlong.manager.pojo.tenant.InlongTenantRequest; +import org.apache.inlong.manager.pojo.user.LoginUserUtils; +import org.apache.inlong.manager.service.operationlog.OperationLog; +import org.apache.inlong.manager.service.tenant.InlongTenantService; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiImplicitParam; +import io.swagger.annotations.ApiOperation; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.validation.annotation.Validated; +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; + +@RestController +@RequestMapping("/openapi") +@Api(tags = "Open-InlongTenant-API") +public class OpenInlongTenantController { + + @Autowired + private InlongTenantService tenantService; + + @RequestMapping(value = "/tenant/get/{name}", method = RequestMethod.GET) + @ApiOperation(value = "Get inlong tenant by name") + @ApiImplicitParam(name = "name", dataTypeClass = String.class, required = true) + public Response get(@PathVariable String name) { + return Response.success(tenantService.getByName(name)); + } + + @RequestMapping(value = "/tenant/save", method = RequestMethod.POST) + @OperationLog(operation = OperationType.CREATE) + @ApiOperation(value = "Save inlong tenant") + public Response save(@Validated @RequestBody InlongTenantRequest request) { + return Response.success(tenantService.save(request)); + } + + @RequestMapping(value = "/tenant/list", method = RequestMethod.POST) + @ApiOperation(value = "List tenant by paginating") + public Response> listByCondition(@RequestBody InlongTenantPageRequest request) { + return Response.success(tenantService.listByCondition(request, LoginUserUtils.getLoginUser())); + } + + @RequestMapping(value = "/tenant/update", method = RequestMethod.POST) + @OperationLog(operation = OperationType.UPDATE) + @ApiOperation(value = "Update inlong tenant") + public Response update(@Validated(UpdateByIdValidation.class) @RequestBody InlongTenantRequest request) { + return Response.success(tenantService.update(request)); + } + + @RequestMapping(value = "/tenant/delete/{name}", method = RequestMethod.GET) + @ApiOperation(value = "Delete inlong tenant by name") + @ApiImplicitParam(name = "name", dataTypeClass = String.class, required = true) + public Response delete(@PathVariable String name) { + return Response.success(tenantService.delete(name)); + } +} diff --git a/inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/controller/openapi/OpenInlongTenantRoleController.java b/inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/controller/openapi/OpenInlongTenantRoleController.java new file mode 100644 index 00000000000..37f478cc3d8 --- /dev/null +++ b/inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/controller/openapi/OpenInlongTenantRoleController.java @@ -0,0 +1,92 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.inlong.manager.web.controller.openapi; + +import org.apache.inlong.manager.common.enums.OperationType; +import org.apache.inlong.manager.pojo.common.PageResult; +import org.apache.inlong.manager.pojo.common.Response; +import org.apache.inlong.manager.pojo.user.LoginUserUtils; +import org.apache.inlong.manager.pojo.user.TenantRoleInfo; +import org.apache.inlong.manager.pojo.user.TenantRolePageRequest; +import org.apache.inlong.manager.pojo.user.TenantRoleRequest; +import org.apache.inlong.manager.pojo.user.UserRoleCode; +import org.apache.inlong.manager.service.operationlog.OperationLog; +import org.apache.inlong.manager.service.user.TenantRoleService; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiImplicitParam; +import io.swagger.annotations.ApiOperation; +import org.apache.shiro.authz.annotation.Logical; +import org.apache.shiro.authz.annotation.RequiresRoles; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.validation.annotation.Validated; +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; + +@RestController +@RequestMapping("/openapi") +@Api(tags = "Open-InlongTenantRole-API") +public class OpenInlongTenantRoleController { + + @Autowired + private TenantRoleService tenantRoleService; + + @RequestMapping(value = "/role/tenant/get/{id}", method = RequestMethod.GET) + @ApiOperation(value = "Get tenant role by ID") + @ApiImplicitParam(name = "id", dataTypeClass = Integer.class, required = true) + @RequiresRoles(logical = Logical.OR, value = {UserRoleCode.TENANT_ADMIN, UserRoleCode.TENANT_OPERATOR, + UserRoleCode.INLONG_ADMIN}) + public Response get(@PathVariable int id) { + return Response.success(tenantRoleService.get(id)); + } + + @RequestMapping(value = "/role/tenant/save", method = RequestMethod.POST) + @OperationLog(operation = OperationType.CREATE) + @ApiOperation(value = "Save tenant role") + @RequiresRoles(logical = Logical.OR, value = {UserRoleCode.TENANT_ADMIN, UserRoleCode.INLONG_ADMIN}) + public Response save(@Validated @RequestBody TenantRoleRequest request) { + String operator = LoginUserUtils.getLoginUser().getName(); + return Response.success(tenantRoleService.save(request, operator)); + } + + @RequestMapping(value = "/role/tenant/update", method = RequestMethod.POST) + @OperationLog(operation = OperationType.CREATE) + @ApiOperation(value = "Update tenant role") + @RequiresRoles(logical = Logical.OR, value = {UserRoleCode.TENANT_ADMIN, UserRoleCode.INLONG_ADMIN}) + public Response update(@Validated @RequestBody TenantRoleRequest request) { + String operator = LoginUserUtils.getLoginUser().getName(); + return Response.success(tenantRoleService.update(request, operator)); + } + + @RequestMapping(value = "/role/tenant/list", method = RequestMethod.POST) + @ApiOperation(value = "List tenant roles by paginating") + public Response> listByCondition(@RequestBody TenantRolePageRequest request) { + return Response.success(tenantRoleService.listByCondition(request)); + } + + @RequestMapping(value = "/role/tenant/delete/{id}", method = RequestMethod.DELETE) + @ApiOperation(value = "Delete tenant role by ID") + @ApiImplicitParam(name = "id", dataTypeClass = Integer.class, required = true) + @RequiresRoles(logical = Logical.OR, value = {UserRoleCode.TENANT_ADMIN, UserRoleCode.INLONG_ADMIN}) + public Response delete(@PathVariable int id) { + return Response.success(tenantRoleService.delete(id)); + } +}