From 105e44e24b890cfa36800fcb3da5ddc105013c18 Mon Sep 17 00:00:00 2001 From: Jon Marius Venstad Date: Wed, 20 Jan 2021 15:58:59 +0100 Subject: No default "instance" in plugins — require one for deploy, delete, suspend MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ai/vespa/hosted/plugin/AbstractVespaMojo.java | 30 +++++++++++++--------- .../java/ai/vespa/hosted/plugin/DeleteMojo.java | 3 +++ .../java/ai/vespa/hosted/plugin/DeployMojo.java | 6 ++++- .../java/ai/vespa/hosted/plugin/SubmitMojo.java | 4 +-- .../java/ai/vespa/hosted/plugin/SuspendMojo.java | 3 +++ 5 files changed, 31 insertions(+), 15 deletions(-) (limited to 'vespa-maven-plugin') diff --git a/vespa-maven-plugin/src/main/java/ai/vespa/hosted/plugin/AbstractVespaMojo.java b/vespa-maven-plugin/src/main/java/ai/vespa/hosted/plugin/AbstractVespaMojo.java index 7119bde7a09..ad10c1af608 100644 --- a/vespa-maven-plugin/src/main/java/ai/vespa/hosted/plugin/AbstractVespaMojo.java +++ b/vespa-maven-plugin/src/main/java/ai/vespa/hosted/plugin/AbstractVespaMojo.java @@ -2,8 +2,8 @@ package ai.vespa.hosted.plugin; import ai.vespa.hosted.api.ControllerHttpClient; -import ai.vespa.hosted.api.Properties; import com.yahoo.config.provision.ApplicationId; +import com.yahoo.config.provision.InstanceName; import com.yahoo.yolean.Exceptions; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; @@ -16,7 +16,6 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.Optional; import java.util.function.Function; -import java.util.stream.Collectors; import java.util.stream.Stream; import static java.util.stream.Collectors.joining; @@ -56,6 +55,8 @@ public abstract class AbstractVespaMojo extends AbstractMojo { protected ApplicationId id; protected ControllerHttpClient controller; + protected boolean requireInstance() { return false; } + @Override public final void execute() throws MojoExecutionException, MojoFailureException { try { @@ -80,19 +81,24 @@ public abstract class AbstractVespaMojo extends AbstractMojo { /** Return the name of the relevant entity, e.g., application with or without instance. */ protected String name() { return tenant + "." + application; } - protected void setup() { - tenant = firstNonBlank(tenant, project.getProperties().getProperty("tenant")); - application = firstNonBlank(application, project.getProperties().getProperty("application")); - instance = firstNonBlank(instance, project.getProperties().getProperty("instance"), Properties.user()); + protected void setup() throws MojoExecutionException { + tenant = firstNonBlank(tenant, project.getProperties().getProperty("tenant")) + .orElseThrow(() -> new MojoExecutionException("'tenant' must be specified as a parameter or project property")); + application = firstNonBlank(application, project.getProperties().getProperty("application")) + .orElseThrow(() -> new MojoExecutionException("'application' must be specified as a parameter or project property")); + instance = firstNonBlank(instance, project.getProperties().getProperty("instance"), requireInstance() ? null : InstanceName.defaultName().value()) + .orElseThrow(() -> new MojoExecutionException("'instance' must be specified as a parameter or project property")); id = ApplicationId.from(tenant, application, instance); - if (!isNullOrBlank(apiKey)) { + if ( ! isNullOrBlank(apiKey)) { controller = ControllerHttpClient.withSignatureKey(URI.create(endpoint), apiKey, id); - } else if (!isNullOrBlank(apiKeyFile)) { + } + else if ( ! isNullOrBlank(apiKeyFile)) { controller = isNullOrBlank(apiCertificateFile) ? ControllerHttpClient.withSignatureKey(URI.create(endpoint), Paths.get(apiKeyFile), id) : ControllerHttpClient.withKeyAndCertificate(URI.create(endpoint), Paths.get(apiKeyFile), Paths.get(apiCertificateFile)); - } else { + } + else { throw new IllegalArgumentException("One of the properties 'apiKey' or 'apiKeyFile' is required."); } } @@ -102,12 +108,12 @@ public abstract class AbstractVespaMojo extends AbstractMojo { } /** Returns the first of the given strings which is non-null and non-blank, or throws IllegalArgumentException. */ - protected static String firstNonBlank(String... values) { + protected static Optional firstNonBlank(String... values) { for (String value : values) if (value != null && ! value.isBlank()) - return value; + return Optional.of(value); - throw new IllegalArgumentException("No valid value given"); + return Optional.empty(); } protected static Optional optionalOf(String value) { diff --git a/vespa-maven-plugin/src/main/java/ai/vespa/hosted/plugin/DeleteMojo.java b/vespa-maven-plugin/src/main/java/ai/vespa/hosted/plugin/DeleteMojo.java index 03b4dab246f..30a246019e5 100644 --- a/vespa-maven-plugin/src/main/java/ai/vespa/hosted/plugin/DeleteMojo.java +++ b/vespa-maven-plugin/src/main/java/ai/vespa/hosted/plugin/DeleteMojo.java @@ -12,6 +12,9 @@ import org.apache.maven.plugins.annotations.Mojo; @Mojo(name = "delete") public class DeleteMojo extends AbstractVespaDeploymentMojo { + @Override + protected boolean requireInstance() { return true; } + @Override protected void doExecute() { if (!isNullOrBlank(environment) && ! Environment.from(environment).isManuallyDeployed()) diff --git a/vespa-maven-plugin/src/main/java/ai/vespa/hosted/plugin/DeployMojo.java b/vespa-maven-plugin/src/main/java/ai/vespa/hosted/plugin/DeployMojo.java index 3ca628cdc84..e141261f62b 100644 --- a/vespa-maven-plugin/src/main/java/ai/vespa/hosted/plugin/DeployMojo.java +++ b/vespa-maven-plugin/src/main/java/ai/vespa/hosted/plugin/DeployMojo.java @@ -36,11 +36,15 @@ public class DeployMojo extends AbstractVespaDeploymentMojo { private String vespaLogLevel; private DeploymentLog.Level loggable; + @Override + protected boolean requireInstance() { return true; } + @Override protected void doExecute() throws MojoFailureException, MojoExecutionException { loggable = DeploymentLog.Level.valueOf(vespaLogLevel); Deployment deployment = Deployment.ofPackage(Paths.get(firstNonBlank(applicationZip, - projectPathOf("target", "application.zip")))); + projectPathOf("target", "application.zip")) + .orElseThrow())); // Fallback always exists. if ( ! isNullOrBlank(vespaVersion)) deployment = deployment.atVersion(vespaVersion); ZoneId zone = zoneOf(environment, region); diff --git a/vespa-maven-plugin/src/main/java/ai/vespa/hosted/plugin/SubmitMojo.java b/vespa-maven-plugin/src/main/java/ai/vespa/hosted/plugin/SubmitMojo.java index 6669f771a0e..96b5ab4244c 100644 --- a/vespa-maven-plugin/src/main/java/ai/vespa/hosted/plugin/SubmitMojo.java +++ b/vespa-maven-plugin/src/main/java/ai/vespa/hosted/plugin/SubmitMojo.java @@ -43,8 +43,8 @@ public class SubmitMojo extends AbstractVespaMojo { @Override public void doExecute() { - applicationZip = firstNonBlank(applicationZip, projectPathOf("target", "application.zip")); - applicationTestZip = firstNonBlank(applicationTestZip, projectPathOf("target", "application-test.zip")); + applicationZip = firstNonBlank(applicationZip, projectPathOf("target", "application.zip")).orElseThrow(); + applicationTestZip = firstNonBlank(applicationTestZip, projectPathOf("target", "application-test.zip")).orElseThrow(); Submission submission = new Submission(optionalOf(repository), optionalOf(branch), optionalOf(commit), optionalOf(sourceUrl), optionalOf(authorEmail), Paths.get(applicationZip), Paths.get(applicationTestZip), diff --git a/vespa-maven-plugin/src/main/java/ai/vespa/hosted/plugin/SuspendMojo.java b/vespa-maven-plugin/src/main/java/ai/vespa/hosted/plugin/SuspendMojo.java index 52057e237d7..40ede218d52 100644 --- a/vespa-maven-plugin/src/main/java/ai/vespa/hosted/plugin/SuspendMojo.java +++ b/vespa-maven-plugin/src/main/java/ai/vespa/hosted/plugin/SuspendMojo.java @@ -15,6 +15,9 @@ public class SuspendMojo extends AbstractVespaDeploymentMojo { @Parameter(property = "suspend", required = true) private boolean suspend; + @Override + protected boolean requireInstance() { return true; } + @Override protected void doExecute() { getLog().info(controller.suspend(id, zoneOf(environment, region), suspend)); -- cgit v1.2.3