Skip to content

Commit

Permalink
Allow region override in table resource
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbyiliev committed Nov 22, 2023
1 parent bb9750d commit 60282d7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
17 changes: 13 additions & 4 deletions pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ 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 @@ -146,6 +147,7 @@ func providerConfigure(ctx context.Context, d *schema.ResourceData, version stri

// Initialize the Cloud API client using the Frontegg client and endpoint
cloudAPIClient := clients.NewCloudAPIClient(fronteggClient, cloudEndpoint)
regionsEnabled := make(map[clients.Region]bool)

// Get the list of cloud providers
providers, err := cloudAPIClient.ListCloudProviders(ctx)
Expand All @@ -158,11 +160,14 @@ func providerConfigure(ctx context.Context, d *schema.ResourceData, version stri

for _, provider := range providers {
regionDetails, err := cloudAPIClient.GetRegionDetails(ctx, provider)

if err != nil {
// Handle the error, possibly continue to the next provider
continue
}

regionsEnabled[clients.Region(provider.ID)] = regionDetails.RegionInfo != nil && regionDetails.RegionInfo.Resolvable

// Get the database connection details for the region
host, port, err := clients.SplitHostPort(regionDetails.RegionInfo.SqlAddress)
if err != nil {
Expand All @@ -189,12 +194,16 @@ func providerConfigure(ctx context.Context, d *schema.ResourceData, version stri
return nil, diag.Errorf("No regions were initialized. Please check your configuration.")
}

// Debug log the dbClients map
log.Printf("[DEBUG] Initialized DB clients for regions: %v\n", dbClients)

// Construct and return the provider meta with all clients initialized.
providerMeta := &utils.ProviderMeta{
DB: dbClients,
Frontegg: fronteggClient,
CloudAPI: cloudAPIClient,
DefaultRegion: clients.Region(defaultRegion),
DB: dbClients,
Frontegg: fronteggClient,
CloudAPI: cloudAPIClient,
DefaultRegion: clients.Region(defaultRegion),
RegionsEnabled: regionsEnabled,
}

return providerMeta, nil
Expand Down
19 changes: 15 additions & 4 deletions pkg/utils/provider_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import (
)

type ProviderMeta struct {
DB map[clients.Region]*clients.DBClient
Frontegg *clients.FronteggClient
CloudAPI *clients.CloudAPIClient
DefaultRegion clients.Region
DB map[clients.Region]*clients.DBClient
Frontegg *clients.FronteggClient
CloudAPI *clients.CloudAPIClient
DefaultRegion clients.Region
RegionsEnabled map[clients.Region]bool
}

func GetProviderMeta(meta interface{}) (*ProviderMeta, bool) {
Expand All @@ -35,6 +36,16 @@ func GetDBClientFromMeta(meta interface{}, d *schema.ResourceData) (*sqlx.DB, er
region = providerMeta.DefaultRegion
}

// Check if the region is enabled using the stored information
enabled, exists := providerMeta.RegionsEnabled[region]
if !exists {
return nil, fmt.Errorf("no information available for region: %s", region)
}

if !enabled {
return nil, fmt.Errorf("region '%s' is not enabled", region)
}

// Retrieve the appropriate DBClient for the region from the map
dbClient, exists := providerMeta.DB[region]
if !exists {
Expand Down

0 comments on commit 60282d7

Please sign in to comment.