Skip to content

Commit

Permalink
Fix table import (#413)
Browse files Browse the repository at this point in the history
Fixes #412
  • Loading branch information
emerkle826 authored Oct 31, 2024
1 parent d7d1407 commit b56f2ac
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 16 deletions.
12 changes: 6 additions & 6 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func configure(providerVersion string, p *schema.Provider) func(context.Context,
return nil, diag.FromErr(err)
}

var clientCache = make(map[string]astrarestapi.Client)
var clientCache = make(map[string]*astrarestapi.ClientWithResponses)

clients := astraClients{
astraClient: astraClient,
Expand All @@ -217,7 +217,7 @@ func configure(providerVersion string, p *schema.Provider) func(context.Context,
}
}

func newRestClient(dbid string, providerVersion string, userAgent string, region string) (astrarestapi.Client, error) {
func newRestClient(dbid string, providerVersion string, userAgent string, region string) (*astrarestapi.ClientWithResponses, error) {
clientVersion := fmt.Sprintf("go/%s", astra.Version)
// Build a retryable http astraClient to automatically
// handle intermittent api errors
Expand All @@ -232,7 +232,7 @@ func newRestClient(dbid string, providerVersion string, userAgent string, region
}

serverURL := fmt.Sprintf("https://%s-%s.%s/api/rest/", dbid, region, astraAppsDomain)
restClient, err := astrarestapi.NewClient(serverURL, func(c *astrarestapi.Client) error {
restClient, err := astrarestapi.NewClientWithResponses(serverURL, func(c *astrarestapi.Client) error {
c.Client = retryClient.StandardClient()
c.RequestEditors = append(c.RequestEditors, func(ctx context.Context, req *http.Request) error {
req.Header.Set("User-Agent", userAgent)
Expand All @@ -243,17 +243,17 @@ func newRestClient(dbid string, providerVersion string, userAgent string, region
return nil
})
if err != nil {
return *restClient, err
return restClient, err
}
return *restClient, nil
return restClient, nil
}

type astraClients struct {
astraClient interface{}
astraStreamingClient interface{}
token string
astraStreamingClientv3 *astrastreaming.ClientWithResponses
stargateClientCache map[string]astrarestapi.Client
stargateClientCache map[string]*astrarestapi.ClientWithResponses
providerVersion string
userAgent string
streamingClusterSuffix string
Expand Down
57 changes: 47 additions & 10 deletions internal/provider/resource_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func resourceTableCreate(ctx context.Context, d *schema.ResourceData, meta inter
TableOptions: nil,
}

var restClient astrarestapi.Client
var restClient *astrarestapi.ClientWithResponses
if val, ok := stargateCache[databaseID]; ok {
restClient = val
} else {
Expand Down Expand Up @@ -230,7 +230,7 @@ func resourceTableRead(ctx context.Context, d *schema.ResourceData, meta interfa

stargateCache := meta.(astraClients).stargateClientCache

var restClient astrarestapi.Client
var restClient *astrarestapi.ClientWithResponses
if val, ok := stargateCache[databaseID]; ok {
restClient = val
} else {
Expand All @@ -243,24 +243,25 @@ func resourceTableRead(ctx context.Context, d *schema.ResourceData, meta interfa

fmt.Printf("%v", restClient)

raw := true
params := astrarestapi.GetTableParams{
Raw: nil,
Raw: &raw,
XCassandraToken: token,
}
resp, err := restClient.GetTable(ctx, keyspaceName, tableName, &params)
resp, err := restClient.GetTableWithResponse(ctx, keyspaceName, tableName, &params)
if err != nil {
return diag.FromErr(fmt.Errorf("error getting table (not retrying) err: %w", err))
} else if resp.StatusCode == 409 {
} else if resp.StatusCode() == 409 {
// DevOps API returns 409 for concurrent modifications, these need to be retried.
b, _ := io.ReadAll(resp.Body)
return diag.FromErr(fmt.Errorf("error getting table (retrying): %s", b))
} else if resp.StatusCode >= 400 {
return diag.Errorf("error getting table (retrying): %s", string(resp.Body))
} else if resp.StatusCode() >= 400 {
//table not found
d.SetId("")
return nil
}

if err := setTableResourceData(d, databaseID, region, keyspaceName, tableName); err != nil {
tableData := resp.JSON200
if err := setTableResourceDataWithTableData(d, databaseID, region, keyspaceName, tableName, tableData); err != nil {
return diag.FromErr(fmt.Errorf("Error setting keyspace data (not retrying) %s", err))
}

Expand All @@ -282,7 +283,7 @@ func resourceTableDelete(ctx context.Context, d *schema.ResourceData, meta inter

stargateCache := meta.(astraClients).stargateClientCache

var restClient astrarestapi.Client
var restClient *astrarestapi.ClientWithResponses
if val, ok := stargateCache[databaseID]; ok {
restClient = val
} else {
Expand Down Expand Up @@ -333,6 +334,42 @@ func setTableResourceData(d *schema.ResourceData, databaseID, region, keyspaceNa
return nil
}

func setTableResourceDataWithTableData(d *schema.ResourceData, databaseID, region, keyspaceName, table string, tableData *astrarestapi.Table) error {
if err := setTableResourceData(d, databaseID,region, keyspaceName, table); err != nil {
return err
}
if tableData == nil {
return fmt.Errorf("Table Data was nil")
}
// now set the rest of the table data
// partition_key
if err := d.Set("partition_keys", strings.Join(tableData.PrimaryKey.PartitionKey, ":")); err != nil {
return err
}

// clustering_columns
if tableData.PrimaryKey.ClusteringKey != nil {
if err := d.Set("clustering_columns", strings.Join(*tableData.PrimaryKey.ClusteringKey, ":")); err != nil {
return err
}
}

// column_definitions
cdefs := make([]map[string]string, len(tableData.ColumnDefinitions))
for index, cdef := range tableData.ColumnDefinitions {
defs := map[string]string {
"Name": cdef.Name,
"TypeDefinition": string(cdef.TypeDefinition),
"Static": strconv.FormatBool(*cdef.Static),
}
cdefs[index] = defs
}
if err := d.Set("column_definitions", cdefs); err != nil {
return err
}
return nil
}

// parseTableID returns the databaseID, region, keyspace, tablename, error (if the format is invalid).
func parseTableID(id string) (string, string, string, string, error) {
idParts := strings.Split(id, "/")
Expand Down

0 comments on commit b56f2ac

Please sign in to comment.