Skip to content

Commit

Permalink
Update panther url (#42)
Browse files Browse the repository at this point in the history
* fix: update panther base url

* chore: extract function and add test

* update test
  • Loading branch information
markosfount authored Jan 31, 2025
1 parent e2ac4d8 commit 9aeee34
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 11 deletions.
15 changes: 13 additions & 2 deletions internal/client/panther/panther.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ import (
"github.com/hasura/go-graphql-client"
"io"
"net/http"
"strings"
"terraform-provider-panther/internal/client"
)

const GraphqlPath = "/v1/public/graphql"
const RestHttpSourcePath = "/v1/log-sources/http"
const GraphqlPath = "/public/graphql"
const RestHttpSourcePath = "/log-sources/http"

var _ client.GraphQLClient = (*GraphQLClient)(nil)

Expand Down Expand Up @@ -74,6 +75,16 @@ func NewAPIClient(graphClient *GraphQLClient, restClient *RestClient) *APIClient
}
}

func CreateAPIClient(url, token string) *APIClient {
// url in previous versions was provided including graphql endpoint,
// we strip it here to keep it backwards compatible
pantherUrl := strings.TrimSuffix(url, GraphqlPath)
graphClient := NewGraphQLClient(pantherUrl, token)
restClient := NewRestClient(pantherUrl, token)

return NewAPIClient(graphClient, restClient)
}

func (c *RestClient) CreateHttpSource(ctx context.Context, input client.CreateHttpSourceInput) (client.HttpSource, error) {
jsonData, err := json.Marshal(input)
if err != nil {
Expand Down
43 changes: 43 additions & 0 deletions internal/client/panther/panther_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package panther

import (
"github.com/stretchr/testify/assert"
"reflect"
"testing"
)

// Customer with custom url that provides the graphql endpoint (legacy behavior)
func TestCreateAPIClient_CustomURLWithGraphEndpoint(t *testing.T) {
url := "panther-url/public/graphql"
client := *CreateAPIClient(url, "token")
graphUrl := reflect.ValueOf(client).FieldByIndex([]int{0}).Elem().FieldByName("url").String()
assert.Equal(t, "panther-url/public/graphql", graphUrl)
assert.Equal(t, "panther-url/log-sources/http", client.RestClient.url)
}

// Customer with custom url that provides the panther root url (new behavior)
func TestCreateAPIClient_CustomUrlWithBaseUrl(t *testing.T) {
url := "panther-url"
client := *CreateAPIClient(url, "token")
graphUrl := reflect.ValueOf(client).FieldByIndex([]int{0}).Elem().FieldByName("url").String()
assert.Equal(t, "panther-url/public/graphql", graphUrl)
assert.Equal(t, "panther-url/log-sources/http", client.RestClient.url)
}

// Customer with API Gateway url that provides the graphql endpoint (legacy behavior)
func TestCreateAPIClient_ApiGWUrlWithGraphEndpoint(t *testing.T) {
url := "panther-url/v1/public/graphql"
client := *CreateAPIClient(url, "token")
graphUrl := reflect.ValueOf(client).FieldByIndex([]int{0}).Elem().FieldByName("url").String()
assert.Equal(t, "panther-url/v1/public/graphql", graphUrl)
assert.Equal(t, "panther-url/v1/log-sources/http", client.RestClient.url)
}

// Customer with API Gateway url that provides the panther root url (new behavior)
func TestCreateAPIClient_ApiGWUrlWithBaseUrl(t *testing.T) {
url := "panther-url/v1"
client := *CreateAPIClient(url, "token")
graphUrl := reflect.ValueOf(client).FieldByIndex([]int{0}).Elem().FieldByName("url").String()
assert.Equal(t, "panther-url/v1/public/graphql", graphUrl)
assert.Equal(t, "panther-url/v1/log-sources/http", client.RestClient.url)
}
10 changes: 1 addition & 9 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ package provider
import (
"context"
"os"
"strings"

"terraform-provider-panther/internal/client/panther"

"github.com/hashicorp/terraform-plugin-framework/datasource"
Expand Down Expand Up @@ -124,13 +122,7 @@ func (p *PantherProvider) Configure(ctx context.Context, req provider.ConfigureR
return
}

// url in previous versions was provided including graphql endpoint,
// we strip it here to keep it backwards compatible
pantherUrl := strings.TrimSuffix(url, panther.GraphqlPath)
graphClient := panther.NewGraphQLClient(pantherUrl, token)
restClient := panther.NewRestClient(pantherUrl, token)

resp.ResourceData = panther.NewAPIClient(graphClient, restClient)
resp.ResourceData = panther.CreateAPIClient(url, token)

}

Expand Down

0 comments on commit 9aeee34

Please sign in to comment.