Skip to content

Commit 23fe95b

Browse files
authored
feat: validate base_url and token upfront (#553)
1 parent 667299b commit 23fe95b

File tree

4 files changed

+151
-1
lines changed

4 files changed

+151
-1
lines changed

internal/apiclient/api.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ servers:
66
- url: https://sentry.io/api
77

88
paths:
9+
/0/internal/health/:
10+
get:
11+
summary: Health Check
12+
operationId: healthCheck
13+
responses:
14+
"200":
15+
description: OK
16+
"401":
17+
description: Unauthorized
918
/0/organizations/{organization_id_or_slug}/:
1019
parameters:
1120
- $ref: "#/components/parameters/organization_id_or_slug"

internal/apiclient/apiclient.gen.go

Lines changed: 91 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/provider/provider.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package provider
33
import (
44
"context"
55
"fmt"
6+
"net/http"
67
"os"
78

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

5960
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
60-
6161
if resp.Diagnostics.HasError() {
6262
return
6363
}
@@ -110,6 +110,21 @@ func (p *SentryProvider) Configure(ctx context.Context, req provider.ConfigureRe
110110
return
111111
}
112112

113+
httpResp, err := apiClient.HealthCheckWithResponse(ctx)
114+
if err != nil {
115+
resp.Diagnostics.AddError("failed to perform health check", err.Error())
116+
return
117+
} else if httpResp.StatusCode() == http.StatusNotFound {
118+
resp.Diagnostics.AddError("failed to perform health check", "Sentry API is not available, please check the base URL")
119+
return
120+
} else if httpResp.StatusCode() == http.StatusUnauthorized {
121+
resp.Diagnostics.AddError("failed to perform health check", "Sentry API is not available, Please check the authentication token")
122+
return
123+
} else if httpResp.StatusCode() != http.StatusOK {
124+
resp.Diagnostics.AddError("failed to perform health check", fmt.Errorf("unexpected status code: %d", httpResp.StatusCode()).Error())
125+
return
126+
}
127+
113128
providerData := &providerdata.ProviderData{
114129
Client: client,
115130
ApiClient: apiClient,

internal/provider/provider_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ package provider
22

33
import (
44
"context"
5+
"testing"
56

67
"github.com/hashicorp/terraform-plugin-framework/providerserver"
78
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
89
"github.com/hashicorp/terraform-plugin-mux/tf5to6server"
910
"github.com/hashicorp/terraform-plugin-mux/tf6muxserver"
11+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
1012
"github.com/jianyuan/go-utils/must"
1113
"github.com/jianyuan/terraform-provider-sentry/internal/acctest"
1214
"github.com/jianyuan/terraform-provider-sentry/sentry"
@@ -40,3 +42,36 @@ var testAccProtoV6ProviderFactories = map[string]func() (tfprotov6.ProviderServe
4042
return muxServer.ProviderServer(), nil
4143
},
4244
}
45+
46+
func TestAccProvider_validation(t *testing.T) {
47+
resource.Test(t, resource.TestCase{
48+
PreCheck: func() { acctest.PreCheck(t) },
49+
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
50+
Steps: []resource.TestStep{
51+
{
52+
Config: `
53+
provider "sentry" {
54+
token = "invalid"
55+
}
56+
57+
data "sentry_organization" "test" {
58+
slug = "invalid"
59+
}
60+
`,
61+
ExpectError: acctest.ExpectLiteralError("Sentry API is not available, Please check the authentication token"),
62+
},
63+
{
64+
Config: `
65+
provider "sentry" {
66+
base_url = "https://github.com/jianyuan/terraform-provider-sentry"
67+
}
68+
69+
data "sentry_organization" "test" {
70+
slug = "invalid"
71+
}
72+
`,
73+
ExpectError: acctest.ExpectLiteralError("Sentry API is not available, please check the base URL"),
74+
},
75+
},
76+
})
77+
}

0 commit comments

Comments
 (0)