-
Notifications
You must be signed in to change notification settings - Fork 808
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(SpEL): Add applicationMetadata SpEL helper function (#3799)
Function to retrieve application metadata (from `front50`) via SpEL Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
- Loading branch information
1 parent
48889eb
commit 3c13b2f
Showing
1 changed file
with
88 additions
and
0 deletions.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
...vy/com/netflix/spinnaker/orca/front50/pipeline/ApplicationExpressionFunctionProvider.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,88 @@ | ||
/* | ||
* Copyright 2020 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.front50.pipeline; | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.netflix.spinnaker.kork.core.RetrySupport; | ||
import com.netflix.spinnaker.kork.expressions.ExpressionFunctionProvider; | ||
import com.netflix.spinnaker.kork.expressions.SpelHelperFunctionException; | ||
import com.netflix.spinnaker.orca.front50.Front50Service; | ||
import com.netflix.spinnaker.orca.front50.model.Application; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class ApplicationExpressionFunctionProvider implements ExpressionFunctionProvider { | ||
// Static because it's needed during expression eval (which is a static) | ||
private static Front50Service front50Service = null; | ||
private static ObjectMapper objectMapper = null; | ||
|
||
ApplicationExpressionFunctionProvider( | ||
ObjectMapper objectMapper, Optional<Front50Service> front50Service) { | ||
front50Service.ifPresent( | ||
service -> ApplicationExpressionFunctionProvider.front50Service = service); | ||
|
||
ApplicationExpressionFunctionProvider.objectMapper = objectMapper; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public String getNamespace() { | ||
return null; | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public Functions getFunctions() { | ||
return new Functions( | ||
new FunctionDefinition( | ||
"applicationMetadata", | ||
"Get application metadata for a specific application", | ||
new FunctionParameter( | ||
String.class, "applicationName", "Application to get metadata for"))); | ||
} | ||
|
||
/** | ||
* Function to retrieve application metadata for a specific application | ||
* | ||
* @param applicationName the application name to look up metadata for | ||
* @return returns a map with application metadata | ||
*/ | ||
public static Map<String, Object> applicationMetadata(String applicationName) { | ||
if (front50Service == null) { | ||
throw new SpelHelperFunctionException( | ||
"front50 service is missing. It's required when using applicationMetadata function"); | ||
} | ||
|
||
try { | ||
RetrySupport retrySupport = new RetrySupport(); | ||
Application application = | ||
retrySupport.retry(() -> front50Service.get(applicationName), 3, 1000, true); | ||
|
||
return objectMapper.convertValue(application, new TypeReference<Map<String, Object>>() {}); | ||
} catch (Exception e) { | ||
throw new SpelHelperFunctionException( | ||
String.format( | ||
"Application metadata for application '%s' could not be retrieved", applicationName), | ||
e); | ||
} | ||
} | ||
} |