Skip to content

Commit

Permalink
feat: validate base_url and token upfront (#553)
Browse files Browse the repository at this point in the history
  • Loading branch information
jianyuan authored Dec 31, 2024
1 parent 667299b commit 23fe95b
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 1 deletion.
9 changes: 9 additions & 0 deletions internal/apiclient/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ servers:
- url: https://sentry.io/api

paths:
/0/internal/health/:
get:
summary: Health Check
operationId: healthCheck
responses:
"200":
description: OK
"401":
description: Unauthorized
/0/organizations/{organization_id_or_slug}/:
parameters:
- $ref: "#/components/parameters/organization_id_or_slug"
Expand Down
91 changes: 91 additions & 0 deletions internal/apiclient/apiclient.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package provider
import (
"context"
"fmt"
"net/http"
"os"

"github.com/hashicorp/terraform-plugin-framework/datasource"
Expand Down Expand Up @@ -57,7 +58,6 @@ func (p *SentryProvider) Configure(ctx context.Context, req provider.ConfigureRe
var data SentryProviderModel

resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)

if resp.Diagnostics.HasError() {
return
}
Expand Down Expand Up @@ -110,6 +110,21 @@ func (p *SentryProvider) Configure(ctx context.Context, req provider.ConfigureRe
return
}

httpResp, err := apiClient.HealthCheckWithResponse(ctx)
if err != nil {
resp.Diagnostics.AddError("failed to perform health check", err.Error())
return
} else if httpResp.StatusCode() == http.StatusNotFound {
resp.Diagnostics.AddError("failed to perform health check", "Sentry API is not available, please check the base URL")
return
} else if httpResp.StatusCode() == http.StatusUnauthorized {
resp.Diagnostics.AddError("failed to perform health check", "Sentry API is not available, Please check the authentication token")
return
} else if httpResp.StatusCode() != http.StatusOK {
resp.Diagnostics.AddError("failed to perform health check", fmt.Errorf("unexpected status code: %d", httpResp.StatusCode()).Error())
return
}

providerData := &providerdata.ProviderData{
Client: client,
ApiClient: apiClient,
Expand Down
35 changes: 35 additions & 0 deletions internal/provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package provider

import (
"context"
"testing"

"github.com/hashicorp/terraform-plugin-framework/providerserver"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-mux/tf5to6server"
"github.com/hashicorp/terraform-plugin-mux/tf6muxserver"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/jianyuan/go-utils/must"
"github.com/jianyuan/terraform-provider-sentry/internal/acctest"
"github.com/jianyuan/terraform-provider-sentry/sentry"
Expand Down Expand Up @@ -40,3 +42,36 @@ var testAccProtoV6ProviderFactories = map[string]func() (tfprotov6.ProviderServe
return muxServer.ProviderServer(), nil
},
}

func TestAccProvider_validation(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: `
provider "sentry" {
token = "invalid"
}
data "sentry_organization" "test" {
slug = "invalid"
}
`,
ExpectError: acctest.ExpectLiteralError("Sentry API is not available, Please check the authentication token"),
},
{
Config: `
provider "sentry" {
base_url = "https://github.com/jianyuan/terraform-provider-sentry"
}
data "sentry_organization" "test" {
slug = "invalid"
}
`,
ExpectError: acctest.ExpectLiteralError("Sentry API is not available, please check the base URL"),
},
},
})
}

0 comments on commit 23fe95b

Please sign in to comment.