This repository has been archived by the owner on Nov 3, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#14] supported formats are detected at runtime using Spring services
- Loading branch information
Daniel Pozzi
committed
Nov 10, 2019
1 parent
afc932d
commit 360f505
Showing
22 changed files
with
204 additions
and
161 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
64 changes: 39 additions & 25 deletions
64
src/main/java/de/bonndan/nivio/input/ItemDescriptionFormatFactory.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 |
---|---|---|
@@ -1,44 +1,58 @@ | ||
package de.bonndan.nivio.input; | ||
|
||
import de.bonndan.nivio.input.compose2.ItemDescriptionFactoryCompose2; | ||
import de.bonndan.nivio.ProcessingException; | ||
import de.bonndan.nivio.input.dto.LandscapeDescription; | ||
import de.bonndan.nivio.input.dto.SourceReference; | ||
import de.bonndan.nivio.input.http.HttpService; | ||
import de.bonndan.nivio.input.kubernetes.ItemDescriptionFactoryKubernetes; | ||
import de.bonndan.nivio.input.nivio.ItemDescriptionFactoryNivio; | ||
import de.bonndan.nivio.input.rancher1.ItemDescriptionFactoryRancher1; | ||
import de.bonndan.nivio.util.URLHelper; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.util.StringUtils; | ||
|
||
import java.net.URL; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
@Service | ||
public class ItemDescriptionFormatFactory { | ||
|
||
private final Map<ItemDescriptionFactory, List<String>> factoryListMap = new ConcurrentHashMap<>(); | ||
|
||
public static ItemDescriptionFormatFactory with(ItemDescriptionFactory factory) { | ||
return new ItemDescriptionFormatFactory(new ArrayList<>(Arrays.asList(factory))); | ||
} | ||
|
||
public ItemDescriptionFormatFactory(List<ItemDescriptionFactory> factories) { | ||
factories.forEach(itemDescriptionFactory -> factoryListMap.put(itemDescriptionFactory, itemDescriptionFactory.getFormats())); | ||
} | ||
|
||
/** | ||
* Returns the proper factory to generate/parse service descriptions based on the input format. | ||
* | ||
* @param reference the reference pointing at a file or url | ||
* @param reference the reference pointing at a file or url | ||
* @param landscapeDescription landscape, may contain a base url | ||
* @return the factory | ||
*/ | ||
public static ItemDescriptionFactory getFactory(SourceReference reference, LandscapeDescription landscapeDescription) { | ||
|
||
URL baseUrl = URLHelper.getParentPath(landscapeDescription.getSource()); | ||
|
||
FileFetcher fetcher = new FileFetcher(new HttpService()); | ||
|
||
if (reference == null || reference.getFormat() == null) | ||
return new ItemDescriptionFactoryNivio(fetcher, baseUrl); | ||
|
||
//TODO use SourceFormat.from or similar | ||
switch (reference.getFormat()) { | ||
case DOCKER_COMPOSE2: | ||
return new ItemDescriptionFactoryCompose2(new FileFetcher(new HttpService()), baseUrl); | ||
case KUBERNETES: | ||
return new ItemDescriptionFactoryKubernetes(reference); | ||
case RANCHER1_PROMETHEUS: | ||
return new ItemDescriptionFactoryRancher1(baseUrl); | ||
public ItemDescriptionFactory getFactory(SourceReference reference, LandscapeDescription landscapeDescription) { | ||
|
||
List<ItemDescriptionFactory> factories = new ArrayList<>(); | ||
factoryListMap.entrySet().stream() | ||
.filter(entry -> entry.getValue().stream().map(s -> { | ||
if (StringUtils.isEmpty(s)) | ||
return ""; | ||
return s.toLowerCase(); | ||
}).anyMatch(s -> s.equals(reference.getFormat()) || (StringUtils.isEmpty(s) && StringUtils.isEmpty(reference.getFormat())))) | ||
.forEach(entry -> factories.add(entry.getKey())); | ||
|
||
if (factories.isEmpty()) { | ||
List<String> knownFormats = new ArrayList<>(); | ||
factoryListMap.values().forEach(knownFormats::addAll); | ||
String msg = "Unknown source reference format: '" + reference.getFormat() + "', known formats are: " | ||
+ StringUtils.collectionToDelimitedString(knownFormats, ", "); | ||
throw new ProcessingException(landscapeDescription, msg); | ||
} | ||
|
||
return new ItemDescriptionFactoryNivio(fetcher, baseUrl); | ||
//last one wins | ||
return factories.get(factories.size() - 1); | ||
} | ||
} |
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
46 changes: 0 additions & 46 deletions
46
src/main/java/de/bonndan/nivio/input/dto/SourceFormat.java
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.