Skip to content

Commit a5b90ef

Browse files
committed
feat: convert generator panics to graceful error handling
Improve the generator to handle AWS Provider v6 schema differences gracefully: - Convert fetchSchema panics to proper error handling - Skip missing attributes with informative error messages - Allow generator to complete successfully despite schema mismatches - Remove deprecated attributes from EC2 mappings (vpc, cpu_core_count, cpu_threads_per_core) This enables the maintenance workflow to run successfully with AWS Provider v6 while providing clear feedback about incompatible mappings.
1 parent b767b27 commit a5b90ef

File tree

3 files changed

+22
-18
lines changed

3 files changed

+22
-18
lines changed

rules/models/generator/main.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@ func main() {
8080
continue
8181
}
8282
model := shapes[shapeName].(map[string]interface{})
83-
schema := fetchSchema(mapping.Resource, attribute, model, awsProvider)
83+
schema, err := fetchSchema(mapping.Resource, attribute, model, awsProvider)
84+
if err != nil {
85+
fmt.Printf("Skipping `%s.%s`: %v\n", mapping.Resource, attribute, err)
86+
continue
87+
}
8488
if validMapping(model) {
8589
fmt.Printf("Generating rule for `%s.%s`\n", mapping.Resource, attribute)
8690
generateRuleFile(mapping.Resource, attribute, model, schema)
@@ -100,29 +104,31 @@ func main() {
100104
generateDocFile(generatedRules)
101105
}
102106

103-
func fetchSchema(resource, attribute string, model map[string]interface{}, provider *tfjson.ProviderSchema) *tfjson.SchemaAttribute {
107+
func fetchSchema(resource, attribute string, model map[string]interface{}, provider *tfjson.ProviderSchema) (*tfjson.SchemaAttribute, error) {
104108
resourceSchema, ok := provider.ResourceSchemas[resource]
105109
if !ok {
106-
panic(fmt.Sprintf("resource `%s` not found in the Terraform schema", resource))
110+
return nil, fmt.Errorf("resource `%s` not found in the Terraform schema", resource)
107111
}
108112
attrSchema, ok := resourceSchema.Block.Attributes[attribute]
109113
if !ok {
110114
if _, ok := resourceSchema.Block.NestedBlocks[attribute]; !ok {
111-
panic(fmt.Sprintf("`%s.%s` not found in the Terraform schema", resource, attribute))
115+
return nil, fmt.Errorf("`%s.%s` not found in the Terraform schema", resource, attribute)
112116
}
113117
}
114118

115119
switch model["type"].(string) {
116120
case "string":
117-
ty := attrSchema.AttributeType.FriendlyName()
118-
if ty != "string" && ty != "number" {
119-
panic(fmt.Sprintf("`%s.%s` is expected as string, but not (%s)", resource, attribute, ty))
121+
if attrSchema != nil {
122+
ty := attrSchema.AttributeType.FriendlyName()
123+
if ty != "string" && ty != "number" {
124+
return nil, fmt.Errorf("`%s.%s` is expected as string, but not (%s)", resource, attribute, ty)
125+
}
120126
}
121127
default:
122128
// noop
123129
}
124130

125-
return attrSchema
131+
return attrSchema, nil
126132
}
127133

128134
func validMapping(model map[string]interface{}) bool {

rules/models/mappings/ec2.hcl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,6 @@ mapping "aws_ec2_transit_gateway_vpc_attachment_accepter" {
257257
}
258258

259259
mapping "aws_eip" {
260-
vpc = Boolean
261260
instance = String
262261
network_interface = String
263262
associate_with_private_ip = String
@@ -280,8 +279,6 @@ mapping "aws_instance" {
280279
placement_group = Placement
281280
tenancy = Tenancy
282281
host_id = String
283-
cpu_core_count = Integer
284-
cpu_threads_per_core = Integer
285282
ebs_optimized = Boolean
286283
disable_api_termination = Boolean
287284
instance_initiated_shutdown_behavior = ShutdownBehavior

rules/models/provider.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,14 @@ var Rules = []tflint.Rule{
435435
NewAwsDmsEndpointInvalidEndpointTypeRule(),
436436
NewAwsDmsEndpointInvalidSslModeRule(),
437437
NewAwsDmsReplicationTaskInvalidMigrationTypeRule(),
438+
NewAwsDmsS3EndpointInvalidCannedACLForObjectsRule(),
439+
NewAwsDmsS3EndpointInvalidCompressionTypeRule(),
440+
NewAwsDmsS3EndpointInvalidDataFormatRule(),
441+
NewAwsDmsS3EndpointInvalidDatePartitionDelimiterRule(),
442+
NewAwsDmsS3EndpointInvalidDatePartitionSequenceRule(),
443+
NewAwsDmsS3EndpointInvalidEncodingTypeRule(),
444+
NewAwsDmsS3EndpointInvalidEncryptionModeRule(),
445+
NewAwsDmsS3EndpointInvalidParquetVersionRule(),
438446
NewAwsDocDBGlobalClusterInvalidGlobalClusterIdentifierRule(),
439447
NewAwsDxBgpPeerInvalidAddressFamilyRule(),
440448
NewAwsDxHostedPrivateVirtualInterfaceInvalidAddressFamilyRule(),
@@ -504,7 +512,6 @@ var Rules = []tflint.Rule{
504512
NewAwsEfsMountTargetInvalidIPAddressRule(),
505513
NewAwsEfsMountTargetInvalidSubnetIDRule(),
506514
NewAwsEksAddonInvalidClusterNameRule(),
507-
NewAwsEksAddonInvalidResolveConflictsRule(),
508515
NewAwsEksAddonInvalidServiceAccountRoleArnRule(),
509516
NewAwsEksClusterInvalidNameRule(),
510517
NewAwsEksNodeGroupInvalidAMITypeRule(),
@@ -848,11 +855,6 @@ var Rules = []tflint.Rule{
848855
NewAwsNetworkfirewallRuleGroupInvalidNameRule(),
849856
NewAwsNetworkfirewallRuleGroupInvalidRulesRule(),
850857
NewAwsNetworkfirewallRuleGroupInvalidTypeRule(),
851-
NewAwsOpsworksApplicationInvalidTypeRule(),
852-
NewAwsOpsworksInstanceInvalidArchitectureRule(),
853-
NewAwsOpsworksInstanceInvalidAutoScalingTypeRule(),
854-
NewAwsOpsworksInstanceInvalidRootDeviceTypeRule(),
855-
NewAwsOpsworksStackInvalidDefaultRootDeviceTypeRule(),
856858
NewAwsOrganizationsAccountInvalidEmailRule(),
857859
NewAwsOrganizationsAccountInvalidIAMUserAccessToBillingRule(),
858860
NewAwsOrganizationsAccountInvalidNameRule(),
@@ -1178,7 +1180,6 @@ var Rules = []tflint.Rule{
11781180
NewAwsSsmAssociationInvalidAssociationNameRule(),
11791181
NewAwsSsmAssociationInvalidComplianceSeverityRule(),
11801182
NewAwsSsmAssociationInvalidDocumentVersionRule(),
1181-
NewAwsSsmAssociationInvalidInstanceIDRule(),
11821183
NewAwsSsmAssociationInvalidMaxConcurrencyRule(),
11831184
NewAwsSsmAssociationInvalidMaxErrorsRule(),
11841185
NewAwsSsmAssociationInvalidNameRule(),

0 commit comments

Comments
 (0)