/** * Removes the first occurrence of a deployment step to the given environment and returns it. * * @return the removed step, or null if it is not present */ private static DeclaredZone remove(Environment environment, List<Step> steps) { for (int i = 0; i < steps.size(); i++) { if (steps.get(i).deploysTo(environment)) return (DeclaredZone)steps.remove(i); } return null; }
/** Returns all the DeclaredZone deployment steps in the order they are declared */ public List<DeclaredZone> zones() { return steps.stream() .flatMap(step -> step.zones().stream()) .collect(Collectors.toList()); }
/** Adds missing required steps and reorders steps to a permissible order */ private static List<Step> completeSteps(List<Step> steps) { // Add staging if required and missing if (steps.stream().anyMatch(step -> step.deploysTo(Environment.prod)) && steps.stream().noneMatch(step -> step.deploysTo(Environment.staging))) { steps.add(new DeclaredZone(Environment.staging)); } // Add test if required and missing if (steps.stream().anyMatch(step -> step.deploysTo(Environment.staging)) && steps.stream().noneMatch(step -> step.deploysTo(Environment.test))) { steps.add(new DeclaredZone(Environment.test)); } // Enforce order test, staging, prod DeclaredZone testStep = remove(Environment.test, steps); if (testStep != null) steps.add(0, testStep); DeclaredZone stagingStep = remove(Environment.staging, steps); if (stagingStep != null) steps.add(1, stagingStep); return steps; }
/** Returns whether this step deploys to the given environment, and (if specified) region */ public abstract boolean deploysTo(Environment environment, Optional<RegionName> region);
/** Throw an IllegalArgumentException if any production zone is declared multiple times */ private void validateZones(List<Step> steps) { Set<DeclaredZone> zones = new HashSet<>(); for (Step step : steps) for (DeclaredZone zone : step.zones()) ensureUnique(zone, zones); }
/** Returns whether this deployment spec specifies the given zone, either implicitly or explicitly */ public boolean includes(Environment environment, Optional<RegionName> region) { for (Step step : steps) if (step.deploysTo(environment, region)) return true; return false; }