Skip to content

Commit

Permalink
feat: add tags to AWS Glue tables (#2402)
Browse files Browse the repository at this point in the history
  • Loading branch information
pdecat authored Feb 10, 2025
1 parent 955abe4 commit 1002c1e
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 6 deletions.
58 changes: 52 additions & 6 deletions aws/table_aws_glue_catalog_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ func tableAwsGlueCatalogDatabase(_ context.Context) *plugin.Table {
Description: "The name of the database. For Hive compatibility, this is folded to lowercase when it is stored.",
Type: proto.ColumnType_STRING,
},
{
Name: "arn",
Description: "The Amazon Resource Name (ARN) of the catalog database.",
Type: proto.ColumnType_STRING,
Hydrate: getGlueCatalogDatabaseArn,
Transform: transform.FromValue(),
},
{
Name: "catalog_id",
Description: "The ID of the Data Catalog in which the database resides.",
Expand Down Expand Up @@ -87,12 +94,18 @@ func tableAwsGlueCatalogDatabase(_ context.Context) *plugin.Table {
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Name"),
},
{
Name: "tags",
Description: resourceInterfaceDescription("tags"),
Type: proto.ColumnType_JSON,
Hydrate: getTagsForGlueCatalogDatabase,
},
{
Name: "akas",
Description: resourceInterfaceDescription("akas"),
Type: proto.ColumnType_JSON,
Hydrate: getGlueCatalogDatabaseAkas,
Transform: transform.FromValue(),
Hydrate: getGlueCatalogDatabaseArn,
Transform: transform.FromValue().Transform(transform.EnsureStringArray),
},
}),
}
Expand Down Expand Up @@ -192,19 +205,52 @@ func getGlueCatalogDatabase(ctx context.Context, d *plugin.QueryData, _ *plugin.
return *data.Database, nil
}

func getGlueCatalogDatabaseAkas(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
func getTagsForGlueResource(ctx context.Context, d *plugin.QueryData, arn string) (interface{}, error) {
// Create session
svc, err := GlueClient(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("aws_glue_catalog_database.getTagsForGlueCatalogDatabase", "connection_error", err)
return nil, err
}
if svc == nil {
// Unsupported region check
return nil, nil
}

// Build param
param := &glue.GetTagsInput{
ResourceArn: aws.String(arn),
}

tags, err := svc.GetTags(ctx, param)
if err != nil {
plugin.Logger(ctx).Error("aws_glue_catalog_database.getTagsForGlueCatalogDatabase", "api_error", err)
return nil, err
}
return tags, nil
}

func getTagsForGlueCatalogDatabase(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
arn, _ := getGlueCatalogDatabaseArn(ctx, d, h)
return getTagsForGlueResource(ctx, d, arn.(string))
}

func getGlueCatalogDatabaseArn(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
region := d.EqualsQualString(matrixKeyRegion)
data := h.Item.(types.Database)

// Get common columns

c, err := getCommonColumns(ctx, d, h)
if err != nil {
plugin.Logger(ctx).Error("aws_glue_catalog_database.getGlueCatalogDatabaseAkas", "common_data_error", err)
plugin.Logger(ctx).Error("aws_glue_catalog_database.getGlueCatalogDatabaseArn", "common_data_error", err)
return nil, err
}
commonColumnData := c.(*awsCommonColumnData)
aka := "arn:" + commonColumnData.Partition + ":glue:" + region + ":" + commonColumnData.AccountId + ":database/" + *data.Name

return []string{aka}, nil
// arn format - https://docs.aws.amazon.com/glue/latest/dg/glue-specifying-resource-arns.html
// arn:aws:glue:region:account-id:database/database-name
arn := "arn:" + commonColumnData.Partition + ":glue:" + region + ":" + commonColumnData.AccountId + ":database/" + *data.Name

return arn, nil
}
11 changes: 11 additions & 0 deletions aws/table_aws_glue_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ func tableAwsGlueConnection(_ context.Context) *plugin.Table {
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Name"),
},
{
Name: "tags",
Description: resourceInterfaceDescription("tags"),
Type: proto.ColumnType_JSON,
Hydrate: getTagsForGlueConnection,
},
{
Name: "akas",
Description: resourceInterfaceDescription("akas"),
Expand Down Expand Up @@ -215,6 +221,11 @@ func getGlueConnection(ctx context.Context, d *plugin.QueryData, _ *plugin.Hydra
return *data.Connection, nil
}

func getTagsForGlueConnection(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
arn, _ := getGlueConnectionArn(ctx, d, h)
return getTagsForGlueResource(ctx, d, arn.(string))
}

func getGlueConnectionArn(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
region := d.EqualsQualString(matrixKeyRegion)
data := h.Item.(types.Connection)
Expand Down
11 changes: 11 additions & 0 deletions aws/table_aws_glue_crawler.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@ func tableAwsGlueCrawler(_ context.Context) *plugin.Table {
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Name"),
},
{
Name: "tags",
Description: resourceInterfaceDescription("tags"),
Type: proto.ColumnType_JSON,
Hydrate: getTagsForGlueCrawler,
},
{
Name: "akas",
Description: resourceInterfaceDescription("akas"),
Expand Down Expand Up @@ -262,6 +268,11 @@ func getGlueCrawler(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateD
return *data.Crawler, nil
}

func getTagsForGlueCrawler(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
arn, _ := getGlueCrawlerArn(ctx, d, h)
return getTagsForGlueResource(ctx, d, arn.(string))
}

func getGlueCrawlerArn(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
region := d.EqualsQualString(matrixKeyRegion)
data := h.Item.(types.Crawler)
Expand Down
11 changes: 11 additions & 0 deletions aws/table_aws_glue_dev_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ func tableAwsGlueDevEndpoint(_ context.Context) *plugin.Table {
Type: proto.ColumnType_STRING,
Transform: transform.FromField("EndpointName"),
},
{
Name: "tags",
Description: resourceInterfaceDescription("tags"),
Type: proto.ColumnType_JSON,
Hydrate: getTagsForGlueDevEndpoint,
},
{
Name: "akas",
Description: resourceInterfaceDescription("akas"),
Expand Down Expand Up @@ -276,6 +282,11 @@ func getGlueDevEndpoint(ctx context.Context, d *plugin.QueryData, _ *plugin.Hydr
return *data.DevEndpoint, nil
}

func getTagsForGlueDevEndpoint(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
arn, _ := getGlueDevEndpointArn(ctx, d, h)
return getTagsForGlueResource(ctx, d, arn.(string))
}

func getGlueDevEndpointArn(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
region := d.EqualsQualString(matrixKeyRegion)
data := h.Item.(types.DevEndpoint)
Expand Down
11 changes: 11 additions & 0 deletions aws/table_aws_glue_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ func tableAwsGlueJob(_ context.Context) *plugin.Table {
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Name"),
},
{
Name: "tags",
Description: resourceInterfaceDescription("tags"),
Type: proto.ColumnType_JSON,
Hydrate: getTagsForGlueJob,
},
{
Name: "akas",
Description: resourceInterfaceDescription("akas"),
Expand Down Expand Up @@ -309,6 +315,11 @@ func getGlueJobBookmark(ctx context.Context, d *plugin.QueryData, h *plugin.Hydr
return data.JobBookmarkEntry, nil
}

func getTagsForGlueJob(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
arn, _ := getGlueJobArn(ctx, d, h)
return getTagsForGlueResource(ctx, d, arn.(string))
}

func getGlueJobArn(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
region := d.EqualsQualString(matrixKeyRegion)
data := h.Item.(types.Job)
Expand Down

0 comments on commit 1002c1e

Please sign in to comment.