-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add /registry endpoint to the Cloud Gateway (#3076)
Signed-off-by: alexandr cumarav <[email protected]> Signed-off-by: sj895092 <[email protected]> Signed-off-by: JirkaAichler <[email protected]> Co-authored-by: sj895092 <[email protected]> Co-authored-by: ShobhaJayanna <[email protected]> Co-authored-by: Jiri Aichler <[email protected]> Co-authored-by: JirkaAichler <[email protected]> Co-authored-by: Pavel Jareš <[email protected]>
- Loading branch information
1 parent
f0ac1e7
commit ff8ee9b
Showing
21 changed files
with
973 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...rvice/src/main/java/org/zowe/apiml/cloudgatewayservice/controller/RegistryController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* This program and the accompanying materials are made available under the terms of the | ||
* Eclipse Public License v2.0 which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Copyright Contributors to the Zowe Project. | ||
*/ | ||
|
||
package org.zowe.apiml.cloudgatewayservice.controller; | ||
|
||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.zowe.apiml.cloudgatewayservice.service.CentralApimlInfoMapper; | ||
import org.zowe.apiml.cloudgatewayservice.service.GatewayIndexService; | ||
import org.zowe.apiml.cloudgatewayservice.service.model.ApimlInfo; | ||
import org.zowe.apiml.services.ServiceInfo; | ||
import reactor.core.publisher.Flux; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static com.google.common.base.Strings.emptyToNull; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
@RestController | ||
@Tag(name = "Central Registry") | ||
@RequestMapping(value = "cloud-gateway/api/v1", produces = MediaType.APPLICATION_JSON_VALUE) | ||
@ConditionalOnProperty(value = "apiml.cloudGateway.registry.enabled", havingValue = "true") | ||
public class RegistryController { | ||
|
||
private final CentralApimlInfoMapper centralApimlInfoMapper; | ||
private final GatewayIndexService gatewayIndexService; | ||
|
||
@GetMapping(value = {"/registry", "/registry/{apimlId}"}) | ||
public Flux<ApimlInfo> getServices(@PathVariable(required = false) String apimlId, @RequestParam(name = "apiId", required = false) String apiId, @RequestParam(name = "serviceId", required = false) String serviceId) { | ||
Map<String, List<ServiceInfo>> apimlList = gatewayIndexService.listRegistry(emptyToNull(apimlId), emptyToNull(apiId), emptyToNull(serviceId)); | ||
return Flux.fromIterable(apimlList.entrySet()) | ||
.map(this::buildEntry) | ||
.onErrorContinue(RuntimeException.class, (ex, consumer) -> log.debug("Unexpected mapping error", ex)); | ||
} | ||
|
||
private ApimlInfo buildEntry(Map.Entry<String, List<ServiceInfo>> entry) { | ||
return centralApimlInfoMapper.buildApimlServiceInfo(entry.getKey(), entry.getValue()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
...vice/src/main/java/org/zowe/apiml/cloudgatewayservice/service/CentralApimlInfoMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* This program and the accompanying materials are made available under the terms of the | ||
* Eclipse Public License v2.0 which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Copyright Contributors to the Zowe Project. | ||
*/ | ||
|
||
package org.zowe.apiml.cloudgatewayservice.service; | ||
|
||
import lombok.NonNull; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
import org.zowe.apiml.cloudgatewayservice.service.model.ApimlInfo; | ||
import org.zowe.apiml.cloudgatewayservice.service.model.CentralServiceInfo; | ||
import org.zowe.apiml.config.ApiInfo; | ||
import org.zowe.apiml.services.ServiceInfo; | ||
|
||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
|
||
import static org.springframework.util.CollectionUtils.isEmpty; | ||
|
||
|
||
/** | ||
* Build Central Registry Apiml services DTO | ||
*/ | ||
@Component | ||
public class CentralApimlInfoMapper { | ||
|
||
@Value("${apiml.cloudGateway.registry.metadata-key-allow-list:}") | ||
Set<String> metadataKeysAllowList = new HashSet<>(); | ||
|
||
public ApimlInfo buildApimlServiceInfo(@NonNull String apimlId, List<ServiceInfo> gatewayServices) { | ||
List<CentralServiceInfo> services = Optional.ofNullable(gatewayServices).orElse(Collections.emptyList()).stream() | ||
.filter(Objects::nonNull) | ||
.map(this::mapServices) | ||
.collect(Collectors.toList()); | ||
|
||
return ApimlInfo.builder() | ||
.apimlId(apimlId) | ||
.services(services) | ||
.build(); | ||
} | ||
|
||
private CentralServiceInfo mapServices(ServiceInfo gws) { | ||
return CentralServiceInfo.builder() | ||
.serviceId(gws.getServiceId()) | ||
.status(gws.getStatus()) | ||
.apiId(extractApiId(gws.getApiml())) | ||
.customMetadata(extractMetadata(gws)) | ||
.build(); | ||
} | ||
|
||
private Map<String, String> extractMetadata(ServiceInfo gws) { | ||
return Optional.ofNullable(gws.getInstances()).orElseGet(Collections::emptyMap) | ||
.entrySet().stream() | ||
.findFirst() | ||
.map(Map.Entry::getValue) | ||
.filter(i -> !isEmpty(i.getCustomMetadata())) | ||
.map(ServiceInfo.Instances::getCustomMetadata) | ||
.orElse(Collections.emptyMap()) | ||
.entrySet().stream() | ||
.filter(entry -> metadataKeysAllowList.contains(entry.getKey())) | ||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); | ||
} | ||
|
||
private Set<String> extractApiId(ServiceInfo.Apiml apiml) { | ||
return Optional.ofNullable(apiml) | ||
.map(ServiceInfo.Apiml::getApiInfo) | ||
.orElse(Collections.emptyList()).stream() | ||
.map(ApiInfo::getApiId) | ||
.collect(Collectors.toSet()); | ||
} | ||
} |
Oops, something went wrong.