Search in sources :

Example 1 with Binding

use of com.microsoft.azure.toolkit.lib.legacy.function.bindings.Binding in project azure-tools-for-java by Microsoft.

the class FunctionUtils method generateConfiguration.

private static FunctionConfiguration generateConfiguration(PsiMethod method) throws AzureExecutionException {
    final FunctionConfiguration config = new FunctionConfiguration();
    final List<Binding> bindings = new ArrayList<>();
    processParameterAnnotations(method, bindings);
    processMethodAnnotations(method, bindings);
    patchStorageBinding(method, bindings);
    config.setEntryPoint(method.getContainingClass().getQualifiedName() + "." + method.getName());
    // Todo: add set bindings method in tools-common
    config.setBindings(bindings);
    return config;
}
Also used : Binding(com.microsoft.azure.toolkit.lib.legacy.function.bindings.Binding) FunctionConfiguration(com.microsoft.azure.toolkit.lib.legacy.function.configurations.FunctionConfiguration) ArrayList(java.util.ArrayList)

Example 2 with Binding

use of com.microsoft.azure.toolkit.lib.legacy.function.bindings.Binding in project azure-tools-for-java by Microsoft.

the class FunctionUtils method getHTTPOutBinding.

private static Binding getHTTPOutBinding() {
    final Binding result = new Binding(BindingEnum.HttpOutput);
    result.setName(HTTP_OUTPUT_DEFAULT_NAME);
    return result;
}
Also used : Binding(com.microsoft.azure.toolkit.lib.legacy.function.bindings.Binding)

Example 3 with Binding

use of com.microsoft.azure.toolkit.lib.legacy.function.bindings.Binding in project azure-tools-for-java by Microsoft.

the class FunctionUtils method getUserDefinedBinding.

private static Binding getUserDefinedBinding(final Project project, PsiAnnotation annotation) throws AzureExecutionException {
    final PsiJavaCodeReferenceElement referenceElement = annotation.getNameReferenceElement();
    if (referenceElement == null) {
        return null;
    }
    final PsiAnnotation customBindingAnnotation = AnnotationUtil.findAnnotation((PsiModifierListOwner) referenceElement.resolve(), AZURE_FUNCTION_CUSTOM_BINDING_CLASS);
    if (customBindingAnnotation == null) {
        return null;
    }
    final Map<String, Object> annotationProperties = AnnotationHelper.evaluateAnnotationProperties(project, annotation, CUSTOM_BINDING_RESERVED_PROPERTIES);
    final Map<String, Object> customBindingProperties = AnnotationHelper.evaluateAnnotationProperties(project, customBindingAnnotation, null);
    final Map<String, Object> mergedMap = new HashMap<>(annotationProperties);
    customBindingProperties.forEach(mergedMap::putIfAbsent);
    final Binding extendBinding = new Binding(BindingEnum.CustomBinding) {

        public String getName() {
            return (String) mergedMap.get("name");
        }

        public String getDirection() {
            return (String) mergedMap.get("direction");
        }

        public String getType() {
            return (String) mergedMap.get("type");
        }
    };
    annotationProperties.forEach((name, value) -> {
        if (!CUSTOM_BINDING_RESERVED_PROPERTIES.contains(name)) {
            extendBinding.setAttribute(name, value);
        }
    });
    return extendBinding;
}
Also used : Binding(com.microsoft.azure.toolkit.lib.legacy.function.bindings.Binding) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) PsiJavaCodeReferenceElement(com.intellij.psi.PsiJavaCodeReferenceElement) JsonObject(com.google.gson.JsonObject) PsiAnnotation(com.intellij.psi.PsiAnnotation)

Example 4 with Binding

use of com.microsoft.azure.toolkit.lib.legacy.function.bindings.Binding in project azure-tools-for-java by Microsoft.

the class FunctionUtils method writeFunctionJsonFile.

private static void writeFunctionJsonFile(File file, FunctionConfiguration config) throws IOException {
    final Map<String, Object> json = new LinkedHashMap<>();
    json.put("scriptFile", config.getScriptFile());
    json.put("entryPoint", config.getEntryPoint());
    final List<Map<String, Object>> lists = new ArrayList<>();
    if (config.getBindings() != null) {
        for (final Binding binding : config.getBindings()) {
            final Map<String, Object> bindingJson = new LinkedHashMap<>();
            bindingJson.put("type", binding.getType());
            bindingJson.put("direction", binding.getDirection());
            bindingJson.put("name", binding.getName());
            final Map<String, Object> attributes = binding.getBindingAttributes();
            for (final Map.Entry<String, Object> entry : attributes.entrySet()) {
                // Skip 'name' property since we have serialized before the for-loop
                if (bindingJson.containsKey(entry.getKey())) {
                    continue;
                }
                bindingJson.put(entry.getKey(), entry.getValue());
            }
            lists.add(bindingJson);
        }
        json.put("bindings", lists.toArray());
    }
    file.getParentFile().mkdirs();
    JsonUtils.writeJsonToFile(file, json);
}
Also used : Binding(com.microsoft.azure.toolkit.lib.legacy.function.bindings.Binding) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with Binding

use of com.microsoft.azure.toolkit.lib.legacy.function.bindings.Binding in project azure-tools-for-java by Microsoft.

the class FunctionUtils method patchStorageBinding.

private static void patchStorageBinding(final PsiMethod method, final List<Binding> bindings) {
    final PsiAnnotation storageAccount = AnnotationUtil.findAnnotation(method, StorageAccount.class.getCanonicalName());
    if (storageAccount != null) {
        // todo: Remove System.out.println
        System.out.println(message("function.binding.storage.found"));
        final String connectionString = AnnotationUtil.getDeclaredStringAttributeValue(storageAccount, "value");
        // Replace empty connection string
        bindings.stream().filter(binding -> binding.getBindingEnum().isStorage()).filter(binding -> StringUtils.isEmpty((String) binding.getAttribute("connection"))).forEach(binding -> binding.setAttribute("connection", connectionString));
    } else {
        // todo: Remove System.out.println
        System.out.println(message("function.binding.storage.notFound"));
    }
}
Also used : JsonObject(com.google.gson.JsonObject) Azure(com.microsoft.azure.toolkit.lib.Azure) AzureExecutionException(com.microsoft.azure.toolkit.lib.common.exception.AzureExecutionException) Arrays(java.util.Arrays) AzureConfiguration(com.microsoft.azure.toolkit.lib.AzureConfiguration) AzureBundle.message(com.microsoft.intellij.ui.messages.AzureBundle.message) OrderEnumerator(com.intellij.openapi.roots.OrderEnumerator) JsonUtils(com.microsoft.azuretools.utils.JsonUtils) VirtualFile(com.intellij.openapi.vfs.VirtualFile) StringUtils(org.apache.commons.lang3.StringUtils) Library(com.intellij.openapi.roots.libraries.Library) ClassUtils(org.apache.commons.lang3.ClassUtils) PsiJavaCodeReferenceElement(com.intellij.psi.PsiJavaCodeReferenceElement) Map(java.util.Map) Module(com.intellij.openapi.module.Module) CompilerModuleExtension(com.intellij.openapi.roots.CompilerModuleExtension) Path(java.nio.file.Path) MavenProject(org.jetbrains.idea.maven.project.MavenProject) FunctionCliResolver(com.microsoft.azure.toolkit.lib.appservice.utils.FunctionCliResolver) OrderRootType(com.intellij.openapi.roots.OrderRootType) Nullable(com.microsoft.azuretools.azurecommons.helpers.Nullable) MetaAnnotationUtil(com.intellij.codeInsight.MetaAnnotationUtil) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) Collectors(java.util.stream.Collectors) MavenProjectsManager(org.jetbrains.idea.maven.project.MavenProjectsManager) List(java.util.List) AnnotatedElementsSearch(com.intellij.psi.search.searches.AnnotatedElementsSearch) AnnotationUtil(com.intellij.codeInsight.AnnotationUtil) PsiAnnotation(com.intellij.psi.PsiAnnotation) IntelliJSecureStore(com.microsoft.intellij.secure.IntelliJSecureStore) JvmParameter(com.intellij.lang.jvm.JvmParameter) AzureOperation(com.microsoft.azure.toolkit.lib.common.operation.AzureOperation) ModuleManager(com.intellij.openapi.module.ModuleManager) JavaPsiFacade(com.intellij.psi.JavaPsiFacade) ArrayUtils(org.apache.commons.lang3.ArrayUtils) HashMap(java.util.HashMap) ContainerUtil(com.intellij.util.containers.ContainerUtil) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) PsiClass(com.intellij.psi.PsiClass) Charset(java.nio.charset.Charset) Project(com.intellij.openapi.project.Project) FunctionConfiguration(com.microsoft.azure.toolkit.lib.legacy.function.configurations.FunctionConfiguration) Binding(com.microsoft.azure.toolkit.lib.legacy.function.bindings.Binding) AzureToolkitRuntimeException(com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException) PsiModifierListOwner(com.intellij.psi.PsiModifierListOwner) PsiMethod(com.intellij.psi.PsiMethod) MapUtils(org.apache.commons.collections.MapUtils) Files(java.nio.file.Files) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) JvmAnnotation(com.intellij.lang.jvm.JvmAnnotation) File(java.io.File) BindingEnum(com.microsoft.azure.toolkit.lib.legacy.function.bindings.BindingEnum) Paths(java.nio.file.Paths) Log(com.microsoft.azure.toolkit.lib.common.logging.Log) StorageAccount(com.microsoft.azure.functions.annotation.StorageAccount) StorageAccount(com.microsoft.azure.functions.annotation.StorageAccount) PsiAnnotation(com.intellij.psi.PsiAnnotation)

Aggregations

Binding (com.microsoft.azure.toolkit.lib.legacy.function.bindings.Binding)7 ArrayList (java.util.ArrayList)4 JsonObject (com.google.gson.JsonObject)3 JvmAnnotation (com.intellij.lang.jvm.JvmAnnotation)2 PsiAnnotation (com.intellij.psi.PsiAnnotation)2 PsiJavaCodeReferenceElement (com.intellij.psi.PsiJavaCodeReferenceElement)2 FunctionConfiguration (com.microsoft.azure.toolkit.lib.legacy.function.configurations.FunctionConfiguration)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 AnnotationUtil (com.intellij.codeInsight.AnnotationUtil)1 MetaAnnotationUtil (com.intellij.codeInsight.MetaAnnotationUtil)1 JvmParameter (com.intellij.lang.jvm.JvmParameter)1 Module (com.intellij.openapi.module.Module)1 ModuleManager (com.intellij.openapi.module.ModuleManager)1 Project (com.intellij.openapi.project.Project)1 CompilerModuleExtension (com.intellij.openapi.roots.CompilerModuleExtension)1 OrderEnumerator (com.intellij.openapi.roots.OrderEnumerator)1 OrderRootType (com.intellij.openapi.roots.OrderRootType)1 Library (com.intellij.openapi.roots.libraries.Library)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1