|
9 | 9 | import com.amazonaws.services.lambda.model.FunctionConfiguration; |
10 | 10 | import com.amazonaws.services.lambda.model.InvokeRequest; |
11 | 11 | import com.amazonaws.services.lambda.model.InvokeResult; |
| 12 | +import com.amazonaws.services.lambda.model.ListAliasesRequest; |
| 13 | +import com.amazonaws.services.lambda.model.ListAliasesResult; |
12 | 14 | import com.amazonaws.services.lambda.model.ListFunctionsResult; |
| 15 | +import com.amazonaws.services.lambda.model.ListVersionsByFunctionRequest; |
| 16 | +import com.amazonaws.services.lambda.model.ListVersionsByFunctionResult; |
13 | 17 | import com.amazonaws.services.lambda.model.ResourceNotFoundException; |
14 | 18 | import com.appsmith.external.exceptions.pluginExceptions.AppsmithPluginError; |
15 | 19 | import com.appsmith.external.exceptions.pluginExceptions.AppsmithPluginException; |
@@ -70,6 +74,10 @@ public Mono<ActionExecutionResult> execute( |
70 | 74 | ActionExecutionResult result; |
71 | 75 | switch (Objects.requireNonNull(command)) { |
72 | 76 | case "LIST_FUNCTIONS" -> result = listFunctions(actionConfiguration, connection); |
| 77 | + case "LIST_FUNCTION_VERSIONS" -> result = |
| 78 | + listFunctionVersions(actionConfiguration, connection); |
| 79 | + case "LIST_FUNCTION_ALIASES" -> result = |
| 80 | + listFunctionAliases(actionConfiguration, connection); |
73 | 81 | case "INVOKE_FUNCTION" -> result = invokeFunction(actionConfiguration, connection); |
74 | 82 | default -> throw new IllegalStateException("Unexpected value: " + command); |
75 | 83 | } |
@@ -98,24 +106,84 @@ public Mono<TriggerResultDTO> trigger( |
98 | 106 | throw new AppsmithPluginException( |
99 | 107 | AppsmithPluginError.PLUGIN_EXECUTE_ARGUMENT_ERROR, "request type is missing"); |
100 | 108 | } |
101 | | - ActionExecutionResult actionExecutionResult = listFunctions(null, connection); |
102 | | - ArrayNode body = (ArrayNode) actionExecutionResult.getBody(); |
103 | | - List<Map<String, String>> functionNames = StreamSupport.stream(body.spliterator(), false) |
104 | | - .map(function -> function.get("functionName").asText()) |
105 | | - .sorted() |
106 | | - .map(functionName -> Map.of("label", functionName, "value", functionName)) |
107 | | - .collect(Collectors.toList()); |
| 109 | + |
| 110 | + String requestType = request.getRequestType(); |
| 111 | + ActionExecutionResult actionExecutionResult; |
| 112 | + List<Map<String, String>> options; |
| 113 | + |
| 114 | + switch (requestType) { |
| 115 | + case "FUNCTION_NAMES" -> { |
| 116 | + actionExecutionResult = listFunctions(null, connection); |
| 117 | + ArrayNode body = (ArrayNode) actionExecutionResult.getBody(); |
| 118 | + options = StreamSupport.stream(body.spliterator(), false) |
| 119 | + .map(function -> function.get("functionName").asText()) |
| 120 | + .sorted() |
| 121 | + .map(functionName -> Map.of("label", functionName, "value", functionName)) |
| 122 | + .collect(Collectors.toList()); |
| 123 | + } |
| 124 | + case "FUNCTION_VERSIONS" -> { |
| 125 | + String functionName = request.getParams().get("functionName"); |
| 126 | + if (!StringUtils.hasText(functionName)) { |
| 127 | + throw new AppsmithPluginException( |
| 128 | + AppsmithPluginError.PLUGIN_EXECUTE_ARGUMENT_ERROR, |
| 129 | + "function name is required for listing versions"); |
| 130 | + } |
| 131 | + actionExecutionResult = listFunctionVersions(null, connection, functionName); |
| 132 | + ArrayNode body = (ArrayNode) actionExecutionResult.getBody(); |
| 133 | + options = StreamSupport.stream(body.spliterator(), false) |
| 134 | + .map(version -> version.get("version").asText()) |
| 135 | + .sorted() |
| 136 | + .map(version -> Map.of("label", version, "value", version)) |
| 137 | + .collect(Collectors.toList()); |
| 138 | + } |
| 139 | + case "FUNCTION_ALIASES" -> { |
| 140 | + String functionName = request.getParams().get("functionName"); |
| 141 | + if (!StringUtils.hasText(functionName)) { |
| 142 | + throw new AppsmithPluginException( |
| 143 | + AppsmithPluginError.PLUGIN_EXECUTE_ARGUMENT_ERROR, |
| 144 | + "function name is required for listing aliases"); |
| 145 | + } |
| 146 | + actionExecutionResult = listFunctionAliases(null, connection, functionName); |
| 147 | + ArrayNode body = (ArrayNode) actionExecutionResult.getBody(); |
| 148 | + options = StreamSupport.stream(body.spliterator(), false) |
| 149 | + .map(alias -> alias.get("name").asText()) |
| 150 | + .sorted() |
| 151 | + .map(alias -> Map.of("label", alias, "value", alias)) |
| 152 | + .collect(Collectors.toList()); |
| 153 | + } |
| 154 | + default -> throw new AppsmithPluginException( |
| 155 | + AppsmithPluginError.PLUGIN_EXECUTE_ARGUMENT_ERROR, "Unsupported request type: " + requestType); |
| 156 | + } |
108 | 157 |
|
109 | 158 | TriggerResultDTO triggerResultDTO = new TriggerResultDTO(); |
110 | | - triggerResultDTO.setTrigger(functionNames); |
| 159 | + triggerResultDTO.setTrigger(options); |
111 | 160 |
|
112 | 161 | return Mono.just(triggerResultDTO); |
113 | 162 | } |
114 | 163 |
|
115 | 164 | ActionExecutionResult invokeFunction(ActionConfiguration actionConfiguration, AWSLambda connection) { |
116 | 165 | InvokeRequest invokeRequest = new InvokeRequest(); |
117 | | - invokeRequest.setFunctionName( |
118 | | - getDataValueSafelyFromFormData(actionConfiguration.getFormData(), "functionName", STRING_TYPE)); |
| 166 | + |
| 167 | + // Build function name with version/alias if specified |
| 168 | + String functionName = |
| 169 | + getDataValueSafelyFromFormData(actionConfiguration.getFormData(), "functionName", STRING_TYPE); |
| 170 | + String functionVersion = |
| 171 | + getDataValueSafelyFromFormData(actionConfiguration.getFormData(), "functionVersion", STRING_TYPE); |
| 172 | + String functionAlias = |
| 173 | + getDataValueSafelyFromFormData(actionConfiguration.getFormData(), "functionAlias", STRING_TYPE); |
| 174 | + |
| 175 | + // Construct the full function name |
| 176 | + String fullFunctionName = functionName; |
| 177 | + if (StringUtils.hasText(functionAlias)) { |
| 178 | + // If alias is specified, use it (alias takes precedence over version) |
| 179 | + fullFunctionName = functionName + ":" + functionAlias; |
| 180 | + } else if (StringUtils.hasText(functionVersion)) { |
| 181 | + // If version is specified and no alias, use version |
| 182 | + fullFunctionName = functionName + ":" + functionVersion; |
| 183 | + } |
| 184 | + // If neither version nor alias is specified, use the function name as-is (defaults to $LATEST) |
| 185 | + |
| 186 | + invokeRequest.setFunctionName(fullFunctionName); |
119 | 187 | invokeRequest.setPayload( |
120 | 188 | getDataValueSafelyFromFormData(actionConfiguration.getFormData(), "body", STRING_TYPE)); |
121 | 189 | invokeRequest.setInvocationType( |
@@ -145,6 +213,56 @@ ActionExecutionResult listFunctions(ActionConfiguration actionConfiguration, AWS |
145 | 213 | return result; |
146 | 214 | } |
147 | 215 |
|
| 216 | + ActionExecutionResult listFunctionVersions( |
| 217 | + ActionConfiguration actionConfiguration, AWSLambda connection, String functionName) { |
| 218 | + if (actionConfiguration != null) { |
| 219 | + functionName = |
| 220 | + getDataValueSafelyFromFormData(actionConfiguration.getFormData(), "functionName", STRING_TYPE); |
| 221 | + } |
| 222 | + |
| 223 | + ListVersionsByFunctionRequest request = new ListVersionsByFunctionRequest(); |
| 224 | + request.setFunctionName(functionName); |
| 225 | + |
| 226 | + ListVersionsByFunctionResult listVersionsResult = connection.listVersionsByFunction(request); |
| 227 | + List<FunctionConfiguration> versions = listVersionsResult.getVersions(); |
| 228 | + |
| 229 | + ActionExecutionResult result = new ActionExecutionResult(); |
| 230 | + result.setBody(objectMapper.valueToTree(versions)); |
| 231 | + result.setIsExecutionSuccess(true); |
| 232 | + return result; |
| 233 | + } |
| 234 | + |
| 235 | + ActionExecutionResult listFunctionVersions(ActionConfiguration actionConfiguration, AWSLambda connection) { |
| 236 | + String functionName = |
| 237 | + getDataValueSafelyFromFormData(actionConfiguration.getFormData(), "functionName", STRING_TYPE); |
| 238 | + return listFunctionVersions(null, connection, functionName); |
| 239 | + } |
| 240 | + |
| 241 | + ActionExecutionResult listFunctionAliases( |
| 242 | + ActionConfiguration actionConfiguration, AWSLambda connection, String functionName) { |
| 243 | + if (actionConfiguration != null) { |
| 244 | + functionName = |
| 245 | + getDataValueSafelyFromFormData(actionConfiguration.getFormData(), "functionName", STRING_TYPE); |
| 246 | + } |
| 247 | + |
| 248 | + ListAliasesRequest request = new ListAliasesRequest(); |
| 249 | + request.setFunctionName(functionName); |
| 250 | + |
| 251 | + ListAliasesResult listAliasesResult = connection.listAliases(request); |
| 252 | + List<com.amazonaws.services.lambda.model.AliasConfiguration> aliases = listAliasesResult.getAliases(); |
| 253 | + |
| 254 | + ActionExecutionResult result = new ActionExecutionResult(); |
| 255 | + result.setBody(objectMapper.valueToTree(aliases)); |
| 256 | + result.setIsExecutionSuccess(true); |
| 257 | + return result; |
| 258 | + } |
| 259 | + |
| 260 | + ActionExecutionResult listFunctionAliases(ActionConfiguration actionConfiguration, AWSLambda connection) { |
| 261 | + String functionName = |
| 262 | + getDataValueSafelyFromFormData(actionConfiguration.getFormData(), "functionName", STRING_TYPE); |
| 263 | + return listFunctionAliases(null, connection, functionName); |
| 264 | + } |
| 265 | + |
148 | 266 | @Override |
149 | 267 | public Mono<AWSLambda> datasourceCreate(DatasourceConfiguration datasourceConfiguration) { |
150 | 268 | log.debug(Thread.currentThread().getName() + ": datasourceCreate() called for AWS Lambda plugin."); |
|
0 commit comments