Skip to content

Commit

Permalink
Improve error handling
Browse files Browse the repository at this point in the history
Took 29 minutes
  • Loading branch information
tdabasinskas committed Jan 5, 2024
1 parent fd490e6 commit 1d8b0b8
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions backstage/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type backstageProviderModel struct {
}

const (
patternURL = `https?://.+`
envBaseURL = "BACKSTAGE_BASE_URL"
envDefaultNamespace = "BACKSTAGE_DEFAULT_NAMESPACE"
descriptionProviderBaseURL = "Base URL of the Backstage instance, e.g. https://demo.backstage.io. May also be provided via `" + envBaseURL +
Expand All @@ -57,7 +58,7 @@ func (p *backstageProvider) Schema(_ context.Context, _ provider.SchemaRequest,
"[releases](https://github.com/tdabasinskas/terraform-provider-backstage/releases) for version information and release notes.",
Attributes: map[string]schema.Attribute{
"base_url": schema.StringAttribute{Optional: true, MarkdownDescription: descriptionProviderBaseURL, Validators: []validator.String{
stringvalidator.RegexMatches(regexp.MustCompile(`https?://.+`), "must be a valid URL"),
stringvalidator.RegexMatches(regexp.MustCompile(patternURL), "must be a valid URL"),
}},
"default_namespace": schema.StringAttribute{Optional: true, MarkdownDescription: descriptionProviderDefaultNamespace, Validators: []validator.String{
stringvalidator.LengthBetween(1, 63),
Expand Down Expand Up @@ -97,6 +98,13 @@ func (p *backstageProvider) Configure(ctx context.Context, req provider.Configur
baseURL = config.BaseURL.ValueString()
}

if regex := regexp.MustCompile(patternURL); baseURL == "" || !regex.MatchString(baseURL) {
resp.Diagnostics.AddAttributeError(path.Root("base_url"), "Missing or invalid Base URL of Backstage instance", fmt.Sprintf(
"The provider cannot create the Backstage API client as there is empty or invalid value for the Backstage Base URL. Set the host value in the "+
"configuration or use the %s environment variable. If either is already set, ensure the value is not empty and valid.", envBaseURL))

}

Check warning on line 106 in backstage/provider.go

View check run for this annotation

Codecov / codecov/patch

backstage/provider.go#L102-L106

Added lines #L102 - L106 were not covered by tests

defaultNamespace := os.Getenv(envDefaultNamespace)
if !config.DefaultNamespace.IsNull() {
defaultNamespace = config.DefaultNamespace.ValueString()
Expand All @@ -105,11 +113,10 @@ func (p *backstageProvider) Configure(ctx context.Context, req provider.Configur
defaultNamespace = backstage.DefaultNamespaceName
}

if baseURL == "" {
resp.Diagnostics.AddAttributeError(path.Root("base_url"), "Missing Base URL of Backstage instance", fmt.Sprintf(
"The provider cannot create the Backstage API client as there is a missing or empty value for the Backstage Base URL. Set the host value in the "+
"configuration or use the %s environment variable. If either is already set, ensure the value is not empty.", envBaseURL))

if regex := regexp.MustCompile(patternEntityName); !regex.MatchString(defaultNamespace) {
resp.Diagnostics.AddAttributeError(path.Root("default_namespace"), "Invalid default namespace of Backstage instance", fmt.Sprintf(
"The provider cannot create the Backstage API client as there is invalid value for the default namespace. Set the host value in the "+
"configuration or use the %s environment variable. If either is already set, ensure the value is not empty and valid.", envDefaultNamespace))

Check warning on line 119 in backstage/provider.go

View check run for this annotation

Codecov / codecov/patch

backstage/provider.go#L117-L119

Added lines #L117 - L119 were not covered by tests
}

if resp.Diagnostics.HasError() {
Expand Down

0 comments on commit 1d8b0b8

Please sign in to comment.