Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r/glue_catalog_table - add partition index support #16194

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 101 additions & 3 deletions aws/resource_aws_glue_catalog_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,31 @@ func resourceAwsGlueCatalogTable() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
"partition_index": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
MaxItems: 3,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"index_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(1, 255),
},
"keys": {
Type: schema.TypeSet,
Required: true,
MinItems: 1,
Elem: &schema.Schema{Type: schema.TypeString},
},
"index_status": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}
Expand All @@ -238,9 +263,10 @@ func resourceAwsGlueCatalogTableCreate(d *schema.ResourceData, meta interface{})
name := d.Get("name").(string)

input := &glue.CreateTableInput{
CatalogId: aws.String(catalogID),
DatabaseName: aws.String(dbName),
TableInput: expandGlueTableInput(d),
CatalogId: aws.String(catalogID),
DatabaseName: aws.String(dbName),
TableInput: expandGlueTableInput(d),
PartitionIndexes: expandGlueTablePartitionIndexes(d.Get("partition_index").([]interface{})),
}

_, err := conn.CreateTable(input)
Expand Down Expand Up @@ -311,6 +337,22 @@ func resourceAwsGlueCatalogTableRead(d *schema.ResourceData, meta interface{}) e
return fmt.Errorf("error setting parameters: %s", err)
}

partIndexInput := &glue.GetPartitionIndexesInput{
CatalogId: out.Table.CatalogId,
TableName: out.Table.Name,
DatabaseName: out.Table.DatabaseName,
}
partOut, err := conn.GetPartitionIndexes(partIndexInput)
if err != nil {
return fmt.Errorf("error getting glue partition indexes: %w", err)
DrFaust92 marked this conversation as resolved.
Show resolved Hide resolved
}

if partOut != nil && len(partOut.PartitionIndexDescriptorList) > 0 {
if err := d.Set("partition_index", flattenGluePartitionIndexes(partOut.PartitionIndexDescriptorList)); err != nil {
return fmt.Errorf("error setting partition_index: %w", err)
}
}

return nil
}

Expand Down Expand Up @@ -404,6 +446,25 @@ func expandGlueTableInput(d *schema.ResourceData) *glue.TableInput {
return tableInput
}

func expandGlueTablePartitionIndexes(a []interface{}) []*glue.PartitionIndex {
partitionIndexes := make([]*glue.PartitionIndex, 0, len(a))

for _, m := range a {
partitionIndexes = append(partitionIndexes, expandGlueTablePartitionIndex(m.(map[string]interface{})))
}

return partitionIndexes
}

func expandGlueTablePartitionIndex(m map[string]interface{}) *glue.PartitionIndex {
partitionIndex := &glue.PartitionIndex{
IndexName: aws.String(m["index_name"].(string)),
Keys: expandStringSet(m["keys"].(*schema.Set)),
}

return partitionIndex
}

func expandGlueStorageDescriptor(l []interface{}) *glue.StorageDescriptor {
if len(l) == 0 || l[0] == nil {
return nil
Expand Down Expand Up @@ -638,6 +699,43 @@ func flattenGlueColumn(c *glue.Column) map[string]string {
return column
}

func flattenGluePartitionIndexes(cs []*glue.PartitionIndexDescriptor) []map[string]interface{} {
partitionIndexSlice := make([]map[string]interface{}, len(cs))
if len(cs) > 0 {
for i, v := range cs {
partitionIndexSlice[i] = flattenGluePartitionIndex(v)
}
}

return partitionIndexSlice
}

func flattenGluePartitionIndex(c *glue.PartitionIndexDescriptor) map[string]interface{} {
partitionIndex := make(map[string]interface{})

if c == nil {
return partitionIndex
}

if v := aws.StringValue(c.IndexName); v != "" {
partitionIndex["index_name"] = v
}

if v := aws.StringValue(c.IndexStatus); v != "" {
partitionIndex["index_status"] = v
}

if c.Keys != nil {
names := make([]*string, 0, len(c.Keys))
for _, key := range c.Keys {
names = append(names, key.Name)
}
partitionIndex["keys"] = flattenStringSet(names)
}

return partitionIndex
}

func flattenGlueSerDeInfo(s *glue.SerDeInfo) []map[string]interface{} {
if s == nil {
serDeInfos := make([]map[string]interface{}, 0)
Expand Down
Loading