Skip to content

Commit 6688c4e

Browse files
authored
Validate platform by checking the existence of API documentation (#258)
1 parent d58f53a commit 6688c4e

File tree

2 files changed

+35
-149
lines changed

2 files changed

+35
-149
lines changed

sentry/platform_categories.go

Lines changed: 0 additions & 147 deletions
This file was deleted.

sentry/resource_sentry_project.go

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ package sentry
33
import (
44
"context"
55
"errors"
6+
"fmt"
67
"net/http"
8+
"strings"
79

10+
"github.com/hashicorp/go-cty/cty"
811
"github.com/hashicorp/go-multierror"
912
"github.com/hashicorp/terraform-plugin-log/tflog"
1013
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1114
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1315
"github.com/jianyuan/go-sentry/v2/sentry"
1416
)
1517

@@ -64,7 +66,7 @@ func resourceSentryProject() *schema.Resource {
6466
Type: schema.TypeString,
6567
Optional: true,
6668
Computed: true,
67-
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice(platformCategories, false)),
69+
ValidateDiagFunc: validatePlatform,
6870
},
6971
"internal_id": {
7072
Description: "The internal ID for this project.",
@@ -344,3 +346,34 @@ func resourceSentryProjectDelete(ctx context.Context, d *schema.ResourceData, me
344346

345347
return diag.FromErr(err)
346348
}
349+
350+
func validatePlatform(i interface{}, path cty.Path) diag.Diagnostics {
351+
var diagnostics diag.Diagnostics
352+
353+
v := i.(string)
354+
url := fmt.Sprintf(
355+
"https://docs.sentry.io/_platforms/%s.json",
356+
strings.Replace(v, "-", "/", 1),
357+
)
358+
resp, err := http.Get(url)
359+
360+
if err != nil {
361+
msg := "could not validate the platform at this time"
362+
diagnostics = append(diagnostics, diag.Diagnostic{
363+
Severity: diag.Error,
364+
Summary: msg,
365+
Detail: msg,
366+
AttributePath: path,
367+
})
368+
} else if resp.StatusCode != 200 {
369+
msg := fmt.Sprintf("%s is not a valid platform", v)
370+
diagnostics = append(diagnostics, diag.Diagnostic{
371+
Severity: diag.Error,
372+
Summary: msg,
373+
Detail: msg,
374+
AttributePath: path,
375+
})
376+
}
377+
378+
return diagnostics
379+
}

0 commit comments

Comments
 (0)