From cdb9a5c9957ef867c91566441ecd54eec191d8c6 Mon Sep 17 00:00:00 2001 From: Blaize M Kaye Date: Tue, 26 Sep 2023 10:00:28 +1300 Subject: [PATCH] Different processes for binary and text types --- internal/handler/main.go | 91 ++++++++++++++++++++++++++++------------ main.go | 4 +- 2 files changed, 67 insertions(+), 28 deletions(-) diff --git a/internal/handler/main.go b/internal/handler/main.go index bc94825..94f4b4b 100644 --- a/internal/handler/main.go +++ b/internal/handler/main.go @@ -271,42 +271,81 @@ func (h *Messaging) sendToLagoonAPI(incoming *InsightsMessage, resource Resource if insights.InputPayload == Payload { for _, p := range incoming.Payload { - for _, filter := range parserFilters { - var result []interface{} - var source string + parserFilterLoopForPayloads(insights, p, h, apiClient, resource) + } + } - if insights.LagoonType == Facts { - json, err := json.Marshal(p) - if err != nil { - log.Println(fmt.Errorf(err.Error())) - } + if insights.InputPayload == BinaryPayload { + for _, p := range incoming.BinaryPayload { + parserFilterLoopForBinaryPayloads(insights, p, h, apiClient, resource) + } + } - result, source, err = filter(h, insights, fmt.Sprintf("%s", json), apiClient, resource) + return nil +} + +func parserFilterLoopForBinaryPayloads(insights InsightsData, p string, h *Messaging, apiClient graphql.Client, resource ResourceDestination) { + for _, filter := range parserFilters { + + if insights.LagoonType == Facts { // This should be more or less trivially true + + result, source, err := filter(h, insights, p, apiClient, resource) + if err != nil { + log.Println(fmt.Errorf(err.Error())) + } + + for _, r := range result { + if fact, ok := r.(LagoonFact); ok { + // Handle single fact + err = h.sendFactsToLagoonAPI([]LagoonFact{fact}, apiClient, resource, source) if err != nil { - log.Println(fmt.Errorf(err.Error())) + fmt.Println(err) } + } else if facts, ok := r.([]LagoonFact); ok { + // Handle slice of facts + h.sendFactsToLagoonAPI(facts, apiClient, resource, source) + } else { + // Unexpected type returned from filter() + log.Printf("unexpected type returned from filter(): %T\n", r) + } + } + } + } +} + +func parserFilterLoopForPayloads(insights InsightsData, p PayloadInput, h *Messaging, apiClient graphql.Client, resource ResourceDestination) { + for _, filter := range parserFilters { + var result []interface{} + var source string + + if insights.LagoonType == Facts { // This should be more or less trivially true + json, err := json.Marshal(p) + if err != nil { + log.Println(fmt.Errorf(err.Error())) + } + + result, source, err = filter(h, insights, fmt.Sprintf("%s", json), apiClient, resource) + if err != nil { + log.Println(fmt.Errorf(err.Error())) + } - for _, r := range result { - if fact, ok := r.(LagoonFact); ok { - // Handle single fact - err = h.sendFactsToLagoonAPI([]LagoonFact{fact}, apiClient, resource, source) - if err != nil { - fmt.Println(err) - } - } else if facts, ok := r.([]LagoonFact); ok { - // Handle slice of facts - h.sendFactsToLagoonAPI(facts, apiClient, resource, source) - } else { - // Unexpected type returned from filter() - log.Printf("unexpected type returned from filter(): %T\n", r) - } + for _, r := range result { + if fact, ok := r.(LagoonFact); ok { + // Handle single fact + err = h.sendFactsToLagoonAPI([]LagoonFact{fact}, apiClient, resource, source) + if err != nil { + fmt.Println(err) } + } else if facts, ok := r.([]LagoonFact); ok { + // Handle slice of facts + h.sendFactsToLagoonAPI(facts, apiClient, resource, source) + } else { + // Unexpected type returned from filter() + log.Printf("unexpected type returned from filter(): %T\n", r) } } } } - - return nil } func (h *Messaging) sendFactsToLagoonAPI(facts []LagoonFact, apiClient graphql.Client, resource ResourceDestination, source string) error { diff --git a/main.go b/main.go index f533f68..500a43e 100644 --- a/main.go +++ b/main.go @@ -55,8 +55,8 @@ func main() { flag.StringVar(&lagoonAPIHost, "lagoon-api-host", "http://localhost:3000/graphql", "The host for the lagoon api.") flag.StringVar(&jwtTokenSigningKey, "jwt-token-signing-key", "super-secret-string", "The jwt signing token key or secret.") flag.StringVar(&jwtAudience, "jwt-audience", "api.dev", "The jwt audience.") - flag.StringVar(&jwtSubject, "jwt-subject", "actions-handler", "The jwt audience.") - flag.StringVar(&jwtIssuer, "jwt-issuer", "actions-handler", "The jwt audience.") + flag.StringVar(&jwtSubject, "jwt-subject", "insights-handler", "The jwt audience.") + flag.StringVar(&jwtIssuer, "jwt-issuer", "insights-handler", "The jwt issuer.") flag.StringVar(&insightsQueueName, "insights-queue-name", "lagoon-insights:items", "The name of the queue in rabbitmq to use.") flag.StringVar(&insightsExchange, "insights-exchange", "lagoon-insights", "The name of the exchange in rabbitmq to use.") flag.StringVar(&s3SecretAccessKey, "secret-access-key", "minio123", "s3 secret access key to use.")