Skip to content

Commit

Permalink
chore(plugins): fix plugin properties
Browse files Browse the repository at this point in the history
  • Loading branch information
link108 committed Aug 14, 2019
1 parent 55dd6b6 commit 9c3cdc4
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.google.common.annotations.Beta;
import com.netflix.spinnaker.kork.plugins.spring.configs.PluginConfiguration;
import com.netflix.spinnaker.kork.plugins.spring.configs.PluginProperties;
import com.netflix.spinnaker.kork.plugins.spring.configs.PluginPropertyDetails;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
Expand Down Expand Up @@ -66,8 +69,8 @@ public void loadPlugins(Class source) {
return;
}
try (InputStream inputStream = openFromFile()) {
PluginProperties props = parsePluginConfigs(inputStream);
List<PluginProperties.PluginConfiguration> pluginConfigurations = getEnabledJars(props);
PluginPropertyDetails props = parsePluginConfigs(inputStream);
List<PluginConfiguration> pluginConfigurations = getEnabledJars(props);
logPlugins(pluginConfigurations);
urls = getJarPathsFromPluginConfigurations(pluginConfigurations, props.downloadingEnabled);
} catch (IOException e) {
Expand All @@ -85,10 +88,12 @@ public void loadPlugins(Class source) {
* @param inputStream The list of plugins
* @return PluginProperties
*/
private PluginProperties parsePluginConfigs(InputStream inputStream) {
private PluginPropertyDetails parsePluginConfigs(InputStream inputStream) {
ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
try {
PluginProperties pluginConfigs = objectMapper.readValue(inputStream, PluginProperties.class);
PluginProperties pluginProperties =
objectMapper.readValue(inputStream, PluginProperties.class);
PluginPropertyDetails pluginConfigs = pluginProperties.getPluginsPropertyDetails();
pluginConfigs.validate();
return pluginConfigs;
} catch (IOException e) {
Expand All @@ -113,12 +118,11 @@ private boolean checkFileExists() {
/**
* This method returns a list of Plugin Configurations that are enabled
*
* @param pluginProperties Plugin configs, deserialized
* @param pluginPropertyDetails Plugin configs, deserialized
* @return List<PluginConfiguration> List of pluginConfigurations where enabled is true
*/
private List<PluginProperties.PluginConfiguration> getEnabledJars(
PluginProperties pluginProperties) {
return pluginProperties.pluginConfigurationList.stream()
private List<PluginConfiguration> getEnabledJars(PluginPropertyDetails pluginPropertyDetails) {
return pluginPropertyDetails.getPluginConfigurations().stream()
.filter(p -> p.enabled == true)
.collect(Collectors.toList());
}
Expand All @@ -128,9 +132,9 @@ private List<PluginProperties.PluginConfiguration> getEnabledJars(
* @return List<URL> List of paths to jars, where enabled is true
*/
private URL[] getJarPathsFromPluginConfigurations(
List<PluginProperties.PluginConfiguration> pluginConfigurations, boolean downloadingEnabled) {
List<PluginConfiguration> pluginConfigurations, boolean downloadingEnabled) {
return pluginConfigurations.stream()
.map(PluginProperties.PluginConfiguration::getJars)
.map(PluginConfiguration::getJars)
.flatMap(Collection::stream)
.map(s -> convertToUrl(s, downloadingEnabled))
.distinct()
Expand All @@ -145,9 +149,7 @@ private URL convertToUrl(String jarLocation, boolean downloadingEnabled) {
try {
if (jarLocation.startsWith("/")) {
return Paths.get(jarLocation).toUri().toURL();
} else if (jarLocation.startsWith("file://")) {
return new URL(jarLocation);
} else if (downloadingEnabled) {
} else if (jarLocation.startsWith("file://") || downloadingEnabled) {
return new URL(jarLocation);
} else {
throw new MalformedPluginConfigurationException(
Expand All @@ -163,9 +165,9 @@ private URL convertToUrl(String jarLocation, boolean downloadingEnabled) {
*
* @param pluginConfigurations
*/
private void logPlugins(List<PluginProperties.PluginConfiguration> pluginConfigurations) {
private void logPlugins(List<PluginConfiguration> pluginConfigurations) {
if (!pluginConfigurations.isEmpty()) {
for (PluginProperties.PluginConfiguration pluginConfiguration : pluginConfigurations) {
for (PluginConfiguration pluginConfiguration : pluginConfigurations) {
log.info("Loading {}", pluginConfiguration);
}
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2019 Armory, Inc.
*
* 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.netflix.spinnaker.kork.plugins.spring.configs;

import com.netflix.spinnaker.kork.plugins.spring.MalformedPluginConfigurationException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class PluginConfiguration {

public String name;
List<String> jars;
public boolean enabled;

static final String regex = "^[a-zA-Z0-9]+\\/[\\w-]+$";
static final Pattern pattern = Pattern.compile(regex);

public void validate() {
Matcher matcher = pattern.matcher(name);
if (!matcher.find()) {
throw new MalformedPluginConfigurationException(
String.format("Invalid plugin name: %s", name));
}
}

@Override
public String toString() {
return new StringBuilder()
.append("Plugin: " + name + ", ")
.append("Jars: " + String.join(", ", jars))
.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2019 Armory, Inc.
*
* 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.netflix.spinnaker.kork.plugins.spring.configs;

import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class PluginConfigurationList {

@JsonProperty("pluginConfigurationList")
List<PluginConfiguration> pluginConfigurationList;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2019 Armory, Inc.
*
* 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.netflix.spinnaker.kork.plugins.spring.configs;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class PluginProperties {

@JsonProperty("plugins")
PluginPropertyDetails pluginsPropertyDetails;

public void validate() {
pluginsPropertyDetails.validate();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.netflix.spinnaker.kork.plugins.spring;

package com.netflix.spinnaker.kork.plugins.spring.configs;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.netflix.spinnaker.kork.plugins.spring.MalformedPluginConfigurationException;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import lombok.Data;
Expand All @@ -28,25 +28,25 @@
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PluginProperties {
public class PluginPropertyDetails {

@JsonProperty("plugins")
List<PluginConfiguration> pluginConfigurationList;
@JsonProperty("pluginConfigurations")
List<PluginConfiguration> pluginConfigurations;

@JsonProperty("downloadingEnabled")
public Boolean downloadingEnabled;

public void validate() {
validateUniquePlugins();
for (PluginConfiguration pluginConfiguration : pluginConfigurationList) {
for (PluginConfiguration pluginConfiguration : getPluginConfigurations()) {
pluginConfiguration.validate();
}
}

public void validateUniquePlugins() {

List<String> pluginNames =
pluginConfigurationList.stream()
getPluginConfigurations().stream()
.map(PluginConfiguration::getName)
.collect(Collectors.toList());

Expand All @@ -60,33 +60,4 @@ public void validateUniquePlugins() {
String.format("The following plugins have been defined multiple times: %s", duplicates));
}
}

@Data
@AllArgsConstructor
@NoArgsConstructor
public static class PluginConfiguration {

public String name;
List<String> jars;
public boolean enabled;

static final String regex = "^[a-zA-Z0-9]+\\/[\\w-]+$";
static final Pattern pattern = Pattern.compile(regex);

public void validate() {
Matcher matcher = pattern.matcher(name);
if (!matcher.find()) {
throw new MalformedPluginConfigurationException(
String.format("Invalid plugin name: %s", name));
}
}

@Override
public String toString() {
return new StringBuilder()
.append("Plugin: " + name + ", ")
.append("Jars: " + String.join(", ", jars))
.toString();
}
}
}
Loading

0 comments on commit 9c3cdc4

Please sign in to comment.