Skip to content

Commit

Permalink
[INLONG-8663][Manager] Add tenant related OpenAPI (#8665)
Browse files Browse the repository at this point in the history
Co-authored-by: castorqin <[email protected]>
  • Loading branch information
castorqin and castorqin authored Aug 10, 2023
1 parent de95116 commit 4a01e5d
Show file tree
Hide file tree
Showing 5 changed files with 237 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -34,6 +39,12 @@ public static UserInfo getLoginUser() {
return LOGIN_USER_DETAIL_TL.get();
}

public static boolean isInlongAdminOrTenantAdmin() {
Set<String> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -95,8 +94,7 @@ public Response<ClusterTagResponse> getTag(@PathVariable Integer id) {
@ApiOperation(value = "List cluster tags")
public Response<PageResult<ClusterTagResponse>> 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));
}

Expand Down Expand Up @@ -179,7 +177,7 @@ public Response<ClusterInfo> get(@PathVariable Integer id) {
public Response<PageResult<ClusterInfo>> 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));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,15 +33,21 @@
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;

import io.swagger.annotations.Api;
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;
Expand Down Expand Up @@ -80,6 +87,8 @@ public Response<ClusterTagResponse> getTag(@PathVariable Integer id) {
public Response<List<ClusterTagResponse>> 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()));
}

Expand Down Expand Up @@ -125,6 +134,8 @@ public Response<ClusterInfo> get(@PathVariable Integer id) {
public Response<List<ClusterInfo>> 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()));
}

Expand Down Expand Up @@ -227,4 +238,42 @@ public Response<Boolean> 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<Integer> 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<PageResult<TenantClusterTagInfo>> 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<PageResult<ClusterTagResponse>> listTagByTenantRole(
@RequestBody TenantClusterTagPageRequest request) {
return Response.success(clusterService.listTagByTenantRole(request));
}

@PostMapping(value = "/cluster/listByTenantRole")
@ApiOperation(value = "List cluster by tenant condition")
public Response<PageResult<ClusterInfo>> 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<Boolean> deleteTenantTag(@PathVariable Integer id) {
return Response.success(clusterService.deleteTenantTag(id, LoginUserUtils.getLoginUser().getName()));
}
}
Original file line number Diff line number Diff line change
@@ -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<InlongTenantInfo> 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<Integer> 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<PageResult<InlongTenantInfo>> 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<Boolean> 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<Boolean> delete(@PathVariable String name) {
return Response.success(tenantService.delete(name));
}
}
Original file line number Diff line number Diff line change
@@ -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<TenantRoleInfo> 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<Integer> 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<Boolean> 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<PageResult<TenantRoleInfo>> 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<Boolean> delete(@PathVariable int id) {
return Response.success(tenantRoleService.delete(id));
}
}

0 comments on commit 4a01e5d

Please sign in to comment.