Skip to content

Commit

Permalink
fix(logFilters): Fix log filter error response (#26)
Browse files Browse the repository at this point in the history
* check for duplicate create error. add test for duplicate create

* build. adding versions to matrix.go

* revert workflows. Updating go mod verison

* chainging go version to 1.19

* updating sum

* updating ci

* switching to go 1.23

* fixing ci get mod

* fixing linting issues
  • Loading branch information
michael-anderson-swi authored Sep 4, 2024
1 parent 97b4dbb commit b62aa88
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 54 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: ci

on:
pull_request:
branches:
- main

permissions:
contents: read
pull-requests: read
checks: write

jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: stable
- name: golangci-lint
uses: golangci/golangci-lint-action@v6
with:
version: latest
build-and-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: stable
- name: Install dependencies
run: go mod tidy
- name: Build
run: go build -v ./...
- name: Test
run: go test ./...
28 changes: 0 additions & 28 deletions .github/workflows/test.yml

This file was deleted.

13 changes: 13 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#https://golangci-lint.run/usage/linters/
linters:
disable-all: true
enable:
# err113 will fail because dynamic errors are defined in several places.
#- err113
# ireturn will fail because all pkg clients return interfaces.
#- ireturn
- gosec
- errcheck
- nilnil
- nilerr
- staticcheck
9 changes: 2 additions & 7 deletions example/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,9 @@ import (
swo "github.com/solarwinds/swo-client-go/pkg/client"
)

var (
apiTokenVar = "SWO_API_TOKEN"
baseUrlVar = "SWO_BASE_URL"
)

func Setup() (context.Context, *swo.Client) {
baseUrl := getEnvVar(baseUrlVar)
apiToken := getEnvVar(apiTokenVar)
baseUrl := getEnvVar("SWO_BASE_URL")
apiToken := getEnvVar("SWO_API_TOKEN")

ctx := context.Background()

Expand Down
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/solarwinds/swo-client-go

go 1.20
go 1.23

require (
github.com/Khan/genqlient v0.6.0
Expand All @@ -10,7 +10,6 @@ require (

require (
github.com/kr/pretty v0.3.0 // indirect
github.com/sergi/go-diff v1.2.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
)

Expand All @@ -19,7 +18,7 @@ require (
github.com/alexflint/go-arg v1.4.2 // indirect
github.com/alexflint/go-scalar v1.0.0 // indirect
github.com/google/go-cmp v0.6.0
github.com/vektah/gqlparser/v2 v2.5.1 // indirect
github.com/vektah/gqlparser/v2 v2.5.16 // indirect
golang.org/x/mod v0.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/tools v0.8.0 // indirect
Expand Down
21 changes: 9 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
github.com/Khan/genqlient v0.6.0 h1:Bwb1170ekuNIVIwTJEqvO8y7RxBxXu639VJOkKSrwAk=
github.com/Khan/genqlient v0.6.0/go.mod h1:rvChwWVTqXhiapdhLDV4bp9tz/Xvtewwkon4DpWWCRM=
github.com/agnivade/levenshtein v1.0.1/go.mod h1:CURSv5d9Uaml+FovSIICkLbAUZ9S4RqaHDIsdSBg7lM=
github.com/agnivade/levenshtein v1.1.1 h1:QY8M92nrzkmr798gCo3kmMyqXFzdQVpxLlGPRBij0P8=
github.com/agnivade/levenshtein v1.1.1/go.mod h1:veldBMzWxcCG2ZvUTKD2kJNRdCk5hVbJomOvKkmgYbo=
github.com/alexflint/go-arg v1.4.2 h1:lDWZAXxpAnZUq4qwb86p/3rIJJ2Li81EoMbTMujhVa0=
Expand All @@ -12,6 +11,7 @@ github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo
github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0 h1:jfIu9sQUG6Ig+0+Ap1h4unLjW6YQJpKZVmUzxsD4E/Q=
github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0/go.mod h1:t2tdKJDJF9BV14lnkjHmOQgcvEKgtqs5a1N3LNdJhGE=
github.com/bradleyjkemp/cupaloy/v2 v2.6.0 h1:knToPYa2xtfg42U3I6punFEjaGFKWQRXJwj0JTv4mTs=
github.com/bradleyjkemp/cupaloy/v2 v2.6.0/go.mod h1:bm7JXdkRd4BHJk9HpwqAI8BoAY1lps46Enkdqw6aRX0=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand All @@ -34,36 +34,33 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8=
github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/vektah/gqlparser/v2 v2.5.1 h1:ZGu+bquAY23jsxDRcYpWjttRZrUz07LbiY77gUOHcr4=
github.com/vektah/gqlparser/v2 v2.5.1/go.mod h1:mPgqFBu/woKTVYWyNk8cO3kh4S/f4aRFZrvOnp3hmCs=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/vektah/gqlparser/v2 v2.5.16 h1:1gcmLTvs3JLKXckwCwlUagVn/IlV2bwqle0vJ0vy5p8=
github.com/vektah/gqlparser/v2 v2.5.16/go.mod h1:1lz1OeCqgQbQepsGxPVywrjdBHW2T08PUS3pJqepRww=
golang.org/x/mod v0.10.0 h1:lFO9qtOdlre5W1jxS3r/4szv2/6iXxScdzjoBMXNhYk=
golang.org/x/mod v0.10.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/tools v0.8.0 h1:vSDcovVPld282ceKgDimkRSC8kpaH1dgyc9UMzlt84Y=
golang.org/x/tools v0.8.0/go.mod h1:JxBZ99ISMI5ViVkT1tr6tdNmXeTrcpVSD3vZ1RsRdN4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
4 changes: 3 additions & 1 deletion pkg/client/apiTokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package client

import (
"context"
"errors"
"fmt"
"log"
)

Expand Down Expand Up @@ -58,7 +60,7 @@ func (as *ApiTokenService) Read(ctx context.Context, id string) (*ReadApiTokenRe
}

if len(resp.User.CurrentOrganization.Tokens) == 0 {
return nil, nil
return nil, errors.New(fmt.Sprintf("api token not found. id=%s", id))
}

return &resp.User.CurrentOrganization.Tokens[0], nil
Expand Down
14 changes: 11 additions & 3 deletions pkg/client/logFilters.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package client

import (
"context"
"errors"
"fmt"
"log"
)

Expand Down Expand Up @@ -30,10 +32,16 @@ func (as *LogFilterService) Create(ctx context.Context, input CreateExclusionFil
return nil, err
}

result := resp.CreateExclusionFilter.ExclusionFilter
log.Printf("create LogFilter success. id=%s", result.Id)
createFilter := resp.CreateExclusionFilter
if createFilter.Success == false {
err := fmt.Sprintf("create LogFilter failed. code=%s message=%s", createFilter.Code, createFilter.Message)
log.Print(err)
return nil, errors.New(err)
}

return result, nil
filter := createFilter.ExclusionFilter
log.Printf("create LogFilter success. id=%s", filter.Id)
return filter, nil
}

// Returns the LogFilter entity with the given Id.
Expand Down
54 changes: 54 additions & 0 deletions pkg/client/logFilters_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client

import (
"fmt"
"net/http"
"testing"

Expand Down Expand Up @@ -229,3 +230,56 @@ func TestSwoService_LogFilterServerErrors(t *testing.T) {
t.Error("Swo.LogFilterServerErrors expected an error response")
}
}

func TestSwoService_LogFilterDupicateEntryError(t *testing.T) {
ctx, client, server, _, teardown := setup()
defer teardown()

input := &CreateExclusionFilterInput{
Name: "swo-client-go - logFilter",
Description: "logFilter description",
Expressions: []CreateExclusionFilterExpressionInput{
{
Kind: ExclusionFilterExpressionKindString,
Expression: "test string",
},
},
}

message := "Global filter for this org already exists"

server.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
gqlInput, err := getGraphQLInput[__createLogFilterInput](r)
if err != nil {
t.Errorf("Swo.LogFilterDupicateEntryError error: %v", err)
}

got := gqlInput.Input
want := input

if !testObjects(t, got, *want) {
t.Errorf("Request got = %+v, want = %+v", got, want)
}

sendGraphQLResponse(t, w, createLogFilterResponse{
CreateExclusionFilter: createLogFilterCreateExclusionFilterCreateExclusionFilterResponse{
Code: ExclusionFilterResponseCodeDuplicateEntry,
Success: false,
Message: message,
ExclusionFilter: nil,
},
})
})

_, err := client.LogFilterService().Create(ctx, *input)

if err == nil {
t.Error("Swo.LogFilterDupicateEntryError expected an error response")
}

want := fmt.Errorf("create LogFilter failed. code=%s message=%s", ExclusionFilterResponseCodeDuplicateEntry, message)

if !testObjects(t, err.Error(), want.Error()) {
t.Errorf("Swo.LogFilterDupicateEntryError returned %+v, want %+v", err, want)
}
}

0 comments on commit b62aa88

Please sign in to comment.