-
Notifications
You must be signed in to change notification settings - Fork 805
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(stages): Add SpEL expression for converting canary config name t…
…o id. (#4030) * feat(stages): Add SpEL expression for converting canary config name to id. Add javadocs. Add javadocs. feat(stages): Update canManuallySkip to look at stage context and override it on the PipelineStage to enable pipeline stages to be skippable. * Address NIT's from code review feedback.
- Loading branch information
Showing
3 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
...ix/spinnaker/orca/kayenta/pipeline/functions/KayentaConfigExpressionFunctionProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright 2019 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.netflix.spinnaker.orca.kayenta.pipeline.functions; | ||
|
||
import com.google.common.base.Strings; | ||
import com.netflix.spinnaker.kork.api.expressions.ExpressionFunctionProvider; | ||
import com.netflix.spinnaker.kork.expressions.SpelHelperFunctionException; | ||
import com.netflix.spinnaker.orca.kayenta.KayentaCanaryConfig; | ||
import com.netflix.spinnaker.orca.kayenta.KayentaService; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import javax.annotation.Nullable; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class KayentaConfigExpressionFunctionProvider implements ExpressionFunctionProvider { | ||
|
||
// Static because it's needed during expression eval (which is a static) | ||
private static KayentaService kayentaService; | ||
|
||
public KayentaConfigExpressionFunctionProvider(KayentaService kayentaService) { | ||
this.kayentaService = kayentaService; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public String getNamespace() { | ||
return null; | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public Functions getFunctions() { | ||
return new Functions( | ||
new FunctionDefinition( | ||
"canaryConfigNameToId", | ||
"Look up the canary config ID for the given config name and app", | ||
new FunctionParameter(String.class, "name", "The name of the config"), | ||
new FunctionParameter(String.class, "app", "The name of the app"))); | ||
} | ||
|
||
/** | ||
* SpEL expression used to convert the name of a canary config to the ID. | ||
* | ||
* @param name Name of the config. | ||
* @param app Application which owns the config. | ||
* @return The ID of the config which corresponds to the name and app provided. | ||
*/ | ||
public static String canaryConfigNameToId(String name, String app) { | ||
if (Strings.isNullOrEmpty(name)) { | ||
throw new SpelHelperFunctionException( | ||
"Config name is a required field for the canaryConfigNameToId function."); | ||
} | ||
if (Strings.isNullOrEmpty(app)) { | ||
throw new SpelHelperFunctionException( | ||
"App is a required field for the canaryConfigNameToId function."); | ||
} | ||
|
||
List<KayentaCanaryConfig> configs = kayentaService.getAllCanaryConfigs(); | ||
Optional<KayentaCanaryConfig> found = | ||
configs.stream() | ||
.filter(c -> c.getApplications().contains(app)) | ||
.filter(c -> c.getName().equals(name)) | ||
.findFirst(); | ||
if (found.isPresent()) { | ||
return found.get().getId(); | ||
} | ||
|
||
throw new SpelHelperFunctionException( | ||
"Unable to find config with name " + name + " for app " + app + "."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...pinnaker/orca/kayenta/pipeline/functions/KayentaConfigExpressionFunctionProviderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.netflix.spinnaker.orca.kayenta.pipeline.functions; | ||
|
||
import static org.mockito.Mockito.when; | ||
|
||
import com.google.common.collect.Lists; | ||
import com.netflix.spinnaker.kork.expressions.SpelHelperFunctionException; | ||
import com.netflix.spinnaker.orca.kayenta.KayentaCanaryConfig; | ||
import com.netflix.spinnaker.orca.kayenta.KayentaService; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.mockito.Mockito; | ||
|
||
public class KayentaConfigExpressionFunctionProviderTest { | ||
|
||
@Test(expected = SpelHelperFunctionException.class) | ||
public void missingName() { | ||
KayentaConfigExpressionFunctionProvider provider = | ||
new KayentaConfigExpressionFunctionProvider(Mockito.mock(KayentaService.class)); | ||
provider.canaryConfigNameToId(null, "myapp"); | ||
} | ||
|
||
@Test(expected = SpelHelperFunctionException.class) | ||
public void missingApp() { | ||
KayentaConfigExpressionFunctionProvider provider = | ||
new KayentaConfigExpressionFunctionProvider(Mockito.mock(KayentaService.class)); | ||
provider.canaryConfigNameToId("myname", null); | ||
} | ||
|
||
@Test | ||
public void conversionWorks() { | ||
KayentaService kayentaService = Mockito.mock(KayentaService.class); | ||
List<KayentaCanaryConfig> canaryConfigs = Lists.newArrayList(); | ||
KayentaCanaryConfig config = | ||
new KayentaCanaryConfig("myconfig", "myname", 0L, null, Collections.singletonList("myapp")); | ||
canaryConfigs.add(config); | ||
when(kayentaService.getAllCanaryConfigs()).thenReturn(canaryConfigs); | ||
|
||
KayentaConfigExpressionFunctionProvider provider = | ||
new KayentaConfigExpressionFunctionProvider(kayentaService); | ||
String configId = provider.canaryConfigNameToId("myname", "myapp"); | ||
|
||
Assert.assertEquals("myconfig", configId); | ||
} | ||
|
||
@Test(expected = SpelHelperFunctionException.class) | ||
public void nothingFound() { | ||
KayentaService kayentaService = Mockito.mock(KayentaService.class); | ||
List<KayentaCanaryConfig> canaryConfigs = Lists.newArrayList(); | ||
KayentaCanaryConfig config = | ||
new KayentaCanaryConfig("myconfig", "myname", 0L, null, Collections.singletonList("myapp")); | ||
canaryConfigs.add(config); | ||
when(kayentaService.getAllCanaryConfigs()).thenReturn(canaryConfigs); | ||
|
||
KayentaConfigExpressionFunctionProvider provider = | ||
new KayentaConfigExpressionFunctionProvider(kayentaService); | ||
provider.canaryConfigNameToId("someothername", "myapp"); | ||
} | ||
} |