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

Allow URL to be provided as region #42

Merged
merged 3 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
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
32 changes: 24 additions & 8 deletions internal/provider/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"net/http"
"strings"
)

type SquaredUpClient struct {
Expand All @@ -18,22 +19,37 @@ func NewSquaredUpClient(region string, apiKey string) (*SquaredUpClient, error)
return nil, err
}

return &SquaredUpClient{
req, err := http.NewRequest("GET", baseURL+"/api/plugins/latest", nil)
if err != nil {
return nil, fmt.Errorf("failed to create request: %v", err)
}

client := &http.Client{}

squaredUpClient := &SquaredUpClient{
baseURL: baseURL,
apiKey: apiKey,
httpClient: &http.Client{},
}, nil
httpClient: client,
}

_, err = squaredUpClient.doRequest(req)
if err != nil {
return nil, fmt.Errorf("invalid api key with the provided region. check the api key and region and try again")
}

return squaredUpClient, nil
}

func determineBaseURL(region string) (string, error) {
switch region {
case "us":
if region == "us" {
return "https://api.squaredup.com", nil
case "eu":
} else if region == "eu" {
return "https://eu.api.squaredup.com", nil
default:
return "", fmt.Errorf("unsupported region: %s", region)
} else if strings.HasPrefix(region, "https://") {
region = strings.TrimSuffix(region, "/")
return region, nil
shaswot77 marked this conversation as resolved.
Show resolved Hide resolved
}
return "", fmt.Errorf("unsupported region or URL scheme: %s", region)
}

func (c *SquaredUpClient) doRequest(req *http.Request) ([]byte, error) {
Expand Down
10 changes: 4 additions & 6 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package provider
import (
"context"
"os"
"regexp"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource"
Expand Down Expand Up @@ -48,10 +49,7 @@ func (p *squaredupProvider) Schema(_ context.Context, _ provider.SchemaRequest,
"region": schema.StringAttribute{
Description: "Region of your SquaredUp instance. May also be set via the SQUAREDUP_REGION environment variable.",
Optional: true,
Validators: []validator.String{stringvalidator.OneOf(
"us",
"eu",
)},
Validators: []validator.String{stringvalidator.RegexMatches(regexp.MustCompile(`^(us|eu|https://.*)$`), "Invalid region format. It must be either 'us', 'eu', or start with 'https://'")},
},
"api_key": schema.StringAttribute{
Description: "API Key for SquaredUp API. May also be set via the SQUAREDUP_API_KEY environment variable.",
Expand Down Expand Up @@ -137,8 +135,8 @@ func (p *squaredupProvider) Configure(ctx context.Context, req provider.Configur
resp.Diagnostics.AddError(
"Unable to Create SquaredUp API Client",
"An unexpected error occurred while creating the SquaredUp API client. "+
"Please check the configuration and try again. If the error persists, please open an issue on GitHub. "+
err.Error(),
"Please check the configuration and try again. If the error persists, please open an issue on GitHub. \n"+
"Error: "+err.Error(),
)
return
}
Expand Down
Loading