Skip to content

Commit

Permalink
Provide listDeployedServices service for ISV
Browse files Browse the repository at this point in the history
  • Loading branch information
WangLiNaruto committed Nov 10, 2023
1 parent f2f35b2 commit 0ad0605
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* SPDX-License-Identifier: Apache-2.0
* SPDX-FileCopyrightText: Huawei Inc.
*
*/

package org.eclipse.xpanse.api.controllers;

import static org.eclipse.xpanse.modules.models.security.constant.RoleConstants.ROLE_ISV;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.xpanse.modules.deployment.DeployService;
import org.eclipse.xpanse.modules.models.common.exceptions.UserNotLoggedInException;
import org.eclipse.xpanse.modules.models.service.common.enums.Category;
import org.eclipse.xpanse.modules.models.service.common.enums.Csp;
import org.eclipse.xpanse.modules.models.service.deploy.enums.ServiceDeploymentState;
import org.eclipse.xpanse.modules.models.service.query.ServiceQueryModel;
import org.eclipse.xpanse.modules.models.service.view.ServiceVo;
import org.eclipse.xpanse.modules.security.IdentityProviderManager;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

/**
* REST interface methods for processing OCL.
*/
@Slf4j
@RestController
@RequestMapping("/xpanse")
@CrossOrigin
@Secured({ROLE_ISV})
public class IsvServiceDeployApi {

@Resource
private DeployService deployService;

@Resource
private IdentityProviderManager identityProviderManager;

/**
* List all deployed services by a user of ISV.
*
* @return list of all services deployed by a user.
*/
@Tag(name = "Service", description = "APIs to manage the service instances")
@Operation(description = "List all deployed services by a user.")
@GetMapping(value = "/isv/services",
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
public List<ServiceVo> listDeployedServicesOfIsv(
@Parameter(name = "categoryName", description = "category of the service")
@RequestParam(name = "categoryName", required = false) Category category,
@Parameter(name = "cspName", description = "name of the cloud service provider")
@RequestParam(name = "cspName", required = false) Csp csp,
@Parameter(name = "serviceName", description = "name of the service")
@RequestParam(name = "serviceName", required = false) String serviceName,
@Parameter(name = "serviceVersion", description = "version of the service")
@RequestParam(name = "serviceVersion", required = false) String serviceVersion,
@Parameter(name = "serviceState", description = "deployment state of the service")
@RequestParam(name = "serviceState", required = false)
ServiceDeploymentState serviceState) {
ServiceQueryModel query =
getServiceQueryModel(category, csp, serviceName, serviceVersion, serviceState);
return this.deployService.listDeployedServicesOfIsv(query);
}

private ServiceQueryModel getServiceQueryModel(Category category, Csp csp,
String serviceName, String serviceVersion, ServiceDeploymentState state) {
ServiceQueryModel query = new ServiceQueryModel();
if (Objects.nonNull(category)) {
query.setCategory(category);
}
if (Objects.nonNull(csp)) {
query.setCsp(csp);
}
if (StringUtils.isNotBlank(serviceName)) {
query.setServiceName(serviceName);
}
if (StringUtils.isNotBlank(serviceVersion)) {
query.setServiceVersion(serviceVersion);
}
if (Objects.nonNull(state)) {
query.setServiceState(state);
}
Optional<String> userIdOptional = identityProviderManager.getCurrentLoginUserId();
if (!userIdOptional.isPresent()) {
throw new UserNotLoggedInException("Unable to get current login information");
}
query.setUserId(userIdOptional.get());
return query;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ public class DeployServiceEntity extends CreateModifiedTime {
*/
private String version;

/**
* Namespace of the user who registered service template.
*/
private String namespace;

/**
* The csp of the Service.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class DeployServiceEntityTest {
private final String NAME = "kafka";
private final String CUSTOMER_SERVICE_NAME = "kafka-cluster";
private final String VERSION = "2.0";
private final String NAMESPACE = "defaultUserId";
private final Csp CSP = Csp.HUAWEI;
private final String FLAVOR = "1-zookeeper-with-3-worker-nodes-normal";
private final ServiceDeploymentState SERVICE_STATE = ServiceDeploymentState.DEPLOYING;
Expand All @@ -49,6 +50,7 @@ void setUp() {
test.setName(NAME);
test.setCustomerServiceName(CUSTOMER_SERVICE_NAME);
test.setVersion(VERSION);
test.setNamespace(NAMESPACE);
test.setCsp(CSP);
test.setFlavor(FLAVOR);
test.setServiceDeploymentState(SERVICE_STATE);
Expand All @@ -68,6 +70,7 @@ void testToString() {
+ "name=" + NAME + ", "
+ "customerServiceName=" + CUSTOMER_SERVICE_NAME + ", "
+ "version=" + VERSION + ", "
+ "namespace=" + NAMESPACE + ", "
+ "csp=" + CSP + ", "
+ "flavor=" + FLAVOR + ", "
+ "serviceDeploymentState=" + SERVICE_STATE + ", "
Expand Down Expand Up @@ -140,6 +143,14 @@ void testEqualsAndHashCode() {
Assertions.assertNotEquals(test.hashCode(), test1.hashCode());
Assertions.assertNotEquals(test1.hashCode(), test2.hashCode());

test1.setNamespace(NAMESPACE);
Assertions.assertNotEquals(test, test1);
Assertions.assertNotEquals(test, test2);
Assertions.assertNotEquals(test1, test2);
Assertions.assertNotEquals(test.hashCode(), test1.hashCode());
Assertions.assertNotEquals(test1.hashCode(), test2.hashCode());


test1.setCsp(CSP);
Assertions.assertNotEquals(test, test1);
Assertions.assertNotEquals(test, test2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ public DeployServiceEntity getNewDeployServiceTask(DeployTask deployTask) {
entity.setUserId(deployTask.getDeployRequest().getUserId());
entity.setDeployRequest(deployTask.getDeployRequest());
entity.setDeployResourceList(new ArrayList<>());
entity.setNamespace(deployTask.getNamespace());
return entity;
}

Expand Down Expand Up @@ -160,6 +161,7 @@ public Deployment getDeployHandler(DeployTask deployTask) {
}
encodeDeployVariable(serviceTemplate,
deployTask.getDeployRequest().getServiceRequestProperties());
deployTask.setNamespace(serviceTemplate.getNamespace());
// Set Ocl and CreateRequest
deployTask.setOcl(serviceTemplate.getOcl());
deployTask.getDeployRequest().setOcl(serviceTemplate.getOcl());
Expand Down Expand Up @@ -651,4 +653,21 @@ public void updateServiceStatus(UUID id, ServiceDeploymentState serviceDeploymen
deployServiceEntity.setServiceDeploymentState(serviceDeploymentState);
deployServiceStorage.storeAndFlush(deployServiceEntity);
}

/**
* Use query model to list SV deployment services.
*
* @param query service query model.
* @return serviceVos
*/
public List<ServiceVo> listDeployedServicesOfIsv(ServiceQueryModel query) {
Optional<String> namespace = identityProviderManager.getUserNamespace();
if (namespace.isEmpty()) {
return new ArrayList<>();
}
return deployServiceStorage.listServices(query).stream()
.filter(deployServiceEntity -> namespace.get()
.equals(deployServiceEntity.getNamespace()))
.map(this::convertToServiceVo).toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public class DeployTask {
*/
private UUID id;

/**
* Namespace of the user who registered service template.
*/
private String namespace;

/**
* The Ocl object of the DeployTask.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ void testToString() {
assertNotEquals(test.toString(), null);

String exceptedString = "DeployTask(id=23cc529b-64d9-4875-a2f0-08b415705964, "
+ "namespace=null, "
+ "deployRequest=DeployRequest(super=DeployRequestBase(userId=null, category=null, "
+ "serviceName=null, customerServiceName=null, version=null, region=null, "
+ "csp=null, flavor=null, serviceHostingType=null, ocl=null, serviceRequestProperties=null), id=null),"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.Optional;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.xpanse.modules.models.security.model.CurrentUserInfo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
Expand Down Expand Up @@ -80,4 +81,17 @@ public Optional<String> getCurrentLoginUserId() {
}
}

/**
* Get current login user namespace.
*
* @return current login user namespace.
*/
public Optional<String> getUserNamespace() {
CurrentUserInfo currentUserInfo = getCurrentUserInfo();
if (Objects.isNull(currentUserInfo) || StringUtils.isBlank(
currentUserInfo.getNamespace())) {
return Optional.empty();
}
return Optional.ofNullable(currentUserInfo.getNamespace());
}
}

0 comments on commit 0ad0605

Please sign in to comment.