diff --git a/internal/handler/insightsFactsParserFilter.go b/internal/handler/insightsFactsParserFilter.go deleted file mode 100644 index 0343188..0000000 --- a/internal/handler/insightsFactsParserFilter.go +++ /dev/null @@ -1,100 +0,0 @@ -package handler - -import ( - "encoding/json" - "fmt" - "io/ioutil" - "log/slog" - "strings" - - "github.com/Khan/genqlient/graphql" -) - -type FactsPayload struct { - Facts []LagoonFact `json:"facts,omitempty"` -} - -// Processes facts from insights payloads that come from reconcilled kubernetes payloads (e.g. include labels/annotations and compressed/encoded data) -func processFactsInsightsData(h *Messaging, insights InsightsData, v string, apiClient graphql.Client, resource ResourceDestination) ([]LagoonFact, string, error) { - source := fmt.Sprintf("insights:facts:%s", resource.Service) - logger := slog.With("ProjectName", resource.Project, "EnvironmentName", resource.Environment, "Source", source) - if insights.LagoonType == Facts && insights.InsightsType == Raw { - return ProcessFactsData(v, logger, source) - } - return nil, "", nil -} - -func ProcessFactsData(v string, logger *slog.Logger, source string) ([]LagoonFact, string, error) { - r := strings.NewReader(v) - // Decode base64 - res, err := ioutil.ReadAll(r) - if err != nil { - slog.Error("Error reading insights data", "Error", err) - } - - facts, err := processFactsFromJSON(logger, res, source) - if err != nil { - return nil, "", err - } - - facts, err = KeyFactsFilter(facts) - if err != nil { - return nil, "", err - } - - if len(facts) == 0 { - return nil, "", fmt.Errorf("no facts to process") - } - - logger.Info("Successfully processed facts", "number", len(facts)) - - return facts, source, nil -} - -func processFactsFromJSON(logger *slog.Logger, facts []byte, source string) ([]LagoonFact, error) { - var factsInput []LagoonFact - - var factsPayload FactsPayload - err := json.Unmarshal(facts, &factsPayload) - if err != nil { - return factsInput, err - } - - if len(factsPayload.Facts) == 0 { - return factsInput, nil - } - - var filteredFacts []LagoonFact - keyFactsExistMap := make(map[string]bool) - - // Filter key facts - for _, v := range factsPayload.Facts { - if _, ok := keyFactsExistMap[v.Name]; !ok { - keyFactsExistMap[v.Name] = true - filteredFacts = append(filteredFacts, v) - } - } - - for _, f := range filteredFacts { - fact := LagoonFact{ - Environment: f.Environment, - Name: f.Name, - Value: f.Value, - Source: source, - Description: f.Description, - KeyFact: f.KeyFact, - Type: FactTypeText, - } - logger.Debug("Processing fact", "name", f.Name, "value", f.Value) - fact, err = ProcessLagoonFactAgainstRegisteredFilters(fact, f) - if err != nil { - return factsInput, err - } - factsInput = append(factsInput, fact) - } - return factsInput, nil -} - -func init() { - RegisterParserFilter(processFactsInsightsData) -} diff --git a/internal/handler/insightsFactsParserFilter_test.go b/internal/handler/insightsFactsParserFilter_test.go deleted file mode 100644 index 1f16cff..0000000 --- a/internal/handler/insightsFactsParserFilter_test.go +++ /dev/null @@ -1,124 +0,0 @@ -package handler - -import ( - "encoding/json" - "io" - "io/ioutil" - "log" - "net/http" - "net/http/httptest" - "reflect" - "testing" - - "github.com/Khan/genqlient/graphql" - "github.com/cheshir/go-mq" -) - -func Test_processFactsInsightsData(t *testing.T) { - type args struct { - h *Messaging - insights InsightsData - v string - apiClient graphql.Client - resource ResourceDestination - } - - h := Messaging{ - Config: mq.Config{}, - LagoonAPI: LagoonAPI{ - Endpoint: "http://localhost:3000/graphql", - }, - } - apiClient := h.getApiClient() - - testResponse, err := ioutil.ReadFile("./testassets/rawFactsInsightsPayload.json") - if err != nil { - t.Fatalf("Could not open file") - } - server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.URL.Path != "/" { - t.Errorf("Expected to request '/fixedvalue', got: %s", r.URL.Path) - } - w.WriteHeader(http.StatusOK) - w.Write(testResponse) - })) - defer server.Close() - - resp, err := http.Get(server.URL) - if err != nil { - panic(err) - } - defer resp.Body.Close() - - b, err := io.ReadAll(resp.Body) - if err != nil { - log.Fatalln(err) - } - - incoming := &InsightsMessage{} - json.Unmarshal(b, incoming) - - var payload PayloadInput - for _, p := range incoming.Payload { - payload = p - } - - data, err := json.Marshal(payload) - if err != nil { - panic(err) - } - - var tests = []struct { - name string - args args - want []LagoonFact - want1 string - wantErr bool - }{ - { - name: "raw facts insights payload", - args: args{ - h: &Messaging{}, - insights: InsightsData{ - InputPayload: Payload, - InsightsType: Raw, - LagoonType: Facts, - }, - v: string(data), - apiClient: apiClient, - resource: ResourceDestination{ - Project: "lagoon-demo-org", - Service: "cli", - }, - }, - want: []LagoonFact{ - { - Environment: 3, - Name: "drupal-core", - Value: "9.0.1", - Source: "insights:facts:cli", - Description: "Drupal CMS version found on environment", - KeyFact: true, - Type: "TEXT", - }, - }, - want1: "insights:facts:cli", - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, got1, err := processFactsInsightsData(tt.args.h, tt.args.insights, tt.args.v, tt.args.apiClient, tt.args.resource) - if (err != nil) != tt.wantErr { - t.Errorf("processFactsInsightsData() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("processFactsInsightsData() got = %v, want %v", got, tt.want) - } - if got1 != tt.want1 { - t.Errorf("processFactsInsightsData() got1 = %v, want %v", got1, tt.want1) - } - }) - } -} diff --git a/internal/handler/messaging.go b/internal/handler/messaging.go index ae873a0..506c255 100644 --- a/internal/handler/messaging.go +++ b/internal/handler/messaging.go @@ -215,7 +215,7 @@ func preprocessIncomingMessageData(incoming *InsightsMessage) (ResourceDestinati case "image-gz": insights.InsightsType = Image case "direct": - insights.InsightsType = Direct + return resource, insights, fmt.Errorf("insightsType of 'direct' is deprecated, expect 'direct.facts' - will not process") default: insights.InsightsType = Raw } diff --git a/internal/handler/testassets/rawFactsInsightsPayload.json b/internal/handler/testassets/rawFactsInsightsPayload.json deleted file mode 100644 index 564fa66..0000000 --- a/internal/handler/testassets/rawFactsInsightsPayload.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "payload": [ - { - "project": "lagoon-demo-org", - "environment": "main", - "facts": [ - { - "name": "drupal-core", - "value": "9.0.1", - "environment": 3, - "source": "insights:facts:cli", - "description": "Drupal CMS version found on environment", - "category": "Framework", - "keyFact": true, - "type": "TEXT" - }, - { - "name": "php-version", - "value": "8.0.3", - "environment": 3, - "source": "insights:facts:cli", - "description": "PHP version found on environment", - "category": "Programming language", - "keyFact": false, - "type": "TEXT" - } - ] - } - ], - "binaryPayload": {}, - "annotations": {}, - "labels": { - "lagoon.sh/project": "lagoon-demo-org", - "lagoon.sh/environment": "main", - "lagoon.sh/service": "cli", - "lagoon.sh/insightsType": "raw", - "lagoon.sh/insightsOutputFileMIMEType": "application/json", - "lagoon.sh/insightsOutputFileExt": "json" - } -}