Skip to content

Commit

Permalink
add apiCredentialController and JupyterController (#31)
Browse files Browse the repository at this point in the history
* add apiCredentialController and JupyterController

* add queryTODOList
  • Loading branch information
cyjseagull authored Aug 30, 2024
1 parent e372119 commit 09ed567
Show file tree
Hide file tree
Showing 15 changed files with 526 additions and 6 deletions.
1 change: 1 addition & 0 deletions wedpr-adm/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ dependencies {
compile project(":wedpr-components-agency")
compile project(":wedpr-components-security")
compile project(":wedpr-components-report")
compile project(":wedpr-components-jupyter-intergration")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*
* Copyright 2017-2025 [webank-wedpr]
*
* Licensed 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 com.webank.wedpr.adm.controller;

import com.github.pagehelper.PageInfo;
import com.webank.wedpr.adm.controller.request.CredentialRequest;
import com.webank.wedpr.adm.controller.response.CredentialResponse;
import com.webank.wedpr.components.api.credential.dao.ApiCredentialDO;
import com.webank.wedpr.components.api.credential.service.ApiCredentialService;
import com.webank.wedpr.components.mybatis.PageHelperWrapper;
import com.webank.wedpr.components.token.auth.TokenUtils;
import com.webank.wedpr.components.token.auth.model.UserToken;
import com.webank.wedpr.core.utils.Constant;
import com.webank.wedpr.core.utils.WeDPRResponse;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(
path = Constant.WEDPR_API_PREFIX + "/credential",
produces = {"application/json"})
public class ApiCredentialController {
private static final Logger logger = LoggerFactory.getLogger(ApiCredentialController.class);

@Autowired private ApiCredentialService apiCredentialService;

/**
* apply for new credential
*
* @param credentialDO the basic information for the credential
* @param request the request(for obtain user information)
* @return the id
*/
@PostMapping("/applyFor")
public WeDPRResponse applyFor(
@RequestBody ApiCredentialDO credentialDO, HttpServletRequest request) {
try {
UserToken userToken = TokenUtils.getLoginUser(request);
String id =
apiCredentialService.applyForCredential(userToken.getUsername(), credentialDO);
WeDPRResponse response =
new WeDPRResponse(Constant.WEDPR_SUCCESS, Constant.WEDPR_SUCCESS_MSG);
response.setData(id);
return response;
} catch (Exception e) {
logger.warn(
"applyFor api credential exception, credential: {}, error: ",
credentialDO.toString(),
e);
return new WeDPRResponse(
Constant.WEDPR_FAILED, "apply for credential failed for " + e.getMessage());
}
}

/**
* query credentials by condition
*
* @param credentialRequest the condition
* @param request the request(for obtain user information)
* @return the queried credentials
*/
@PostMapping("/query")
public WeDPRResponse queryCredentials(
@RequestBody CredentialRequest credentialRequest, HttpServletRequest request) {
try (PageHelperWrapper pageHelperWrapper = new PageHelperWrapper(credentialRequest)) {
UserToken userToken = TokenUtils.getLoginUser(request);
List<ApiCredentialDO> credentialList =
apiCredentialService.queryCredentials(
userToken.getUsername(), credentialRequest.getCondition());
WeDPRResponse response =
new WeDPRResponse(Constant.WEDPR_SUCCESS, Constant.WEDPR_SUCCESS_MSG);
response.setData(
new CredentialResponse(
credentialList,
new PageInfo<ApiCredentialDO>(credentialList).getTotal()));
return response;
} catch (Exception e) {
logger.warn(
"queryCredentials exception, request: {}, error: ",
credentialRequest.toString(),
e);
return new WeDPRResponse(
Constant.WEDPR_FAILED,
"query credentials failed for "
+ e.getMessage()
+ ", request detail: "
+ credentialRequest.toString());
}
}

/**
* delete credentials by condition
*
* @param condition the condition to delete the credential
* @param request the request(for obtain user information)
* @return success/failed
*/
@PostMapping("/delete")
public WeDPRResponse deleteCredential(
@RequestBody ApiCredentialDO condition, HttpServletRequest request) {
try {
UserToken userToken = TokenUtils.getLoginUser(request);
boolean success =
apiCredentialService.deleteCredential(userToken.getUsername(), condition);
WeDPRResponse response =
new WeDPRResponse(Constant.WEDPR_SUCCESS, Constant.WEDPR_SUCCESS_MSG);
response.setData(success);
return response;
} catch (Exception e) {
logger.warn(
"deleteCredential exception, condition: {}, error: ", condition.toString(), e);
return new WeDPRResponse(
Constant.WEDPR_FAILED,
"deleteCredential failed for "
+ e.getMessage()
+ ", condition: "
+ condition.toString());
}
}

/**
* update credentials by condition
*
* @param condition the condition to update the credential
* @param request the request(for obtain user information)
* @return success/failed
*/
@PostMapping("/update")
public WeDPRResponse updateCredentials(
@RequestBody ApiCredentialDO condition, HttpServletRequest request) {
try {
UserToken userToken = TokenUtils.getLoginUser(request);
boolean success =
apiCredentialService.updateCredential(userToken.getUsername(), condition);
WeDPRResponse response =
new WeDPRResponse(Constant.WEDPR_SUCCESS, Constant.WEDPR_SUCCESS_MSG);
response.setData(success);
return response;
} catch (Exception e) {
logger.warn(
"updateCredentials exception, condition: {}, error: ", condition.toString(), e);
return new WeDPRResponse(
Constant.WEDPR_FAILED,
"updateCredentials failed for "
+ e.getMessage()
+ ", condition: "
+ condition.toString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.webank.wedpr.components.authorization.model.*;
import com.webank.wedpr.components.authorization.service.AuthorizationService;
import com.webank.wedpr.components.token.auth.TokenUtils;
import com.webank.wedpr.components.token.auth.model.UserToken;
import com.webank.wedpr.core.config.WeDPRCommonConfig;
import com.webank.wedpr.core.utils.Constant;
import com.webank.wedpr.core.utils.PageRequest;
Expand Down Expand Up @@ -67,6 +68,32 @@ public WeDPRResponse updateAuth(
Constant.WEDPR_FAILED, "updateAuth error for " + e.getMessage());
}
}

/**
* query the todoList
*
* @param condition the condition
* @param request the request to obtain the user information
* @return the queried result
*/
@PostMapping("/queryTODOList")
public WeDPRResponse queryTODOList(
@RequestBody SingleAuthRequest condition, HttpServletRequest request) {
try {
UserToken userToken = TokenUtils.getLoginUser(request);
AuthListResponse authList =
this.authorizationService.queryTODOList(userToken.getUsername(), condition);
WeDPRResponse response =
new WeDPRResponse(Constant.WEDPR_SUCCESS, Constant.WEDPR_SUCCESS_MSG);
response.setData(authList);
return response;
} catch (Exception e) {
logger.warn("queryTODOList error, condition: {}, error: ", condition.toString(), e);
return new WeDPRResponse(
Constant.WEDPR_FAILED, "queryTODOList failed for " + e.getMessage());
}
}

// query the authorization-meta-information according to given condition
@PostMapping("/queryAuthList")
public WeDPRResponse queryAuthList(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
/*
* Copyright 2017-2025 [webank-wedpr]
*
* Licensed 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 com.webank.wedpr.adm.controller;

import com.github.pagehelper.PageInfo;
import com.webank.wedpr.adm.controller.request.JupyterRequest;
import com.webank.wedpr.adm.controller.response.JupyterResponse;
import com.webank.wedpr.components.integration.jupyter.dao.JupyterInfoDO;
import com.webank.wedpr.components.integration.jupyter.service.JupyterService;
import com.webank.wedpr.components.mybatis.PageHelperWrapper;
import com.webank.wedpr.components.token.auth.TokenUtils;
import com.webank.wedpr.components.token.auth.model.UserToken;
import com.webank.wedpr.core.config.WeDPRCommonConfig;
import com.webank.wedpr.core.utils.Constant;
import com.webank.wedpr.core.utils.WeDPRResponse;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(
path = Constant.WEDPR_API_PREFIX + "/jupyter",
produces = {"application/json"})
public class JupyterController {
private static final Logger logger = LoggerFactory.getLogger(JupyterController.class);

@Autowired private JupyterService jupyterService;

/**
* allocate the jupyter
*
* @param request the request to obtain user information
* @return the allocated jupyterID
*/
@GetMapping("/allocate")
public WeDPRResponse allocate(HttpServletRequest request) {
try {
UserToken userToken = TokenUtils.getLoginUser(request);
String jupyterID =
this.jupyterService.allocate(
userToken.getUsername(), WeDPRCommonConfig.getAgency());
WeDPRResponse response =
new WeDPRResponse(Constant.WEDPR_SUCCESS, Constant.WEDPR_SUCCESS_MSG);
response.setData(jupyterID);
return response;
} catch (Exception e) {
logger.warn("allocate jupyter failed, error: ", e);
return new WeDPRResponse(
Constant.WEDPR_FAILED, "allocate jupyter failed for " + e.getMessage());
}
}

/**
* query jupyter by condition
*
* @param queryRequest the query request
* @param request the request to obtain user information
* @return
*/
@PostMapping("/query")
public WeDPRResponse queryJupyters(
@RequestBody JupyterRequest queryRequest, HttpServletRequest request) {
try (PageHelperWrapper pageHelperWrapper = new PageHelperWrapper(queryRequest)) {
UserToken userToken = TokenUtils.getLoginUser(request);
List<JupyterInfoDO> result =
this.jupyterService.queryJupyters(
userToken.isAdmin(),
userToken.getUsername(),
queryRequest.getCondition());
WeDPRResponse response =
new WeDPRResponse(Constant.WEDPR_SUCCESS, Constant.WEDPR_SUCCESS_MSG);
response.setData(
new JupyterResponse(new PageInfo<JupyterInfoDO>(result).getTotal(), result));
return response;
} catch (Exception e) {
logger.warn(
"queryJupyters jupyter failed, condition: {}, error: ",
queryRequest.toString(),
e);
return new WeDPRResponse(
Constant.WEDPR_FAILED,
"queryJupyters failed for "
+ e.getMessage()
+ ", requestDetail: "
+ queryRequest.toString());
}
}

/**
* open jupyter
*
* @param id the jupyter id
* @param request the request to obtain user information
* @return the opened jupyter information
*/
@GetMapping("/open")
public WeDPRResponse open(@RequestParam String id, HttpServletRequest request) {
try {
UserToken userToken = TokenUtils.getLoginUser(request);
JupyterInfoDO jupyterInfo = this.jupyterService.open(userToken.getUsername(), id);
WeDPRResponse response =
new WeDPRResponse(Constant.WEDPR_SUCCESS, Constant.WEDPR_SUCCESS_MSG);
response.setData(jupyterInfo);
return response;
} catch (Exception e) {
logger.warn("open jupyter failed, id: {}, error: ", id, e);
return new WeDPRResponse(
Constant.WEDPR_FAILED,
"open failed for " + id + " failed, reason:" + e.getMessage());
}
}

/**
* close the jupyter
*
* @param id the jupyter id
* @param request the request to obtain user information
* @return the closed jupyter information
*/
@GetMapping("/close")
public WeDPRResponse close(@RequestParam String id, HttpServletRequest request) {
try {
UserToken userToken = TokenUtils.getLoginUser(request);
JupyterInfoDO jupyterInfo = this.jupyterService.close(userToken.getUsername(), id);
WeDPRResponse response =
new WeDPRResponse(Constant.WEDPR_SUCCESS, Constant.WEDPR_SUCCESS_MSG);
response.setData(jupyterInfo);
return response;
} catch (Exception e) {
logger.warn("close jupyter failed, id: {}, error: ", id, e);
return new WeDPRResponse(
Constant.WEDPR_FAILED,
"close failed for " + id + " failed, reason:" + e.getMessage());
}
}

/**
* destroy the jupyter
*
* @param id the jupyter id
* @param request the request to obtain user information
* @return
*/
@DeleteMapping("/destroy")
public WeDPRResponse destroy(@RequestParam String id, HttpServletRequest request) {
try {
UserToken userToken = TokenUtils.getLoginUser(request);
boolean success =
this.jupyterService.destroy(userToken.isAdmin(), userToken.getUsername(), id);
WeDPRResponse response =
new WeDPRResponse(Constant.WEDPR_SUCCESS, Constant.WEDPR_SUCCESS_MSG);
response.setData(success);
return response;
} catch (Exception e) {
logger.warn("destroy jupyter failed, id: {}, error: ", id, e);
return new WeDPRResponse(
Constant.WEDPR_FAILED,
"destroy failed for " + id + " failed, reason:" + e.getMessage());
}
}
}
Loading

0 comments on commit 09ed567

Please sign in to comment.