Esempio n. 1
0
        private AFunctionSource ConvertFunctionSource(ModuleItemNode function, int index, FunctionSourceNode source)
        {
            var type = DeterminNodeType("source", index, source, FunctionSourceNode.FieldCombinations, new[] {
                "Api",
                "Schedule",
                "S3",
                "SlackCommand",
                "Topic",
                "Sqs",
                "Alexa",
                "DynamoDB",
                "Kinesis",
                "WebSocket"
            });

            switch (type)
            {
            case "Api":
                return(AtLocation("Api", () => {
                    // extract http method from route
                    var api = source.Api.Trim();
                    var pathSeparatorIndex = api.IndexOfAny(new[] { ':', ' ' });
                    if (pathSeparatorIndex < 0)
                    {
                        LogError("invalid api format");
                        return new RestApiSource {
                            HttpMethod = "ANY",
                            Path = new string[0],
                            Integration = ApiGatewaySourceIntegration.RequestResponse
                        };
                    }
                    var method = api.Substring(0, pathSeparatorIndex).ToUpperInvariant();
                    if (method == "*")
                    {
                        method = "ANY";
                    }
                    var path = api.Substring(pathSeparatorIndex + 1).TrimStart().Split('/', StringSplitOptions.RemoveEmptyEntries);

                    // parse integration into a valid enum
                    var integration = AtLocation("Integration", () => Enum.Parse <ApiGatewaySourceIntegration>(source.Integration ?? "RequestResponse", ignoreCase: true));
                    return new RestApiSource {
                        HttpMethod = method,
                        Path = path,
                        Integration = integration,
                        OperationName = source.OperationName,
                        ApiKeyRequired = source.ApiKeyRequired,
                        Invoke = source.Invoke
                    };
                }));

            case "Schedule":
                return(AtLocation("Schedule", () => new ScheduleSource {
                    Expression = source.Schedule,
                    Name = source.Name
                }));

            case "S3":
                return(AtLocation("S3", () => new S3Source {
                    Bucket = source.S3,
                    Events = source.Events ?? new List <string> {
                        // default S3 events to listen to
                        "s3:ObjectCreated:*"
                    },
                    Prefix = source.Prefix,
                    Suffix = source.Suffix
                }));

            case "SlackCommand":
                return(AtLocation("SlackCommand", () => new RestApiSource {
                    HttpMethod = "POST",
                    Path = source.SlackCommand.Split('/', StringSplitOptions.RemoveEmptyEntries),
                    Integration = ApiGatewaySourceIntegration.SlackCommand,
                    OperationName = source.OperationName
                }));

            case "Topic":
                return(AtLocation("Topic", () => new TopicSource {
                    TopicName = source.Topic,
                    Filters = source.Filters
                }));

            case "Sqs":
                return(AtLocation("Sqs", () => new SqsSource {
                    Queue = source.Sqs,
                    BatchSize = source.BatchSize ?? 10
                }));

            case "Alexa":
                return(AtLocation("Alexa", () => new AlexaSource {
                    EventSourceToken = source.Alexa
                }));

            case "DynamoDB":
                return(AtLocation("DynamoDB", () => new DynamoDBSource {
                    DynamoDB = source.DynamoDB,
                    BatchSize = source.BatchSize ?? 100,
                    StartingPosition = source.StartingPosition ?? "LATEST"
                }));

            case "Kinesis":
                return(AtLocation("Kinesis", () => new KinesisSource {
                    Kinesis = source.Kinesis,
                    BatchSize = source.BatchSize ?? 100,
                    StartingPosition = source.StartingPosition ?? "LATEST"
                }));

            case "WebSocket":
                return(AtLocation("WebSocket", () => new WebSocketSource {
                    RouteKey = source.WebSocket.Trim(),
                    OperationName = source.OperationName,
                    ApiKeyRequired = source.ApiKeyRequired,
                    Invoke = source.Invoke
                }));
            }
            return(null);
        }
        public AFunctionSource ConvertFunctionSource(FunctionNode function, int index, FunctionSourceNode source)
        {
            return(AtLocation <AFunctionSource>($"{index}", () => {
                if (source.Topic != null)
                {
                    return new TopicSource {
                        TopicName = source.Topic
                    };
                }
                if (source.Schedule != null)
                {
                    return AtLocation("Schedule", () => {
                        return new ScheduleSource {
                            Expression = source.Schedule,
                            Name = source.Name
                        };
                    }, null);
                }
                if (source.Api != null)
                {
                    return AtLocation("Api", () => {
                        // extract http method from route
                        var api = source.Api.Trim();
                        var pathSeparatorIndex = api.IndexOfAny(new[] { ':', ' ' });
                        if (pathSeparatorIndex < 0)
                        {
                            AddError("invalid api format");
                            return new ApiGatewaySource {
                                Method = "ANY",
                                Path = new string[0],
                                Integration = ApiGatewaySourceIntegration.RequestResponse
                            };
                        }
                        var method = api.Substring(0, pathSeparatorIndex).ToUpperInvariant();
                        if (method == "*")
                        {
                            method = "ANY";
                        }
                        var path = api.Substring(pathSeparatorIndex + 1).TrimStart().Split('/', StringSplitOptions.RemoveEmptyEntries);

                        // parse integration into a valid enum
                        var integration = AtLocation("Integration", () => Enum.Parse <ApiGatewaySourceIntegration>(source.Integration ?? "RequestResponse", ignoreCase: true), ApiGatewaySourceIntegration.Unsupported);
                        return new ApiGatewaySource {
                            Method = method,
                            Path = path,
                            Integration = integration,
                            OperationName = source.OperationName,
                            ApiKeyRequired = source.ApiKeyRequired
                        };
                    }, null);
                }
                if (source.SlackCommand != null)
                {
                    return AtLocation("SlackCommand", () => {
                        // parse integration into a valid enum
                        return new ApiGatewaySource {
                            Method = "POST",
                            Path = source.SlackCommand.Split('/', StringSplitOptions.RemoveEmptyEntries),
                            Integration = ApiGatewaySourceIntegration.SlackCommand,
                            OperationName = source.OperationName
                        };
                    }, null);
                }
                if (source.S3 != null)
                {
                    return new S3Source {
                        Bucket = source.S3,
                        Events = source.Events ?? new List <string> {
                            // default S3 events to listen to
                            "s3:ObjectCreated:*"
                        },
                        Prefix = source.Prefix,
                        Suffix = source.Suffix
                    };
                }
                if (source.Sqs != null)
                {
                    return new SqsSource {
                        Queue = source.Sqs,
                        BatchSize = source.BatchSize ?? 10
                    };
                }
                if (source.Alexa != null)
                {
                    var alexaSkillId = string.IsNullOrWhiteSpace(source.Alexa) || (source.Alexa == "*")
                        ? null
                        : source.Alexa;
                    return new AlexaSource {
                        EventSourceToken = alexaSkillId
                    };
                }
                if (source.DynamoDB != null)
                {
                    return new DynamoDBSource {
                        DynamoDB = source.DynamoDB,
                        BatchSize = source.BatchSize ?? 100,
                        StartingPosition = source.StartingPosition ?? "LATEST"
                    };
                }
                if (source.Kinesis != null)
                {
                    return new KinesisSource {
                        Kinesis = source.Kinesis,
                        BatchSize = source.BatchSize ?? 100,
                        StartingPosition = source.StartingPosition ?? "LATEST"
                    };
                }
                if (source.Macro != null)
                {
                    var macroName = source.Macro;
                    if ((macroName == "") || (macroName == "*"))
                    {
                        macroName = function.Name;
                    }
                    return new MacroSource {
                        MacroName = macroName
                    };
                }
                return null;
            }, null));

            throw new ModelParserException("invalid function event");
        }