Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Base URL handling #77

Merged
merged 3 commits into from
Jan 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions backstage/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
}

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,17 +58,11 @@
"[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?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)`),
"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),
stringvalidator.RegexMatches(
regexp.MustCompile(patternEntityName),
"must follow Backstage format restrictions",
),
stringvalidator.RegexMatches(regexp.MustCompile(patternEntityName), "must follow Backstage format restrictions"),
}},
},
}
Expand Down Expand Up @@ -103,6 +98,13 @@
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 @@ -111,11 +113,10 @@
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
Loading