From f0729835febdb02d33f87da9517aa3dddc6cd19c Mon Sep 17 00:00:00 2001 From: Sandor Molnar Date: Tue, 31 Oct 2023 10:07:31 +0100 Subject: [PATCH] KNOX-2958 - Fixed API samples for certain services Additionally, a general improvement is implemented that adds the missing slash at the beginning of the path element if it was missing from the service definition sample. --- .../services/cm-api/1.0.0/service.xml | 4 +- .../services/oozie/5.0.0/service.xml | 4 +- .../services/webhdfs/2.4.0/service.xml | 6 +-- .../services/yarn-rm/2.5.0/service.xml | 4 +- .../service/metadata/ServiceModel.java | 3 +- .../service/metadata/ServiceModelTest.java | 37 +++++++++++++++++++ 6 files changed, 48 insertions(+), 10 deletions(-) diff --git a/gateway-service-definitions/src/main/resources/services/cm-api/1.0.0/service.xml b/gateway-service-definitions/src/main/resources/services/cm-api/1.0.0/service.xml index bf776307e0..5ce63bd47a 100644 --- a/gateway-service-definitions/src/main/resources/services/cm-api/1.0.0/service.xml +++ b/gateway-service-definitions/src/main/resources/services/cm-api/1.0.0/service.xml @@ -30,12 +30,12 @@ Fetch all CM-managed clusters GET - clusters + /v41/clusters Fetches HDFS service details from cluster named 'c1' GET - clusters/c1/services/HDFS + /v41/clusters/c1/services/HDFS You can checkout CM's API (v41) document here diff --git a/gateway-service-definitions/src/main/resources/services/oozie/5.0.0/service.xml b/gateway-service-definitions/src/main/resources/services/oozie/5.0.0/service.xml index e43d2ad6fc..6b940c33ac 100644 --- a/gateway-service-definitions/src/main/resources/services/oozie/5.0.0/service.xml +++ b/gateway-service-definitions/src/main/resources/services/oozie/5.0.0/service.xml @@ -25,12 +25,12 @@ Fetch the supported Oozie protocol versions by the server GET - oozie/versions + /oozie/versions Fetch the system status GET - oozie/v1/admin/status + /oozie/v1/admin/status You may check out Apache Oozie's REST API documentation here diff --git a/gateway-service-definitions/src/main/resources/services/webhdfs/2.4.0/service.xml b/gateway-service-definitions/src/main/resources/services/webhdfs/2.4.0/service.xml index 48a7d416f9..b1dde9b42d 100644 --- a/gateway-service-definitions/src/main/resources/services/webhdfs/2.4.0/service.xml +++ b/gateway-service-definitions/src/main/resources/services/webhdfs/2.4.0/service.xml @@ -25,17 +25,17 @@ List all files under 'testPath' GET - v1/testPath?op=LISTSTATUS + /v1/testPath?op=LISTSTATUS Rename a File/Directory under PUT - v1/testPath/testFile?op=RENAME&destination=testPath/renamedFile + /v1/testPath/testFile?op=RENAME&destination=testPath/renamedFile Get Home Directory GET - v1/?op=GETHOMEDIRECTORY + /v1/?op=GETHOMEDIRECTORY You may check out Apache WebHDFS's REST API documentation here diff --git a/gateway-service-definitions/src/main/resources/services/yarn-rm/2.5.0/service.xml b/gateway-service-definitions/src/main/resources/services/yarn-rm/2.5.0/service.xml index 76bbb185ed..07436c1edd 100644 --- a/gateway-service-definitions/src/main/resources/services/yarn-rm/2.5.0/service.xml +++ b/gateway-service-definitions/src/main/resources/services/yarn-rm/2.5.0/service.xml @@ -25,12 +25,12 @@ Fetch cluster info GET - ws/v1/cluster/info + /ws/v1/cluster/info Fetch cluster metrics GET - ws/v1/cluster/metrics + /ws/v1/cluster/metrics You may check out Apache YARN Resource Manager's REST API documentation here diff --git a/gateway-service-metadata/src/main/java/org/apache/knox/gateway/service/metadata/ServiceModel.java b/gateway-service-metadata/src/main/java/org/apache/knox/gateway/service/metadata/ServiceModel.java index 3d9c57adf8..d4b08d0882 100644 --- a/gateway-service-metadata/src/main/java/org/apache/knox/gateway/service/metadata/ServiceModel.java +++ b/gateway-service-metadata/src/main/java/org/apache/knox/gateway/service/metadata/ServiceModel.java @@ -178,7 +178,8 @@ public List getSamples() { resolvedSample.setValue(sample.getValue()); } else { final String method = StringUtils.isBlank(sample.getMethod()) ? "GET" : sample.getMethod(); - resolvedSample.setValue(String.format(Locale.ROOT, CURL_SAMPLE_TEMPLATE, method, getServiceUrl(), sample.getPath())); + final String path = sample.getPath().startsWith("/") ? sample.getPath() : ("/" + sample.getPath()); + resolvedSample.setValue(String.format(Locale.ROOT, CURL_SAMPLE_TEMPLATE, method, getServiceUrl(), path)); } samples.add(resolvedSample); }); diff --git a/gateway-service-metadata/src/test/java/org/apache/knox/gateway/service/metadata/ServiceModelTest.java b/gateway-service-metadata/src/test/java/org/apache/knox/gateway/service/metadata/ServiceModelTest.java index 95ff5e6f30..4058ee2f60 100644 --- a/gateway-service-metadata/src/test/java/org/apache/knox/gateway/service/metadata/ServiceModelTest.java +++ b/gateway-service-metadata/src/test/java/org/apache/knox/gateway/service/metadata/ServiceModelTest.java @@ -21,12 +21,17 @@ import static org.apache.knox.gateway.service.metadata.ServiceModel.HIVE_SERVICE_URL_TEMPLATE; import static org.apache.knox.gateway.service.metadata.ServiceModel.SERVICE_URL_TEMPLATE; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import java.util.Arrays; +import java.util.List; import java.util.Locale; import javax.servlet.http.HttpServletRequest; import org.apache.knox.gateway.service.definition.Metadata; +import org.apache.knox.gateway.service.definition.Sample; import org.apache.knox.gateway.topology.Service; import org.apache.knox.gateway.topology.Version; import org.easymock.EasyMock; @@ -231,4 +236,36 @@ public void shouldReturnServiceUrlWithSchemeHostAndPortPlaceholders() throws Exc assertEquals(String.format(Locale.ROOT, SERVICE_URL_TEMPLATE, SERVER_SCHEME, SERVER_NAME, SERVER_PORT, gatewayPath, topologyName, context.replace("{{SCHEME}}", "https").replace("{{HOST}}", "localhost").replace("{{PORT}}", "5555")), serviceModel.getServiceUrl()); } + + @Test + public void shouldReturnProperSampleValueEvenIfPathDoesNotStartWithSlash() throws Exception { + final ServiceModel serviceModel = new ServiceModel(); + final String gatewayPath = "gateway"; + final String topologyName = "sandbox"; + serviceModel.setGatewayPath(gatewayPath); + serviceModel.setTopologyName(topologyName); + serviceModel.setRequest(setUpHttpRequestMock()); + + final Metadata metadata = EasyMock.createNiceMock(Metadata.class); + final Sample sampleStartsWithSlash = new Sample(); + sampleStartsWithSlash.setDescription("sampleStartsWithSlash"); + sampleStartsWithSlash.setMethod("PUT"); + sampleStartsWithSlash.setPath("/sampleStartsWithSlashPath/operation"); + final Sample sampleStartsWithoutSlash = new Sample(); + sampleStartsWithoutSlash.setDescription("sampleStartsWithoutSlash"); + sampleStartsWithoutSlash.setPath("sampleStartsWithoutSlashPath/operation"); // note the missing starting slash + EasyMock.expect(metadata.getSamples()).andReturn(Arrays.asList(sampleStartsWithSlash, sampleStartsWithoutSlash)).anyTimes(); + serviceModel.setServiceMetadata(metadata); + + final String context = "/testContext"; + EasyMock.expect(metadata.getContext()).andReturn(context).anyTimes(); + EasyMock.replay(metadata); + final List samples = serviceModel.getSamples(); + assertNotNull(samples); + assertFalse(samples.isEmpty()); + assertEquals(2, samples.size()); + + assertEquals(samples.get(0).getValue(), "curl -iv -X PUT \"https://localhost:8443/gateway/sandbox/testContext/sampleStartsWithSlashPath/operation\""); + assertEquals(samples.get(1).getValue(), "curl -iv -X GET \"https://localhost:8443/gateway/sandbox/testContext/sampleStartsWithoutSlashPath/operation\""); + } }