diff options
author | Jon Marius Venstad <venstad@gmail.com> | 2021-01-20 15:58:59 +0100 |
---|---|---|
committer | Jon Marius Venstad <venstad@gmail.com> | 2021-01-20 15:58:59 +0100 |
commit | 105e44e24b890cfa36800fcb3da5ddc105013c18 (patch) | |
tree | a5ea99e61660098a99285c1b27dd3785997acf80 /vespa-maven-plugin/src/main | |
parent | 23f604510b693f5f74365c785bf2bb3efd90ee6a (diff) |
No default "instance" in plugins — require one for deploy, delete, suspend
Diffstat (limited to 'vespa-maven-plugin/src/main')
5 files changed, 31 insertions, 15 deletions
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<String> 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<String> 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 @@ -13,6 +13,9 @@ import org.apache.maven.plugins.annotations.Mojo; public class DeleteMojo extends AbstractVespaDeploymentMojo { @Override + protected boolean requireInstance() { return true; } + + @Override protected void doExecute() { if (!isNullOrBlank(environment) && ! Environment.from(environment).isManuallyDeployed()) throw new IllegalArgumentException("Manual deletion is not permitted in " + environment); 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 @@ -37,10 +37,14 @@ public class DeployMojo extends AbstractVespaDeploymentMojo { 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 @@ -16,6 +16,9 @@ public class SuspendMojo extends AbstractVespaDeploymentMojo { private boolean suspend; @Override + protected boolean requireInstance() { return true; } + + @Override protected void doExecute() { getLog().info(controller.suspend(id, zoneOf(environment, region), suspend)); } |