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

STREAM-160: customize apps domain #285

Merged
merged 1 commit into from
Aug 11, 2023
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
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ $ terraform plan
### Optional

- `astra_api_url` (String) URL for Astra API. May also be provided via ASTRA_API_URL environment variable.
- `astra_apps_domain` (String) DNS suffix for Astra databases. May also be provided via ASTRA_APPS_DOMAIN environment variable.
- `streaming_api_url` (String) URL for Astra Streaming API. May also be provided via ASTRA_STREAMING_API_URL environment variable.
- `token` (String, Sensitive) Authentication token for Astra API. May also be provided via ASTRA_API_TOKEN environment variable.

Expand Down
15 changes: 13 additions & 2 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

var (
astraAppsDomain = DefaultAstraAppsDomain
)

func init() {
// Set descriptions to support markdown syntax, this will be used in document generation
// and the language server.
Expand Down Expand Up @@ -94,6 +98,11 @@ func NewSDKProvider(version string) func() *schema.Provider {
Optional: true,
Description: "URL for Astra API. May also be provided via ASTRA_API_URL environment variable.",
},
"astra_apps_domain": {
Type: schema.TypeString,
Optional: true,
Description: "DNS suffix for Astra databases. May also be provided via ASTRA_APPS_DOMAIN environment variable.",
},
"streaming_api_url": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -122,6 +131,8 @@ func configure(providerVersion string, p *schema.Provider) func(context.Context,
return nil, diag.FromErr(fmt.Errorf("invalid Astra server API URL: %w", err))
}

astraAppsDomain = firstNonEmptyString(d.Get("astra_apps_domain").(string), os.Getenv("ASTRA_APPS_DOMAIN"), DefaultAstraAppsDomain)

streamingAPIServerURL := firstNonEmptyString(d.Get("streaming_api_url").(string), os.Getenv("ASTRA_STREAMING_API_URL"), DefaultStreamingAPIURL)
if _, err := url.Parse(astraAPIServerURL); err != nil {
return nil, diag.FromErr(fmt.Errorf("invalid Astra Streaming server API URL: %w", err))
Expand Down Expand Up @@ -209,13 +220,13 @@ func newRestClient(dbid string, providerVersion string, userAgent string, region
retryClient.RetryMax = 10
retryClient.CheckRetry = func(ctx context.Context, resp *http.Response, err error) (bool, error) {
// Never retry POST requests because of side effects
if resp.Request.Method == "POST" {
if err != nil || resp == nil || resp.Request.Method == "POST" {
return false, err
}
return retryablehttp.DefaultRetryPolicy(ctx, resp, err)
}

serverURL := fmt.Sprintf("https://%s-%s.apps.astra.datastax.com/api/rest/", dbid, region)
serverURL := fmt.Sprintf("https://%s-%s.%s/api/rest/", dbid, region, astraAppsDomain)
restClient, err := astrarestapi.NewClient(serverURL, func(c *astrarestapi.Client) error {
c.Client = retryClient.StandardClient()
c.RequestEditors = append(c.RequestEditors, func(ctx context.Context, req *http.Request) error {
Expand Down
6 changes: 6 additions & 0 deletions internal/provider/provider_framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const (
fullProviderName = "terraform-astra-provider"

DefaultAstraAPIURL = astra.ServerURL
DefaultAstraAppsDomain = "apps.astra.datastax.com"
DefaultStreamingAPIURL = "https://api.streaming.datastax.com/"
)

Expand All @@ -51,6 +52,7 @@ type astraProvider struct {
type astraProviderModel struct {
Token types.String `tfsdk:"token"`
AstraServerURL types.String `tfsdk:"astra_api_url"`
AstraAppsDomain types.String `tfsdk:"astra_apps_domain"`
AstraStreamingServerURL types.String `tfsdk:"streaming_api_url"`
}

Expand Down Expand Up @@ -85,6 +87,10 @@ func (p *astraProvider) Schema(_ context.Context, _ provider.SchemaRequest, resp
MarkdownDescription: "URL for Astra API. May also be provided via ASTRA_API_URL environment variable.",
Optional: true,
},
"astra_apps_domain": schema.StringAttribute{
MarkdownDescription: "DNS suffix for Astra databases. May also be provided via ASTRA_APPS_DOMAIN environment variable.",
Optional: true,
},
"streaming_api_url": schema.StringAttribute{
MarkdownDescription: "URL for Astra Streaming API. May also be provided via ASTRA_STREAMING_API_URL environment variable.",
Optional: true,
Expand Down
9 changes: 4 additions & 5 deletions internal/provider/resource_cdc.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,16 +431,15 @@ func getPulsarToken(ctx context.Context, pulsarCluster string, token string, org

pulsarTokenResponse, err := streamingClient.GetPulsarTokensByTenantWithResponse(ctx, tenantName, &tenantTokenParams)
if err != nil {
fmt.Println("Can't generate token", err)
diag.Errorf("Can't get pulsar token")
return "", err
return "", fmt.Errorf("failed to get pulsar token: %w", err)
} else if pulsarTokenResponse.StatusCode() > 299 {
return "", fmt.Errorf("failed to get pulsar token, status code: %d, message: %s", pulsarTokenResponse.StatusCode(), string(pulsarTokenResponse.Body))
}

var streamingTokens StreamingTokens
err = json.Unmarshal(pulsarTokenResponse.Body, &streamingTokens)
if err != nil {
fmt.Println("Can't deserialize", pulsarTokenResponse.Body)
return "", err
return "", fmt.Errorf("failed to read token response: %w", err)
}

tokenId := streamingTokens[0].Tokenid
Expand Down
5 changes: 4 additions & 1 deletion internal/provider/resource_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,10 @@ func resourceTableCreate(ctx context.Context, d *schema.ResourceData, meta inter
case astra.ACTIVE:
resp, err := restClient.CreateTable(ctx, keyspaceName, &tableParams, createJSON)
if err != nil {
b, _ := io.ReadAll(resp.Body)
b := []byte{}
if resp != nil {
b, _ = io.ReadAll(resp.Body)
}
return retry.NonRetryableError(fmt.Errorf("Error adding table (not retrying) err: %s, body: %s", err, b))
} else if resp.StatusCode == 409 {
// DevOps API returns 409 for concurrent modifications, these need to be retried.
Expand Down
Loading