Skip to content

Commit

Permalink
chore(): remove indices from table resource
Browse files Browse the repository at this point in the history
  • Loading branch information
newtonnthiga committed May 8, 2024
1 parent 5f02562 commit 5ce22a6
Show file tree
Hide file tree
Showing 6 changed files with 4 additions and 378 deletions.
45 changes: 1 addition & 44 deletions docs/resources/google_spanner_table.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,6 @@ resource "alis_google_spanner_table" "test" {
name = "data",
type = "BYTES",
}
],
indices = [
{
name = "display_name_idx",
columns = [
{
name = "display_name",
order = "asc",
},
],
unique = false,
},
]
}
}
Expand Down Expand Up @@ -116,10 +104,6 @@ Required:

- `columns` (Attributes List) The columns of the table. (see [below for nested schema](#nestedatt--schema--columns))

Optional:

- `indices` (Attributes List) The indices/indexes of the table. (see [below for nested schema](#nestedatt--schema--indices))

<a id="nestedatt--schema--columns"></a>
### Nested Schema for `schema.columns`

Expand Down Expand Up @@ -148,31 +132,4 @@ The maximum is 17
- `scale` (Number) The maximum number of digits after the decimal point in the column.
This is only applicable to columns of type `FLOAT64`.
- `size` (Number) The maximum size of the column.
- `unique` (Boolean) Indicates if the column is unique.


<a id="nestedatt--schema--indices"></a>
### Nested Schema for `schema.indices`

Required:

- `columns` (Attributes List) The columns that make up the index.
The order of the columns is significant. (see [below for nested schema](#nestedatt--schema--indices--columns))
- `name` (String) The name of the index.
The name must contain only letters (a-z, A-Z), numbers (0-9), or hyphens (-), and must start with a letter and not end in a hyphen.

Optional:

- `unique` (Boolean) Indicates if the index is unique.

<a id="nestedatt--schema--indices--columns"></a>
### Nested Schema for `schema.indices.columns`

Required:

- `name` (String) The name of the column that makes up the index.

Optional:

- `order` (String) The sorting order of the column in the index.
Valid values are: `asc` or `desc`. If not specified the default is `asc`.
- `unique` (Boolean) Indicates if the column is unique.
12 changes: 0 additions & 12 deletions examples/resources/alis_google_spanner_table/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,6 @@ resource "alis_google_spanner_table" "test" {
name = "data",
type = "BYTES",
}
],
indices = [
{
name = "display_name_idx",
columns = [
{
name = "display_name",
order = "asc",
},
],
unique = false,
},
]
}
}
Expand Down
228 changes: 1 addition & 227 deletions internal/spanner/alis_google_spanner_table_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ type spannerTableModel struct {

type spannerTableSchema struct {
Columns types.List `tfsdk:"columns"`
Indices types.List `tfsdk:"indices"`
}

type spannerTableColumn struct {
Expand Down Expand Up @@ -81,27 +80,6 @@ func (o spannerTableColumn) attrTypes() map[string]attr.Type {
}
}

type spannerTableIndex struct {
Name types.String `tfsdk:"name"`
Columns types.List `tfsdk:"columns"`
Unique types.Bool `tfsdk:"unique"`
}

func (o spannerTableIndex) attrTypes() map[string]attr.Type {
return map[string]attr.Type{
"name": types.StringType,
"columns": types.ListType{
ElemType: types.ObjectType{
AttrTypes: map[string]attr.Type{
"name": types.StringType,
"order": types.StringType,
},
},
},
"unique": types.BoolType,
}
}

// Metadata returns the resource type name.
func (r *spannerTableResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_google_spanner_table"
Expand Down Expand Up @@ -210,66 +188,6 @@ func (r *spannerTableResource) Schema(_ context.Context, _ resource.SchemaReques
},
Description: "The columns of the table.",
},
"indices": schema.ListNestedAttribute{
Optional: true,
CustomType: types.ListType{
ElemType: types.ObjectType{
AttrTypes: spannerTableIndex{}.attrTypes(),
},
},
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Required: true,
Description: "The name of the index.\n" +
"The name must contain only letters (a-z, A-Z), numbers (0-9), or hyphens (-), and must start with a letter and not end in a hyphen.",
Validators: []validator.String{
validators.RegexMatches([]*regexp.Regexp{
regexp.MustCompile(utils.SpannerGoogleSqlIndexIdRegex),
regexp.MustCompile(utils.SpannerPostgresSqlIndexIdRegex),
}, "Name must be a valid Spanner Index ID, See https://cloud.google.com/spanner/docs/reference/standard-sql/data-definition-language#naming_conventions"),
},
},
"columns": schema.ListNestedAttribute{
Required: true,
CustomType: types.ListType{
ElemType: types.ObjectType{
AttrTypes: spannerTableIndexColumn{}.attrTypes(),
},
},
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Required: true,
Description: "The name of the column that makes up the index.",
Validators: []validator.String{
validators.RegexMatches([]*regexp.Regexp{
regexp.MustCompile(utils.SpannerGoogleSqlColumnIdRegex),
regexp.MustCompile(utils.SpannerPostgresSqlColumnIdRegex),
}, "Name must be a valid Spanner Column ID, See https://cloud.google.com/spanner/docs/reference/standard-sql/data-definition-language#naming_conventions"),
},
},
"order": schema.StringAttribute{
Optional: true,
Description: "The sorting order of the column in the index.\n" +
"Valid values are: `asc` or `desc`. If not specified the default is `asc`.",
Validators: []validator.String{
stringvalidator.OneOf(services.SpannerTableIndexColumnOrders...),
},
},
},
},
Description: "The columns that make up the index.\n" +
"The order of the columns is significant.",
},
"unique": schema.BoolAttribute{
Optional: true,
Description: "Indicates if the index is unique.",
},
},
},
Description: "The indices/indexes of the table.",
},
},
Description: "The schema of the table.",
},
Expand Down Expand Up @@ -297,7 +215,6 @@ func (r *spannerTableResource) Create(ctx context.Context, req resource.CreateRe
Name: "",
Schema: &services.SpannerTableSchema{
Columns: nil,
Indices: nil,
},
}

Expand All @@ -311,7 +228,6 @@ func (r *spannerTableResource) Create(ctx context.Context, req resource.CreateRe
if plan.Schema != nil {
tableSchema := &services.SpannerTableSchema{
Columns: nil,
Indices: nil,
}

if !plan.Schema.Columns.IsNull() {
Expand Down Expand Up @@ -380,57 +296,6 @@ func (r *spannerTableResource) Create(ctx context.Context, req resource.CreateRe
}
}

if !plan.Schema.Indices.IsNull() {
indices := make([]spannerTableIndex, 0, len(plan.Schema.Indices.Elements()))
d := plan.Schema.Indices.ElementsAs(ctx, &indices, false)
if d.HasError() {
tflog.Error(ctx, fmt.Sprintf("Error reading indices: %v", d))
return
}
diags.Append(d...)

for _, index := range indices {
idx := &services.SpannerTableIndex{}

// Populate index name
if !index.Name.IsNull() {
idx.Name = index.Name.ValueString()
}

// Populate unique
if !index.Unique.IsNull() {
idx.Unique = wrapperspb.Bool(index.Unique.ValueBool())
}

// Populate columns
if !index.Columns.IsNull() {
columns := make([]spannerTableIndexColumn, 0, len(index.Columns.Elements()))
d := index.Columns.ElementsAs(ctx, &columns, false)
if d.HasError() {
tflog.Error(ctx, fmt.Sprintf("Error reading index columns: %v", d))
return
}
diags.Append(d...)

for _, column := range columns {
order := services.SpannerTableIndexColumnOrder_ASC
switch column.Order.ValueString() {
case "asc":
order = services.SpannerTableIndexColumnOrder_ASC
case "desc":
order = services.SpannerTableIndexColumnOrder_DESC
}
idx.Columns = append(idx.Columns, &services.SpannerTableIndexColumn{
Name: column.Name.ValueString(),
Order: order,
})
}
}

tableSchema.Indices = append(tableSchema.Indices, idx)
}
}

table.Schema = tableSchema
}

Expand Down Expand Up @@ -567,44 +432,6 @@ func (r *spannerTableResource) Read(ctx context.Context, req resource.ReadReques

s.Columns = generatedList
}
if table.Schema.Indices != nil {
indices := make([]*spannerTableIndex, 0, len(table.Schema.Indices))
for _, index := range table.Schema.Indices {
idx := &spannerTableIndex{
Name: types.StringValue(index.Name),
Columns: types.List{},
}

// Get unique
if index.Unique != nil {
idx.Unique = types.BoolValue(index.Unique.GetValue())
}

// Get columns
columns := make([]*spannerTableIndexColumn, 0)
for _, column := range index.Columns {
columns = append(columns, &spannerTableIndexColumn{
Name: types.StringValue(column.Name),
Order: types.StringValue(column.Order.String()),
})
}
generatedList, d := types.ListValueFrom(ctx, types.ObjectType{
AttrTypes: spannerTableIndexColumn{}.attrTypes(),
}, columns)
diags.Append(d...)

idx.Columns = generatedList

indices = append(indices, idx)
}

generatedList, d := types.ListValueFrom(ctx, types.ObjectType{
AttrTypes: spannerTableIndex{}.attrTypes(),
}, indices)
diags.Append(d...)

s.Indices = generatedList
}

state.Schema = s
}
Expand Down Expand Up @@ -640,15 +467,13 @@ func (r *spannerTableResource) Update(ctx context.Context, req resource.UpdateRe
Name: fmt.Sprintf("projects/%s/instances/%s/databases/%s/tables/%s", project, instanceName, databaseId, tableId),
Schema: &services.SpannerTableSchema{
Columns: nil,
Indices: nil,
},
}

// Populate schema if any
if plan.Schema != nil {
tableSchema := &services.SpannerTableSchema{
Columns: nil,
Indices: nil,
}

if !plan.Schema.Columns.IsNull() {
Expand Down Expand Up @@ -717,63 +542,12 @@ func (r *spannerTableResource) Update(ctx context.Context, req resource.UpdateRe
}
}

if !plan.Schema.Indices.IsNull() {
indices := make([]spannerTableIndex, 0, len(plan.Schema.Indices.Elements()))
d := plan.Schema.Indices.ElementsAs(ctx, &indices, false)
if d.HasError() {
tflog.Error(ctx, fmt.Sprintf("Error reading indices: %v", d))
return
}
diags.Append(d...)

for _, index := range indices {
idx := &services.SpannerTableIndex{}

// Populate index name
if !index.Name.IsNull() {
idx.Name = index.Name.ValueString()
}

// Populate unique
if !index.Unique.IsNull() {
idx.Unique = wrapperspb.Bool(index.Unique.ValueBool())
}

// Populate columns
if !index.Columns.IsNull() {
columns := make([]spannerTableIndexColumn, 0, len(index.Columns.Elements()))
d := index.Columns.ElementsAs(ctx, &columns, false)
if d.HasError() {
tflog.Error(ctx, fmt.Sprintf("Error reading index columns: %v", d))
return
}
diags.Append(d...)

for _, column := range columns {
order := services.SpannerTableIndexColumnOrder_ASC
switch column.Order.ValueString() {
case "asc":
order = services.SpannerTableIndexColumnOrder_ASC
case "desc":
order = services.SpannerTableIndexColumnOrder_DESC
}
idx.Columns = append(idx.Columns, &services.SpannerTableIndexColumn{
Name: column.Name.ValueString(),
Order: order,
})
}
}

tableSchema.Indices = append(tableSchema.Indices, idx)
}
}

table.Schema = tableSchema
}

// Update table
_, err := r.config.SpannerService.UpdateSpannerTable(ctx, table, &fieldmaskpb.FieldMask{
Paths: []string{"schema.columns", "schema.indices"},
Paths: []string{"schema.columns"},
}, false)
if err != nil {
resp.Diagnostics.AddError(
Expand Down
Loading

0 comments on commit 5ce22a6

Please sign in to comment.