Skip to content
This repository has been archived by the owner on Nov 3, 2022. It is now read-only.

Commit

Permalink
[#46] reading labels
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Pozzi committed Nov 11, 2019
1 parent 09e44c3 commit e9854be
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 8 deletions.
46 changes: 46 additions & 0 deletions src/main/java/de/bonndan/nivio/input/dto/LabelProcessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package de.bonndan.nivio.input.dto;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Optional;

public class LabelProcessor {

private static final Logger LOGGER = LoggerFactory.getLogger(LabelProcessor.class);
public static final String NIVIO_LABEL_PREFIX = "nivio.";

/**
* Copies label key and value to item labels, or, if prefixed "nivio.", sets the field to the value.
*
* @param item landscape item
* @param key label name
* @param value label value
*/
public static void applyLabel(ItemDescription item, String key, Object value) {

if (!key.toLowerCase().startsWith(NIVIO_LABEL_PREFIX)) {
item.getLabels().put(key, (String) value);
return;
}

String field = key.substring(NIVIO_LABEL_PREFIX.length());
setValue(item, field, (String) value);
}

private static void setValue(ItemDescription item, String name, String value) {
Field[] declaredFields = item.getClass().getDeclaredFields();
Optional<Field> field = Arrays.stream(declaredFields).filter(field1 -> field1.getName().equals(name)).findFirst();
field.ifPresent(field1 -> {
try {
field1.setAccessible(true);
field1.set(item, value);
field1.setAccessible(false);
} catch (IllegalAccessException e) {
LOGGER.warn("Failed to set field {} via label: " + e.getMessage(), name);
}
});
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package de.bonndan.nivio.input.kubernetes;

import de.bonndan.nivio.input.FileFetcher;
import de.bonndan.nivio.input.ItemDescriptionFactory;
import de.bonndan.nivio.input.ItemDescriptionFormatFactory;
import de.bonndan.nivio.input.dto.ItemDescription;
import de.bonndan.nivio.input.dto.RelationDescription;
import de.bonndan.nivio.input.dto.SourceReference;
import de.bonndan.nivio.model.Items;
import de.bonndan.nivio.model.LandscapeItem;
import de.bonndan.nivio.model.RelationBuilder;
import de.bonndan.nivio.model.Items;
import de.bonndan.nivio.util.URLHelper;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.Pod;
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/de/bonndan/nivio/input/rancher1/APIWalker.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import de.bonndan.nivio.ProcessingException;
import de.bonndan.nivio.input.dto.ItemDescription;
import de.bonndan.nivio.input.dto.LabelProcessor;
import de.bonndan.nivio.input.dto.RelationDescription;
import de.bonndan.nivio.input.dto.SourceReference;
import io.rancher.Rancher;
Expand All @@ -24,9 +25,6 @@

/**
* Gathers projects, stacks and services from a Rancher 1.6 API
*
*
*
*/
class APIWalker {

Expand Down Expand Up @@ -111,15 +109,16 @@ private List<ItemDescription> asDescriptions(List<Service> data, final Map<Strin
});
}

//TODO data/metadata to labels
//copy all labels
if (service.getLaunchConfig() != null && service.getLaunchConfig().getLabels() != null)
service.getLaunchConfig().getLabels().forEach((s, o) -> LabelProcessor.applyLabel(item, s, o));

descriptions.add(item);
});

return descriptions;
}


private Rancher.Config getConfig(SourceReference reference) {
Rancher.Config config = null;
try {
Expand Down
46 changes: 46 additions & 0 deletions src/test/java/de/bonndan/nivio/input/dto/LabelProcessorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package de.bonndan.nivio.input.dto;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class LabelProcessorTest {

ItemDescription item;

@BeforeEach
public void setup() {
item = new ItemDescription();
}

@Test
public void regularLabels() {
LabelProcessor.applyLabel(item, "foo", "bar");
LabelProcessor.applyLabel(item, "niviofoo", "baz");

assertEquals(2, item.getLabels().size());
assertEquals("bar", item.getLabels().get("foo"));
assertEquals("baz", item.getLabels().get("niviofoo"));
}

@Test
public void noField() {
LabelProcessor.applyLabel(item, "foo", "bar");
LabelProcessor.applyLabel(item, "nivio.foo", "baz");

assertEquals(1, item.getLabels().size());
assertEquals("bar", item.getLabels().get("foo"));
assertNull(item.getLabels().get("nivio.foo"));
}

@Test
public void fieldLabel() {
LabelProcessor.applyLabel(item, "foo", "bar");
LabelProcessor.applyLabel(item, "nivio.description", "baz");

assertEquals(1, item.getLabels().size());
assertEquals("bar", item.getLabels().get("foo"));
assertEquals("baz", item.getDescription());
}
}

0 comments on commit e9854be

Please sign in to comment.