diff --git a/internal/client/panther/panther.go b/internal/client/panther/panther.go index 31b2ab3..f4a4a6b 100644 --- a/internal/client/panther/panther.go +++ b/internal/client/panther/panther.go @@ -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) @@ -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 { diff --git a/internal/client/panther/panther_test.go b/internal/client/panther/panther_test.go new file mode 100644 index 0000000..c9135f5 --- /dev/null +++ b/internal/client/panther/panther_test.go @@ -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) +} diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 9641071..2ce7f13 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -19,8 +19,6 @@ package provider import ( "context" "os" - "strings" - "terraform-provider-panther/internal/client/panther" "github.com/hashicorp/terraform-plugin-framework/datasource" @@ -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) }