From 3a69d9ace60c7859d3a92704a8a82b3d26851667 Mon Sep 17 00:00:00 2001 From: Matt Bush Date: Sat, 16 Mar 2024 00:16:59 -0700 Subject: [PATCH] New script for generating group overrides Signed-off-by: Matt Bush --- scripts/gen-api-group-exceptions.py | 89 +++++++++++++++++++++++++++++ scripts/gen-tf-resource-lists.sh | 7 +++ 2 files changed, 96 insertions(+) create mode 100644 scripts/gen-api-group-exceptions.py create mode 100755 scripts/gen-tf-resource-lists.sh diff --git a/scripts/gen-api-group-exceptions.py b/scripts/gen-api-group-exceptions.py new file mode 100644 index 0000000000..b69e831052 --- /dev/null +++ b/scripts/gen-api-group-exceptions.py @@ -0,0 +1,89 @@ +#!/usr/bin/env python3 +from functools import cmp_to_key + + +# First run gen-tf-resource-list.sh to generate the list of sdk and framework resources +# from the source code of terraform-provider-aws. Then run this script to generate the +# contents of the GroupMap in config/groups.go. +def find_exceptions(filename: str): + needs_change = [] + with open(filename) as f: + resources = f.readlines() + for r in resources: + line = r.strip().split(" ") + if len(line) != 3: + raise Exception(f"Invalid line: {r}") + [aws_group, tf_filename, tf_resource_name] = line + + # Some customizations for places where terraform-provider-aws is inconsistent, or where this provider + # was inconsistent in the past and we need to keep that inconsistency to avoid a breaking change. + + # This resource is in the route53resolver SDK group, but it's in the route53 crossplane provider api group, + # because of a bug in the previous manually-maintained mapping. + if tf_resource_name == "aws_route53_resolver_config": + continue + + # This resource is in the ec2 SDK group, but it's in the vpc crossplane provider api group (and it's the only + # resource in that group) + if tf_resource_name == "aws_vpc_network_performance_metric_subscription": + continue + + # terraform-provider-aws doesn't use the same group names as the AWS SDKs for cloudwatch logs and events + if aws_group in ["logs", "events"]: + aws_group = "cloudwatch" + aws_group + + # The vpc ipam resources don't include "vpc_" in their filenames, but it is in the terraform resource name + if tf_filename.startswith("ipam_"): + tf_filename = "vpc_" + tf_filename + + # Some of the ec2 resources have filenames like "ec2_.go" and others are just ".go" + if aws_group == "ec2" and tf_filename.startswith("ec2_"): + tf_filename = tf_filename.removeprefix("ec2_") + # terraform-provider-aws uses tag_gen.go as a filename for generated resources that implement tag-like behavior, + # and are named "tag" + if tf_filename == "tag_gen": + tf_filename = "tag" + + name = tf_resource_name.removeprefix("aws_") + if name.endswith(tf_filename): + calculated_group = name.removesuffix(tf_filename).removesuffix("_") + elif aws_group == "ec2" and name.startswith("ec2_"): + # Several ec2 resources have filenames like transitgateway_policy_table.go, and a resource name like + # transit_gateway_policy_table. + calculated_group = "ec2" + else: + # These are mostly the really old resources like "aws_subnet" that predate the concept of api groups + calculated_group = "" + words_to_drop = len([w for w in calculated_group.split("_") if w]) + if aws_group == "elbv2" and tf_resource_name.startswith("aws_lb"): + # The elbv2 resources currently all have a kind that starts with LB, so we need to not drop that word. + words_to_drop = 0 + if aws_group != calculated_group: + needs_change.append([tf_resource_name, aws_group, words_to_drop]) + return needs_change + + +# python and golang differ in their sorting behavior when one string is a prefix of another. Implement the golang order. +# hacked together to only sort lists by their first element, which is a string. +@cmp_to_key +def golang_cmp(a, b): + if a[0] == b[0]: + return 0 + if a[0].startswith(b[0]): + return -1 + if b[0].startswith(a[0]): + return 1 + return 1 if a > b else -1 + + +if __name__ == "__main__": + exceptions = find_exceptions("../hack/sdk-resources.lst") + exceptions.extend(find_exceptions("../hack/framework-resources.lst")) + exceptions.sort(key=golang_cmp) + for l in exceptions: + [tf_resource_name, aws_group, drop] = l + print(f'"{tf_resource_name}": ReplaceGroupWords("{aws_group}", {drop}),') + + + + diff --git a/scripts/gen-tf-resource-lists.sh b/scripts/gen-tf-resource-lists.sh new file mode 100755 index 0000000000..108ef00e41 --- /dev/null +++ b/scripts/gen-tf-resource-lists.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +XP_PROVIDER_PATH=`pwd` + +cd $TF_PROVIDER_PATH/internal/service +git grep -l '@FrameworkResource' | xargs grep '\.TypeName = "aws_' | sed -n 's|\([^/]*\)/\(\w*\)\.go:.*"\(aws_[a-z0-9_]*\)".*|\1 \2 \3|p' > $XP_PROVIDER_PATH/hack/framework-resources.lst +git grep '// @SDKResource("aws' | sed -n 's|\([^/]*\)/\(\w*\)\.go:// @SDKResource(.\([a-z0-9_]*\).*|\1 \2 \3|p' > $XP_PROVIDER_PATH/hack/sdk-resources.lst \ No newline at end of file