-
Notifications
You must be signed in to change notification settings - Fork 251
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KNOX-3002 - KnoxCLI command for generating descriptor for a role type…
… from a list of hosts (#835)
- Loading branch information
Showing
4 changed files
with
309 additions
and
4 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
gateway-server/src/main/java/org/apache/knox/gateway/util/DescriptorGenerator.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,84 @@ | ||
/* | ||
* 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.knox.gateway.util; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.apache.commons.io.FilenameUtils; | ||
import org.apache.knox.gateway.model.DescriptorConfiguration; | ||
import org.apache.knox.gateway.model.Topology; | ||
|
||
public class DescriptorGenerator { | ||
private static final ObjectMapper mapper = new ObjectMapper(); | ||
private final String descriptorName; | ||
private final String providerName; | ||
private final String serviceName; | ||
private final ServiceUrls serviceUrls; | ||
private final Map<String, String> params; | ||
|
||
static { | ||
/* skip printing out null fields */ | ||
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); | ||
} | ||
|
||
public DescriptorGenerator(String descriptorName, String providerName, String serviceName, ServiceUrls serviceUrls, Map<String, String> params) { | ||
this.descriptorName = descriptorName; | ||
this.providerName = providerName; | ||
this.serviceName = serviceName.toUpperCase(Locale.ROOT); | ||
this.serviceUrls = serviceUrls; | ||
this.params = params; | ||
} | ||
|
||
public void saveDescriptor(File outputDir, boolean forceOverwrite) { | ||
File outputFile = new File(outputDir, descriptorName); | ||
if (outputFile.exists() && !forceOverwrite) { | ||
throw new IllegalArgumentException(outputFile + " already exists"); | ||
} | ||
DescriptorConfiguration descriptor = new DescriptorConfiguration(); | ||
descriptor.setName(FilenameUtils.removeExtension(descriptorName)); | ||
descriptor.setProviderConfig(FilenameUtils.removeExtension(providerName)); | ||
Topology.Service service = new Topology.Service(); | ||
service.setRole(serviceName); | ||
service.setUrls(serviceUrls.toList()); | ||
setParams(service, params); | ||
descriptor.setServices(Arrays.asList(service)); | ||
try { | ||
mapper.writerWithDefaultPrettyPrinter() | ||
.writeValue(outputFile, descriptor); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
} | ||
|
||
private void setParams(Topology.Service service, Map<String, String> params) { | ||
List<Topology.Param> paramList = new ArrayList<>(); | ||
for (Map.Entry<String, String> each : params.entrySet()) { | ||
paramList.add(new Topology.Param(each.getKey(), each.getValue())); | ||
} | ||
service.setParams(paramList); | ||
} | ||
} |
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
gateway-server/src/main/java/org/apache/knox/gateway/util/ServiceUrls.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 @@ | ||
/* | ||
* 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.knox.gateway.util; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.nio.charset.Charset; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import org.apache.commons.io.FileUtils; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
public class ServiceUrls { | ||
private final List<String> urls; | ||
|
||
public static ServiceUrls fromFile(String urlsFilePath) { | ||
return fromFile(new File(urlsFilePath)); | ||
} | ||
|
||
public static ServiceUrls fromFile(File urlsFilePath) { | ||
try { | ||
List<String> lines = FileUtils.readLines(urlsFilePath, Charset.defaultCharset()).stream() | ||
.map(String::trim) | ||
.filter(StringUtils::isNotBlank) | ||
.collect(Collectors.toList()); | ||
return new ServiceUrls(lines); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
} | ||
|
||
public ServiceUrls(List<String> urls) { | ||
this.urls = urls; | ||
} | ||
|
||
public List<String> toList() { | ||
return Collections.unmodifiableList(urls); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
gateway-server/src/test/java/org/apache/knox/gateway/util/DescriptorGeneratorTest.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,81 @@ | ||
/* | ||
* 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.knox.gateway.util; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.io.File; | ||
import java.nio.charset.Charset; | ||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.apache.commons.io.FileUtils; | ||
import org.apache.commons.io.FilenameUtils; | ||
import org.apache.knox.gateway.model.DescriptorConfiguration; | ||
import org.apache.knox.gateway.model.Topology; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.TemporaryFolder; | ||
|
||
public class DescriptorGeneratorTest { | ||
private static final ObjectMapper mapper = new ObjectMapper(); | ||
private static final String TEST_DESC_1 = "test_desc1.json"; | ||
private static final String TEST_PROV_1 = "test_prov1.json"; | ||
private static final String IMPALA_UI = "IMPALAUI"; | ||
private static final List<String> URLS = | ||
Arrays.asList("http://amagyar-1.test.site:25000/", "http://amagyar-2.test.site:25000"); | ||
|
||
private static final Map<String,String> PARAMS = new HashMap<>(); | ||
static { PARAMS.put("KEY_1", "VAL_1"); } | ||
|
||
@Rule | ||
public TemporaryFolder folder= new TemporaryFolder(); | ||
|
||
@Test | ||
public void testCreateDescriptor() throws Exception { | ||
DescriptorGenerator generator = new DescriptorGenerator(TEST_DESC_1, TEST_PROV_1, IMPALA_UI, new ServiceUrls(URLS), PARAMS); | ||
File outputDir = folder.newFolder().getAbsoluteFile(); | ||
File outputFile = new File(outputDir, TEST_DESC_1); | ||
generator.saveDescriptor(outputDir, false); | ||
System.out.println(FileUtils.readFileToString(outputFile, Charset.defaultCharset())); | ||
DescriptorConfiguration result = mapper | ||
.readerFor(DescriptorConfiguration.class) | ||
.readValue(outputFile); | ||
assertEquals(FilenameUtils.removeExtension(TEST_PROV_1), result.getProviderConfig()); | ||
assertEquals(FilenameUtils.removeExtension(TEST_DESC_1), result.getName()); | ||
assertEquals(1, result.getServices().size()); | ||
Topology.Service service = result.getServices().get(0); | ||
assertEquals(IMPALA_UI, service.getRole()); | ||
assertEquals(URLS, service.getUrls()); | ||
assertEquals(1, service.getParams().size()); | ||
assertEquals("KEY_1", service.getParams().get(0).getName()); | ||
assertEquals("VAL_1", service.getParams().get(0).getValue()); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void testOutputAlreadyExists() throws Exception { | ||
DescriptorGenerator generator = new DescriptorGenerator(TEST_DESC_1, TEST_PROV_1, IMPALA_UI, new ServiceUrls(URLS), PARAMS); | ||
File outputDir = folder.newFolder().getAbsoluteFile(); | ||
File outputFile = new File(outputDir, TEST_DESC_1); | ||
outputFile.createNewFile(); | ||
generator.saveDescriptor(outputDir, false); | ||
} | ||
} |