diff options
author | Jon Marius Venstad <venstad@gmail.com> | 2019-01-03 18:54:01 +0100 |
---|---|---|
committer | Jon Marius Venstad <venstad@gmail.com> | 2019-01-03 18:54:01 +0100 |
commit | cea4ddb1d2d8b3ae8a51b99fb92fa73cd1964807 (patch) | |
tree | bcaba7a8ae6ddc4d2936dddcb07753dfbed9c1a5 /config-model-api | |
parent | 5a678c888e69f29713a1050e3c18fed03735207d (diff) |
Update notifications syntax
Diffstat (limited to 'config-model-api')
3 files changed, 110 insertions, 24 deletions
diff --git a/config-model-api/src/main/java/com/yahoo/config/application/api/DeploymentSpec.java b/config-model-api/src/main/java/com/yahoo/config/application/api/DeploymentSpec.java index 7f63abe72c3..bdc9e41c85d 100644 --- a/config-model-api/src/main/java/com/yahoo/config/application/api/DeploymentSpec.java +++ b/config-model-api/src/main/java/com/yahoo/config/application/api/DeploymentSpec.java @@ -2,6 +2,7 @@ package com.yahoo.config.application.api; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; import com.yahoo.config.application.api.xml.DeploymentSpecXmlReader; import com.yahoo.config.provision.AthenzDomain; import com.yahoo.config.provision.AthenzService; @@ -17,6 +18,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Objects; import java.util.Optional; import java.util.Set; @@ -458,34 +460,101 @@ public class DeploymentSpec { /** * Configuration of notifications for deployment jobs. * - * Supports a list of email recipients, and a flag for whether to send to the commit author. + * Supports a list of email addresses, and a list of roles for which email addresses are known. + * The currently supported roles are: + * <ul> + * <li><strong>author</strong>: the author of the git commit of a given application package. </li> + * </ul> */ public static class Notifications { - private static final Notifications none = new Notifications(Collections.emptyList(), false); + private static final Notifications none = new Notifications(Collections.emptyMap(), Collections.emptyMap()); public static Notifications none() { return none; } - private final List<String> staticEmails; - private final boolean includeAuthor; + private final Map<String, When> staticEmails; + private final Map<Role, When> roleEmails; - private Notifications(List<String> staticEmails, boolean includeAuthor) { - this.staticEmails = ImmutableList.copyOf(staticEmails); - this.includeAuthor = includeAuthor; + private Notifications(Map<String, When> staticEmails, Map<Role, When> roleEmails) { + this.staticEmails = ImmutableMap.copyOf(staticEmails); + this.roleEmails = ImmutableMap.copyOf(roleEmails); } - public static Notifications of(List<String> staticEmails, boolean includeAuthor) { - if (staticEmails.isEmpty() && ! includeAuthor) + /** + * Returns a new Notifications as specified by the given String input. + * + * @param when Optional string name of the default condition for sending notifications; defaults to failingCommit. + * @param staticEmails Map from email addresses to optional overrides for when to send to these. + * @param roleEmails Map from email roles to optional overrides for when to send to these. + * @return The Notifications as specified. + */ + public static Notifications of(Optional<String> when, Map<String, Optional<String>> staticEmails, Map<String, Optional<String>> roleEmails) { + if (staticEmails.isEmpty() && roleEmails.isEmpty()) return none; - return new Notifications(staticEmails, includeAuthor); + When defaultWhen = when.map(When::fromValue).orElse(When.failingCommit); + return new Notifications(staticEmails.entrySet().stream() + .collect(Collectors.toMap(entry -> entry.getKey(), + entry -> entry.getValue().map(When::fromValue).orElse(defaultWhen))), + roleEmails.entrySet().stream() + .collect(Collectors.toMap(entry -> Role.fromValue(entry.getKey()), + entry -> entry.getValue().map(When::fromValue).orElse(defaultWhen)))); } - public List<String> staticEmails() { + public Map<String, When> staticEmails() { return staticEmails; } - public boolean includeAuthor() { - return includeAuthor; + public Map<Role, When> roleEmails() { + return roleEmails; + } + + + public enum Role { + + /** Author of the last commit of an application package. */ + author; + + public static String toValue(Role role) { + switch (role) { + case author: return "author"; + default: throw new IllegalArgumentException("Unexpected constant '" + role.name() + "'."); + } + } + + public static Role fromValue(String value) { + switch (value) { + case "author": return author; + default: throw new IllegalArgumentException("Unknown value '" + value + "'."); + } + } + + } + + + public enum When { + + /** Send notifications whenever a job fails. */ + failing, + + /** Send notifications whenever a job fails while deploying a new commit. */ + failingCommit; + + public static String toValue(When when) { + switch (when) { + case failing: return "failing"; + case failingCommit: return "failing-commit"; + default: throw new IllegalArgumentException("Unexpected constant '" + when.name() + "'."); + } + } + + public static When fromValue(String value) { + switch (value) { + case "failing": return failing; + case "failing-commit": return failingCommit; + default: throw new IllegalArgumentException("Unknown value '" + value + "'."); + } + } + } } diff --git a/config-model-api/src/main/java/com/yahoo/config/application/api/xml/DeploymentSpecXmlReader.java b/config-model-api/src/main/java/com/yahoo/config/application/api/xml/DeploymentSpecXmlReader.java index 04f8594d18e..8ee1d05df21 100644 --- a/config-model-api/src/main/java/com/yahoo/config/application/api/xml/DeploymentSpecXmlReader.java +++ b/config-model-api/src/main/java/com/yahoo/config/application/api/xml/DeploymentSpecXmlReader.java @@ -21,7 +21,9 @@ import java.time.Duration; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Optional; import java.util.stream.Collectors; @@ -118,11 +120,20 @@ public class DeploymentSpecXmlReader { if (notificationsElement == null) return DeploymentSpec.Notifications.none(); - List<String> staticEmails = XML.getChildren(notificationsElement, "email").stream() - .map(XML::getValue) - .collect(Collectors.toList()); - boolean includeAuthor = XML.getChild(notificationsElement, "author") != null; - return DeploymentSpec.Notifications.of(staticEmails, includeAuthor); + Optional<String> when = stringAttribute("when", notificationsElement); + Map<String, Optional<String>> staticEmails = new HashMap<>(); + Map<String, Optional<String>> roleEmails = new HashMap<>(); + + for (Element emailElement : XML.getChildren(notificationsElement, "email")) { + Optional<String> addressAttribute = stringAttribute("address", emailElement); + Optional<String> roleAttribute = stringAttribute("role", emailElement); + if (addressAttribute.isPresent() == roleAttribute.isPresent()) + throw new IllegalArgumentException("Exactly one of 'role' and 'address' must be present in 'email' elements."); + + addressAttribute.ifPresent(address -> staticEmails.put(address, stringAttribute("when", emailElement))); + roleAttribute.ifPresent(role -> roleEmails.put(role, stringAttribute("when", emailElement))); + } + return DeploymentSpec.Notifications.of(when, staticEmails, roleEmails); } /** Imposes some constraints on tag order which are not expressible in the schema */ diff --git a/config-model-api/src/test/java/com/yahoo/config/application/api/DeploymentSpecTest.java b/config-model-api/src/test/java/com/yahoo/config/application/api/DeploymentSpecTest.java index ae1d8bd6d9b..e29ce7f6c39 100644 --- a/config-model-api/src/test/java/com/yahoo/config/application/api/DeploymentSpecTest.java +++ b/config-model-api/src/test/java/com/yahoo/config/application/api/DeploymentSpecTest.java @@ -1,6 +1,7 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.config.application.api; +import com.google.common.collect.ImmutableMap; import com.yahoo.config.provision.Environment; import com.yahoo.config.provision.RegionName; import org.junit.Test; @@ -11,6 +12,9 @@ import java.time.ZoneId; import java.util.Arrays; import java.util.Optional; +import static com.yahoo.config.application.api.DeploymentSpec.Notifications.Role.author; +import static com.yahoo.config.application.api.DeploymentSpec.Notifications.When.failing; +import static com.yahoo.config.application.api.DeploymentSpec.Notifications.When.failingCommit; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -454,14 +458,16 @@ public class DeploymentSpecTest { @Test public void someNotifications() { DeploymentSpec spec = DeploymentSpec.fromXml("<deployment>\n" + - " <notifications>\n" + - " <author />\n" + - " <email>john@dev</email>\n" + - " <email>jane@dev</email>\n" + + " <notifications when=\"failing\">\n" + + " <email role=\"author\"/>\n" + + " <email address=\"john@dev\" when=\"failing-commit\"/>\n" + + " <email address=\"jane@dev\"/>\n" + " </notifications>\n" + "</deployment>"); - assertTrue(spec.notifications().includeAuthor()); - assertEquals(Arrays.asList("john@dev", "jane@dev"), + assertEquals(ImmutableMap.of(author, failing), + spec.notifications().roleEmails()); + assertEquals(ImmutableMap.of("john@dev", failingCommit, + "jane@dev", failing), spec.notifications().staticEmails()); } |