Skip to content

Commit

Permalink
Init db clients for all enabled regions
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbyiliev committed Nov 22, 2023
1 parent b71cb08 commit bb9750d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 33 deletions.
3 changes: 3 additions & 0 deletions pkg/clients/cloud_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"strconv"
"strings"
Expand Down Expand Up @@ -77,6 +78,8 @@ func (c *CloudAPIClient) ListCloudProviders(ctx context.Context) ([]CloudProvide
return nil, err
}

log.Printf("[DEBUG] Cloud providers response body: %+v\n", response)

return response.Data, nil
}

Expand Down
61 changes: 28 additions & 33 deletions pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package provider

import (
"context"
"log"

"github.com/MaterializeInc/terraform-provider-materialize/pkg/clients"
"github.com/MaterializeInc/terraform-provider-materialize/pkg/datasources"
Expand Down Expand Up @@ -145,9 +144,6 @@ func providerConfigure(ctx context.Context, d *schema.ResourceData, version stri
return nil, diag.Errorf("Unable to create Frontegg client: %s", err)
}

// Map to store DB clients for different regions.
dbClients := make(map[clients.Region]*clients.DBClient)

// Initialize the Cloud API client using the Frontegg client and endpoint
cloudAPIClient := clients.NewCloudAPIClient(fronteggClient, cloudEndpoint)

Expand All @@ -157,42 +153,41 @@ func providerConfigure(ctx context.Context, d *schema.ResourceData, version stri
return nil, diag.Errorf("Unable to list cloud providers: %s", err)
}

// Find the provider that matches the default region
var defaultProvider *clients.CloudProvider
// Map to store DB clients for all regions.
dbClients := make(map[clients.Region]*clients.DBClient)

for _, provider := range providers {
if provider.ID == string(defaultRegion) {
defaultProvider = &provider
break
regionDetails, err := cloudAPIClient.GetRegionDetails(ctx, provider)
if err != nil {
// Handle the error, possibly continue to the next provider
continue
}
}

if defaultProvider == nil {
return nil, diag.Errorf("Default region '%s' not found among cloud providers", defaultRegion)
}
// Get the database connection details for the region
host, port, err := clients.SplitHostPort(regionDetails.RegionInfo.SqlAddress)
if err != nil {
// Handle the error, possibly continue to the next provider
continue
}

// Get the region details using the found provider
regionDetails, err := cloudAPIClient.GetRegionDetails(ctx, *defaultProvider)
if err != nil {
return nil, diag.Errorf("Unable to get region details for region '%s': %s", defaultRegion, err)
}
// Assuming email is used as the username and is the same for all regions
user := fronteggClient.Email

if regionDetails != nil && regionDetails.RegionInfo != nil {
log.Printf("[DEBUG] Region details: %+v\n", *regionDetails.RegionInfo)
} else {
log.Printf("[DEBUG] Region details: nil or empty\n")
}
// Get the database connection details
host, port, err := clients.SplitHostPort(regionDetails.RegionInfo.SqlAddress)
if err != nil {
return nil, diag.Errorf("Unable to parse host and port from '%s': %s", regionDetails.RegionInfo.SqlAddress, err)
// Instantiate a new DB client for the region
dbClient, diags := clients.NewDBClient(host, user, password, port, database, application_name_suffix, version, sslmode)
if diags.HasError() {
// Handle the error, possibly continue to the next provider
continue
}

// Store the DB client in the map, keyed by the region
dbClients[clients.Region(provider.ID)] = dbClient
}
user := fronteggClient.Email

dbClient, diags := clients.NewDBClient(host, user, password, port, database, application_name_suffix, version, sslmode)
if diags.HasError() {
return nil, diags
// Check if at least one region has been initialized successfully
if len(dbClients) == 0 {
return nil, diag.Errorf("No regions were initialized. Please check your configuration.")
}
dbClients[clients.Region(defaultRegion)] = dbClient

// Construct and return the provider meta with all clients initialized.
providerMeta := &utils.ProviderMeta{
Expand All @@ -202,5 +197,5 @@ func providerConfigure(ctx context.Context, d *schema.ResourceData, version stri
DefaultRegion: clients.Region(defaultRegion),
}

return providerMeta, diags
return providerMeta, nil
}

0 comments on commit bb9750d

Please sign in to comment.