Skip to content

Commit

Permalink
Use parsers v2 API
Browse files Browse the repository at this point in the history
  • Loading branch information
SaaldjorMike committed May 31, 2024
1 parent 5c006b6 commit b3abad4
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 40 deletions.
44 changes: 44 additions & 0 deletions api/internal/humiographql/parsers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package humiographql

type ParserTestEventInput struct {
RawString string `graphql:"rawString"`
}

type FieldHasValueInput struct {
FieldName string `graphql:"fieldName"`
ExpectedValue string `graphql:"expectedValue"`
}

type ParserTestCaseOutputAssertionsInput struct {
FieldsNotPresent []string `graphql:"fieldsNotPresent"`
FieldsHaveValues []FieldHasValueInput `graphql:"fieldsHaveValues"`
}

type ParserTestCaseAssertionsForOutputInput struct {
OutputEventIndex int `graphql:"outputEventIndex"`
Assertions ParserTestCaseOutputAssertionsInput `graphql:"assertions"`
}

type ParserTestCaseInput struct {
Event ParserTestEventInput `graphql:"event"`
OutputAssertions []ParserTestCaseAssertionsForOutputInput `graphql:"outputAssertions"`
}

type ParserTestEvent struct {
RawString string `graphql:"rawString"`
}

type ParserTestCaseOutputAssertions struct {
FieldsNotPresent []string `graphql:"fieldsNotPresent"`
FieldsHaveValues []FieldHasValueInput `graphql:"fieldsHaveValues"`
}

type ParserTestCaseAssertionsForOutput struct {
OutputEventIndex int `graphql:"outputEventIndex"`
Assertions ParserTestCaseOutputAssertions `graphql:"assertions"`
}

type ParserTestCase struct {
Event ParserTestEvent `graphql:"event"`
OutputAssertions []ParserTestCaseAssertionsForOutput `graphql:"outputAssertions"`
}
82 changes: 46 additions & 36 deletions api/parsers.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package api

import graphql "github.com/cli/shurcooL-graphql"

type ParserTestCase struct {
Input string
Output map[string]string
}
import (
graphql "github.com/cli/shurcooL-graphql"
"github.com/humio/cli/api/internal/humiographql"
)

type Parser struct {
ID string
Name string
Tests []string `yaml:",omitempty"`
Example string `yaml:",omitempty"`
Script string `yaml:",flow"`
TagFields []string `yaml:",omitempty"`
ID string
Name string
Tests []string `yaml:",omitempty"`
Example string `yaml:",omitempty"`
Script string `yaml:",flow"`
TagFields []string `yaml:",omitempty"`
FieldsToBeRemovedBeforeParsing []string `yaml:",omitempty"`
}

type Parsers struct {
Expand Down Expand Up @@ -47,12 +46,12 @@ func (p *Parsers) List(repositoryName string) ([]ParserListItem, error) {
return parsers, err
}

func (p *Parsers) Remove(repositoryName string, parserName string) error {
func (p *Parsers) Delete(repositoryName string, parserName string) error {
var mutation struct {
RemoveParser struct {
DeleteParser struct {
// We have to make a selection, so just take __typename
Typename graphql.String `graphql:"__typename"`
} `graphql:"removeParser(input: { id: $id, repositoryName: $repositoryName })"`
} `graphql:"deleteParser(input: { id: $id, repositoryName: $repositoryName })"`
}

parser, err := p.client.Parsers().Get(repositoryName, parserName)
Expand All @@ -68,34 +67,41 @@ func (p *Parsers) Remove(repositoryName string, parserName string) error {
return p.client.Mutate(&mutation, variables)
}

func (p *Parsers) Add(repositoryName string, parser *Parser, force bool) error {
func (p *Parsers) Add(repositoryName string, parser *Parser, allowOverwritingExistingParser bool) error {

var mutation struct {
CreateParser struct {
// We have to make a selection, so just take __typename
Typename graphql.String `graphql:"__typename"`
} `graphql:"createParser(input: { name: $name, repositoryName: $repositoryName, testData: $testData, tagFields: $tagFields, sourceCode: $sourceCode, force: $force})"`
} `graphql:"createParserV2(input: { name: $name, repositoryName: $repositoryName, testCases: $testCases, fieldsToTag: $fieldsToTag, fieldsToBeRemovedBeforeParsing: $fieldsToBeRemovedBeforeParsing, script: $script, allowOverwritingExistingParser: $allowOverwritingExistingParser})"`
}

tagFieldsGQL := make([]graphql.String, len(parser.TagFields))

fieldsToTagGQL := make([]graphql.String, len(parser.TagFields))
for i, field := range parser.TagFields {
tagFieldsGQL[i] = graphql.String(field)
fieldsToTagGQL[i] = graphql.String(field)
}

testsGQL := make([]graphql.String, len(parser.Tests))
fieldsToBeRemovedBeforeParsingGQL := make([]graphql.String, len(parser.FieldsToBeRemovedBeforeParsing))
for i, field := range parser.FieldsToBeRemovedBeforeParsing {
fieldsToBeRemovedBeforeParsingGQL[i] = graphql.String(field)
}

testCasesGQL := make([]humiographql.ParserTestCaseInput, len(parser.Tests))
for i, field := range parser.Tests {
testsGQL[i] = graphql.String(field)
testCasesGQL[i] = humiographql.ParserTestCaseInput{
Event: humiographql.ParserTestEventInput{RawString: field},
OutputAssertions: []humiographql.ParserTestCaseAssertionsForOutputInput{},
}
}

variables := map[string]interface{}{
"name": graphql.String(parser.Name),
"sourceCode": graphql.String(parser.Script),
"repositoryName": graphql.String(repositoryName),
"testData": testsGQL,
"tagFields": tagFieldsGQL,
"force": graphql.Boolean(force),
"name": graphql.String(parser.Name),
"script": graphql.String(parser.Script),
"repositoryName": humiographql.RepoOrViewName(repositoryName),
"testCases": testCasesGQL,
"fieldsToTag": fieldsToTagGQL,
"fieldsToBeRemovedBeforeParsing": fieldsToBeRemovedBeforeParsingGQL,
"allowOverwritingExistingParser": graphql.Boolean(allowOverwritingExistingParser),
}

return p.client.Mutate(&mutation, variables)
Expand All @@ -105,11 +111,11 @@ func (p *Parsers) Get(repositoryName string, parserName string) (*Parser, error)
var query struct {
Repository struct {
Parser *struct {
ID string
Name string
SourceCode string
TestData []string
TagFields []string
ID string
Name string
Script string
TestCases []humiographql.ParserTestCase
FieldsToTag []string
} `graphql:"parser(name: $parserName)"`
} `graphql:"repository(name: $repositoryName)"`
}
Expand All @@ -128,12 +134,16 @@ func (p *Parsers) Get(repositoryName string, parserName string) (*Parser, error)
return nil, ParserNotFound(parserName)
}

tests := make([]string, len(query.Repository.Parser.TestCases))
for i := range query.Repository.Parser.TestCases {
tests[i] = query.Repository.Parser.TestCases[i].Event.RawString
}
parser := Parser{
ID: query.Repository.Parser.ID,
Name: query.Repository.Parser.Name,
Tests: query.Repository.Parser.TestData,
Script: query.Repository.Parser.SourceCode,
TagFields: query.Repository.Parser.TagFields,
Tests: tests,
Script: query.Repository.Parser.Script,
TagFields: query.Repository.Parser.FieldsToTag,
}

return &parser, nil
Expand Down
1 change: 1 addition & 0 deletions cmd/humioctl/parsers.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func newParsersCmd() *cobra.Command {
cmd.AddCommand(newParsersListCmd())
cmd.AddCommand(newParsersRemoveCmd())
cmd.AddCommand(newParsersExportCmd())
cmd.AddCommand(newParsersShowCmd())

return cmd
}
35 changes: 35 additions & 0 deletions cmd/humioctl/parsers_get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"github.com/humio/cli/cmd/internal/format"
"github.com/spf13/cobra"
"strings"
)

func newParsersShowCmd() *cobra.Command {
cmd := cobra.Command{
Use: "show <repo> <parser>",
Short: "Show details for a parser in a repository.",
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
repoName := args[0]
parserName := args[1]
client := NewApiClient(cmd)

parser, err := client.Parsers().Get(repoName, parserName)
exitOnError(cmd, err, "Error fetching parser")

details := [][]format.Value{
{format.String("ID"), format.String(parser.ID)},
{format.String("Name"), format.String(parser.Name)},
{format.String("Script"), format.String(parser.Script)},
{format.String("TagFields"), format.String(strings.Join(parser.TagFields, "\n"))},
{format.String("TestCases"), format.String(strings.Join(parser.Tests, "\n"))},
}

printDetailsTable(cmd, details)
},
}

return &cmd
}
6 changes: 3 additions & 3 deletions cmd/humioctl/parsers_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
)

func newParsersInstallCmd() *cobra.Command {
var force bool
var allowOverwritingExistingParser bool
var filePath, url, name string

cmd := cobra.Command{
Expand Down Expand Up @@ -70,12 +70,12 @@ Use the --force flag to update existing parsers with conflicting names.
parser.Name = name
}

err = client.Parsers().Add(repositoryName, &parser, force)
err = client.Parsers().Add(repositoryName, &parser, allowOverwritingExistingParser)
exitOnError(cmd, err, "Error installing parser")
},
}

cmd.Flags().BoolVarP(&force, "force", "f", false, "Overrides any parser with the same name. This can be used for updating parser that are already installed. (See --name)")
cmd.Flags().BoolVar(&allowOverwritingExistingParser, "allow-overwriting-existing-parser", false, "Overrides any parser with the same name. This can be used for updating parser that are already installed. (See --name)")
cmd.Flags().StringVar(&filePath, "file", "", "The local file path to the parser to install.")
cmd.Flags().StringVar(&url, "url", "", "A URL to fetch the parser file from.")
cmd.Flags().StringVarP(&name, "name", "n", "", "Install the parser under a specific name, ignoring the `name` attribute in the parser file.")
Expand Down
2 changes: 1 addition & 1 deletion cmd/humioctl/parsers_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func newParsersRemoveCmd() *cobra.Command {
parser := args[1]
client := NewApiClient(cmd)

err := client.Parsers().Remove(repo, parser)
err := client.Parsers().Delete(repo, parser)
exitOnError(cmd, err, "Error removing parser")

fmt.Fprintf(cmd.OutOrStdout(), "Successfully removed parser %q from repository %q\n", parser, repo)
Expand Down

0 comments on commit b3abad4

Please sign in to comment.