Skip to content

Commit b56f2ac

Browse files
authored
Fix table import (#413)
Fixes #412
1 parent d7d1407 commit b56f2ac

File tree

2 files changed

+53
-16
lines changed

2 files changed

+53
-16
lines changed

internal/provider/provider.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ func configure(providerVersion string, p *schema.Provider) func(context.Context,
202202
return nil, diag.FromErr(err)
203203
}
204204

205-
var clientCache = make(map[string]astrarestapi.Client)
205+
var clientCache = make(map[string]*astrarestapi.ClientWithResponses)
206206

207207
clients := astraClients{
208208
astraClient: astraClient,
@@ -217,7 +217,7 @@ func configure(providerVersion string, p *schema.Provider) func(context.Context,
217217
}
218218
}
219219

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

234234
serverURL := fmt.Sprintf("https://%s-%s.%s/api/rest/", dbid, region, astraAppsDomain)
235-
restClient, err := astrarestapi.NewClient(serverURL, func(c *astrarestapi.Client) error {
235+
restClient, err := astrarestapi.NewClientWithResponses(serverURL, func(c *astrarestapi.Client) error {
236236
c.Client = retryClient.StandardClient()
237237
c.RequestEditors = append(c.RequestEditors, func(ctx context.Context, req *http.Request) error {
238238
req.Header.Set("User-Agent", userAgent)
@@ -243,17 +243,17 @@ func newRestClient(dbid string, providerVersion string, userAgent string, region
243243
return nil
244244
})
245245
if err != nil {
246-
return *restClient, err
246+
return restClient, err
247247
}
248-
return *restClient, nil
248+
return restClient, nil
249249
}
250250

251251
type astraClients struct {
252252
astraClient interface{}
253253
astraStreamingClient interface{}
254254
token string
255255
astraStreamingClientv3 *astrastreaming.ClientWithResponses
256-
stargateClientCache map[string]astrarestapi.Client
256+
stargateClientCache map[string]*astrarestapi.ClientWithResponses
257257
providerVersion string
258258
userAgent string
259259
streamingClusterSuffix string

internal/provider/resource_table.go

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func resourceTableCreate(ctx context.Context, d *schema.ResourceData, meta inter
142142
TableOptions: nil,
143143
}
144144

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

231231
stargateCache := meta.(astraClients).stargateClientCache
232232

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

244244
fmt.Printf("%v", restClient)
245245

246+
raw := true
246247
params := astrarestapi.GetTableParams{
247-
Raw: nil,
248+
Raw: &raw,
248249
XCassandraToken: token,
249250
}
250-
resp, err := restClient.GetTable(ctx, keyspaceName, tableName, &params)
251+
resp, err := restClient.GetTableWithResponse(ctx, keyspaceName, tableName, &params)
251252
if err != nil {
252253
return diag.FromErr(fmt.Errorf("error getting table (not retrying) err: %w", err))
253-
} else if resp.StatusCode == 409 {
254+
} else if resp.StatusCode() == 409 {
254255
// DevOps API returns 409 for concurrent modifications, these need to be retried.
255-
b, _ := io.ReadAll(resp.Body)
256-
return diag.FromErr(fmt.Errorf("error getting table (retrying): %s", b))
257-
} else if resp.StatusCode >= 400 {
256+
return diag.Errorf("error getting table (retrying): %s", string(resp.Body))
257+
} else if resp.StatusCode() >= 400 {
258258
//table not found
259259
d.SetId("")
260260
return nil
261261
}
262262

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

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

283284
stargateCache := meta.(astraClients).stargateClientCache
284285

285-
var restClient astrarestapi.Client
286+
var restClient *astrarestapi.ClientWithResponses
286287
if val, ok := stargateCache[databaseID]; ok {
287288
restClient = val
288289
} else {
@@ -333,6 +334,42 @@ func setTableResourceData(d *schema.ResourceData, databaseID, region, keyspaceNa
333334
return nil
334335
}
335336

337+
func setTableResourceDataWithTableData(d *schema.ResourceData, databaseID, region, keyspaceName, table string, tableData *astrarestapi.Table) error {
338+
if err := setTableResourceData(d, databaseID,region, keyspaceName, table); err != nil {
339+
return err
340+
}
341+
if tableData == nil {
342+
return fmt.Errorf("Table Data was nil")
343+
}
344+
// now set the rest of the table data
345+
// partition_key
346+
if err := d.Set("partition_keys", strings.Join(tableData.PrimaryKey.PartitionKey, ":")); err != nil {
347+
return err
348+
}
349+
350+
// clustering_columns
351+
if tableData.PrimaryKey.ClusteringKey != nil {
352+
if err := d.Set("clustering_columns", strings.Join(*tableData.PrimaryKey.ClusteringKey, ":")); err != nil {
353+
return err
354+
}
355+
}
356+
357+
// column_definitions
358+
cdefs := make([]map[string]string, len(tableData.ColumnDefinitions))
359+
for index, cdef := range tableData.ColumnDefinitions {
360+
defs := map[string]string {
361+
"Name": cdef.Name,
362+
"TypeDefinition": string(cdef.TypeDefinition),
363+
"Static": strconv.FormatBool(*cdef.Static),
364+
}
365+
cdefs[index] = defs
366+
}
367+
if err := d.Set("column_definitions", cdefs); err != nil {
368+
return err
369+
}
370+
return nil
371+
}
372+
336373
// parseTableID returns the databaseID, region, keyspace, tablename, error (if the format is invalid).
337374
func parseTableID(id string) (string, string, string, string, error) {
338375
idParts := strings.Split(id, "/")

0 commit comments

Comments
 (0)