aboutsummaryrefslogtreecommitdiffstats
path: root/vespa-maven-plugin
diff options
context:
space:
mode:
authorJon Marius Venstad <venstad@gmail.com>2019-08-22 15:09:50 +0200
committerJon Marius Venstad <venstad@gmail.com>2019-08-22 15:09:50 +0200
commita9f2a119a941253d2b64c4b4f8724acce08407a2 (patch)
treeaa3a7c97bf422bfa5651d93e13d0ffdca58b65a6 /vespa-maven-plugin
parent37cc1486513e3d530c9e6a6534f1297b61f65ba7 (diff)
Add effective services mojo
Diffstat (limited to 'vespa-maven-plugin')
-rw-r--r--vespa-maven-plugin/pom.xml14
-rw-r--r--vespa-maven-plugin/src/main/java/ai/vespa/hosted/plugin/EffectiveServicesMojo.java59
-rw-r--r--vespa-maven-plugin/src/test/java/ai/vespa/hosted/plugin/EffectiveServicesMojoTest.java43
-rw-r--r--vespa-maven-plugin/src/test/resources/effective-services/dev.xml10
-rw-r--r--vespa-maven-plugin/src/test/resources/effective-services/prod_us-east-3.xml10
-rw-r--r--vespa-maven-plugin/src/test/resources/effective-services/prod_us-west-1.xml9
-rw-r--r--vespa-maven-plugin/src/test/resources/effective-services/services.xml11
-rw-r--r--vespa-maven-plugin/src/test/resources/effective-services/test_us-east-1.xml10
8 files changed, 164 insertions, 2 deletions
diff --git a/vespa-maven-plugin/pom.xml b/vespa-maven-plugin/pom.xml
index 57ce1ccadf8..de3e2f44a67 100644
--- a/vespa-maven-plugin/pom.xml
+++ b/vespa-maven-plugin/pom.xml
@@ -39,6 +39,16 @@
<artifactId>vespajlib</artifactId>
<version>${project.version}</version>
</dependency>
+ <dependency>
+ <groupId>com.yahoo.vespa</groupId>
+ <artifactId>vespalog</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>com.yahoo.vespa</groupId>
+ <artifactId>config-application-package</artifactId>
+ <version>${project.version}</version>
+ </dependency>
<dependency>
<groupId>commons-cli</groupId>
@@ -66,8 +76,8 @@
</dependency>
<dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
+ <groupId>org.junit.jupiter</groupId>
+ <artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
diff --git a/vespa-maven-plugin/src/main/java/ai/vespa/hosted/plugin/EffectiveServicesMojo.java b/vespa-maven-plugin/src/main/java/ai/vespa/hosted/plugin/EffectiveServicesMojo.java
new file mode 100644
index 00000000000..905198c1f0d
--- /dev/null
+++ b/vespa-maven-plugin/src/main/java/ai/vespa/hosted/plugin/EffectiveServicesMojo.java
@@ -0,0 +1,59 @@
+package ai.vespa.hosted.plugin;
+
+import com.yahoo.config.application.XmlPreProcessor;
+import com.yahoo.config.provision.zone.ZoneId;
+import org.apache.maven.plugins.annotations.Mojo;
+import org.apache.maven.plugins.annotations.Parameter;
+import org.w3c.dom.Document;
+
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+import java.io.File;
+import java.io.StringWriter;
+import java.io.Writer;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+/**
+ * Computes the effective services.xml for the indicated zone.
+ *
+ * @author jonmv
+ * @author olaa
+ */
+@Mojo(name = "effectiveServices")
+public class EffectiveServicesMojo extends AbstractVespaDeploymentMojo {
+
+ @Parameter(property = "servicesFile", defaultValue = "src/main/application/services.xml")
+ private String servicesFile;
+
+ @Parameter(property = "outputDirectory", defaultValue = "target")
+ private String outputDirectory;
+
+ @Override
+ protected void doExecute() throws Exception {
+ File services = new File(servicesFile);
+ if ( ! services.isFile())
+ throw new IllegalArgumentException(servicesFile + " does not exist. Set correct path with -DservicesFile=<path to services.xml>");
+
+ ZoneId zone = zoneOf(environment, region);
+ Path output = Paths.get(outputDirectory).resolve("services-" + zone.environment().value() + "-" + zone.region().value() + ".xml");
+ Files.write(output, effectiveServices(services, zone).getBytes(StandardCharsets.UTF_8));
+ getLog().info("Effective services for " + zone + " written to " + output);
+ }
+
+ static String effectiveServices(File servicesFile, ZoneId zone) throws Exception {
+ Document processedServicesXml = new XmlPreProcessor(servicesFile.getParentFile(), servicesFile, zone.environment(), zone.region()).run();
+ Transformer transformer = TransformerFactory.newInstance().newTransformer();
+ transformer.setOutputProperty(OutputKeys.INDENT, "yes");
+ Writer writer = new StringWriter();
+ transformer.transform(new DOMSource(processedServicesXml), new StreamResult(writer));
+ return writer.toString().replaceAll("\n(\\s*\n)+","\n");
+ }
+
+}
+
diff --git a/vespa-maven-plugin/src/test/java/ai/vespa/hosted/plugin/EffectiveServicesMojoTest.java b/vespa-maven-plugin/src/test/java/ai/vespa/hosted/plugin/EffectiveServicesMojoTest.java
new file mode 100644
index 00000000000..17c233fa71a
--- /dev/null
+++ b/vespa-maven-plugin/src/test/java/ai/vespa/hosted/plugin/EffectiveServicesMojoTest.java
@@ -0,0 +1,43 @@
+package ai.vespa.hosted.plugin;
+
+import com.yahoo.config.provision.zone.ZoneId;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+
+import java.io.File;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * @author jonmv
+ * @author olaa
+ */
+@DisplayName("Effective services are correctly generated")
+class EffectiveServicesMojoTest {
+
+ private final File servicesFile = new File("src/test/resources/effective-services/services.xml");
+
+ @Test
+ @DisplayName("when zone matches environment-only directive")
+ void devServices() throws Exception {
+ assertEquals(Files.readString(Paths.get("src/test/resources/effective-services/dev.xml")),
+ EffectiveServicesMojo.effectiveServices(servicesFile, ZoneId.from("dev", "us-east-3")));
+ }
+
+ @Test
+ @DisplayName("when zone matches region-and-environment directive")
+ void prodUsEast3() throws Exception {
+ assertEquals(Files.readString(Paths.get("src/test/resources/effective-services/prod_us-east-3.xml")),
+ EffectiveServicesMojo.effectiveServices(servicesFile, ZoneId.from("prod", "us-east-3")));
+ }
+
+ @Test
+ @DisplayName("when zone doesn't match any directives")
+ void prodUsWest1Services() throws Exception {
+ assertEquals(Files.readString(Paths.get("src/test/resources/effective-services/prod_us-west-1.xml")),
+ EffectiveServicesMojo.effectiveServices(servicesFile, ZoneId.from("prod", "us-west-1")));
+ }
+
+}
diff --git a/vespa-maven-plugin/src/test/resources/effective-services/dev.xml b/vespa-maven-plugin/src/test/resources/effective-services/dev.xml
new file mode 100644
index 00000000000..d6343e7ace7
--- /dev/null
+++ b/vespa-maven-plugin/src/test/resources/effective-services/dev.xml
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<services xmlns:deploy="vespa">
+ <container>
+ <component id="good-service-client">
+ <config>
+ <url>dev.tld</url>
+ </config>
+ </component>
+ </container>
+</services>
diff --git a/vespa-maven-plugin/src/test/resources/effective-services/prod_us-east-3.xml b/vespa-maven-plugin/src/test/resources/effective-services/prod_us-east-3.xml
new file mode 100644
index 00000000000..0bab4b7e74a
--- /dev/null
+++ b/vespa-maven-plugin/src/test/resources/effective-services/prod_us-east-3.xml
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<services xmlns:deploy="vespa">
+ <container>
+ <component id="good-service-client">
+ <config>
+ <url>us-east-3.prod.tld</url>
+ </config>
+ </component>
+ </container>
+</services>
diff --git a/vespa-maven-plugin/src/test/resources/effective-services/prod_us-west-1.xml b/vespa-maven-plugin/src/test/resources/effective-services/prod_us-west-1.xml
new file mode 100644
index 00000000000..e29ba231092
--- /dev/null
+++ b/vespa-maven-plugin/src/test/resources/effective-services/prod_us-west-1.xml
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<services xmlns:deploy="vespa">
+ <container>
+ <component id="good-service-client">
+ <config>
+ </config>
+ </component>
+ </container>
+</services>
diff --git a/vespa-maven-plugin/src/test/resources/effective-services/services.xml b/vespa-maven-plugin/src/test/resources/effective-services/services.xml
new file mode 100644
index 00000000000..bf183f474e4
--- /dev/null
+++ b/vespa-maven-plugin/src/test/resources/effective-services/services.xml
@@ -0,0 +1,11 @@
+<?xml version='1.0' encoding='UTF-8'?>
+<services xmlns:deploy="vespa">
+ <container>
+ <component id="good-service-client">
+ <config>
+ <url deploy:environment="dev">dev.tld</url>
+ <url deploy:environment="prod" deploy:region="us-east-3">us-east-3.prod.tld</url>
+ </config>
+ </component>
+ </container>
+</services> \ No newline at end of file
diff --git a/vespa-maven-plugin/src/test/resources/effective-services/test_us-east-1.xml b/vespa-maven-plugin/src/test/resources/effective-services/test_us-east-1.xml
new file mode 100644
index 00000000000..7f9e1bb93b9
--- /dev/null
+++ b/vespa-maven-plugin/src/test/resources/effective-services/test_us-east-1.xml
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<services xmlns:deploy="vespa">
+ <container>
+ <component id="good-service-client">
+ <config>
+ <url>us-east-1.tld</url>
+ </config>
+ </component>
+ </container>
+</services>