From 086bf4eb294990e2527bba4b7824e88714e0bea9 Mon Sep 17 00:00:00 2001 From: wata_mac Date: Sun, 20 Mar 2022 17:30:52 +0900 Subject: [PATCH] Bump tflint-plugin-sdk for gRPC-based new plugin system --- go.mod | 15 ++- go.sum | 39 ++++---- google/config.go | 6 +- google/provider.go | 91 ++++++------------- google/ruleset.go | 48 ++++------ main.go | 2 - rules/api/google_disabled_api.go | 31 ++++--- ...mposer_environment_invalid_machine_type.go | 64 +++++++------ ...e_compute_instance_invalid_machine_type.go | 35 +++++-- ..._instance_template_invalid_machine_type.go | 35 +++++-- ...ompute_reservation_invalid_machine_type.go | 63 +++++++------ ...le_compute_resource_policy_invalid_name.go | 31 +++++-- ..._container_cluster_invalid_machine_type.go | 49 ++++++---- ...ontainer_node_pool_invalid_machine_type.go | 49 ++++++---- ...oogle_dataflow_job_invalid_machine_type.go | 35 +++++-- ...project_iam_audit_config_invalid_member.go | 74 +++++++-------- ...ogle_project_iam_binding_invalid_member.go | 33 +++++-- ...oogle_project_iam_member_invalid_member.go | 32 +++++-- ...oogle_project_iam_policy_invalid_member.go | 33 +++++-- ...ervice_perimeter_invalid_perimeter_type.go | 31 +++++-- ...ry_domain_trust_invalid_trust_direction.go | 31 +++++-- ...rectory_domain_trust_invalid_trust_type.go | 31 +++++-- ...pigee_organization_invalid_runtime_type.go | 31 +++++-- ...omain_mapping_invalid_override_strategy.go | 31 +++++-- ...app_engine_firewall_rule_invalid_action.go | 31 +++++-- ...ible_app_version_invalid_serving_status.go | 31 +++++-- ...query_routine_invalid_determinism_level.go | 31 +++++-- ...oogle_bigquery_routine_invalid_language.go | 31 +++++-- ...e_bigquery_routine_invalid_routine_type.go | 31 +++++-- ...y_invalid_global_policy_evaluation_mode.go | 31 +++++-- ..._asset_folder_feed_invalid_content_type.go | 31 +++++-- ..._organization_feed_invalid_content_type.go | 31 +++++-- ...asset_project_feed_invalid_content_type.go | 31 +++++-- ...tity_group_invalid_initial_group_config.go | 31 +++++-- ...oogle_cloudiot_device_invalid_log_level.go | 31 +++++-- ...gle_cloudiot_registry_invalid_log_level.go | 31 +++++-- ...le_compute_address_invalid_address_type.go | 31 +++++-- .../google_compute_address_invalid_name.go | 31 +++++-- ...le_compute_address_invalid_network_tier.go | 31 +++++-- ...gle_compute_backend_bucket_invalid_name.go | 31 +++++-- ...kend_bucket_signed_url_key_invalid_name.go | 31 +++++-- ...d_service_invalid_load_balancing_scheme.go | 31 +++++-- ...kend_service_invalid_locality_lb_policy.go | 31 +++++-- ...ompute_backend_service_invalid_protocol.go | 31 +++++-- ...ackend_service_invalid_session_affinity.go | 31 +++++-- ...end_service_signed_url_key_invalid_name.go | 31 +++++-- ...nal_vpn_gateway_invalid_redundancy_type.go | 31 +++++-- ...ogle_compute_firewall_invalid_direction.go | 31 +++++-- ...ute_forwarding_rule_invalid_ip_protocol.go | 31 +++++-- ...ding_rule_invalid_load_balancing_scheme.go | 31 +++++-- ...te_forwarding_rule_invalid_network_tier.go | 31 +++++-- ...ute_global_address_invalid_address_type.go | 31 +++++-- ...mpute_global_address_invalid_ip_version.go | 31 +++++-- ...bal_forwarding_rule_invalid_ip_protocol.go | 31 +++++-- ...obal_forwarding_rule_invalid_ip_version.go | 31 +++++-- ...ding_rule_invalid_load_balancing_scheme.go | 31 +++++-- ...int_group_invalid_network_endpoint_type.go | 31 +++++-- ...terconnect_attachment_invalid_bandwidth.go | 31 +++++-- ...erconnect_attachment_invalid_encryption.go | 31 +++++-- ...te_interconnect_attachment_invalid_name.go | 31 +++++-- ...te_interconnect_attachment_invalid_type.go | 31 +++++-- ...te_managed_ssl_certificate_invalid_type.go | 31 +++++-- ...int_group_invalid_network_endpoint_type.go | 31 +++++-- ...de_template_invalid_cpu_overcommit_type.go | 31 +++++-- ...d_service_invalid_load_balancing_scheme.go | 31 +++++-- ...kend_service_invalid_locality_lb_policy.go | 31 +++++-- ...region_backend_service_invalid_protocol.go | 31 +++++-- ...ackend_service_invalid_session_affinity.go | 31 +++++-- ...int_group_invalid_network_endpoint_type.go | 31 +++++-- .../google_compute_route_invalid_name.go | 31 +++++-- ...uter_nat_invalid_nat_ip_allocate_option.go | 31 +++++-- ...alid_source_subnetwork_ip_ranges_to_nat.go | 31 +++++-- ...pute_router_peer_invalid_advertise_mode.go | 31 +++++-- ...pute_ssl_policy_invalid_min_tls_version.go | 31 +++++-- ...ogle_compute_ssl_policy_invalid_profile.go | 31 +++++-- ...ute_subnetwork_invalid_ipv6_access_type.go | 31 +++++-- .../google_compute_subnetwork_invalid_role.go | 31 +++++-- ...e_compute_subnetwork_invalid_stack_type.go | 31 +++++-- ...arget_https_proxy_invalid_quic_override.go | 31 +++++-- ...pute_target_instance_invalid_nat_policy.go | 31 +++++-- ...e_target_ssl_proxy_invalid_proxy_header.go | 31 +++++-- ...e_target_tcp_proxy_invalid_proxy_header.go | 31 +++++-- ...alog_entry_group_invalid_entry_group_id.go | 31 +++++-- .../google_data_catalog_entry_invalid_type.go | 31 +++++-- ...log_entry_invalid_user_specified_system.go | 31 +++++-- ...talog_entry_invalid_user_specified_type.go | 31 +++++-- ...og_tag_template_invalid_tag_template_id.go | 31 +++++-- ...s_prevention_job_trigger_invalid_status.go | 31 +++++-- ...google_datastore_index_invalid_ancestor.go | 31 +++++-- ...anager_deployment_invalid_create_policy.go | 31 +++++-- ...anager_deployment_invalid_delete_policy.go | 31 +++++-- ...le_dialogflow_agent_invalid_api_version.go | 31 +++++-- ...gle_dialogflow_agent_invalid_match_mode.go | 31 +++++-- .../google_dialogflow_agent_invalid_tier.go | 31 +++++-- ...entity_type_invalid_auto_expansion_mode.go | 31 +++++-- ..._dialogflow_cx_entity_type_invalid_kind.go | 31 +++++-- ...gle_dialogflow_entity_type_invalid_kind.go | 31 +++++-- ...dialogflow_intent_invalid_webhook_state.go | 31 +++++-- ...gle_dns_managed_zone_invalid_visibility.go | 31 +++++-- ...gle_firestore_index_invalid_query_scope.go | 31 +++++-- ...e_healthcare_fhir_store_invalid_version.go | 31 +++++-- .../google_kms_crypto_key_invalid_purpose.go | 31 +++++-- ...y_ring_import_job_invalid_import_method.go | 31 +++++-- ...ing_import_job_invalid_protection_level.go | 31 +++++-- ...cache_instance_invalid_memcache_version.go | 31 +++++-- ...onitoring_alert_policy_invalid_combiner.go | 31 +++++-- ...oring_custom_service_invalid_service_id.go | 31 +++++-- ..._metric_descriptor_invalid_launch_stage.go | 31 +++++-- ...g_metric_descriptor_invalid_metric_kind.go | 31 +++++-- ...ng_metric_descriptor_invalid_value_type.go | 31 +++++-- ..._monitoring_slo_invalid_calendar_period.go | 31 +++++-- .../google_monitoring_slo_invalid_slo_id.go | 31 +++++-- ...ices_edge_cache_origin_invalid_protocol.go | 31 +++++-- ...tebooks_instance_invalid_boot_disk_type.go | 31 +++++-- ...tebooks_instance_invalid_data_disk_type.go | 31 +++++-- ...ebooks_instance_invalid_disk_encryption.go | 31 +++++-- ..._deployment_invalid_patch_deployment_id.go | 31 +++++-- .../google_privateca_ca_pool_invalid_tier.go | 31 +++++-- ...teca_certificate_authority_invalid_type.go | 31 +++++-- .../google_pubsub_schema_invalid_type.go | 31 +++++-- ...gle_redis_instance_invalid_connect_mode.go | 31 +++++-- .../google_redis_instance_invalid_name.go | 31 +++++-- .../google_redis_instance_invalid_tier.go | 31 +++++-- ...nstance_invalid_transit_encryption_mode.go | 31 +++++-- .../google_scc_source_invalid_display_name.go | 31 +++++-- .../google_spanner_database_invalid_name.go | 31 +++++-- .../google_spanner_instance_invalid_name.go | 31 +++++-- ...ation_instance_invalid_database_version.go | 31 +++++-- ...rage_bucket_access_control_invalid_role.go | 31 +++++-- ...ault_object_access_control_invalid_role.go | 31 +++++-- .../google_storage_hmac_key_invalid_state.go | 31 +++++-- ...rage_object_access_control_invalid_role.go | 31 +++++-- rules/provider.go | 41 ++++++--- 133 files changed, 3300 insertions(+), 1009 deletions(-) diff --git a/go.mod b/go.mod index 58dc1f9..79812e0 100644 --- a/go.mod +++ b/go.mod @@ -4,13 +4,13 @@ go 1.17 require ( github.com/dave/dst v0.26.2 - github.com/google/go-cmp v0.5.6 - github.com/hashicorp/hcl/v2 v2.10.1 + github.com/google/go-cmp v0.5.7 + github.com/hashicorp/hcl/v2 v2.11.1 github.com/hashicorp/terraform-plugin-sdk/v2 v2.9.0 github.com/onsi/ginkgo v1.15.2 // indirect github.com/onsi/gomega v1.11.0 // indirect github.com/serenize/snaker v0.0.0-20201027110005-a7ad2135616e - github.com/terraform-linters/tflint-plugin-sdk v0.9.1 + github.com/terraform-linters/tflint-plugin-sdk v0.9.2-0.20220327063603-919fa2776c6c google.golang.org/api v0.60.0 ) @@ -24,13 +24,12 @@ require ( github.com/googleapis/gax-go/v2 v2.1.1 // indirect github.com/hashicorp/errwrap v1.0.0 // indirect github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 // indirect - github.com/hashicorp/go-hclog v0.16.2 // indirect + github.com/hashicorp/go-hclog v1.2.0 // indirect github.com/hashicorp/go-multierror v1.0.0 // indirect - github.com/hashicorp/go-plugin v1.4.2 // indirect + github.com/hashicorp/go-plugin v1.4.3 // indirect github.com/hashicorp/go-uuid v1.0.1 // indirect github.com/hashicorp/go-version v1.3.0 // indirect github.com/hashicorp/terraform-plugin-go v0.4.0 // indirect - github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 // indirect github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d // indirect github.com/mattn/go-colorable v0.1.4 // indirect github.com/mattn/go-isatty v0.0.10 // indirect @@ -43,7 +42,7 @@ require ( github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect github.com/vmihailenco/msgpack/v4 v4.3.12 // indirect github.com/vmihailenco/tagparser v0.1.1 // indirect - github.com/zclconf/go-cty v1.9.1 // indirect + github.com/zclconf/go-cty v1.10.0 // indirect go.opencensus.io v0.23.0 // indirect golang.org/x/mod v0.4.2 // indirect golang.org/x/net v0.0.0-20210503060351-7fd8e65b6420 // indirect @@ -54,6 +53,6 @@ require ( golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect google.golang.org/appengine v1.6.7 // indirect google.golang.org/genproto v0.0.0-20211021150943-2b146023228c // indirect - google.golang.org/grpc v1.40.0 // indirect + google.golang.org/grpc v1.45.0 // indirect google.golang.org/protobuf v1.27.1 // indirect ) diff --git a/go.sum b/go.sum index 43bbc31..3cf95c1 100644 --- a/go.sum +++ b/go.sum @@ -78,6 +78,7 @@ github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d/go.mod h1:6QX/PXZ github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= +github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cheggaaa/pb v1.0.27/go.mod h1:pQciLPpbU0oxA0h+VJYYLxO+XeDQb5pZijXscXHm81s= github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= @@ -86,7 +87,11 @@ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDk github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= +github.com/cncf/udpa/go v0.0.0-20210930031921-04548b0d99d4/go.mod h1:6pvJx4me5XPnfI9Z40ddWsdw2W/uZgQLFXToKeRcDiI= github.com/cncf/xds/go v0.0.0-20210312221358-fbca930ec8ed/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= +github.com/cncf/xds/go v0.0.0-20210805033703-aa0b78936158/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= +github.com/cncf/xds/go v0.0.0-20210922020428-25de7278fc84/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= +github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/dave/dst v0.26.2 h1:lnxLAKI3tx7MgLNVDirFCsDTlTG9nKTk7GcptKcWSwY= github.com/dave/dst v0.26.2/go.mod h1:UMDJuIRPfyUCC78eFuB+SV/WI8oDeyFDvM/JR6NI3IU= @@ -105,6 +110,7 @@ github.com/envoyproxy/go-control-plane v0.9.7/go.mod h1:cwu0lG7PUMfa9snN8LXBig5y github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk= github.com/envoyproxy/go-control-plane v0.9.9-0.20210217033140-668b12f5399d/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk= github.com/envoyproxy/go-control-plane v0.9.9-0.20210512163311-63b5d3c536b0/go.mod h1:hliV/p42l8fGbc6Y9bQ70uLwIvmJyVE5k4iMKlh8wCQ= +github.com/envoyproxy/go-control-plane v0.9.10-0.20210907150352-cf90f659a021/go.mod h1:AFq3mo9L8Lqqiid3OhADV3RfLJnjiw63cSpi+fDTRC0= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= @@ -171,8 +177,9 @@ github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o= +github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0= github.com/google/martian/v3 v3.1.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0= @@ -204,7 +211,6 @@ github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/U github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= github.com/hashicorp/go-checkpoint v0.5.0/go.mod h1:7nfLNL10NsxqO4iWuW6tWW0HjZuDrwkBuEQsVcpCOgg= github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80= -github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80= github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48= github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 h1:1/D3zfFHttUKaCaGKZ/dR2roBXv0vKbSCnssIldfQdI= github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320/go.mod h1:EiZBMaudVLy8fmjf9Npq1dq9RalhveqZG5w/yz3mHWs= @@ -212,28 +218,26 @@ github.com/hashicorp/go-getter v1.5.3/go.mod h1:BrrV/1clo8cCYu6mxvboYg+KutTiFnXj github.com/hashicorp/go-hclog v0.0.0-20180709165350-ff2cf002a8dd/go.mod h1:9bjs9uLqI8l75knNv3lV1kA55veR+WUPSiKIWcQHudI= github.com/hashicorp/go-hclog v0.14.1/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ= github.com/hashicorp/go-hclog v0.15.0/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ= -github.com/hashicorp/go-hclog v0.16.2 h1:K4ev2ib4LdQETX5cSZBG0DVLk1jwGqSPXBjdah3veNs= -github.com/hashicorp/go-hclog v0.16.2/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ= +github.com/hashicorp/go-hclog v1.2.0 h1:La19f8d7WIlm4ogzNHB0JGqs5AUDAZ2UfCY4sJXcJdM= +github.com/hashicorp/go-hclog v1.2.0/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ= github.com/hashicorp/go-multierror v1.0.0 h1:iVjPR7a6H0tWELX5NxNe7bYopibicUzc7uPribsnS6o= github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk= github.com/hashicorp/go-plugin v1.3.0/go.mod h1:F9eH4LrE/ZsRdbwhfjs9k9HoDUwAHnYtXdgmf1AVNs0= github.com/hashicorp/go-plugin v1.4.1/go.mod h1:5fGEH17QVwTTcR0zV7yhDPLLmFX9YSZ38b18Udy6vYQ= -github.com/hashicorp/go-plugin v1.4.2 h1:yFvG3ufXXpqiMiZx9HLcaK3XbIqQ1WJFR/F1a2CuVw0= -github.com/hashicorp/go-plugin v1.4.2/go.mod h1:5fGEH17QVwTTcR0zV7yhDPLLmFX9YSZ38b18Udy6vYQ= +github.com/hashicorp/go-plugin v1.4.3 h1:DXmvivbWD5qdiBts9TpBC7BYL1Aia5sxbRgQB+v6UZM= +github.com/hashicorp/go-plugin v1.4.3/go.mod h1:5fGEH17QVwTTcR0zV7yhDPLLmFX9YSZ38b18Udy6vYQ= github.com/hashicorp/go-safetemp v1.0.0/go.mod h1:oaerMy3BhqiTbVye6QuFhFtIceqFoDHxNAB65b+Rj1I= github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= github.com/hashicorp/go-uuid v1.0.1 h1:fv1ep09latC32wFoVwnqcnKJGnMSdBanPczbHAYm1BE= github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= github.com/hashicorp/go-version v1.1.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= -github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/hashicorp/go-version v1.3.0 h1:McDWVJIU/y+u1BRV06dPaLfLCaT7fUTJLp5r04x7iNw= github.com/hashicorp/go-version v1.3.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/hcl/v2 v2.3.0/go.mod h1:d+FwDBbOLvpAM3Z6J7gPj/VoAGkNe/gm352ZhjJ/Zv8= -github.com/hashicorp/hcl/v2 v2.10.0/go.mod h1:FwWsfWEjyV/CMj8s/gqAuiviY72rJ1/oayI9WftqcKg= -github.com/hashicorp/hcl/v2 v2.10.1 h1:h4Xx4fsrRE26ohAk/1iGF/JBqRQbyUqu5Lvj60U54ys= -github.com/hashicorp/hcl/v2 v2.10.1/go.mod h1:FwWsfWEjyV/CMj8s/gqAuiviY72rJ1/oayI9WftqcKg= +github.com/hashicorp/hcl/v2 v2.11.1 h1:yTyWcXcm9XB0TEkyU/JCRU6rYy4K+mgLtzn2wlrJbcc= +github.com/hashicorp/hcl/v2 v2.11.1/go.mod h1:FwWsfWEjyV/CMj8s/gqAuiviY72rJ1/oayI9WftqcKg= github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64= github.com/hashicorp/terraform-exec v0.15.0/go.mod h1:H4IG8ZxanU+NW0ZpDRNsvh9f0ul7C0nHP+rUR/CHs7I= github.com/hashicorp/terraform-json v0.13.0/go.mod h1:y5OdLBCT+rxbwnpxZs9kGL7R9ExU76+cpdY8zHwoazk= @@ -241,8 +245,6 @@ github.com/hashicorp/terraform-plugin-go v0.4.0 h1:LFbXNeLDo0J/wR0kUzSPq0RpdmFh2 github.com/hashicorp/terraform-plugin-go v0.4.0/go.mod h1:7u/6nt6vaiwcWE2GuJKbJwNlDFnf5n95xKw4hqIVr58= github.com/hashicorp/terraform-plugin-sdk/v2 v2.9.0 h1:me5GUReyzlmNzDEuUzQCr2qDjNluKdvYj/W4LItUqKQ= github.com/hashicorp/terraform-plugin-sdk/v2 v2.9.0/go.mod h1:JRe/T0PPn9kGowtePeYnTbDm6ViYNcnyxf1esw5yu90= -github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 h1:HKLsbzeOsfXmKNpr3GiT18XAblV0BjCbzL8KQAMZGa0= -github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734/go.mod h1:kNDNcF7sN4DocDLBkQYz73HGKwN1ANB1blq4lIYLYvg= github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM= github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d h1:kJCB4vdITiW1eC1vq2e6IsrXKrZit1bv/TDYFGMp4BQ= github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM= @@ -345,8 +347,8 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5 github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/terraform-linters/tflint-plugin-sdk v0.9.1 h1:Q7+QmvkxrINjsxQ9iOR9GDIppS7kT2hXQBHNaZ+01ug= -github.com/terraform-linters/tflint-plugin-sdk v0.9.1/go.mod h1:2pu+KHPrxfV/Y0inO9c5w4ptL6dNIHu8Em7ZxXBNP4E= +github.com/terraform-linters/tflint-plugin-sdk v0.9.2-0.20220327063603-919fa2776c6c h1:O/HVL2Lt+1hn2pwPZ7KTqU8H8rpi+OTEkkwdDEOy0KE= +github.com/terraform-linters/tflint-plugin-sdk v0.9.2-0.20220327063603-919fa2776c6c/go.mod h1:buSG6YRD4H7GzQpPerADmNBFaYOx31B8o8u9l+7SegY= github.com/ulikunitz/xz v0.5.8/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14= github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk= github.com/vmihailenco/msgpack v4.0.4+incompatible h1:dSLoQfGFAo3F6OoNhwUmLwVgaUXK79GlxNBwueZn0xI= @@ -361,12 +363,11 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= -github.com/zclconf/go-cty v1.1.0/go.mod h1:xnAOWiHeOqg2nWS62VtQ7pbOu17FtxJNW8RLEih+O3s= github.com/zclconf/go-cty v1.2.0/go.mod h1:hOPWgoHbaTUnI5k4D2ld+GRpFJSCe6bCM7m1q/N4PQ8= github.com/zclconf/go-cty v1.8.0/go.mod h1:vVKLxnk3puL4qRAv72AO+W99LUD4da90g3uUAzyuvAk= -github.com/zclconf/go-cty v1.9.0/go.mod h1:vVKLxnk3puL4qRAv72AO+W99LUD4da90g3uUAzyuvAk= -github.com/zclconf/go-cty v1.9.1 h1:viqrgQwFl5UpSxc046qblj78wZXVDFnSOufaOTER+cc= github.com/zclconf/go-cty v1.9.1/go.mod h1:vVKLxnk3puL4qRAv72AO+W99LUD4da90g3uUAzyuvAk= +github.com/zclconf/go-cty v1.10.0 h1:mp9ZXQeIcN8kAwuqorjH+Q+njbJKjLrvB2yIh4q7U+0= +github.com/zclconf/go-cty v1.10.0/go.mod h1:vVKLxnk3puL4qRAv72AO+W99LUD4da90g3uUAzyuvAk= github.com/zclconf/go-cty-debug v0.0.0-20191215020915-b22d67c1ba0b/go.mod h1:ZRKQfBXbGkpdV6QMzT3rU1kSTAnfu1dO8dPKjYprgj8= go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU= go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8= @@ -439,7 +440,6 @@ golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20190628185345-da137c7871d7/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20190724013045-ca1201d0de80/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20191009170851-d66e71096ffb/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= @@ -753,8 +753,9 @@ google.golang.org/grpc v1.37.1/go.mod h1:NREThFqKR1f3iQ6oBuvc5LadQuXVGo9rkm5ZGrQ google.golang.org/grpc v1.38.0/go.mod h1:NREThFqKR1f3iQ6oBuvc5LadQuXVGo9rkm5ZGrQdJfM= google.golang.org/grpc v1.39.0/go.mod h1:PImNr+rS9TWYb2O4/emRugxiyHZ5JyHW5F+RPnDzfrE= google.golang.org/grpc v1.39.1/go.mod h1:PImNr+rS9TWYb2O4/emRugxiyHZ5JyHW5F+RPnDzfrE= -google.golang.org/grpc v1.40.0 h1:AGJ0Ih4mHjSeibYkFGh1dD9KJ/eOtZ93I6hoHhukQ5Q= google.golang.org/grpc v1.40.0/go.mod h1:ogyxbiOoUXAkP+4+xa6PZSE9DZgIHtSpzjDTB9KAK34= +google.golang.org/grpc v1.45.0 h1:NEpgUqV3Z+ZjkqMsxMg11IaDrXY4RY6CQukSGK0uI1M= +google.golang.org/grpc v1.45.0/go.mod h1:lN7owxKUQEqMfSyQikvvk5tf/6zMPsrK+ONuO11+0rQ= google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.1.0/go.mod h1:6Kw0yEErY5E/yWrBtf03jp27GLLJujG4z/JK95pnjjw= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= diff --git a/google/config.go b/google/config.go index 64db065..33d4972 100644 --- a/google/config.go +++ b/google/config.go @@ -1,10 +1,6 @@ package google -import "github.com/hashicorp/hcl/v2" - // Config is the configuration for the ruleset. type Config struct { - DeepCheck bool `hcl:"deep_check,optional"` - - Remain hcl.Body `hcl:",remain"` + DeepCheck bool `hclext:"deep_check,optional"` } diff --git a/google/provider.go b/google/provider.go index ec47a62..7ccb535 100644 --- a/google/provider.go +++ b/google/provider.go @@ -3,44 +3,48 @@ package google import ( "os" - "github.com/hashicorp/hcl/v2" - "github.com/terraform-linters/tflint-plugin-sdk/terraform/configs" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleProviderBlockSchema is a schema of `google` provider block -var GoogleProviderBlockSchema = &hcl.BodySchema{ - Attributes: []hcl.AttributeSchema{ +var GoogleProviderBlockSchema = &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{ {Name: "project"}, }, } -// ProviderData represents a provider block with an eval context (runner) -type ProviderData struct { - provider *configs.Provider - runner tflint.Runner - attributes hcl.Attributes - blocks hcl.Blocks -} - // GetProject retrieves project_id from the "provider" block in the Terraform configuration and environment variables func GetProject(runner tflint.Runner) (string, error) { - provider, err := runner.RootProvider("google") + providers, err := runner.GetModuleContent( + &hclext.BodySchema{ + Blocks: []hclext.BlockSchema{ + { + Type: "provider", + LabelNames: []string{"name"}, + Body: GoogleProviderBlockSchema, + }, + }, + }, + &tflint.GetModuleContentOption{ModuleCtx: tflint.RootModuleCtxType}, + ) if err != nil { return "", err } - if provider == nil { - return getProjectFromEnv(), nil - } - d, err := newProviderData(provider, runner) - if err != nil { - return "", err - } + for _, provider := range providers.Blocks { + if provider.Labels[0] != "google" { + continue + } + + opts := &tflint.EvaluateExprOption{ModuleCtx: tflint.RootModuleCtxType} - project, exists, err := d.Get("project") - if exists { - return project, err + if attr, exists := provider.Body.Attributes["project"]; exists { + var project string + if err := runner.EvaluateExpr(attr.Expr, &project, opts); err != nil { + return project, err + } + } } return getProjectFromEnv(), nil @@ -55,44 +59,3 @@ func getProjectFromEnv() string { } return "" } - -func newProviderData(provider *configs.Provider, runner tflint.Runner) (*ProviderData, error) { - providerData := &ProviderData{ - provider: provider, - runner: runner, - attributes: map[string]*hcl.Attribute{}, - blocks: []*hcl.Block{}, - } - - if provider != nil { - content, _, diags := provider.Config.PartialContent(GoogleProviderBlockSchema) - if diags.HasErrors() { - return nil, diags - } - - providerData.attributes = content.Attributes - providerData.blocks = content.Blocks - } - - return providerData, nil -} - -// Get returns a value corresponding to the given key -// It should be noted that the value is evaluated if it is evaluable -// The second return value is a flag that determines whether a value exists -// We assume the provider has only simple attributes, so it just returns string -func (d *ProviderData) Get(key string) (string, bool, error) { - attribute, exists := d.attributes[key] - if !exists { - return "", false, nil - } - - var val string - err := d.runner.EvaluateExprOnRootCtx(attribute.Expr, &val, nil) - - err = d.runner.EnsureNoError(err, func() error { return nil }) - if err != nil { - return "", true, err - } - return val, true, nil -} diff --git a/google/ruleset.go b/google/ruleset.go index 7b28990..0aa713a 100644 --- a/google/ruleset.go +++ b/google/ruleset.go @@ -3,54 +3,42 @@ package google import ( "fmt" - "github.com/hashicorp/hcl/v2/gohcl" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // RuleSet is the custom ruleset for the Google provider plugin. type RuleSet struct { tflint.BuiltinRuleSet - APIRules []tflint.Rule - config *Config + config *Config } -// RuleNames is a list of rule names provided by the plugin. -func (r *RuleSet) RuleNames() []string { - names := []string{} - for _, rule := range r.Rules { - names = append(names, rule.Name()) - } - for _, rule := range r.APIRules { - names = append(names, rule.Name()) - } - return names +func (r *RuleSet) ConfigSchema() *hclext.BodySchema { + r.config = &Config{} + return hclext.ImpliedBodySchema(r.config) } // ApplyConfig reflects the plugin configuration to the ruleset. -func (r *RuleSet) ApplyConfig(config *tflint.Config) error { - r.ApplyCommonConfig(config) - - // Apply "plugin" block config - cfg := Config{} - diags := gohcl.DecodeBody(config.Body, nil, &cfg) +func (r *RuleSet) ApplyConfig(body *hclext.BodyContent) error { + diags := hclext.DecodeBody(body, nil, r.config) if diags.HasErrors() { return diags } - r.config = &cfg - // Apply config for API rules - for _, rule := range r.APIRules { - enabled := rule.Enabled() - if cfg := config.Rules[rule.Name()]; cfg != nil { - enabled = cfg.Enabled - } else if config.DisabledByDefault { - enabled = false - } + if r.config.DeepCheck { + return nil + } - if cfg.DeepCheck && enabled { - r.EnabledRules = append(r.EnabledRules, rule) + // Disable deep checking rules + enabledRules := []tflint.Rule{} + for _, rule := range r.EnabledRules { + meta := rule.Metadata() + // Deep checking rules must have metadata like `map[string]bool{"deep": true}`` + if meta == nil { + enabledRules = append(enabledRules, rule) } } + r.EnabledRules = enabledRules return nil } diff --git a/main.go b/main.go index af6c818..a7537d2 100644 --- a/main.go +++ b/main.go @@ -6,7 +6,6 @@ import ( "github.com/terraform-linters/tflint-ruleset-google/google" "github.com/terraform-linters/tflint-ruleset-google/project" "github.com/terraform-linters/tflint-ruleset-google/rules" - "github.com/terraform-linters/tflint-ruleset-google/rules/api" ) func main() { @@ -17,7 +16,6 @@ func main() { Version: project.Version, Rules: rules.Rules, }, - APIRules: api.Rules, }, }) } diff --git a/rules/api/google_disabled_api.go b/rules/api/google_disabled_api.go index 995742e..cd85a2f 100644 --- a/rules/api/google_disabled_api.go +++ b/rules/api/google_disabled_api.go @@ -4,7 +4,7 @@ import ( "context" "fmt" - "github.com/terraform-linters/tflint-plugin-sdk/terraform/configs" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" "github.com/terraform-linters/tflint-ruleset-google/google" "github.com/terraform-linters/tflint-ruleset-google/project" @@ -14,6 +14,8 @@ import ( // GoogleDisabledAPIRule checks whether the API required by resources is disabled type GoogleDisabledAPIRule struct { + tflint.DefaultRule + prepared bool enabledAPIs map[string]*serviceusage.GoogleApiServiceusageV1Service } @@ -37,7 +39,7 @@ func (r *GoogleDisabledAPIRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleDisabledAPIRule) Severity() string { +func (r *GoogleDisabledAPIRule) Severity() tflint.Severity { return tflint.ERROR } @@ -46,6 +48,11 @@ func (r *GoogleDisabledAPIRule) Link() string { return project.ReferenceLink(r.Name()) } +// Metadata returns the metadata about deep checking +func (r *GoogleDisabledAPIRule) Metadata() interface{} { + return map[string]bool{"deep": true} +} + // Check checks whether the API required by resources is disabled func (r *GoogleDisabledAPIRule) Check(rr tflint.Runner) error { runner := rr.(*google.Runner) @@ -63,13 +70,7 @@ func (r *GoogleDisabledAPIRule) Check(rr tflint.Runner) error { return nil }) if err != nil { - err := &tflint.Error{ - Code: tflint.ExternalAPIError, - Level: tflint.ErrorLevel, - Message: "An error occurred while invoking ServiceUsage.List", - Cause: err, - } - return err + return fmt.Errorf("An error occurred while invoking ServiceUsage.List; %w", err) } } @@ -78,21 +79,25 @@ func (r *GoogleDisabledAPIRule) Check(rr tflint.Runner) error { continue } - err := runner.WalkResources(resource, func(resource *configs.Resource) error { + resources, err := runner.GetResourceContent(resource, &hclext.BodySchema{}, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { for _, ref := range product.APIsRequired { if _, ok := r.enabledAPIs[ref.Name]; !ok { err := runner.EmitIssue( r, fmt.Sprintf("%s has not been used in %s before or it is disabled.", ref.Name, runner.Project), - resource.DeclRange, + resource.DefRange, ) if err != nil { return err } } } - return nil - }) + } if err != nil { return err } diff --git a/rules/google_composer_environment_invalid_machine_type.go b/rules/google_composer_environment_invalid_machine_type.go index 6d58aba..8819103 100644 --- a/rules/google_composer_environment_invalid_machine_type.go +++ b/rules/google_composer_environment_invalid_machine_type.go @@ -3,12 +3,14 @@ package rules import ( "fmt" - hcl "github.com/hashicorp/hcl/v2" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComposerEnvironmentInvalidMachineTypeRule checks whether the machine type is invalid -type GoogleComposerEnvironmentInvalidMachineTypeRule struct{} +type GoogleComposerEnvironmentInvalidMachineTypeRule struct { + tflint.DefaultRule +} // NewGoogleComposerEnvironmentInvalidMachineTypeRule returns a new rule func NewGoogleComposerEnvironmentInvalidMachineTypeRule() *GoogleComposerEnvironmentInvalidMachineTypeRule { @@ -26,7 +28,7 @@ func (r *GoogleComposerEnvironmentInvalidMachineTypeRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleComposerEnvironmentInvalidMachineTypeRule) Severity() string { +func (r *GoogleComposerEnvironmentInvalidMachineTypeRule) Severity() tflint.Severity { return tflint.ERROR } @@ -37,28 +39,35 @@ func (r *GoogleComposerEnvironmentInvalidMachineTypeRule) Link() string { // Check checks whether the machine type is invalid func (r *GoogleComposerEnvironmentInvalidMachineTypeRule) Check(runner tflint.Runner) error { - return runner.WalkResourceBlocks("google_composer_environment", "config", func(block *hcl.Block) error { - content, _, diags := block.Body.PartialContent(&hcl.BodySchema{ - Blocks: []hcl.BlockHeaderSchema{ - {Type: "node_config"}, - }, - }) - if diags.HasErrors() { - return diags - } - - for _, block := range content.Blocks { - content, _, diags := block.Body.PartialContent(&hcl.BodySchema{ - Attributes: []hcl.AttributeSchema{ - {Name: "machine_type"}, + resources, err := runner.GetResourceContent("google_composer_environment", &hclext.BodySchema{ + Blocks: []hclext.BlockSchema{ + { + Type: "config", + Body: &hclext.BodySchema{ + Blocks: []hclext.BlockSchema{ + { + Type: "node_config", + Body: &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: "machine_type"}}, + }, + }, + }, }, - }) - - if diags.HasErrors() { - return diags - } + }, + }, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + for _, config := range resource.Body.Blocks { + for _, nodeConfig := range config.Body.Blocks { + attribute, exists := nodeConfig.Body.Attributes["machine_type"] + if !exists { + continue + } - if attribute, exists := content.Attributes["machine_type"]; exists { var machineType string err := runner.EvaluateExpr(attribute.Expr, &machineType, nil) @@ -67,19 +76,18 @@ func (r *GoogleComposerEnvironmentInvalidMachineTypeRule) Check(runner tflint.Ru return nil } - return runner.EmitIssueOnExpr( + return runner.EmitIssue( r, fmt.Sprintf(`"%s" is an invalid as machine type`, machineType), - attribute.Expr, + attribute.Expr.Range(), ) }) - if err != nil { return err } } } + } - return nil - }) + return nil } diff --git a/rules/google_compute_instance_invalid_machine_type.go b/rules/google_compute_instance_invalid_machine_type.go index 6da18c1..e8c3a05 100644 --- a/rules/google_compute_instance_invalid_machine_type.go +++ b/rules/google_compute_instance_invalid_machine_type.go @@ -3,12 +3,14 @@ package rules import ( "fmt" - hcl "github.com/hashicorp/hcl/v2" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeInstanceInvalidMachineTypeRule checks whether the machine type is invalid -type GoogleComputeInstanceInvalidMachineTypeRule struct{} +type GoogleComputeInstanceInvalidMachineTypeRule struct { + tflint.DefaultRule +} // NewGoogleComputeInstanceInvalidMachineTypeRule returns a new rule func NewGoogleComputeInstanceInvalidMachineTypeRule() *GoogleComputeInstanceInvalidMachineTypeRule { @@ -26,7 +28,7 @@ func (r *GoogleComputeInstanceInvalidMachineTypeRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleComputeInstanceInvalidMachineTypeRule) Severity() string { +func (r *GoogleComputeInstanceInvalidMachineTypeRule) Severity() tflint.Severity { return tflint.ERROR } @@ -37,20 +39,37 @@ func (r *GoogleComputeInstanceInvalidMachineTypeRule) Link() string { // Check checks whether the machine type is invalid func (r *GoogleComputeInstanceInvalidMachineTypeRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes("google_compute_instance", "machine_type", func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent("google_compute_instance", &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: "machine_type"}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes["machine_type"] + if !exists { + continue + } + var machineType string err := runner.EvaluateExpr(attribute.Expr, &machineType, nil) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { if validMachineTypes[machineType] || isCustomType(machineType) { return nil } - return runner.EmitIssueOnExpr( + return runner.EmitIssue( r, fmt.Sprintf(`"%s" is an invalid as machine type`, machineType), - attribute.Expr, + attribute.Expr.Range(), ) }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/google_compute_instance_template_invalid_machine_type.go b/rules/google_compute_instance_template_invalid_machine_type.go index d932e55..1e59b7d 100644 --- a/rules/google_compute_instance_template_invalid_machine_type.go +++ b/rules/google_compute_instance_template_invalid_machine_type.go @@ -3,12 +3,14 @@ package rules import ( "fmt" - hcl "github.com/hashicorp/hcl/v2" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeInstanceTemplateInvalidMachineTypeRule checks whether the machine type is invalid -type GoogleComputeInstanceTemplateInvalidMachineTypeRule struct{} +type GoogleComputeInstanceTemplateInvalidMachineTypeRule struct { + tflint.DefaultRule +} // NewGoogleComputeInstanceTemplateInvalidMachineTypeRule returns a new rule func NewGoogleComputeInstanceTemplateInvalidMachineTypeRule() *GoogleComputeInstanceTemplateInvalidMachineTypeRule { @@ -26,7 +28,7 @@ func (r *GoogleComputeInstanceTemplateInvalidMachineTypeRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleComputeInstanceTemplateInvalidMachineTypeRule) Severity() string { +func (r *GoogleComputeInstanceTemplateInvalidMachineTypeRule) Severity() tflint.Severity { return tflint.ERROR } @@ -37,20 +39,37 @@ func (r *GoogleComputeInstanceTemplateInvalidMachineTypeRule) Link() string { // Check checks whether the machine type is invalid func (r *GoogleComputeInstanceTemplateInvalidMachineTypeRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes("google_compute_instance_template", "machine_type", func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent("google_compute_instance_template", &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: "machine_type"}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes["machine_type"] + if !exists { + continue + } + var machineType string err := runner.EvaluateExpr(attribute.Expr, &machineType, nil) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { if validMachineTypes[machineType] || isCustomType(machineType) { return nil } - return runner.EmitIssueOnExpr( + return runner.EmitIssue( r, fmt.Sprintf(`"%s" is an invalid as machine type`, machineType), - attribute.Expr, + attribute.Expr.Range(), ) }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/google_compute_reservation_invalid_machine_type.go b/rules/google_compute_reservation_invalid_machine_type.go index c54f781..7c494bb 100644 --- a/rules/google_compute_reservation_invalid_machine_type.go +++ b/rules/google_compute_reservation_invalid_machine_type.go @@ -3,12 +3,14 @@ package rules import ( "fmt" - hcl "github.com/hashicorp/hcl/v2" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeReservationInvalidMachineTypeRule checks whether the machine type is invalid -type GoogleComputeReservationInvalidMachineTypeRule struct{} +type GoogleComputeReservationInvalidMachineTypeRule struct { + tflint.DefaultRule +} // NewGoogleComputeReservationInvalidMachineTypeRule returns a new rule func NewGoogleComputeReservationInvalidMachineTypeRule() *GoogleComputeReservationInvalidMachineTypeRule { @@ -26,7 +28,7 @@ func (r *GoogleComputeReservationInvalidMachineTypeRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleComputeReservationInvalidMachineTypeRule) Severity() string { +func (r *GoogleComputeReservationInvalidMachineTypeRule) Severity() tflint.Severity { return tflint.ERROR } @@ -37,28 +39,35 @@ func (r *GoogleComputeReservationInvalidMachineTypeRule) Link() string { // Check checks whether the machine type is invalid func (r *GoogleComputeReservationInvalidMachineTypeRule) Check(runner tflint.Runner) error { - return runner.WalkResourceBlocks("google_compute_reservation", "specific_reservation", func(block *hcl.Block) error { - content, _, diags := block.Body.PartialContent(&hcl.BodySchema{ - Blocks: []hcl.BlockHeaderSchema{ - {Type: "instance_properties"}, - }, - }) - if diags.HasErrors() { - return diags - } - - for _, block := range content.Blocks { - content, _, diags := block.Body.PartialContent(&hcl.BodySchema{ - Attributes: []hcl.AttributeSchema{ - {Name: "machine_type"}, + resources, err := runner.GetResourceContent("google_compute_reservation", &hclext.BodySchema{ + Blocks: []hclext.BlockSchema{ + { + Type: "specific_reservation", + Body: &hclext.BodySchema{ + Blocks: []hclext.BlockSchema{ + { + Type: "instance_properties", + Body: &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: "machine_type"}}, + }, + }, + }, }, - }) - - if diags.HasErrors() { - return diags - } + }, + }, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + for _, reservations := range resource.Body.Blocks { + for _, properties := range reservations.Body.Blocks { + attribute, exists := properties.Body.Attributes["machine_type"] + if !exists { + continue + } - if attribute, exists := content.Attributes["machine_type"]; exists { var machineType string err := runner.EvaluateExpr(attribute.Expr, &machineType, nil) @@ -67,10 +76,10 @@ func (r *GoogleComputeReservationInvalidMachineTypeRule) Check(runner tflint.Run return nil } - return runner.EmitIssueOnExpr( + return runner.EmitIssue( r, fmt.Sprintf(`"%s" is an invalid as machine type`, machineType), - attribute.Expr, + attribute.Expr.Range(), ) }) @@ -79,7 +88,7 @@ func (r *GoogleComputeReservationInvalidMachineTypeRule) Check(runner tflint.Run } } } + } - return nil - }) + return nil } diff --git a/rules/google_compute_resource_policy_invalid_name.go b/rules/google_compute_resource_policy_invalid_name.go index 0ad71f4..a1cb18e 100644 --- a/rules/google_compute_resource_policy_invalid_name.go +++ b/rules/google_compute_resource_policy_invalid_name.go @@ -1,12 +1,14 @@ package rules import ( - hcl "github.com/hashicorp/hcl/v2" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeResourcePolicyInvalidNameRule checks whether the name is invalid type GoogleComputeResourcePolicyInvalidNameRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -30,7 +32,7 @@ func (r *GoogleComputeResourcePolicyInvalidNameRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleComputeResourcePolicyInvalidNameRule) Severity() string { +func (r *GoogleComputeResourcePolicyInvalidNameRule) Severity() tflint.Severity { return tflint.ERROR } @@ -41,18 +43,35 @@ func (r *GoogleComputeResourcePolicyInvalidNameRule) Link() string { // Check checks whether the name is invalid func (r *GoogleComputeResourcePolicyInvalidNameRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validateRegexp(`^[a-z]([-a-z0-9]*[a-z0-9])$`) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/google_container_cluster_invalid_machine_type.go b/rules/google_container_cluster_invalid_machine_type.go index 869e660..57e690b 100644 --- a/rules/google_container_cluster_invalid_machine_type.go +++ b/rules/google_container_cluster_invalid_machine_type.go @@ -3,12 +3,14 @@ package rules import ( "fmt" - hcl "github.com/hashicorp/hcl/v2" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleContainerClusterInvalidMachineTypeRule checks whether the machine type is invalid -type GoogleContainerClusterInvalidMachineTypeRule struct{} +type GoogleContainerClusterInvalidMachineTypeRule struct { + tflint.DefaultRule +} // NewGoogleContainerClusterInvalidMachineTypeRule returns a new rule func NewGoogleContainerClusterInvalidMachineTypeRule() *GoogleContainerClusterInvalidMachineTypeRule { @@ -26,7 +28,7 @@ func (r *GoogleContainerClusterInvalidMachineTypeRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleContainerClusterInvalidMachineTypeRule) Severity() string { +func (r *GoogleContainerClusterInvalidMachineTypeRule) Severity() tflint.Severity { return tflint.ERROR } @@ -37,33 +39,46 @@ func (r *GoogleContainerClusterInvalidMachineTypeRule) Link() string { // Check checks whether the machine type is invalid func (r *GoogleContainerClusterInvalidMachineTypeRule) Check(runner tflint.Runner) error { - return runner.WalkResourceBlocks("google_container_cluster", "node_config", func(block *hcl.Block) error { - content, _, diags := block.Body.PartialContent(&hcl.BodySchema{ - Attributes: []hcl.AttributeSchema{ - {Name: "machine_type"}, + resources, err := runner.GetResourceContent("google_container_cluster", &hclext.BodySchema{ + Blocks: []hclext.BlockSchema{ + { + Type: "node_config", + Body: &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: "machine_type"}}, + }, }, - }) - if diags.HasErrors() { - return diags - } + }, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + for _, nodeConfig := range resource.Body.Blocks { + attribute, exists := nodeConfig.Body.Attributes["machine_type"] + if !exists { + continue + } - if attribute, exists := content.Attributes["machine_type"]; exists { var machineType string err := runner.EvaluateExpr(attribute.Expr, &machineType, nil) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { if validMachineTypes[machineType] || isCustomType(machineType) { return nil } - return runner.EmitIssueOnExpr( + return runner.EmitIssue( r, fmt.Sprintf(`"%s" is an invalid as machine type`, machineType), - attribute.Expr, + attribute.Expr.Range(), ) }) + if err != nil { + return err + } } + } - return nil - }) + return nil } diff --git a/rules/google_container_node_pool_invalid_machine_type.go b/rules/google_container_node_pool_invalid_machine_type.go index c5cc342..facb6f4 100644 --- a/rules/google_container_node_pool_invalid_machine_type.go +++ b/rules/google_container_node_pool_invalid_machine_type.go @@ -3,12 +3,14 @@ package rules import ( "fmt" - hcl "github.com/hashicorp/hcl/v2" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleContainerNodePoolInvalidMachineTypeRule checks whether the machine type is invalid -type GoogleContainerNodePoolInvalidMachineTypeRule struct{} +type GoogleContainerNodePoolInvalidMachineTypeRule struct { + tflint.DefaultRule +} // NewGoogleContainerNodePoolInvalidMachineTypeRule returns a new rule func NewGoogleContainerNodePoolInvalidMachineTypeRule() *GoogleContainerNodePoolInvalidMachineTypeRule { @@ -26,7 +28,7 @@ func (r *GoogleContainerNodePoolInvalidMachineTypeRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleContainerNodePoolInvalidMachineTypeRule) Severity() string { +func (r *GoogleContainerNodePoolInvalidMachineTypeRule) Severity() tflint.Severity { return tflint.ERROR } @@ -37,33 +39,46 @@ func (r *GoogleContainerNodePoolInvalidMachineTypeRule) Link() string { // Check checks whether the machine type is invalid func (r *GoogleContainerNodePoolInvalidMachineTypeRule) Check(runner tflint.Runner) error { - return runner.WalkResourceBlocks("google_container_node_pool", "node_config", func(block *hcl.Block) error { - content, _, diags := block.Body.PartialContent(&hcl.BodySchema{ - Attributes: []hcl.AttributeSchema{ - {Name: "machine_type"}, + resources, err := runner.GetResourceContent("google_container_node_pool", &hclext.BodySchema{ + Blocks: []hclext.BlockSchema{ + { + Type: "node_config", + Body: &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: "machine_type"}}, + }, }, - }) - if diags.HasErrors() { - return diags - } + }, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + for _, nodeConfig := range resource.Body.Blocks { + attribute, exists := nodeConfig.Body.Attributes["machine_type"] + if !exists { + continue + } - if attribute, exists := content.Attributes["machine_type"]; exists { var machineType string err := runner.EvaluateExpr(attribute.Expr, &machineType, nil) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { if validMachineTypes[machineType] || isCustomType(machineType) { return nil } - return runner.EmitIssueOnExpr( + return runner.EmitIssue( r, fmt.Sprintf(`"%s" is an invalid as machine type`, machineType), - attribute.Expr, + attribute.Expr.Range(), ) }) + if err != nil { + return err + } } + } - return nil - }) + return nil } diff --git a/rules/google_dataflow_job_invalid_machine_type.go b/rules/google_dataflow_job_invalid_machine_type.go index 9bcd7b2..3a81618 100644 --- a/rules/google_dataflow_job_invalid_machine_type.go +++ b/rules/google_dataflow_job_invalid_machine_type.go @@ -3,12 +3,14 @@ package rules import ( "fmt" - hcl "github.com/hashicorp/hcl/v2" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleDataflowJobInvalidMachineTypeRule checks whether the machine type is invalid -type GoogleDataflowJobInvalidMachineTypeRule struct{} +type GoogleDataflowJobInvalidMachineTypeRule struct { + tflint.DefaultRule +} // NewGoogleDataflowJobInvalidMachineTypeRule returns a new rule func NewGoogleDataflowJobInvalidMachineTypeRule() *GoogleDataflowJobInvalidMachineTypeRule { @@ -26,7 +28,7 @@ func (r *GoogleDataflowJobInvalidMachineTypeRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleDataflowJobInvalidMachineTypeRule) Severity() string { +func (r *GoogleDataflowJobInvalidMachineTypeRule) Severity() tflint.Severity { return tflint.ERROR } @@ -37,20 +39,37 @@ func (r *GoogleDataflowJobInvalidMachineTypeRule) Link() string { // Check checks whether the machine type is invalid func (r *GoogleDataflowJobInvalidMachineTypeRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes("google_dataflow_job", "machine_type", func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent("google_dataflow_job", &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: "machine_type"}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes["machine_type"] + if !exists { + continue + } + var machineType string err := runner.EvaluateExpr(attribute.Expr, &machineType, nil) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { if validMachineTypes[machineType] || isCustomType(machineType) { return nil } - return runner.EmitIssueOnExpr( + return runner.EmitIssue( r, fmt.Sprintf(`"%s" is an invalid as machine type`, machineType), - attribute.Expr, + attribute.Expr.Range(), ) }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/google_project_iam_audit_config_invalid_member.go b/rules/google_project_iam_audit_config_invalid_member.go index 5e841c1..664adc3 100644 --- a/rules/google_project_iam_audit_config_invalid_member.go +++ b/rules/google_project_iam_audit_config_invalid_member.go @@ -3,14 +3,15 @@ package rules import ( "fmt" - hcl "github.com/hashicorp/hcl/v2" - "github.com/terraform-linters/tflint-plugin-sdk/terraform/configs" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" "github.com/terraform-linters/tflint-ruleset-google/project" ) // GoogleProjectIamAuditConfigInvalidMemberRule checks whether member value is invalid type GoogleProjectIamAuditConfigInvalidMemberRule struct { + tflint.DefaultRule + resourceType string blockName string attributeName string @@ -35,7 +36,7 @@ func (r *GoogleProjectIamAuditConfigInvalidMemberRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleProjectIamAuditConfigInvalidMemberRule) Severity() string { +func (r *GoogleProjectIamAuditConfigInvalidMemberRule) Severity() tflint.Severity { return tflint.ERROR } @@ -46,46 +47,47 @@ func (r *GoogleProjectIamAuditConfigInvalidMemberRule) Link() string { // Check checks whether member format is invalid func (r *GoogleProjectIamAuditConfigInvalidMemberRule) Check(runner tflint.Runner) error { - return runner.WalkResources(r.resourceType, func(resource *configs.Resource) error { - content, _, diags := resource.Config.PartialContent(&hcl.BodySchema{ - Blocks: []hcl.BlockHeaderSchema{{Type: r.blockName}}, - }) - if diags.HasErrors() { - return diags - } - - for _, block := range content.Blocks { - content, _, diags := block.Body.PartialContent(&hcl.BodySchema{ - Attributes: []hcl.AttributeSchema{ - {Name: r.attributeName}, + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Blocks: []hclext.BlockSchema{ + { + Type: r.blockName, + Body: &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, }, - }) - if diags.HasErrors() { - return diags + }, + }, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + for _, config := range resource.Body.Blocks { + attribute, exists := config.Body.Attributes[r.attributeName] + if !exists { + continue } - if attribute, exists := content.Attributes[r.attributeName]; exists { - var members []string - err := runner.EvaluateExpr(attribute.Expr, &members, nil) + var members []string + err := runner.EvaluateExpr(attribute.Expr, &members, nil) - err = runner.EnsureNoError(err, func() error { - for _, member := range members { - if !isValidIAMMemberFormat(member) { - return runner.EmitIssueOnExpr( - r, - fmt.Sprintf("%s is an invalid member format", member), - attribute.Expr, - ) - } + err = runner.EnsureNoError(err, func() error { + for _, member := range members { + if !isValidIAMMemberFormat(member) { + return runner.EmitIssue( + r, + fmt.Sprintf("%s is an invalid member format", member), + attribute.Expr.Range(), + ) } - return nil - }) - if err != nil { - return err } + return nil + }) + if err != nil { + return err } } + } - return nil - }) + return nil } diff --git a/rules/google_project_iam_binding_invalid_member.go b/rules/google_project_iam_binding_invalid_member.go index 2c5afdd..1012c4c 100644 --- a/rules/google_project_iam_binding_invalid_member.go +++ b/rules/google_project_iam_binding_invalid_member.go @@ -3,13 +3,15 @@ package rules import ( "fmt" - hcl "github.com/hashicorp/hcl/v2" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" "github.com/terraform-linters/tflint-ruleset-google/project" ) // GoogleProjectIamBindingInvalidMemberRule checks whether member value is invalid type GoogleProjectIamBindingInvalidMemberRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -33,7 +35,7 @@ func (r *GoogleProjectIamBindingInvalidMemberRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleProjectIamBindingInvalidMemberRule) Severity() string { +func (r *GoogleProjectIamBindingInvalidMemberRule) Severity() tflint.Severity { return tflint.ERROR } @@ -44,21 +46,38 @@ func (r *GoogleProjectIamBindingInvalidMemberRule) Link() string { // Check checks whether member format is invalid func (r *GoogleProjectIamBindingInvalidMemberRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var members []string err := runner.EvaluateExpr(attribute.Expr, &members, nil) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { for _, member := range members { if !isValidIAMMemberFormat(member) { - return runner.EmitIssueOnExpr( + return runner.EmitIssue( r, fmt.Sprintf("%s is an invalid member format", member), - attribute.Expr, + attribute.Expr.Range(), ) } } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/google_project_iam_member_invalid_member.go b/rules/google_project_iam_member_invalid_member.go index 0a24c3b..58c556b 100644 --- a/rules/google_project_iam_member_invalid_member.go +++ b/rules/google_project_iam_member_invalid_member.go @@ -3,13 +3,15 @@ package rules import ( "fmt" - hcl "github.com/hashicorp/hcl/v2" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" "github.com/terraform-linters/tflint-ruleset-google/project" ) // GoogleProjectIamMemberInvalidMemberRule checks whether member value is invalid type GoogleProjectIamMemberInvalidMemberRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -33,7 +35,7 @@ func (r *GoogleProjectIamMemberInvalidMemberRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleProjectIamMemberInvalidMemberRule) Severity() string { +func (r *GoogleProjectIamMemberInvalidMemberRule) Severity() tflint.Severity { return tflint.ERROR } @@ -44,20 +46,36 @@ func (r *GoogleProjectIamMemberInvalidMemberRule) Link() string { // Check checks whether member format is invalid func (r *GoogleProjectIamMemberInvalidMemberRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } var member string err := runner.EvaluateExpr(attribute.Expr, &member, nil) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { if isValidIAMMemberFormat(member) { return nil } - return runner.EmitIssueOnExpr( + return runner.EmitIssue( r, fmt.Sprintf("%s is an invalid member format", member), - attribute.Expr, + attribute.Expr.Range(), ) }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/google_project_iam_policy_invalid_member.go b/rules/google_project_iam_policy_invalid_member.go index 228f5b4..47e037a 100644 --- a/rules/google_project_iam_policy_invalid_member.go +++ b/rules/google_project_iam_policy_invalid_member.go @@ -3,13 +3,15 @@ package rules import ( "fmt" - hcl "github.com/hashicorp/hcl/v2" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" "github.com/terraform-linters/tflint-ruleset-google/project" ) // GoogleProjectIamPolicyInvalidMemberRule checks whether member value is invalid type GoogleProjectIamPolicyInvalidMemberRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -33,7 +35,7 @@ func (r *GoogleProjectIamPolicyInvalidMemberRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleProjectIamPolicyInvalidMemberRule) Severity() string { +func (r *GoogleProjectIamPolicyInvalidMemberRule) Severity() tflint.Severity { return tflint.ERROR } @@ -44,22 +46,39 @@ func (r *GoogleProjectIamPolicyInvalidMemberRule) Link() string { // Check checks whether member format is invalid func (r *GoogleProjectIamPolicyInvalidMemberRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var members []string err := runner.EvaluateExpr(attribute.Expr, &members, nil) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { for _, member := range members { if !isValidIAMMemberFormat(member) { - return runner.EmitIssueOnExpr( + return runner.EmitIssue( r, fmt.Sprintf("%s is an invalid member format", member), - attribute.Expr, + attribute.Expr.Range(), ) } } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_access_context_manager_service_perimeter_invalid_perimeter_type.go b/rules/magicmodules/google_access_context_manager_service_perimeter_invalid_perimeter_type.go index f1ae601..7b364a0 100644 --- a/rules/magicmodules/google_access_context_manager_service_perimeter_invalid_perimeter_type.go +++ b/rules/magicmodules/google_access_context_manager_service_perimeter_invalid_perimeter_type.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleAccessContextManagerServicePerimeterInvalidPerimeterTypeRule checks the pattern is valid type GoogleAccessContextManagerServicePerimeterInvalidPerimeterTypeRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleAccessContextManagerServicePerimeterInvalidPerimeterTypeRule) Ena } // Severity returns the rule severity -func (r *GoogleAccessContextManagerServicePerimeterInvalidPerimeterTypeRule) Severity() string { +func (r *GoogleAccessContextManagerServicePerimeterInvalidPerimeterTypeRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleAccessContextManagerServicePerimeterInvalidPerimeterTypeRule) Lin // Check checks the pattern is valid func (r *GoogleAccessContextManagerServicePerimeterInvalidPerimeterTypeRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"PERIMETER_TYPE_REGULAR", "PERIMETER_TYPE_BRIDGE", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_active_directory_domain_trust_invalid_trust_direction.go b/rules/magicmodules/google_active_directory_domain_trust_invalid_trust_direction.go index 6742a55..c50a13b 100644 --- a/rules/magicmodules/google_active_directory_domain_trust_invalid_trust_direction.go +++ b/rules/magicmodules/google_active_directory_domain_trust_invalid_trust_direction.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleActiveDirectoryDomainTrustInvalidTrustDirectionRule checks the pattern is valid type GoogleActiveDirectoryDomainTrustInvalidTrustDirectionRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleActiveDirectoryDomainTrustInvalidTrustDirectionRule) Enabled() bo } // Severity returns the rule severity -func (r *GoogleActiveDirectoryDomainTrustInvalidTrustDirectionRule) Severity() string { +func (r *GoogleActiveDirectoryDomainTrustInvalidTrustDirectionRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleActiveDirectoryDomainTrustInvalidTrustDirectionRule) Link() strin // Check checks the pattern is valid func (r *GoogleActiveDirectoryDomainTrustInvalidTrustDirectionRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"INBOUND", "OUTBOUND", "BIDIRECTIONAL"}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_active_directory_domain_trust_invalid_trust_type.go b/rules/magicmodules/google_active_directory_domain_trust_invalid_trust_type.go index aeb4a15..2e369ac 100644 --- a/rules/magicmodules/google_active_directory_domain_trust_invalid_trust_type.go +++ b/rules/magicmodules/google_active_directory_domain_trust_invalid_trust_type.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleActiveDirectoryDomainTrustInvalidTrustTypeRule checks the pattern is valid type GoogleActiveDirectoryDomainTrustInvalidTrustTypeRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleActiveDirectoryDomainTrustInvalidTrustTypeRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleActiveDirectoryDomainTrustInvalidTrustTypeRule) Severity() string { +func (r *GoogleActiveDirectoryDomainTrustInvalidTrustTypeRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleActiveDirectoryDomainTrustInvalidTrustTypeRule) Link() string { // Check checks the pattern is valid func (r *GoogleActiveDirectoryDomainTrustInvalidTrustTypeRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"FOREST", "EXTERNAL"}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_apigee_organization_invalid_runtime_type.go b/rules/magicmodules/google_apigee_organization_invalid_runtime_type.go index a968f2f..0f91a1f 100644 --- a/rules/magicmodules/google_apigee_organization_invalid_runtime_type.go +++ b/rules/magicmodules/google_apigee_organization_invalid_runtime_type.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleApigeeOrganizationInvalidRuntimeTypeRule checks the pattern is valid type GoogleApigeeOrganizationInvalidRuntimeTypeRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleApigeeOrganizationInvalidRuntimeTypeRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleApigeeOrganizationInvalidRuntimeTypeRule) Severity() string { +func (r *GoogleApigeeOrganizationInvalidRuntimeTypeRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleApigeeOrganizationInvalidRuntimeTypeRule) Link() string { // Check checks the pattern is valid func (r *GoogleApigeeOrganizationInvalidRuntimeTypeRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"CLOUD", "HYBRID", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_app_engine_domain_mapping_invalid_override_strategy.go b/rules/magicmodules/google_app_engine_domain_mapping_invalid_override_strategy.go index 4f6b34d..d0bcb4b 100644 --- a/rules/magicmodules/google_app_engine_domain_mapping_invalid_override_strategy.go +++ b/rules/magicmodules/google_app_engine_domain_mapping_invalid_override_strategy.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleAppEngineDomainMappingInvalidOverrideStrategyRule checks the pattern is valid type GoogleAppEngineDomainMappingInvalidOverrideStrategyRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleAppEngineDomainMappingInvalidOverrideStrategyRule) Enabled() bool } // Severity returns the rule severity -func (r *GoogleAppEngineDomainMappingInvalidOverrideStrategyRule) Severity() string { +func (r *GoogleAppEngineDomainMappingInvalidOverrideStrategyRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleAppEngineDomainMappingInvalidOverrideStrategyRule) Link() string // Check checks the pattern is valid func (r *GoogleAppEngineDomainMappingInvalidOverrideStrategyRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"STRICT", "OVERRIDE", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_app_engine_firewall_rule_invalid_action.go b/rules/magicmodules/google_app_engine_firewall_rule_invalid_action.go index 7a471c5..7d1c670 100644 --- a/rules/magicmodules/google_app_engine_firewall_rule_invalid_action.go +++ b/rules/magicmodules/google_app_engine_firewall_rule_invalid_action.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleAppEngineFirewallRuleInvalidActionRule checks the pattern is valid type GoogleAppEngineFirewallRuleInvalidActionRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleAppEngineFirewallRuleInvalidActionRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleAppEngineFirewallRuleInvalidActionRule) Severity() string { +func (r *GoogleAppEngineFirewallRuleInvalidActionRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleAppEngineFirewallRuleInvalidActionRule) Link() string { // Check checks the pattern is valid func (r *GoogleAppEngineFirewallRuleInvalidActionRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"UNSPECIFIED_ACTION", "ALLOW", "DENY"}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_app_engine_flexible_app_version_invalid_serving_status.go b/rules/magicmodules/google_app_engine_flexible_app_version_invalid_serving_status.go index 7c3cbac..2a803ca 100644 --- a/rules/magicmodules/google_app_engine_flexible_app_version_invalid_serving_status.go +++ b/rules/magicmodules/google_app_engine_flexible_app_version_invalid_serving_status.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleAppEngineFlexibleAppVersionInvalidServingStatusRule checks the pattern is valid type GoogleAppEngineFlexibleAppVersionInvalidServingStatusRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleAppEngineFlexibleAppVersionInvalidServingStatusRule) Enabled() bo } // Severity returns the rule severity -func (r *GoogleAppEngineFlexibleAppVersionInvalidServingStatusRule) Severity() string { +func (r *GoogleAppEngineFlexibleAppVersionInvalidServingStatusRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleAppEngineFlexibleAppVersionInvalidServingStatusRule) Link() strin // Check checks the pattern is valid func (r *GoogleAppEngineFlexibleAppVersionInvalidServingStatusRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"SERVING", "STOPPED", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_bigquery_routine_invalid_determinism_level.go b/rules/magicmodules/google_bigquery_routine_invalid_determinism_level.go index c7971b8..1a9f3fa 100644 --- a/rules/magicmodules/google_bigquery_routine_invalid_determinism_level.go +++ b/rules/magicmodules/google_bigquery_routine_invalid_determinism_level.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleBigqueryRoutineInvalidDeterminismLevelRule checks the pattern is valid type GoogleBigqueryRoutineInvalidDeterminismLevelRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleBigqueryRoutineInvalidDeterminismLevelRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleBigqueryRoutineInvalidDeterminismLevelRule) Severity() string { +func (r *GoogleBigqueryRoutineInvalidDeterminismLevelRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleBigqueryRoutineInvalidDeterminismLevelRule) Link() string { // Check checks the pattern is valid func (r *GoogleBigqueryRoutineInvalidDeterminismLevelRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"DETERMINISM_LEVEL_UNSPECIFIED", "DETERMINISTIC", "NOT_DETERMINISTIC", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_bigquery_routine_invalid_language.go b/rules/magicmodules/google_bigquery_routine_invalid_language.go index f6544c6..e82746a 100644 --- a/rules/magicmodules/google_bigquery_routine_invalid_language.go +++ b/rules/magicmodules/google_bigquery_routine_invalid_language.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleBigqueryRoutineInvalidLanguageRule checks the pattern is valid type GoogleBigqueryRoutineInvalidLanguageRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleBigqueryRoutineInvalidLanguageRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleBigqueryRoutineInvalidLanguageRule) Severity() string { +func (r *GoogleBigqueryRoutineInvalidLanguageRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleBigqueryRoutineInvalidLanguageRule) Link() string { // Check checks the pattern is valid func (r *GoogleBigqueryRoutineInvalidLanguageRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"SQL", "JAVASCRIPT", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_bigquery_routine_invalid_routine_type.go b/rules/magicmodules/google_bigquery_routine_invalid_routine_type.go index 4e157ae..000ba70 100644 --- a/rules/magicmodules/google_bigquery_routine_invalid_routine_type.go +++ b/rules/magicmodules/google_bigquery_routine_invalid_routine_type.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleBigqueryRoutineInvalidRoutineTypeRule checks the pattern is valid type GoogleBigqueryRoutineInvalidRoutineTypeRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleBigqueryRoutineInvalidRoutineTypeRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleBigqueryRoutineInvalidRoutineTypeRule) Severity() string { +func (r *GoogleBigqueryRoutineInvalidRoutineTypeRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleBigqueryRoutineInvalidRoutineTypeRule) Link() string { // Check checks the pattern is valid func (r *GoogleBigqueryRoutineInvalidRoutineTypeRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"SCALAR_FUNCTION", "PROCEDURE", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_binary_authorization_policy_invalid_global_policy_evaluation_mode.go b/rules/magicmodules/google_binary_authorization_policy_invalid_global_policy_evaluation_mode.go index 8b9ffcc..e74f7aa 100644 --- a/rules/magicmodules/google_binary_authorization_policy_invalid_global_policy_evaluation_mode.go +++ b/rules/magicmodules/google_binary_authorization_policy_invalid_global_policy_evaluation_mode.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleBinaryAuthorizationPolicyInvalidGlobalPolicyEvaluationModeRule checks the pattern is valid type GoogleBinaryAuthorizationPolicyInvalidGlobalPolicyEvaluationModeRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleBinaryAuthorizationPolicyInvalidGlobalPolicyEvaluationModeRule) E } // Severity returns the rule severity -func (r *GoogleBinaryAuthorizationPolicyInvalidGlobalPolicyEvaluationModeRule) Severity() string { +func (r *GoogleBinaryAuthorizationPolicyInvalidGlobalPolicyEvaluationModeRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleBinaryAuthorizationPolicyInvalidGlobalPolicyEvaluationModeRule) L // Check checks the pattern is valid func (r *GoogleBinaryAuthorizationPolicyInvalidGlobalPolicyEvaluationModeRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"ENABLE", "DISABLE", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_cloud_asset_folder_feed_invalid_content_type.go b/rules/magicmodules/google_cloud_asset_folder_feed_invalid_content_type.go index c33be77..1e86247 100644 --- a/rules/magicmodules/google_cloud_asset_folder_feed_invalid_content_type.go +++ b/rules/magicmodules/google_cloud_asset_folder_feed_invalid_content_type.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleCloudAssetFolderFeedInvalidContentTypeRule checks the pattern is valid type GoogleCloudAssetFolderFeedInvalidContentTypeRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleCloudAssetFolderFeedInvalidContentTypeRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleCloudAssetFolderFeedInvalidContentTypeRule) Severity() string { +func (r *GoogleCloudAssetFolderFeedInvalidContentTypeRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleCloudAssetFolderFeedInvalidContentTypeRule) Link() string { // Check checks the pattern is valid func (r *GoogleCloudAssetFolderFeedInvalidContentTypeRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"CONTENT_TYPE_UNSPECIFIED", "RESOURCE", "IAM_POLICY", "ORG_POLICY", "ACCESS_POLICY", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_cloud_asset_organization_feed_invalid_content_type.go b/rules/magicmodules/google_cloud_asset_organization_feed_invalid_content_type.go index 0fd1078..385ae57 100644 --- a/rules/magicmodules/google_cloud_asset_organization_feed_invalid_content_type.go +++ b/rules/magicmodules/google_cloud_asset_organization_feed_invalid_content_type.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleCloudAssetOrganizationFeedInvalidContentTypeRule checks the pattern is valid type GoogleCloudAssetOrganizationFeedInvalidContentTypeRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleCloudAssetOrganizationFeedInvalidContentTypeRule) Enabled() bool } // Severity returns the rule severity -func (r *GoogleCloudAssetOrganizationFeedInvalidContentTypeRule) Severity() string { +func (r *GoogleCloudAssetOrganizationFeedInvalidContentTypeRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleCloudAssetOrganizationFeedInvalidContentTypeRule) Link() string { // Check checks the pattern is valid func (r *GoogleCloudAssetOrganizationFeedInvalidContentTypeRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"CONTENT_TYPE_UNSPECIFIED", "RESOURCE", "IAM_POLICY", "ORG_POLICY", "ACCESS_POLICY", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_cloud_asset_project_feed_invalid_content_type.go b/rules/magicmodules/google_cloud_asset_project_feed_invalid_content_type.go index 30876c6..17be5b6 100644 --- a/rules/magicmodules/google_cloud_asset_project_feed_invalid_content_type.go +++ b/rules/magicmodules/google_cloud_asset_project_feed_invalid_content_type.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleCloudAssetProjectFeedInvalidContentTypeRule checks the pattern is valid type GoogleCloudAssetProjectFeedInvalidContentTypeRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleCloudAssetProjectFeedInvalidContentTypeRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleCloudAssetProjectFeedInvalidContentTypeRule) Severity() string { +func (r *GoogleCloudAssetProjectFeedInvalidContentTypeRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleCloudAssetProjectFeedInvalidContentTypeRule) Link() string { // Check checks the pattern is valid func (r *GoogleCloudAssetProjectFeedInvalidContentTypeRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"CONTENT_TYPE_UNSPECIFIED", "RESOURCE", "IAM_POLICY", "ORG_POLICY", "ACCESS_POLICY", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_cloud_identity_group_invalid_initial_group_config.go b/rules/magicmodules/google_cloud_identity_group_invalid_initial_group_config.go index 74c3ec6..e9cafb5 100644 --- a/rules/magicmodules/google_cloud_identity_group_invalid_initial_group_config.go +++ b/rules/magicmodules/google_cloud_identity_group_invalid_initial_group_config.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleCloudIdentityGroupInvalidInitialGroupConfigRule checks the pattern is valid type GoogleCloudIdentityGroupInvalidInitialGroupConfigRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleCloudIdentityGroupInvalidInitialGroupConfigRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleCloudIdentityGroupInvalidInitialGroupConfigRule) Severity() string { +func (r *GoogleCloudIdentityGroupInvalidInitialGroupConfigRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleCloudIdentityGroupInvalidInitialGroupConfigRule) Link() string { // Check checks the pattern is valid func (r *GoogleCloudIdentityGroupInvalidInitialGroupConfigRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"INITIAL_GROUP_CONFIG_UNSPECIFIED", "WITH_INITIAL_OWNER", "EMPTY", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_cloudiot_device_invalid_log_level.go b/rules/magicmodules/google_cloudiot_device_invalid_log_level.go index 50f6974..796e2dd 100644 --- a/rules/magicmodules/google_cloudiot_device_invalid_log_level.go +++ b/rules/magicmodules/google_cloudiot_device_invalid_log_level.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleCloudiotDeviceInvalidLogLevelRule checks the pattern is valid type GoogleCloudiotDeviceInvalidLogLevelRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleCloudiotDeviceInvalidLogLevelRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleCloudiotDeviceInvalidLogLevelRule) Severity() string { +func (r *GoogleCloudiotDeviceInvalidLogLevelRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleCloudiotDeviceInvalidLogLevelRule) Link() string { // Check checks the pattern is valid func (r *GoogleCloudiotDeviceInvalidLogLevelRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"NONE", "ERROR", "INFO", "DEBUG", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_cloudiot_registry_invalid_log_level.go b/rules/magicmodules/google_cloudiot_registry_invalid_log_level.go index 369324c..83e7190 100644 --- a/rules/magicmodules/google_cloudiot_registry_invalid_log_level.go +++ b/rules/magicmodules/google_cloudiot_registry_invalid_log_level.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleCloudiotRegistryInvalidLogLevelRule checks the pattern is valid type GoogleCloudiotRegistryInvalidLogLevelRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleCloudiotRegistryInvalidLogLevelRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleCloudiotRegistryInvalidLogLevelRule) Severity() string { +func (r *GoogleCloudiotRegistryInvalidLogLevelRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleCloudiotRegistryInvalidLogLevelRule) Link() string { // Check checks the pattern is valid func (r *GoogleCloudiotRegistryInvalidLogLevelRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"NONE", "ERROR", "INFO", "DEBUG", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_compute_address_invalid_address_type.go b/rules/magicmodules/google_compute_address_invalid_address_type.go index 2fab4f4..8ef790f 100644 --- a/rules/magicmodules/google_compute_address_invalid_address_type.go +++ b/rules/magicmodules/google_compute_address_invalid_address_type.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeAddressInvalidAddressTypeRule checks the pattern is valid type GoogleComputeAddressInvalidAddressTypeRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleComputeAddressInvalidAddressTypeRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleComputeAddressInvalidAddressTypeRule) Severity() string { +func (r *GoogleComputeAddressInvalidAddressTypeRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleComputeAddressInvalidAddressTypeRule) Link() string { // Check checks the pattern is valid func (r *GoogleComputeAddressInvalidAddressTypeRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"INTERNAL", "EXTERNAL", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_compute_address_invalid_name.go b/rules/magicmodules/google_compute_address_invalid_name.go index d08c781..46e1a70 100644 --- a/rules/magicmodules/google_compute_address_invalid_name.go +++ b/rules/magicmodules/google_compute_address_invalid_name.go @@ -15,12 +15,14 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeAddressInvalidNameRule checks the pattern is valid type GoogleComputeAddressInvalidNameRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -44,7 +46,7 @@ func (r *GoogleComputeAddressInvalidNameRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleComputeAddressInvalidNameRule) Severity() string { +func (r *GoogleComputeAddressInvalidNameRule) Severity() tflint.Severity { return tflint.ERROR } @@ -55,18 +57,35 @@ func (r *GoogleComputeAddressInvalidNameRule) Link() string { // Check checks the pattern is valid func (r *GoogleComputeAddressInvalidNameRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validateRegexp(`^(?:[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?)$`) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_compute_address_invalid_network_tier.go b/rules/magicmodules/google_compute_address_invalid_network_tier.go index e7d417a..3b59f50 100644 --- a/rules/magicmodules/google_compute_address_invalid_network_tier.go +++ b/rules/magicmodules/google_compute_address_invalid_network_tier.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeAddressInvalidNetworkTierRule checks the pattern is valid type GoogleComputeAddressInvalidNetworkTierRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleComputeAddressInvalidNetworkTierRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleComputeAddressInvalidNetworkTierRule) Severity() string { +func (r *GoogleComputeAddressInvalidNetworkTierRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleComputeAddressInvalidNetworkTierRule) Link() string { // Check checks the pattern is valid func (r *GoogleComputeAddressInvalidNetworkTierRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"PREMIUM", "STANDARD", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_compute_backend_bucket_invalid_name.go b/rules/magicmodules/google_compute_backend_bucket_invalid_name.go index ad33c39..9b0b33f 100644 --- a/rules/magicmodules/google_compute_backend_bucket_invalid_name.go +++ b/rules/magicmodules/google_compute_backend_bucket_invalid_name.go @@ -15,12 +15,14 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeBackendBucketInvalidNameRule checks the pattern is valid type GoogleComputeBackendBucketInvalidNameRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -44,7 +46,7 @@ func (r *GoogleComputeBackendBucketInvalidNameRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleComputeBackendBucketInvalidNameRule) Severity() string { +func (r *GoogleComputeBackendBucketInvalidNameRule) Severity() tflint.Severity { return tflint.ERROR } @@ -55,18 +57,35 @@ func (r *GoogleComputeBackendBucketInvalidNameRule) Link() string { // Check checks the pattern is valid func (r *GoogleComputeBackendBucketInvalidNameRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validateRegexp(`^(?:[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?)$`) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_compute_backend_bucket_signed_url_key_invalid_name.go b/rules/magicmodules/google_compute_backend_bucket_signed_url_key_invalid_name.go index 29c09ed..2b001be 100644 --- a/rules/magicmodules/google_compute_backend_bucket_signed_url_key_invalid_name.go +++ b/rules/magicmodules/google_compute_backend_bucket_signed_url_key_invalid_name.go @@ -15,12 +15,14 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeBackendBucketSignedUrlKeyInvalidNameRule checks the pattern is valid type GoogleComputeBackendBucketSignedUrlKeyInvalidNameRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -44,7 +46,7 @@ func (r *GoogleComputeBackendBucketSignedUrlKeyInvalidNameRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleComputeBackendBucketSignedUrlKeyInvalidNameRule) Severity() string { +func (r *GoogleComputeBackendBucketSignedUrlKeyInvalidNameRule) Severity() tflint.Severity { return tflint.ERROR } @@ -55,18 +57,35 @@ func (r *GoogleComputeBackendBucketSignedUrlKeyInvalidNameRule) Link() string { // Check checks the pattern is valid func (r *GoogleComputeBackendBucketSignedUrlKeyInvalidNameRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validateRegexp(`^(?:[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?)$`) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_compute_backend_service_invalid_load_balancing_scheme.go b/rules/magicmodules/google_compute_backend_service_invalid_load_balancing_scheme.go index 464e8d5..fbc561c 100644 --- a/rules/magicmodules/google_compute_backend_service_invalid_load_balancing_scheme.go +++ b/rules/magicmodules/google_compute_backend_service_invalid_load_balancing_scheme.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeBackendServiceInvalidLoadBalancingSchemeRule checks the pattern is valid type GoogleComputeBackendServiceInvalidLoadBalancingSchemeRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleComputeBackendServiceInvalidLoadBalancingSchemeRule) Enabled() bo } // Severity returns the rule severity -func (r *GoogleComputeBackendServiceInvalidLoadBalancingSchemeRule) Severity() string { +func (r *GoogleComputeBackendServiceInvalidLoadBalancingSchemeRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleComputeBackendServiceInvalidLoadBalancingSchemeRule) Link() strin // Check checks the pattern is valid func (r *GoogleComputeBackendServiceInvalidLoadBalancingSchemeRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"EXTERNAL", "INTERNAL_SELF_MANAGED", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_compute_backend_service_invalid_locality_lb_policy.go b/rules/magicmodules/google_compute_backend_service_invalid_locality_lb_policy.go index f989e4b..8e4e8a2 100644 --- a/rules/magicmodules/google_compute_backend_service_invalid_locality_lb_policy.go +++ b/rules/magicmodules/google_compute_backend_service_invalid_locality_lb_policy.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeBackendServiceInvalidLocalityLbPolicyRule checks the pattern is valid type GoogleComputeBackendServiceInvalidLocalityLbPolicyRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleComputeBackendServiceInvalidLocalityLbPolicyRule) Enabled() bool } // Severity returns the rule severity -func (r *GoogleComputeBackendServiceInvalidLocalityLbPolicyRule) Severity() string { +func (r *GoogleComputeBackendServiceInvalidLocalityLbPolicyRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleComputeBackendServiceInvalidLocalityLbPolicyRule) Link() string { // Check checks the pattern is valid func (r *GoogleComputeBackendServiceInvalidLocalityLbPolicyRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"ROUND_ROBIN", "LEAST_REQUEST", "RING_HASH", "RANDOM", "ORIGINAL_DESTINATION", "MAGLEV", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_compute_backend_service_invalid_protocol.go b/rules/magicmodules/google_compute_backend_service_invalid_protocol.go index 732fb3d..e58e7b8 100644 --- a/rules/magicmodules/google_compute_backend_service_invalid_protocol.go +++ b/rules/magicmodules/google_compute_backend_service_invalid_protocol.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeBackendServiceInvalidProtocolRule checks the pattern is valid type GoogleComputeBackendServiceInvalidProtocolRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleComputeBackendServiceInvalidProtocolRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleComputeBackendServiceInvalidProtocolRule) Severity() string { +func (r *GoogleComputeBackendServiceInvalidProtocolRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleComputeBackendServiceInvalidProtocolRule) Link() string { // Check checks the pattern is valid func (r *GoogleComputeBackendServiceInvalidProtocolRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"HTTP", "HTTPS", "HTTP2", "TCP", "SSL", "GRPC", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_compute_backend_service_invalid_session_affinity.go b/rules/magicmodules/google_compute_backend_service_invalid_session_affinity.go index 862895e..fde0a02 100644 --- a/rules/magicmodules/google_compute_backend_service_invalid_session_affinity.go +++ b/rules/magicmodules/google_compute_backend_service_invalid_session_affinity.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeBackendServiceInvalidSessionAffinityRule checks the pattern is valid type GoogleComputeBackendServiceInvalidSessionAffinityRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleComputeBackendServiceInvalidSessionAffinityRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleComputeBackendServiceInvalidSessionAffinityRule) Severity() string { +func (r *GoogleComputeBackendServiceInvalidSessionAffinityRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleComputeBackendServiceInvalidSessionAffinityRule) Link() string { // Check checks the pattern is valid func (r *GoogleComputeBackendServiceInvalidSessionAffinityRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"NONE", "CLIENT_IP", "CLIENT_IP_PORT_PROTO", "CLIENT_IP_PROTO", "GENERATED_COOKIE", "HEADER_FIELD", "HTTP_COOKIE", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_compute_backend_service_signed_url_key_invalid_name.go b/rules/magicmodules/google_compute_backend_service_signed_url_key_invalid_name.go index 15cbff3..2ebd6f2 100644 --- a/rules/magicmodules/google_compute_backend_service_signed_url_key_invalid_name.go +++ b/rules/magicmodules/google_compute_backend_service_signed_url_key_invalid_name.go @@ -15,12 +15,14 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeBackendServiceSignedUrlKeyInvalidNameRule checks the pattern is valid type GoogleComputeBackendServiceSignedUrlKeyInvalidNameRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -44,7 +46,7 @@ func (r *GoogleComputeBackendServiceSignedUrlKeyInvalidNameRule) Enabled() bool } // Severity returns the rule severity -func (r *GoogleComputeBackendServiceSignedUrlKeyInvalidNameRule) Severity() string { +func (r *GoogleComputeBackendServiceSignedUrlKeyInvalidNameRule) Severity() tflint.Severity { return tflint.ERROR } @@ -55,18 +57,35 @@ func (r *GoogleComputeBackendServiceSignedUrlKeyInvalidNameRule) Link() string { // Check checks the pattern is valid func (r *GoogleComputeBackendServiceSignedUrlKeyInvalidNameRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validateRegexp(`^(?:[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?)$`) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_compute_external_vpn_gateway_invalid_redundancy_type.go b/rules/magicmodules/google_compute_external_vpn_gateway_invalid_redundancy_type.go index 042a3ae..f6a9934 100644 --- a/rules/magicmodules/google_compute_external_vpn_gateway_invalid_redundancy_type.go +++ b/rules/magicmodules/google_compute_external_vpn_gateway_invalid_redundancy_type.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeExternalVpnGatewayInvalidRedundancyTypeRule checks the pattern is valid type GoogleComputeExternalVpnGatewayInvalidRedundancyTypeRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleComputeExternalVpnGatewayInvalidRedundancyTypeRule) Enabled() boo } // Severity returns the rule severity -func (r *GoogleComputeExternalVpnGatewayInvalidRedundancyTypeRule) Severity() string { +func (r *GoogleComputeExternalVpnGatewayInvalidRedundancyTypeRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleComputeExternalVpnGatewayInvalidRedundancyTypeRule) Link() string // Check checks the pattern is valid func (r *GoogleComputeExternalVpnGatewayInvalidRedundancyTypeRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"FOUR_IPS_REDUNDANCY", "SINGLE_IP_INTERNALLY_REDUNDANT", "TWO_IPS_REDUNDANCY", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_compute_firewall_invalid_direction.go b/rules/magicmodules/google_compute_firewall_invalid_direction.go index dabbc43..c3d7008 100644 --- a/rules/magicmodules/google_compute_firewall_invalid_direction.go +++ b/rules/magicmodules/google_compute_firewall_invalid_direction.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeFirewallInvalidDirectionRule checks the pattern is valid type GoogleComputeFirewallInvalidDirectionRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleComputeFirewallInvalidDirectionRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleComputeFirewallInvalidDirectionRule) Severity() string { +func (r *GoogleComputeFirewallInvalidDirectionRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleComputeFirewallInvalidDirectionRule) Link() string { // Check checks the pattern is valid func (r *GoogleComputeFirewallInvalidDirectionRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"INGRESS", "EGRESS", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_compute_forwarding_rule_invalid_ip_protocol.go b/rules/magicmodules/google_compute_forwarding_rule_invalid_ip_protocol.go index 9d97ad3..bceef37 100644 --- a/rules/magicmodules/google_compute_forwarding_rule_invalid_ip_protocol.go +++ b/rules/magicmodules/google_compute_forwarding_rule_invalid_ip_protocol.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeForwardingRuleInvalidIpProtocolRule checks the pattern is valid type GoogleComputeForwardingRuleInvalidIpProtocolRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleComputeForwardingRuleInvalidIpProtocolRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleComputeForwardingRuleInvalidIpProtocolRule) Severity() string { +func (r *GoogleComputeForwardingRuleInvalidIpProtocolRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleComputeForwardingRuleInvalidIpProtocolRule) Link() string { // Check checks the pattern is valid func (r *GoogleComputeForwardingRuleInvalidIpProtocolRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"TCP", "UDP", "ESP", "AH", "SCTP", "ICMP", "L3_DEFAULT", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_compute_forwarding_rule_invalid_load_balancing_scheme.go b/rules/magicmodules/google_compute_forwarding_rule_invalid_load_balancing_scheme.go index 01b9b92..fc43bcb 100644 --- a/rules/magicmodules/google_compute_forwarding_rule_invalid_load_balancing_scheme.go +++ b/rules/magicmodules/google_compute_forwarding_rule_invalid_load_balancing_scheme.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeForwardingRuleInvalidLoadBalancingSchemeRule checks the pattern is valid type GoogleComputeForwardingRuleInvalidLoadBalancingSchemeRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleComputeForwardingRuleInvalidLoadBalancingSchemeRule) Enabled() bo } // Severity returns the rule severity -func (r *GoogleComputeForwardingRuleInvalidLoadBalancingSchemeRule) Severity() string { +func (r *GoogleComputeForwardingRuleInvalidLoadBalancingSchemeRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleComputeForwardingRuleInvalidLoadBalancingSchemeRule) Link() strin // Check checks the pattern is valid func (r *GoogleComputeForwardingRuleInvalidLoadBalancingSchemeRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"EXTERNAL", "INTERNAL", "INTERNAL_MANAGED", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_compute_forwarding_rule_invalid_network_tier.go b/rules/magicmodules/google_compute_forwarding_rule_invalid_network_tier.go index c08675e..42a6e34 100644 --- a/rules/magicmodules/google_compute_forwarding_rule_invalid_network_tier.go +++ b/rules/magicmodules/google_compute_forwarding_rule_invalid_network_tier.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeForwardingRuleInvalidNetworkTierRule checks the pattern is valid type GoogleComputeForwardingRuleInvalidNetworkTierRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleComputeForwardingRuleInvalidNetworkTierRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleComputeForwardingRuleInvalidNetworkTierRule) Severity() string { +func (r *GoogleComputeForwardingRuleInvalidNetworkTierRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleComputeForwardingRuleInvalidNetworkTierRule) Link() string { // Check checks the pattern is valid func (r *GoogleComputeForwardingRuleInvalidNetworkTierRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"PREMIUM", "STANDARD", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_compute_global_address_invalid_address_type.go b/rules/magicmodules/google_compute_global_address_invalid_address_type.go index c46f15a..765531a 100644 --- a/rules/magicmodules/google_compute_global_address_invalid_address_type.go +++ b/rules/magicmodules/google_compute_global_address_invalid_address_type.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeGlobalAddressInvalidAddressTypeRule checks the pattern is valid type GoogleComputeGlobalAddressInvalidAddressTypeRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleComputeGlobalAddressInvalidAddressTypeRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleComputeGlobalAddressInvalidAddressTypeRule) Severity() string { +func (r *GoogleComputeGlobalAddressInvalidAddressTypeRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleComputeGlobalAddressInvalidAddressTypeRule) Link() string { // Check checks the pattern is valid func (r *GoogleComputeGlobalAddressInvalidAddressTypeRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"EXTERNAL", "INTERNAL", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_compute_global_address_invalid_ip_version.go b/rules/magicmodules/google_compute_global_address_invalid_ip_version.go index 5beef64..e1006f7 100644 --- a/rules/magicmodules/google_compute_global_address_invalid_ip_version.go +++ b/rules/magicmodules/google_compute_global_address_invalid_ip_version.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeGlobalAddressInvalidIpVersionRule checks the pattern is valid type GoogleComputeGlobalAddressInvalidIpVersionRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleComputeGlobalAddressInvalidIpVersionRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleComputeGlobalAddressInvalidIpVersionRule) Severity() string { +func (r *GoogleComputeGlobalAddressInvalidIpVersionRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleComputeGlobalAddressInvalidIpVersionRule) Link() string { // Check checks the pattern is valid func (r *GoogleComputeGlobalAddressInvalidIpVersionRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"IPV4", "IPV6", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_compute_global_forwarding_rule_invalid_ip_protocol.go b/rules/magicmodules/google_compute_global_forwarding_rule_invalid_ip_protocol.go index cbab8ae..2408bc3 100644 --- a/rules/magicmodules/google_compute_global_forwarding_rule_invalid_ip_protocol.go +++ b/rules/magicmodules/google_compute_global_forwarding_rule_invalid_ip_protocol.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeGlobalForwardingRuleInvalidIpProtocolRule checks the pattern is valid type GoogleComputeGlobalForwardingRuleInvalidIpProtocolRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleComputeGlobalForwardingRuleInvalidIpProtocolRule) Enabled() bool } // Severity returns the rule severity -func (r *GoogleComputeGlobalForwardingRuleInvalidIpProtocolRule) Severity() string { +func (r *GoogleComputeGlobalForwardingRuleInvalidIpProtocolRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleComputeGlobalForwardingRuleInvalidIpProtocolRule) Link() string { // Check checks the pattern is valid func (r *GoogleComputeGlobalForwardingRuleInvalidIpProtocolRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"TCP", "UDP", "ESP", "AH", "SCTP", "ICMP", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_compute_global_forwarding_rule_invalid_ip_version.go b/rules/magicmodules/google_compute_global_forwarding_rule_invalid_ip_version.go index a222bdf..de2dda0 100644 --- a/rules/magicmodules/google_compute_global_forwarding_rule_invalid_ip_version.go +++ b/rules/magicmodules/google_compute_global_forwarding_rule_invalid_ip_version.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeGlobalForwardingRuleInvalidIpVersionRule checks the pattern is valid type GoogleComputeGlobalForwardingRuleInvalidIpVersionRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleComputeGlobalForwardingRuleInvalidIpVersionRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleComputeGlobalForwardingRuleInvalidIpVersionRule) Severity() string { +func (r *GoogleComputeGlobalForwardingRuleInvalidIpVersionRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleComputeGlobalForwardingRuleInvalidIpVersionRule) Link() string { // Check checks the pattern is valid func (r *GoogleComputeGlobalForwardingRuleInvalidIpVersionRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"IPV4", "IPV6", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_compute_global_forwarding_rule_invalid_load_balancing_scheme.go b/rules/magicmodules/google_compute_global_forwarding_rule_invalid_load_balancing_scheme.go index 9b58adf..b78fa70 100644 --- a/rules/magicmodules/google_compute_global_forwarding_rule_invalid_load_balancing_scheme.go +++ b/rules/magicmodules/google_compute_global_forwarding_rule_invalid_load_balancing_scheme.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeGlobalForwardingRuleInvalidLoadBalancingSchemeRule checks the pattern is valid type GoogleComputeGlobalForwardingRuleInvalidLoadBalancingSchemeRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleComputeGlobalForwardingRuleInvalidLoadBalancingSchemeRule) Enable } // Severity returns the rule severity -func (r *GoogleComputeGlobalForwardingRuleInvalidLoadBalancingSchemeRule) Severity() string { +func (r *GoogleComputeGlobalForwardingRuleInvalidLoadBalancingSchemeRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleComputeGlobalForwardingRuleInvalidLoadBalancingSchemeRule) Link() // Check checks the pattern is valid func (r *GoogleComputeGlobalForwardingRuleInvalidLoadBalancingSchemeRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"EXTERNAL", "INTERNAL_SELF_MANAGED", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_compute_global_network_endpoint_group_invalid_network_endpoint_type.go b/rules/magicmodules/google_compute_global_network_endpoint_group_invalid_network_endpoint_type.go index 3dd4aad..4205643 100644 --- a/rules/magicmodules/google_compute_global_network_endpoint_group_invalid_network_endpoint_type.go +++ b/rules/magicmodules/google_compute_global_network_endpoint_group_invalid_network_endpoint_type.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeGlobalNetworkEndpointGroupInvalidNetworkEndpointTypeRule checks the pattern is valid type GoogleComputeGlobalNetworkEndpointGroupInvalidNetworkEndpointTypeRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleComputeGlobalNetworkEndpointGroupInvalidNetworkEndpointTypeRule) } // Severity returns the rule severity -func (r *GoogleComputeGlobalNetworkEndpointGroupInvalidNetworkEndpointTypeRule) Severity() string { +func (r *GoogleComputeGlobalNetworkEndpointGroupInvalidNetworkEndpointTypeRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleComputeGlobalNetworkEndpointGroupInvalidNetworkEndpointTypeRule) // Check checks the pattern is valid func (r *GoogleComputeGlobalNetworkEndpointGroupInvalidNetworkEndpointTypeRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"INTERNET_IP_PORT", "INTERNET_FQDN_PORT"}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_compute_interconnect_attachment_invalid_bandwidth.go b/rules/magicmodules/google_compute_interconnect_attachment_invalid_bandwidth.go index 55e6122..8c507d1 100644 --- a/rules/magicmodules/google_compute_interconnect_attachment_invalid_bandwidth.go +++ b/rules/magicmodules/google_compute_interconnect_attachment_invalid_bandwidth.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeInterconnectAttachmentInvalidBandwidthRule checks the pattern is valid type GoogleComputeInterconnectAttachmentInvalidBandwidthRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleComputeInterconnectAttachmentInvalidBandwidthRule) Enabled() bool } // Severity returns the rule severity -func (r *GoogleComputeInterconnectAttachmentInvalidBandwidthRule) Severity() string { +func (r *GoogleComputeInterconnectAttachmentInvalidBandwidthRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleComputeInterconnectAttachmentInvalidBandwidthRule) Link() string // Check checks the pattern is valid func (r *GoogleComputeInterconnectAttachmentInvalidBandwidthRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"BPS_50M", "BPS_100M", "BPS_200M", "BPS_300M", "BPS_400M", "BPS_500M", "BPS_1G", "BPS_2G", "BPS_5G", "BPS_10G", "BPS_20G", "BPS_50G", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_compute_interconnect_attachment_invalid_encryption.go b/rules/magicmodules/google_compute_interconnect_attachment_invalid_encryption.go index e34ecda..1711f02 100644 --- a/rules/magicmodules/google_compute_interconnect_attachment_invalid_encryption.go +++ b/rules/magicmodules/google_compute_interconnect_attachment_invalid_encryption.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeInterconnectAttachmentInvalidEncryptionRule checks the pattern is valid type GoogleComputeInterconnectAttachmentInvalidEncryptionRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleComputeInterconnectAttachmentInvalidEncryptionRule) Enabled() boo } // Severity returns the rule severity -func (r *GoogleComputeInterconnectAttachmentInvalidEncryptionRule) Severity() string { +func (r *GoogleComputeInterconnectAttachmentInvalidEncryptionRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleComputeInterconnectAttachmentInvalidEncryptionRule) Link() string // Check checks the pattern is valid func (r *GoogleComputeInterconnectAttachmentInvalidEncryptionRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"NONE", "IPSEC", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_compute_interconnect_attachment_invalid_name.go b/rules/magicmodules/google_compute_interconnect_attachment_invalid_name.go index 25d4848..4143be4 100644 --- a/rules/magicmodules/google_compute_interconnect_attachment_invalid_name.go +++ b/rules/magicmodules/google_compute_interconnect_attachment_invalid_name.go @@ -15,12 +15,14 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeInterconnectAttachmentInvalidNameRule checks the pattern is valid type GoogleComputeInterconnectAttachmentInvalidNameRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -44,7 +46,7 @@ func (r *GoogleComputeInterconnectAttachmentInvalidNameRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleComputeInterconnectAttachmentInvalidNameRule) Severity() string { +func (r *GoogleComputeInterconnectAttachmentInvalidNameRule) Severity() tflint.Severity { return tflint.ERROR } @@ -55,18 +57,35 @@ func (r *GoogleComputeInterconnectAttachmentInvalidNameRule) Link() string { // Check checks the pattern is valid func (r *GoogleComputeInterconnectAttachmentInvalidNameRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validateRegexp(`^[a-z]([-a-z0-9]*[a-z0-9])?$`) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_compute_interconnect_attachment_invalid_type.go b/rules/magicmodules/google_compute_interconnect_attachment_invalid_type.go index aa8b2af..258f985 100644 --- a/rules/magicmodules/google_compute_interconnect_attachment_invalid_type.go +++ b/rules/magicmodules/google_compute_interconnect_attachment_invalid_type.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeInterconnectAttachmentInvalidTypeRule checks the pattern is valid type GoogleComputeInterconnectAttachmentInvalidTypeRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleComputeInterconnectAttachmentInvalidTypeRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleComputeInterconnectAttachmentInvalidTypeRule) Severity() string { +func (r *GoogleComputeInterconnectAttachmentInvalidTypeRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleComputeInterconnectAttachmentInvalidTypeRule) Link() string { // Check checks the pattern is valid func (r *GoogleComputeInterconnectAttachmentInvalidTypeRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"DEDICATED", "PARTNER", "PARTNER_PROVIDER", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_compute_managed_ssl_certificate_invalid_type.go b/rules/magicmodules/google_compute_managed_ssl_certificate_invalid_type.go index f176b09..c252105 100644 --- a/rules/magicmodules/google_compute_managed_ssl_certificate_invalid_type.go +++ b/rules/magicmodules/google_compute_managed_ssl_certificate_invalid_type.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeManagedSslCertificateInvalidTypeRule checks the pattern is valid type GoogleComputeManagedSslCertificateInvalidTypeRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleComputeManagedSslCertificateInvalidTypeRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleComputeManagedSslCertificateInvalidTypeRule) Severity() string { +func (r *GoogleComputeManagedSslCertificateInvalidTypeRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleComputeManagedSslCertificateInvalidTypeRule) Link() string { // Check checks the pattern is valid func (r *GoogleComputeManagedSslCertificateInvalidTypeRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"MANAGED", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_compute_network_endpoint_group_invalid_network_endpoint_type.go b/rules/magicmodules/google_compute_network_endpoint_group_invalid_network_endpoint_type.go index e24f9e9..e0b39ab 100644 --- a/rules/magicmodules/google_compute_network_endpoint_group_invalid_network_endpoint_type.go +++ b/rules/magicmodules/google_compute_network_endpoint_group_invalid_network_endpoint_type.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeNetworkEndpointGroupInvalidNetworkEndpointTypeRule checks the pattern is valid type GoogleComputeNetworkEndpointGroupInvalidNetworkEndpointTypeRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleComputeNetworkEndpointGroupInvalidNetworkEndpointTypeRule) Enable } // Severity returns the rule severity -func (r *GoogleComputeNetworkEndpointGroupInvalidNetworkEndpointTypeRule) Severity() string { +func (r *GoogleComputeNetworkEndpointGroupInvalidNetworkEndpointTypeRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleComputeNetworkEndpointGroupInvalidNetworkEndpointTypeRule) Link() // Check checks the pattern is valid func (r *GoogleComputeNetworkEndpointGroupInvalidNetworkEndpointTypeRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"GCE_VM_IP_PORT", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_compute_node_template_invalid_cpu_overcommit_type.go b/rules/magicmodules/google_compute_node_template_invalid_cpu_overcommit_type.go index 83a0b8f..0a08821 100644 --- a/rules/magicmodules/google_compute_node_template_invalid_cpu_overcommit_type.go +++ b/rules/magicmodules/google_compute_node_template_invalid_cpu_overcommit_type.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeNodeTemplateInvalidCpuOvercommitTypeRule checks the pattern is valid type GoogleComputeNodeTemplateInvalidCpuOvercommitTypeRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleComputeNodeTemplateInvalidCpuOvercommitTypeRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleComputeNodeTemplateInvalidCpuOvercommitTypeRule) Severity() string { +func (r *GoogleComputeNodeTemplateInvalidCpuOvercommitTypeRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleComputeNodeTemplateInvalidCpuOvercommitTypeRule) Link() string { // Check checks the pattern is valid func (r *GoogleComputeNodeTemplateInvalidCpuOvercommitTypeRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"ENABLED", "NONE", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_compute_region_backend_service_invalid_load_balancing_scheme.go b/rules/magicmodules/google_compute_region_backend_service_invalid_load_balancing_scheme.go index 52ad800..c18dac2 100644 --- a/rules/magicmodules/google_compute_region_backend_service_invalid_load_balancing_scheme.go +++ b/rules/magicmodules/google_compute_region_backend_service_invalid_load_balancing_scheme.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeRegionBackendServiceInvalidLoadBalancingSchemeRule checks the pattern is valid type GoogleComputeRegionBackendServiceInvalidLoadBalancingSchemeRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleComputeRegionBackendServiceInvalidLoadBalancingSchemeRule) Enable } // Severity returns the rule severity -func (r *GoogleComputeRegionBackendServiceInvalidLoadBalancingSchemeRule) Severity() string { +func (r *GoogleComputeRegionBackendServiceInvalidLoadBalancingSchemeRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleComputeRegionBackendServiceInvalidLoadBalancingSchemeRule) Link() // Check checks the pattern is valid func (r *GoogleComputeRegionBackendServiceInvalidLoadBalancingSchemeRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"EXTERNAL", "INTERNAL", "INTERNAL_MANAGED", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_compute_region_backend_service_invalid_locality_lb_policy.go b/rules/magicmodules/google_compute_region_backend_service_invalid_locality_lb_policy.go index e9f85c7..daaf467 100644 --- a/rules/magicmodules/google_compute_region_backend_service_invalid_locality_lb_policy.go +++ b/rules/magicmodules/google_compute_region_backend_service_invalid_locality_lb_policy.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeRegionBackendServiceInvalidLocalityLbPolicyRule checks the pattern is valid type GoogleComputeRegionBackendServiceInvalidLocalityLbPolicyRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleComputeRegionBackendServiceInvalidLocalityLbPolicyRule) Enabled() } // Severity returns the rule severity -func (r *GoogleComputeRegionBackendServiceInvalidLocalityLbPolicyRule) Severity() string { +func (r *GoogleComputeRegionBackendServiceInvalidLocalityLbPolicyRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleComputeRegionBackendServiceInvalidLocalityLbPolicyRule) Link() st // Check checks the pattern is valid func (r *GoogleComputeRegionBackendServiceInvalidLocalityLbPolicyRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"ROUND_ROBIN", "LEAST_REQUEST", "RING_HASH", "RANDOM", "ORIGINAL_DESTINATION", "MAGLEV", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_compute_region_backend_service_invalid_protocol.go b/rules/magicmodules/google_compute_region_backend_service_invalid_protocol.go index b87b0c6..9285344 100644 --- a/rules/magicmodules/google_compute_region_backend_service_invalid_protocol.go +++ b/rules/magicmodules/google_compute_region_backend_service_invalid_protocol.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeRegionBackendServiceInvalidProtocolRule checks the pattern is valid type GoogleComputeRegionBackendServiceInvalidProtocolRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleComputeRegionBackendServiceInvalidProtocolRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleComputeRegionBackendServiceInvalidProtocolRule) Severity() string { +func (r *GoogleComputeRegionBackendServiceInvalidProtocolRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleComputeRegionBackendServiceInvalidProtocolRule) Link() string { // Check checks the pattern is valid func (r *GoogleComputeRegionBackendServiceInvalidProtocolRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"HTTP", "HTTPS", "HTTP2", "SSL", "TCP", "UDP", "GRPC", "UNSPECIFIED", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_compute_region_backend_service_invalid_session_affinity.go b/rules/magicmodules/google_compute_region_backend_service_invalid_session_affinity.go index c580d21..62bed6b 100644 --- a/rules/magicmodules/google_compute_region_backend_service_invalid_session_affinity.go +++ b/rules/magicmodules/google_compute_region_backend_service_invalid_session_affinity.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeRegionBackendServiceInvalidSessionAffinityRule checks the pattern is valid type GoogleComputeRegionBackendServiceInvalidSessionAffinityRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleComputeRegionBackendServiceInvalidSessionAffinityRule) Enabled() } // Severity returns the rule severity -func (r *GoogleComputeRegionBackendServiceInvalidSessionAffinityRule) Severity() string { +func (r *GoogleComputeRegionBackendServiceInvalidSessionAffinityRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleComputeRegionBackendServiceInvalidSessionAffinityRule) Link() str // Check checks the pattern is valid func (r *GoogleComputeRegionBackendServiceInvalidSessionAffinityRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"NONE", "CLIENT_IP", "CLIENT_IP_PORT_PROTO", "CLIENT_IP_PROTO", "GENERATED_COOKIE", "HEADER_FIELD", "HTTP_COOKIE", "CLIENT_IP_NO_DESTINATION", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_compute_region_network_endpoint_group_invalid_network_endpoint_type.go b/rules/magicmodules/google_compute_region_network_endpoint_group_invalid_network_endpoint_type.go index 81f9b89..f7033d6 100644 --- a/rules/magicmodules/google_compute_region_network_endpoint_group_invalid_network_endpoint_type.go +++ b/rules/magicmodules/google_compute_region_network_endpoint_group_invalid_network_endpoint_type.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeRegionNetworkEndpointGroupInvalidNetworkEndpointTypeRule checks the pattern is valid type GoogleComputeRegionNetworkEndpointGroupInvalidNetworkEndpointTypeRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleComputeRegionNetworkEndpointGroupInvalidNetworkEndpointTypeRule) } // Severity returns the rule severity -func (r *GoogleComputeRegionNetworkEndpointGroupInvalidNetworkEndpointTypeRule) Severity() string { +func (r *GoogleComputeRegionNetworkEndpointGroupInvalidNetworkEndpointTypeRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleComputeRegionNetworkEndpointGroupInvalidNetworkEndpointTypeRule) // Check checks the pattern is valid func (r *GoogleComputeRegionNetworkEndpointGroupInvalidNetworkEndpointTypeRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"SERVERLESS", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_compute_route_invalid_name.go b/rules/magicmodules/google_compute_route_invalid_name.go index 4f48bc3..aeb5792 100644 --- a/rules/magicmodules/google_compute_route_invalid_name.go +++ b/rules/magicmodules/google_compute_route_invalid_name.go @@ -15,12 +15,14 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeRouteInvalidNameRule checks the pattern is valid type GoogleComputeRouteInvalidNameRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -44,7 +46,7 @@ func (r *GoogleComputeRouteInvalidNameRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleComputeRouteInvalidNameRule) Severity() string { +func (r *GoogleComputeRouteInvalidNameRule) Severity() tflint.Severity { return tflint.ERROR } @@ -55,18 +57,35 @@ func (r *GoogleComputeRouteInvalidNameRule) Link() string { // Check checks the pattern is valid func (r *GoogleComputeRouteInvalidNameRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validateRegexp(`^[a-z]([-a-z0-9]*[a-z0-9])?$`) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_compute_router_nat_invalid_nat_ip_allocate_option.go b/rules/magicmodules/google_compute_router_nat_invalid_nat_ip_allocate_option.go index 4dcc193..718691c 100644 --- a/rules/magicmodules/google_compute_router_nat_invalid_nat_ip_allocate_option.go +++ b/rules/magicmodules/google_compute_router_nat_invalid_nat_ip_allocate_option.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeRouterNatInvalidNatIpAllocateOptionRule checks the pattern is valid type GoogleComputeRouterNatInvalidNatIpAllocateOptionRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleComputeRouterNatInvalidNatIpAllocateOptionRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleComputeRouterNatInvalidNatIpAllocateOptionRule) Severity() string { +func (r *GoogleComputeRouterNatInvalidNatIpAllocateOptionRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleComputeRouterNatInvalidNatIpAllocateOptionRule) Link() string { // Check checks the pattern is valid func (r *GoogleComputeRouterNatInvalidNatIpAllocateOptionRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"MANUAL_ONLY", "AUTO_ONLY"}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_compute_router_nat_invalid_source_subnetwork_ip_ranges_to_nat.go b/rules/magicmodules/google_compute_router_nat_invalid_source_subnetwork_ip_ranges_to_nat.go index 27efe57..d2d3344 100644 --- a/rules/magicmodules/google_compute_router_nat_invalid_source_subnetwork_ip_ranges_to_nat.go +++ b/rules/magicmodules/google_compute_router_nat_invalid_source_subnetwork_ip_ranges_to_nat.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeRouterNatInvalidSourceSubnetworkIpRangesToNatRule checks the pattern is valid type GoogleComputeRouterNatInvalidSourceSubnetworkIpRangesToNatRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleComputeRouterNatInvalidSourceSubnetworkIpRangesToNatRule) Enabled } // Severity returns the rule severity -func (r *GoogleComputeRouterNatInvalidSourceSubnetworkIpRangesToNatRule) Severity() string { +func (r *GoogleComputeRouterNatInvalidSourceSubnetworkIpRangesToNatRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleComputeRouterNatInvalidSourceSubnetworkIpRangesToNatRule) Link() // Check checks the pattern is valid func (r *GoogleComputeRouterNatInvalidSourceSubnetworkIpRangesToNatRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"ALL_SUBNETWORKS_ALL_IP_RANGES", "ALL_SUBNETWORKS_ALL_PRIMARY_IP_RANGES", "LIST_OF_SUBNETWORKS"}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_compute_router_peer_invalid_advertise_mode.go b/rules/magicmodules/google_compute_router_peer_invalid_advertise_mode.go index 8d7f560..adf0028 100644 --- a/rules/magicmodules/google_compute_router_peer_invalid_advertise_mode.go +++ b/rules/magicmodules/google_compute_router_peer_invalid_advertise_mode.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeRouterPeerInvalidAdvertiseModeRule checks the pattern is valid type GoogleComputeRouterPeerInvalidAdvertiseModeRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleComputeRouterPeerInvalidAdvertiseModeRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleComputeRouterPeerInvalidAdvertiseModeRule) Severity() string { +func (r *GoogleComputeRouterPeerInvalidAdvertiseModeRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleComputeRouterPeerInvalidAdvertiseModeRule) Link() string { // Check checks the pattern is valid func (r *GoogleComputeRouterPeerInvalidAdvertiseModeRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"DEFAULT", "CUSTOM", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_compute_ssl_policy_invalid_min_tls_version.go b/rules/magicmodules/google_compute_ssl_policy_invalid_min_tls_version.go index c6be1ee..a19c5aa 100644 --- a/rules/magicmodules/google_compute_ssl_policy_invalid_min_tls_version.go +++ b/rules/magicmodules/google_compute_ssl_policy_invalid_min_tls_version.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeSslPolicyInvalidMinTlsVersionRule checks the pattern is valid type GoogleComputeSslPolicyInvalidMinTlsVersionRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleComputeSslPolicyInvalidMinTlsVersionRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleComputeSslPolicyInvalidMinTlsVersionRule) Severity() string { +func (r *GoogleComputeSslPolicyInvalidMinTlsVersionRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleComputeSslPolicyInvalidMinTlsVersionRule) Link() string { // Check checks the pattern is valid func (r *GoogleComputeSslPolicyInvalidMinTlsVersionRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"TLS_1_0", "TLS_1_1", "TLS_1_2", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_compute_ssl_policy_invalid_profile.go b/rules/magicmodules/google_compute_ssl_policy_invalid_profile.go index 1bad1c4..85d4b41 100644 --- a/rules/magicmodules/google_compute_ssl_policy_invalid_profile.go +++ b/rules/magicmodules/google_compute_ssl_policy_invalid_profile.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeSslPolicyInvalidProfileRule checks the pattern is valid type GoogleComputeSslPolicyInvalidProfileRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleComputeSslPolicyInvalidProfileRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleComputeSslPolicyInvalidProfileRule) Severity() string { +func (r *GoogleComputeSslPolicyInvalidProfileRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleComputeSslPolicyInvalidProfileRule) Link() string { // Check checks the pattern is valid func (r *GoogleComputeSslPolicyInvalidProfileRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"COMPATIBLE", "MODERN", "RESTRICTED", "CUSTOM", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_compute_subnetwork_invalid_ipv6_access_type.go b/rules/magicmodules/google_compute_subnetwork_invalid_ipv6_access_type.go index 292f14e..5c2f76c 100644 --- a/rules/magicmodules/google_compute_subnetwork_invalid_ipv6_access_type.go +++ b/rules/magicmodules/google_compute_subnetwork_invalid_ipv6_access_type.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeSubnetworkInvalidIpv6AccessTypeRule checks the pattern is valid type GoogleComputeSubnetworkInvalidIpv6AccessTypeRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleComputeSubnetworkInvalidIpv6AccessTypeRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleComputeSubnetworkInvalidIpv6AccessTypeRule) Severity() string { +func (r *GoogleComputeSubnetworkInvalidIpv6AccessTypeRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleComputeSubnetworkInvalidIpv6AccessTypeRule) Link() string { // Check checks the pattern is valid func (r *GoogleComputeSubnetworkInvalidIpv6AccessTypeRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"EXTERNAL", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_compute_subnetwork_invalid_role.go b/rules/magicmodules/google_compute_subnetwork_invalid_role.go index d3c1b7c..2cea19f 100644 --- a/rules/magicmodules/google_compute_subnetwork_invalid_role.go +++ b/rules/magicmodules/google_compute_subnetwork_invalid_role.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeSubnetworkInvalidRoleRule checks the pattern is valid type GoogleComputeSubnetworkInvalidRoleRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleComputeSubnetworkInvalidRoleRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleComputeSubnetworkInvalidRoleRule) Severity() string { +func (r *GoogleComputeSubnetworkInvalidRoleRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleComputeSubnetworkInvalidRoleRule) Link() string { // Check checks the pattern is valid func (r *GoogleComputeSubnetworkInvalidRoleRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"ACTIVE", "BACKUP", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_compute_subnetwork_invalid_stack_type.go b/rules/magicmodules/google_compute_subnetwork_invalid_stack_type.go index d24012a..a44873c 100644 --- a/rules/magicmodules/google_compute_subnetwork_invalid_stack_type.go +++ b/rules/magicmodules/google_compute_subnetwork_invalid_stack_type.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeSubnetworkInvalidStackTypeRule checks the pattern is valid type GoogleComputeSubnetworkInvalidStackTypeRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleComputeSubnetworkInvalidStackTypeRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleComputeSubnetworkInvalidStackTypeRule) Severity() string { +func (r *GoogleComputeSubnetworkInvalidStackTypeRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleComputeSubnetworkInvalidStackTypeRule) Link() string { // Check checks the pattern is valid func (r *GoogleComputeSubnetworkInvalidStackTypeRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"IPV4_ONLY", "IPV4_IPV6", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_compute_target_https_proxy_invalid_quic_override.go b/rules/magicmodules/google_compute_target_https_proxy_invalid_quic_override.go index dfdcbe7..353387f 100644 --- a/rules/magicmodules/google_compute_target_https_proxy_invalid_quic_override.go +++ b/rules/magicmodules/google_compute_target_https_proxy_invalid_quic_override.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeTargetHttpsProxyInvalidQuicOverrideRule checks the pattern is valid type GoogleComputeTargetHttpsProxyInvalidQuicOverrideRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleComputeTargetHttpsProxyInvalidQuicOverrideRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleComputeTargetHttpsProxyInvalidQuicOverrideRule) Severity() string { +func (r *GoogleComputeTargetHttpsProxyInvalidQuicOverrideRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleComputeTargetHttpsProxyInvalidQuicOverrideRule) Link() string { // Check checks the pattern is valid func (r *GoogleComputeTargetHttpsProxyInvalidQuicOverrideRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"NONE", "ENABLE", "DISABLE", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_compute_target_instance_invalid_nat_policy.go b/rules/magicmodules/google_compute_target_instance_invalid_nat_policy.go index 771a074..a883608 100644 --- a/rules/magicmodules/google_compute_target_instance_invalid_nat_policy.go +++ b/rules/magicmodules/google_compute_target_instance_invalid_nat_policy.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeTargetInstanceInvalidNatPolicyRule checks the pattern is valid type GoogleComputeTargetInstanceInvalidNatPolicyRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleComputeTargetInstanceInvalidNatPolicyRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleComputeTargetInstanceInvalidNatPolicyRule) Severity() string { +func (r *GoogleComputeTargetInstanceInvalidNatPolicyRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleComputeTargetInstanceInvalidNatPolicyRule) Link() string { // Check checks the pattern is valid func (r *GoogleComputeTargetInstanceInvalidNatPolicyRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"NO_NAT", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_compute_target_ssl_proxy_invalid_proxy_header.go b/rules/magicmodules/google_compute_target_ssl_proxy_invalid_proxy_header.go index 5ff4228..f38e0a0 100644 --- a/rules/magicmodules/google_compute_target_ssl_proxy_invalid_proxy_header.go +++ b/rules/magicmodules/google_compute_target_ssl_proxy_invalid_proxy_header.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeTargetSslProxyInvalidProxyHeaderRule checks the pattern is valid type GoogleComputeTargetSslProxyInvalidProxyHeaderRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleComputeTargetSslProxyInvalidProxyHeaderRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleComputeTargetSslProxyInvalidProxyHeaderRule) Severity() string { +func (r *GoogleComputeTargetSslProxyInvalidProxyHeaderRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleComputeTargetSslProxyInvalidProxyHeaderRule) Link() string { // Check checks the pattern is valid func (r *GoogleComputeTargetSslProxyInvalidProxyHeaderRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"NONE", "PROXY_V1", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_compute_target_tcp_proxy_invalid_proxy_header.go b/rules/magicmodules/google_compute_target_tcp_proxy_invalid_proxy_header.go index f5e0b51..b2f23a9 100644 --- a/rules/magicmodules/google_compute_target_tcp_proxy_invalid_proxy_header.go +++ b/rules/magicmodules/google_compute_target_tcp_proxy_invalid_proxy_header.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleComputeTargetTcpProxyInvalidProxyHeaderRule checks the pattern is valid type GoogleComputeTargetTcpProxyInvalidProxyHeaderRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleComputeTargetTcpProxyInvalidProxyHeaderRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleComputeTargetTcpProxyInvalidProxyHeaderRule) Severity() string { +func (r *GoogleComputeTargetTcpProxyInvalidProxyHeaderRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleComputeTargetTcpProxyInvalidProxyHeaderRule) Link() string { // Check checks the pattern is valid func (r *GoogleComputeTargetTcpProxyInvalidProxyHeaderRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"NONE", "PROXY_V1", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_data_catalog_entry_group_invalid_entry_group_id.go b/rules/magicmodules/google_data_catalog_entry_group_invalid_entry_group_id.go index 96119b0..fcd4647 100644 --- a/rules/magicmodules/google_data_catalog_entry_group_invalid_entry_group_id.go +++ b/rules/magicmodules/google_data_catalog_entry_group_invalid_entry_group_id.go @@ -15,12 +15,14 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleDataCatalogEntryGroupInvalidEntryGroupIdRule checks the pattern is valid type GoogleDataCatalogEntryGroupInvalidEntryGroupIdRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -44,7 +46,7 @@ func (r *GoogleDataCatalogEntryGroupInvalidEntryGroupIdRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleDataCatalogEntryGroupInvalidEntryGroupIdRule) Severity() string { +func (r *GoogleDataCatalogEntryGroupInvalidEntryGroupIdRule) Severity() tflint.Severity { return tflint.ERROR } @@ -55,18 +57,35 @@ func (r *GoogleDataCatalogEntryGroupInvalidEntryGroupIdRule) Link() string { // Check checks the pattern is valid func (r *GoogleDataCatalogEntryGroupInvalidEntryGroupIdRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validateRegexp(`^[A-z_][A-z0-9_]{0,63}$`) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_data_catalog_entry_invalid_type.go b/rules/magicmodules/google_data_catalog_entry_invalid_type.go index 967b722..1e4636d 100644 --- a/rules/magicmodules/google_data_catalog_entry_invalid_type.go +++ b/rules/magicmodules/google_data_catalog_entry_invalid_type.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleDataCatalogEntryInvalidTypeRule checks the pattern is valid type GoogleDataCatalogEntryInvalidTypeRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleDataCatalogEntryInvalidTypeRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleDataCatalogEntryInvalidTypeRule) Severity() string { +func (r *GoogleDataCatalogEntryInvalidTypeRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleDataCatalogEntryInvalidTypeRule) Link() string { // Check checks the pattern is valid func (r *GoogleDataCatalogEntryInvalidTypeRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"FILESET", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_data_catalog_entry_invalid_user_specified_system.go b/rules/magicmodules/google_data_catalog_entry_invalid_user_specified_system.go index 20c69ac..1c19272 100644 --- a/rules/magicmodules/google_data_catalog_entry_invalid_user_specified_system.go +++ b/rules/magicmodules/google_data_catalog_entry_invalid_user_specified_system.go @@ -15,12 +15,14 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleDataCatalogEntryInvalidUserSpecifiedSystemRule checks the pattern is valid type GoogleDataCatalogEntryInvalidUserSpecifiedSystemRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -44,7 +46,7 @@ func (r *GoogleDataCatalogEntryInvalidUserSpecifiedSystemRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleDataCatalogEntryInvalidUserSpecifiedSystemRule) Severity() string { +func (r *GoogleDataCatalogEntryInvalidUserSpecifiedSystemRule) Severity() tflint.Severity { return tflint.ERROR } @@ -55,18 +57,35 @@ func (r *GoogleDataCatalogEntryInvalidUserSpecifiedSystemRule) Link() string { // Check checks the pattern is valid func (r *GoogleDataCatalogEntryInvalidUserSpecifiedSystemRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validateRegexp(`^[A-z_][A-z0-9_]{0,63}$`) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_data_catalog_entry_invalid_user_specified_type.go b/rules/magicmodules/google_data_catalog_entry_invalid_user_specified_type.go index befb81e..a4f1dcd 100644 --- a/rules/magicmodules/google_data_catalog_entry_invalid_user_specified_type.go +++ b/rules/magicmodules/google_data_catalog_entry_invalid_user_specified_type.go @@ -15,12 +15,14 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleDataCatalogEntryInvalidUserSpecifiedTypeRule checks the pattern is valid type GoogleDataCatalogEntryInvalidUserSpecifiedTypeRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -44,7 +46,7 @@ func (r *GoogleDataCatalogEntryInvalidUserSpecifiedTypeRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleDataCatalogEntryInvalidUserSpecifiedTypeRule) Severity() string { +func (r *GoogleDataCatalogEntryInvalidUserSpecifiedTypeRule) Severity() tflint.Severity { return tflint.ERROR } @@ -55,18 +57,35 @@ func (r *GoogleDataCatalogEntryInvalidUserSpecifiedTypeRule) Link() string { // Check checks the pattern is valid func (r *GoogleDataCatalogEntryInvalidUserSpecifiedTypeRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validateRegexp(`^[A-z_][A-z0-9_]{0,63}$`) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_data_catalog_tag_template_invalid_tag_template_id.go b/rules/magicmodules/google_data_catalog_tag_template_invalid_tag_template_id.go index 3a8500e..dfb2965 100644 --- a/rules/magicmodules/google_data_catalog_tag_template_invalid_tag_template_id.go +++ b/rules/magicmodules/google_data_catalog_tag_template_invalid_tag_template_id.go @@ -15,12 +15,14 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleDataCatalogTagTemplateInvalidTagTemplateIdRule checks the pattern is valid type GoogleDataCatalogTagTemplateInvalidTagTemplateIdRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -44,7 +46,7 @@ func (r *GoogleDataCatalogTagTemplateInvalidTagTemplateIdRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleDataCatalogTagTemplateInvalidTagTemplateIdRule) Severity() string { +func (r *GoogleDataCatalogTagTemplateInvalidTagTemplateIdRule) Severity() tflint.Severity { return tflint.ERROR } @@ -55,18 +57,35 @@ func (r *GoogleDataCatalogTagTemplateInvalidTagTemplateIdRule) Link() string { // Check checks the pattern is valid func (r *GoogleDataCatalogTagTemplateInvalidTagTemplateIdRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validateRegexp(`^[a-z_][a-z0-9_]{0,63}$`) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_data_loss_prevention_job_trigger_invalid_status.go b/rules/magicmodules/google_data_loss_prevention_job_trigger_invalid_status.go index 90accf7..682b107 100644 --- a/rules/magicmodules/google_data_loss_prevention_job_trigger_invalid_status.go +++ b/rules/magicmodules/google_data_loss_prevention_job_trigger_invalid_status.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleDataLossPreventionJobTriggerInvalidStatusRule checks the pattern is valid type GoogleDataLossPreventionJobTriggerInvalidStatusRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleDataLossPreventionJobTriggerInvalidStatusRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleDataLossPreventionJobTriggerInvalidStatusRule) Severity() string { +func (r *GoogleDataLossPreventionJobTriggerInvalidStatusRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleDataLossPreventionJobTriggerInvalidStatusRule) Link() string { // Check checks the pattern is valid func (r *GoogleDataLossPreventionJobTriggerInvalidStatusRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"PAUSED", "HEALTHY", "CANCELLED", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_datastore_index_invalid_ancestor.go b/rules/magicmodules/google_datastore_index_invalid_ancestor.go index f4c7821..e50a886 100644 --- a/rules/magicmodules/google_datastore_index_invalid_ancestor.go +++ b/rules/magicmodules/google_datastore_index_invalid_ancestor.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleDatastoreIndexInvalidAncestorRule checks the pattern is valid type GoogleDatastoreIndexInvalidAncestorRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleDatastoreIndexInvalidAncestorRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleDatastoreIndexInvalidAncestorRule) Severity() string { +func (r *GoogleDatastoreIndexInvalidAncestorRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleDatastoreIndexInvalidAncestorRule) Link() string { // Check checks the pattern is valid func (r *GoogleDatastoreIndexInvalidAncestorRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"NONE", "ALL_ANCESTORS", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_deployment_manager_deployment_invalid_create_policy.go b/rules/magicmodules/google_deployment_manager_deployment_invalid_create_policy.go index e7fbf53..30c9922 100644 --- a/rules/magicmodules/google_deployment_manager_deployment_invalid_create_policy.go +++ b/rules/magicmodules/google_deployment_manager_deployment_invalid_create_policy.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleDeploymentManagerDeploymentInvalidCreatePolicyRule checks the pattern is valid type GoogleDeploymentManagerDeploymentInvalidCreatePolicyRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleDeploymentManagerDeploymentInvalidCreatePolicyRule) Enabled() boo } // Severity returns the rule severity -func (r *GoogleDeploymentManagerDeploymentInvalidCreatePolicyRule) Severity() string { +func (r *GoogleDeploymentManagerDeploymentInvalidCreatePolicyRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleDeploymentManagerDeploymentInvalidCreatePolicyRule) Link() string // Check checks the pattern is valid func (r *GoogleDeploymentManagerDeploymentInvalidCreatePolicyRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"ACQUIRE", "CREATE_OR_ACQUIRE", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_deployment_manager_deployment_invalid_delete_policy.go b/rules/magicmodules/google_deployment_manager_deployment_invalid_delete_policy.go index 2c4f579..455db32 100644 --- a/rules/magicmodules/google_deployment_manager_deployment_invalid_delete_policy.go +++ b/rules/magicmodules/google_deployment_manager_deployment_invalid_delete_policy.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleDeploymentManagerDeploymentInvalidDeletePolicyRule checks the pattern is valid type GoogleDeploymentManagerDeploymentInvalidDeletePolicyRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleDeploymentManagerDeploymentInvalidDeletePolicyRule) Enabled() boo } // Severity returns the rule severity -func (r *GoogleDeploymentManagerDeploymentInvalidDeletePolicyRule) Severity() string { +func (r *GoogleDeploymentManagerDeploymentInvalidDeletePolicyRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleDeploymentManagerDeploymentInvalidDeletePolicyRule) Link() string // Check checks the pattern is valid func (r *GoogleDeploymentManagerDeploymentInvalidDeletePolicyRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"ABANDON", "DELETE", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_dialogflow_agent_invalid_api_version.go b/rules/magicmodules/google_dialogflow_agent_invalid_api_version.go index c97e819..c1d5e4d 100644 --- a/rules/magicmodules/google_dialogflow_agent_invalid_api_version.go +++ b/rules/magicmodules/google_dialogflow_agent_invalid_api_version.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleDialogflowAgentInvalidApiVersionRule checks the pattern is valid type GoogleDialogflowAgentInvalidApiVersionRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleDialogflowAgentInvalidApiVersionRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleDialogflowAgentInvalidApiVersionRule) Severity() string { +func (r *GoogleDialogflowAgentInvalidApiVersionRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleDialogflowAgentInvalidApiVersionRule) Link() string { // Check checks the pattern is valid func (r *GoogleDialogflowAgentInvalidApiVersionRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"API_VERSION_V1", "API_VERSION_V2", "API_VERSION_V2_BETA_1", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_dialogflow_agent_invalid_match_mode.go b/rules/magicmodules/google_dialogflow_agent_invalid_match_mode.go index 5707fca..7de89c4 100644 --- a/rules/magicmodules/google_dialogflow_agent_invalid_match_mode.go +++ b/rules/magicmodules/google_dialogflow_agent_invalid_match_mode.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleDialogflowAgentInvalidMatchModeRule checks the pattern is valid type GoogleDialogflowAgentInvalidMatchModeRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleDialogflowAgentInvalidMatchModeRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleDialogflowAgentInvalidMatchModeRule) Severity() string { +func (r *GoogleDialogflowAgentInvalidMatchModeRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleDialogflowAgentInvalidMatchModeRule) Link() string { // Check checks the pattern is valid func (r *GoogleDialogflowAgentInvalidMatchModeRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"MATCH_MODE_HYBRID", "MATCH_MODE_ML_ONLY", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_dialogflow_agent_invalid_tier.go b/rules/magicmodules/google_dialogflow_agent_invalid_tier.go index 094d772..f9aca0b 100644 --- a/rules/magicmodules/google_dialogflow_agent_invalid_tier.go +++ b/rules/magicmodules/google_dialogflow_agent_invalid_tier.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleDialogflowAgentInvalidTierRule checks the pattern is valid type GoogleDialogflowAgentInvalidTierRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleDialogflowAgentInvalidTierRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleDialogflowAgentInvalidTierRule) Severity() string { +func (r *GoogleDialogflowAgentInvalidTierRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleDialogflowAgentInvalidTierRule) Link() string { // Check checks the pattern is valid func (r *GoogleDialogflowAgentInvalidTierRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"TIER_STANDARD", "TIER_ENTERPRISE", "TIER_ENTERPRISE_PLUS", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_dialogflow_cx_entity_type_invalid_auto_expansion_mode.go b/rules/magicmodules/google_dialogflow_cx_entity_type_invalid_auto_expansion_mode.go index 8bc9b2c..82f0ed6 100644 --- a/rules/magicmodules/google_dialogflow_cx_entity_type_invalid_auto_expansion_mode.go +++ b/rules/magicmodules/google_dialogflow_cx_entity_type_invalid_auto_expansion_mode.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleDialogflowCxEntityTypeInvalidAutoExpansionModeRule checks the pattern is valid type GoogleDialogflowCxEntityTypeInvalidAutoExpansionModeRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleDialogflowCxEntityTypeInvalidAutoExpansionModeRule) Enabled() boo } // Severity returns the rule severity -func (r *GoogleDialogflowCxEntityTypeInvalidAutoExpansionModeRule) Severity() string { +func (r *GoogleDialogflowCxEntityTypeInvalidAutoExpansionModeRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleDialogflowCxEntityTypeInvalidAutoExpansionModeRule) Link() string // Check checks the pattern is valid func (r *GoogleDialogflowCxEntityTypeInvalidAutoExpansionModeRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"AUTO_EXPANSION_MODE_DEFAULT", "AUTO_EXPANSION_MODE_UNSPECIFIED", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_dialogflow_cx_entity_type_invalid_kind.go b/rules/magicmodules/google_dialogflow_cx_entity_type_invalid_kind.go index cd78e52..21c4d05 100644 --- a/rules/magicmodules/google_dialogflow_cx_entity_type_invalid_kind.go +++ b/rules/magicmodules/google_dialogflow_cx_entity_type_invalid_kind.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleDialogflowCxEntityTypeInvalidKindRule checks the pattern is valid type GoogleDialogflowCxEntityTypeInvalidKindRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleDialogflowCxEntityTypeInvalidKindRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleDialogflowCxEntityTypeInvalidKindRule) Severity() string { +func (r *GoogleDialogflowCxEntityTypeInvalidKindRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleDialogflowCxEntityTypeInvalidKindRule) Link() string { // Check checks the pattern is valid func (r *GoogleDialogflowCxEntityTypeInvalidKindRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"KIND_MAP", "KIND_LIST", "KIND_REGEXP"}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_dialogflow_entity_type_invalid_kind.go b/rules/magicmodules/google_dialogflow_entity_type_invalid_kind.go index cb6c878..fed1525 100644 --- a/rules/magicmodules/google_dialogflow_entity_type_invalid_kind.go +++ b/rules/magicmodules/google_dialogflow_entity_type_invalid_kind.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleDialogflowEntityTypeInvalidKindRule checks the pattern is valid type GoogleDialogflowEntityTypeInvalidKindRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleDialogflowEntityTypeInvalidKindRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleDialogflowEntityTypeInvalidKindRule) Severity() string { +func (r *GoogleDialogflowEntityTypeInvalidKindRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleDialogflowEntityTypeInvalidKindRule) Link() string { // Check checks the pattern is valid func (r *GoogleDialogflowEntityTypeInvalidKindRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"KIND_MAP", "KIND_LIST", "KIND_REGEXP"}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_dialogflow_intent_invalid_webhook_state.go b/rules/magicmodules/google_dialogflow_intent_invalid_webhook_state.go index 12ce4c3..79079b3 100644 --- a/rules/magicmodules/google_dialogflow_intent_invalid_webhook_state.go +++ b/rules/magicmodules/google_dialogflow_intent_invalid_webhook_state.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleDialogflowIntentInvalidWebhookStateRule checks the pattern is valid type GoogleDialogflowIntentInvalidWebhookStateRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleDialogflowIntentInvalidWebhookStateRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleDialogflowIntentInvalidWebhookStateRule) Severity() string { +func (r *GoogleDialogflowIntentInvalidWebhookStateRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleDialogflowIntentInvalidWebhookStateRule) Link() string { // Check checks the pattern is valid func (r *GoogleDialogflowIntentInvalidWebhookStateRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"WEBHOOK_STATE_ENABLED", "WEBHOOK_STATE_ENABLED_FOR_SLOT_FILLING", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_dns_managed_zone_invalid_visibility.go b/rules/magicmodules/google_dns_managed_zone_invalid_visibility.go index cbb9d1c..b585be8 100644 --- a/rules/magicmodules/google_dns_managed_zone_invalid_visibility.go +++ b/rules/magicmodules/google_dns_managed_zone_invalid_visibility.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleDnsManagedZoneInvalidVisibilityRule checks the pattern is valid type GoogleDnsManagedZoneInvalidVisibilityRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleDnsManagedZoneInvalidVisibilityRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleDnsManagedZoneInvalidVisibilityRule) Severity() string { +func (r *GoogleDnsManagedZoneInvalidVisibilityRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleDnsManagedZoneInvalidVisibilityRule) Link() string { // Check checks the pattern is valid func (r *GoogleDnsManagedZoneInvalidVisibilityRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"private", "public", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_firestore_index_invalid_query_scope.go b/rules/magicmodules/google_firestore_index_invalid_query_scope.go index 3eaf0c5..6e5b2c6 100644 --- a/rules/magicmodules/google_firestore_index_invalid_query_scope.go +++ b/rules/magicmodules/google_firestore_index_invalid_query_scope.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleFirestoreIndexInvalidQueryScopeRule checks the pattern is valid type GoogleFirestoreIndexInvalidQueryScopeRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleFirestoreIndexInvalidQueryScopeRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleFirestoreIndexInvalidQueryScopeRule) Severity() string { +func (r *GoogleFirestoreIndexInvalidQueryScopeRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleFirestoreIndexInvalidQueryScopeRule) Link() string { // Check checks the pattern is valid func (r *GoogleFirestoreIndexInvalidQueryScopeRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"COLLECTION", "COLLECTION_GROUP", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_healthcare_fhir_store_invalid_version.go b/rules/magicmodules/google_healthcare_fhir_store_invalid_version.go index 10e0732..3cfd6a6 100644 --- a/rules/magicmodules/google_healthcare_fhir_store_invalid_version.go +++ b/rules/magicmodules/google_healthcare_fhir_store_invalid_version.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleHealthcareFhirStoreInvalidVersionRule checks the pattern is valid type GoogleHealthcareFhirStoreInvalidVersionRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleHealthcareFhirStoreInvalidVersionRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleHealthcareFhirStoreInvalidVersionRule) Severity() string { +func (r *GoogleHealthcareFhirStoreInvalidVersionRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleHealthcareFhirStoreInvalidVersionRule) Link() string { // Check checks the pattern is valid func (r *GoogleHealthcareFhirStoreInvalidVersionRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"DSTU2", "STU3", "R4"}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_kms_crypto_key_invalid_purpose.go b/rules/magicmodules/google_kms_crypto_key_invalid_purpose.go index 762272f..e2057c0 100644 --- a/rules/magicmodules/google_kms_crypto_key_invalid_purpose.go +++ b/rules/magicmodules/google_kms_crypto_key_invalid_purpose.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleKmsCryptoKeyInvalidPurposeRule checks the pattern is valid type GoogleKmsCryptoKeyInvalidPurposeRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleKmsCryptoKeyInvalidPurposeRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleKmsCryptoKeyInvalidPurposeRule) Severity() string { +func (r *GoogleKmsCryptoKeyInvalidPurposeRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleKmsCryptoKeyInvalidPurposeRule) Link() string { // Check checks the pattern is valid func (r *GoogleKmsCryptoKeyInvalidPurposeRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"ENCRYPT_DECRYPT", "ASYMMETRIC_SIGN", "ASYMMETRIC_DECRYPT", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_kms_key_ring_import_job_invalid_import_method.go b/rules/magicmodules/google_kms_key_ring_import_job_invalid_import_method.go index 0efdd16..f79cc15 100644 --- a/rules/magicmodules/google_kms_key_ring_import_job_invalid_import_method.go +++ b/rules/magicmodules/google_kms_key_ring_import_job_invalid_import_method.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleKmsKeyRingImportJobInvalidImportMethodRule checks the pattern is valid type GoogleKmsKeyRingImportJobInvalidImportMethodRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleKmsKeyRingImportJobInvalidImportMethodRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleKmsKeyRingImportJobInvalidImportMethodRule) Severity() string { +func (r *GoogleKmsKeyRingImportJobInvalidImportMethodRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleKmsKeyRingImportJobInvalidImportMethodRule) Link() string { // Check checks the pattern is valid func (r *GoogleKmsKeyRingImportJobInvalidImportMethodRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"RSA_OAEP_3072_SHA1_AES_256", "RSA_OAEP_4096_SHA1_AES_256"}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_kms_key_ring_import_job_invalid_protection_level.go b/rules/magicmodules/google_kms_key_ring_import_job_invalid_protection_level.go index 2df57e2..fc4abf7 100644 --- a/rules/magicmodules/google_kms_key_ring_import_job_invalid_protection_level.go +++ b/rules/magicmodules/google_kms_key_ring_import_job_invalid_protection_level.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleKmsKeyRingImportJobInvalidProtectionLevelRule checks the pattern is valid type GoogleKmsKeyRingImportJobInvalidProtectionLevelRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleKmsKeyRingImportJobInvalidProtectionLevelRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleKmsKeyRingImportJobInvalidProtectionLevelRule) Severity() string { +func (r *GoogleKmsKeyRingImportJobInvalidProtectionLevelRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleKmsKeyRingImportJobInvalidProtectionLevelRule) Link() string { // Check checks the pattern is valid func (r *GoogleKmsKeyRingImportJobInvalidProtectionLevelRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"SOFTWARE", "HSM", "EXTERNAL"}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_memcache_instance_invalid_memcache_version.go b/rules/magicmodules/google_memcache_instance_invalid_memcache_version.go index 81c7a72..055ed2a 100644 --- a/rules/magicmodules/google_memcache_instance_invalid_memcache_version.go +++ b/rules/magicmodules/google_memcache_instance_invalid_memcache_version.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleMemcacheInstanceInvalidMemcacheVersionRule checks the pattern is valid type GoogleMemcacheInstanceInvalidMemcacheVersionRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleMemcacheInstanceInvalidMemcacheVersionRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleMemcacheInstanceInvalidMemcacheVersionRule) Severity() string { +func (r *GoogleMemcacheInstanceInvalidMemcacheVersionRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleMemcacheInstanceInvalidMemcacheVersionRule) Link() string { // Check checks the pattern is valid func (r *GoogleMemcacheInstanceInvalidMemcacheVersionRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"MEMCACHE_1_5", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_monitoring_alert_policy_invalid_combiner.go b/rules/magicmodules/google_monitoring_alert_policy_invalid_combiner.go index deab268..83e50bb 100644 --- a/rules/magicmodules/google_monitoring_alert_policy_invalid_combiner.go +++ b/rules/magicmodules/google_monitoring_alert_policy_invalid_combiner.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleMonitoringAlertPolicyInvalidCombinerRule checks the pattern is valid type GoogleMonitoringAlertPolicyInvalidCombinerRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleMonitoringAlertPolicyInvalidCombinerRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleMonitoringAlertPolicyInvalidCombinerRule) Severity() string { +func (r *GoogleMonitoringAlertPolicyInvalidCombinerRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleMonitoringAlertPolicyInvalidCombinerRule) Link() string { // Check checks the pattern is valid func (r *GoogleMonitoringAlertPolicyInvalidCombinerRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"AND", "OR", "AND_WITH_MATCHING_RESOURCE"}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_monitoring_custom_service_invalid_service_id.go b/rules/magicmodules/google_monitoring_custom_service_invalid_service_id.go index a6a5594..7f119ad 100644 --- a/rules/magicmodules/google_monitoring_custom_service_invalid_service_id.go +++ b/rules/magicmodules/google_monitoring_custom_service_invalid_service_id.go @@ -15,12 +15,14 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleMonitoringCustomServiceInvalidServiceIdRule checks the pattern is valid type GoogleMonitoringCustomServiceInvalidServiceIdRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -44,7 +46,7 @@ func (r *GoogleMonitoringCustomServiceInvalidServiceIdRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleMonitoringCustomServiceInvalidServiceIdRule) Severity() string { +func (r *GoogleMonitoringCustomServiceInvalidServiceIdRule) Severity() tflint.Severity { return tflint.ERROR } @@ -55,18 +57,35 @@ func (r *GoogleMonitoringCustomServiceInvalidServiceIdRule) Link() string { // Check checks the pattern is valid func (r *GoogleMonitoringCustomServiceInvalidServiceIdRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validateRegexp(`^[a-z0-9\-]+$`) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_monitoring_metric_descriptor_invalid_launch_stage.go b/rules/magicmodules/google_monitoring_metric_descriptor_invalid_launch_stage.go index 79c1964..bfd488e 100644 --- a/rules/magicmodules/google_monitoring_metric_descriptor_invalid_launch_stage.go +++ b/rules/magicmodules/google_monitoring_metric_descriptor_invalid_launch_stage.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleMonitoringMetricDescriptorInvalidLaunchStageRule checks the pattern is valid type GoogleMonitoringMetricDescriptorInvalidLaunchStageRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleMonitoringMetricDescriptorInvalidLaunchStageRule) Enabled() bool } // Severity returns the rule severity -func (r *GoogleMonitoringMetricDescriptorInvalidLaunchStageRule) Severity() string { +func (r *GoogleMonitoringMetricDescriptorInvalidLaunchStageRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleMonitoringMetricDescriptorInvalidLaunchStageRule) Link() string { // Check checks the pattern is valid func (r *GoogleMonitoringMetricDescriptorInvalidLaunchStageRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"LAUNCH_STAGE_UNSPECIFIED", "UNIMPLEMENTED", "PRELAUNCH", "EARLY_ACCESS", "ALPHA", "BETA", "GA", "DEPRECATED", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_monitoring_metric_descriptor_invalid_metric_kind.go b/rules/magicmodules/google_monitoring_metric_descriptor_invalid_metric_kind.go index bfb9dab..191f806 100644 --- a/rules/magicmodules/google_monitoring_metric_descriptor_invalid_metric_kind.go +++ b/rules/magicmodules/google_monitoring_metric_descriptor_invalid_metric_kind.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleMonitoringMetricDescriptorInvalidMetricKindRule checks the pattern is valid type GoogleMonitoringMetricDescriptorInvalidMetricKindRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleMonitoringMetricDescriptorInvalidMetricKindRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleMonitoringMetricDescriptorInvalidMetricKindRule) Severity() string { +func (r *GoogleMonitoringMetricDescriptorInvalidMetricKindRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleMonitoringMetricDescriptorInvalidMetricKindRule) Link() string { // Check checks the pattern is valid func (r *GoogleMonitoringMetricDescriptorInvalidMetricKindRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"METRIC_KIND_UNSPECIFIED", "GAUGE", "DELTA", "CUMULATIVE"}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_monitoring_metric_descriptor_invalid_value_type.go b/rules/magicmodules/google_monitoring_metric_descriptor_invalid_value_type.go index 38c88fd..b74e0fb 100644 --- a/rules/magicmodules/google_monitoring_metric_descriptor_invalid_value_type.go +++ b/rules/magicmodules/google_monitoring_metric_descriptor_invalid_value_type.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleMonitoringMetricDescriptorInvalidValueTypeRule checks the pattern is valid type GoogleMonitoringMetricDescriptorInvalidValueTypeRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleMonitoringMetricDescriptorInvalidValueTypeRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleMonitoringMetricDescriptorInvalidValueTypeRule) Severity() string { +func (r *GoogleMonitoringMetricDescriptorInvalidValueTypeRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleMonitoringMetricDescriptorInvalidValueTypeRule) Link() string { // Check checks the pattern is valid func (r *GoogleMonitoringMetricDescriptorInvalidValueTypeRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"BOOL", "INT64", "DOUBLE", "STRING", "DISTRIBUTION"}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_monitoring_slo_invalid_calendar_period.go b/rules/magicmodules/google_monitoring_slo_invalid_calendar_period.go index d468ead..75eb8a0 100644 --- a/rules/magicmodules/google_monitoring_slo_invalid_calendar_period.go +++ b/rules/magicmodules/google_monitoring_slo_invalid_calendar_period.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleMonitoringSloInvalidCalendarPeriodRule checks the pattern is valid type GoogleMonitoringSloInvalidCalendarPeriodRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleMonitoringSloInvalidCalendarPeriodRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleMonitoringSloInvalidCalendarPeriodRule) Severity() string { +func (r *GoogleMonitoringSloInvalidCalendarPeriodRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleMonitoringSloInvalidCalendarPeriodRule) Link() string { // Check checks the pattern is valid func (r *GoogleMonitoringSloInvalidCalendarPeriodRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"DAY", "WEEK", "FORTNIGHT", "MONTH", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_monitoring_slo_invalid_slo_id.go b/rules/magicmodules/google_monitoring_slo_invalid_slo_id.go index fa663bf..70c9d9b 100644 --- a/rules/magicmodules/google_monitoring_slo_invalid_slo_id.go +++ b/rules/magicmodules/google_monitoring_slo_invalid_slo_id.go @@ -15,12 +15,14 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleMonitoringSloInvalidSloIdRule checks the pattern is valid type GoogleMonitoringSloInvalidSloIdRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -44,7 +46,7 @@ func (r *GoogleMonitoringSloInvalidSloIdRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleMonitoringSloInvalidSloIdRule) Severity() string { +func (r *GoogleMonitoringSloInvalidSloIdRule) Severity() tflint.Severity { return tflint.ERROR } @@ -55,18 +57,35 @@ func (r *GoogleMonitoringSloInvalidSloIdRule) Link() string { // Check checks the pattern is valid func (r *GoogleMonitoringSloInvalidSloIdRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validateRegexp(`^[a-z0-9\-]+$`) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_network_services_edge_cache_origin_invalid_protocol.go b/rules/magicmodules/google_network_services_edge_cache_origin_invalid_protocol.go index 2e16649..246961a 100644 --- a/rules/magicmodules/google_network_services_edge_cache_origin_invalid_protocol.go +++ b/rules/magicmodules/google_network_services_edge_cache_origin_invalid_protocol.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleNetworkServicesEdgeCacheOriginInvalidProtocolRule checks the pattern is valid type GoogleNetworkServicesEdgeCacheOriginInvalidProtocolRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleNetworkServicesEdgeCacheOriginInvalidProtocolRule) Enabled() bool } // Severity returns the rule severity -func (r *GoogleNetworkServicesEdgeCacheOriginInvalidProtocolRule) Severity() string { +func (r *GoogleNetworkServicesEdgeCacheOriginInvalidProtocolRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleNetworkServicesEdgeCacheOriginInvalidProtocolRule) Link() string // Check checks the pattern is valid func (r *GoogleNetworkServicesEdgeCacheOriginInvalidProtocolRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"HTTP2", "HTTPS", "HTTP", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_notebooks_instance_invalid_boot_disk_type.go b/rules/magicmodules/google_notebooks_instance_invalid_boot_disk_type.go index 0b6f3b3..c7cb7f1 100644 --- a/rules/magicmodules/google_notebooks_instance_invalid_boot_disk_type.go +++ b/rules/magicmodules/google_notebooks_instance_invalid_boot_disk_type.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleNotebooksInstanceInvalidBootDiskTypeRule checks the pattern is valid type GoogleNotebooksInstanceInvalidBootDiskTypeRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleNotebooksInstanceInvalidBootDiskTypeRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleNotebooksInstanceInvalidBootDiskTypeRule) Severity() string { +func (r *GoogleNotebooksInstanceInvalidBootDiskTypeRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleNotebooksInstanceInvalidBootDiskTypeRule) Link() string { // Check checks the pattern is valid func (r *GoogleNotebooksInstanceInvalidBootDiskTypeRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"DISK_TYPE_UNSPECIFIED", "PD_STANDARD", "PD_SSD", "PD_BALANCED", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_notebooks_instance_invalid_data_disk_type.go b/rules/magicmodules/google_notebooks_instance_invalid_data_disk_type.go index 3efdeda..e87d7ed 100644 --- a/rules/magicmodules/google_notebooks_instance_invalid_data_disk_type.go +++ b/rules/magicmodules/google_notebooks_instance_invalid_data_disk_type.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleNotebooksInstanceInvalidDataDiskTypeRule checks the pattern is valid type GoogleNotebooksInstanceInvalidDataDiskTypeRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleNotebooksInstanceInvalidDataDiskTypeRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleNotebooksInstanceInvalidDataDiskTypeRule) Severity() string { +func (r *GoogleNotebooksInstanceInvalidDataDiskTypeRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleNotebooksInstanceInvalidDataDiskTypeRule) Link() string { // Check checks the pattern is valid func (r *GoogleNotebooksInstanceInvalidDataDiskTypeRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"DISK_TYPE_UNSPECIFIED", "PD_STANDARD", "PD_SSD", "PD_BALANCED", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_notebooks_instance_invalid_disk_encryption.go b/rules/magicmodules/google_notebooks_instance_invalid_disk_encryption.go index f67485b..be87fe6 100644 --- a/rules/magicmodules/google_notebooks_instance_invalid_disk_encryption.go +++ b/rules/magicmodules/google_notebooks_instance_invalid_disk_encryption.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleNotebooksInstanceInvalidDiskEncryptionRule checks the pattern is valid type GoogleNotebooksInstanceInvalidDiskEncryptionRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleNotebooksInstanceInvalidDiskEncryptionRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleNotebooksInstanceInvalidDiskEncryptionRule) Severity() string { +func (r *GoogleNotebooksInstanceInvalidDiskEncryptionRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleNotebooksInstanceInvalidDiskEncryptionRule) Link() string { // Check checks the pattern is valid func (r *GoogleNotebooksInstanceInvalidDiskEncryptionRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"DISK_ENCRYPTION_UNSPECIFIED", "GMEK", "CMEK", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_os_config_patch_deployment_invalid_patch_deployment_id.go b/rules/magicmodules/google_os_config_patch_deployment_invalid_patch_deployment_id.go index 07071f9..211da6f 100644 --- a/rules/magicmodules/google_os_config_patch_deployment_invalid_patch_deployment_id.go +++ b/rules/magicmodules/google_os_config_patch_deployment_invalid_patch_deployment_id.go @@ -15,12 +15,14 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleOsConfigPatchDeploymentInvalidPatchDeploymentIdRule checks the pattern is valid type GoogleOsConfigPatchDeploymentInvalidPatchDeploymentIdRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -44,7 +46,7 @@ func (r *GoogleOsConfigPatchDeploymentInvalidPatchDeploymentIdRule) Enabled() bo } // Severity returns the rule severity -func (r *GoogleOsConfigPatchDeploymentInvalidPatchDeploymentIdRule) Severity() string { +func (r *GoogleOsConfigPatchDeploymentInvalidPatchDeploymentIdRule) Severity() tflint.Severity { return tflint.ERROR } @@ -55,18 +57,35 @@ func (r *GoogleOsConfigPatchDeploymentInvalidPatchDeploymentIdRule) Link() strin // Check checks the pattern is valid func (r *GoogleOsConfigPatchDeploymentInvalidPatchDeploymentIdRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validateRegexp(`(?:(?:[-a-z0-9]{1,63}\.)*(?:[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?):)?(?:[0-9]{1,19}|(?:[a-z0-9](?:[-a-z0-9]{0,61}[a-z0-9])?))`) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_privateca_ca_pool_invalid_tier.go b/rules/magicmodules/google_privateca_ca_pool_invalid_tier.go index d46436f..436e2da 100644 --- a/rules/magicmodules/google_privateca_ca_pool_invalid_tier.go +++ b/rules/magicmodules/google_privateca_ca_pool_invalid_tier.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GooglePrivatecaCaPoolInvalidTierRule checks the pattern is valid type GooglePrivatecaCaPoolInvalidTierRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GooglePrivatecaCaPoolInvalidTierRule) Enabled() bool { } // Severity returns the rule severity -func (r *GooglePrivatecaCaPoolInvalidTierRule) Severity() string { +func (r *GooglePrivatecaCaPoolInvalidTierRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GooglePrivatecaCaPoolInvalidTierRule) Link() string { // Check checks the pattern is valid func (r *GooglePrivatecaCaPoolInvalidTierRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"ENTERPRISE", "DEVOPS"}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_privateca_certificate_authority_invalid_type.go b/rules/magicmodules/google_privateca_certificate_authority_invalid_type.go index 8f59c79..606817d 100644 --- a/rules/magicmodules/google_privateca_certificate_authority_invalid_type.go +++ b/rules/magicmodules/google_privateca_certificate_authority_invalid_type.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GooglePrivatecaCertificateAuthorityInvalidTypeRule checks the pattern is valid type GooglePrivatecaCertificateAuthorityInvalidTypeRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GooglePrivatecaCertificateAuthorityInvalidTypeRule) Enabled() bool { } // Severity returns the rule severity -func (r *GooglePrivatecaCertificateAuthorityInvalidTypeRule) Severity() string { +func (r *GooglePrivatecaCertificateAuthorityInvalidTypeRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GooglePrivatecaCertificateAuthorityInvalidTypeRule) Link() string { // Check checks the pattern is valid func (r *GooglePrivatecaCertificateAuthorityInvalidTypeRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"SELF_SIGNED", "SUBORDINATE", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_pubsub_schema_invalid_type.go b/rules/magicmodules/google_pubsub_schema_invalid_type.go index 715df25..b9c6c8d 100644 --- a/rules/magicmodules/google_pubsub_schema_invalid_type.go +++ b/rules/magicmodules/google_pubsub_schema_invalid_type.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GooglePubsubSchemaInvalidTypeRule checks the pattern is valid type GooglePubsubSchemaInvalidTypeRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GooglePubsubSchemaInvalidTypeRule) Enabled() bool { } // Severity returns the rule severity -func (r *GooglePubsubSchemaInvalidTypeRule) Severity() string { +func (r *GooglePubsubSchemaInvalidTypeRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GooglePubsubSchemaInvalidTypeRule) Link() string { // Check checks the pattern is valid func (r *GooglePubsubSchemaInvalidTypeRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"TYPE_UNSPECIFIED", "PROTOCOL_BUFFER", "AVRO", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_redis_instance_invalid_connect_mode.go b/rules/magicmodules/google_redis_instance_invalid_connect_mode.go index 146391c..e2398f8 100644 --- a/rules/magicmodules/google_redis_instance_invalid_connect_mode.go +++ b/rules/magicmodules/google_redis_instance_invalid_connect_mode.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleRedisInstanceInvalidConnectModeRule checks the pattern is valid type GoogleRedisInstanceInvalidConnectModeRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleRedisInstanceInvalidConnectModeRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleRedisInstanceInvalidConnectModeRule) Severity() string { +func (r *GoogleRedisInstanceInvalidConnectModeRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleRedisInstanceInvalidConnectModeRule) Link() string { // Check checks the pattern is valid func (r *GoogleRedisInstanceInvalidConnectModeRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"DIRECT_PEERING", "PRIVATE_SERVICE_ACCESS", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_redis_instance_invalid_name.go b/rules/magicmodules/google_redis_instance_invalid_name.go index 1974937..ab6fdbb 100644 --- a/rules/magicmodules/google_redis_instance_invalid_name.go +++ b/rules/magicmodules/google_redis_instance_invalid_name.go @@ -15,12 +15,14 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleRedisInstanceInvalidNameRule checks the pattern is valid type GoogleRedisInstanceInvalidNameRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -44,7 +46,7 @@ func (r *GoogleRedisInstanceInvalidNameRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleRedisInstanceInvalidNameRule) Severity() string { +func (r *GoogleRedisInstanceInvalidNameRule) Severity() tflint.Severity { return tflint.ERROR } @@ -55,18 +57,35 @@ func (r *GoogleRedisInstanceInvalidNameRule) Link() string { // Check checks the pattern is valid func (r *GoogleRedisInstanceInvalidNameRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validateRegexp(`^[a-z][a-z0-9-]{0,39}[a-z0-9]$`) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_redis_instance_invalid_tier.go b/rules/magicmodules/google_redis_instance_invalid_tier.go index 4c0ffae..1f25c18 100644 --- a/rules/magicmodules/google_redis_instance_invalid_tier.go +++ b/rules/magicmodules/google_redis_instance_invalid_tier.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleRedisInstanceInvalidTierRule checks the pattern is valid type GoogleRedisInstanceInvalidTierRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleRedisInstanceInvalidTierRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleRedisInstanceInvalidTierRule) Severity() string { +func (r *GoogleRedisInstanceInvalidTierRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleRedisInstanceInvalidTierRule) Link() string { // Check checks the pattern is valid func (r *GoogleRedisInstanceInvalidTierRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"BASIC", "STANDARD_HA", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_redis_instance_invalid_transit_encryption_mode.go b/rules/magicmodules/google_redis_instance_invalid_transit_encryption_mode.go index 5835d4e..8d37693 100644 --- a/rules/magicmodules/google_redis_instance_invalid_transit_encryption_mode.go +++ b/rules/magicmodules/google_redis_instance_invalid_transit_encryption_mode.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleRedisInstanceInvalidTransitEncryptionModeRule checks the pattern is valid type GoogleRedisInstanceInvalidTransitEncryptionModeRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleRedisInstanceInvalidTransitEncryptionModeRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleRedisInstanceInvalidTransitEncryptionModeRule) Severity() string { +func (r *GoogleRedisInstanceInvalidTransitEncryptionModeRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleRedisInstanceInvalidTransitEncryptionModeRule) Link() string { // Check checks the pattern is valid func (r *GoogleRedisInstanceInvalidTransitEncryptionModeRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"SERVER_AUTHENTICATION", "DISABLED", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_scc_source_invalid_display_name.go b/rules/magicmodules/google_scc_source_invalid_display_name.go index fcfdb58..0c20ca0 100644 --- a/rules/magicmodules/google_scc_source_invalid_display_name.go +++ b/rules/magicmodules/google_scc_source_invalid_display_name.go @@ -15,12 +15,14 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleSccSourceInvalidDisplayNameRule checks the pattern is valid type GoogleSccSourceInvalidDisplayNameRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -44,7 +46,7 @@ func (r *GoogleSccSourceInvalidDisplayNameRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleSccSourceInvalidDisplayNameRule) Severity() string { +func (r *GoogleSccSourceInvalidDisplayNameRule) Severity() tflint.Severity { return tflint.ERROR } @@ -55,18 +57,35 @@ func (r *GoogleSccSourceInvalidDisplayNameRule) Link() string { // Check checks the pattern is valid func (r *GoogleSccSourceInvalidDisplayNameRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validateRegexp(`[\p{L}\p{N}]({\p{L}\p{N}_- ]{0,30}[\p{L}\p{N}])?`) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_spanner_database_invalid_name.go b/rules/magicmodules/google_spanner_database_invalid_name.go index 5b98a82..782d7f1 100644 --- a/rules/magicmodules/google_spanner_database_invalid_name.go +++ b/rules/magicmodules/google_spanner_database_invalid_name.go @@ -15,12 +15,14 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleSpannerDatabaseInvalidNameRule checks the pattern is valid type GoogleSpannerDatabaseInvalidNameRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -44,7 +46,7 @@ func (r *GoogleSpannerDatabaseInvalidNameRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleSpannerDatabaseInvalidNameRule) Severity() string { +func (r *GoogleSpannerDatabaseInvalidNameRule) Severity() tflint.Severity { return tflint.ERROR } @@ -55,18 +57,35 @@ func (r *GoogleSpannerDatabaseInvalidNameRule) Link() string { // Check checks the pattern is valid func (r *GoogleSpannerDatabaseInvalidNameRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validateRegexp(`^[a-z][a-z0-9_\-]*[a-z0-9]$`) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_spanner_instance_invalid_name.go b/rules/magicmodules/google_spanner_instance_invalid_name.go index e8d9ef7..5dc26f5 100644 --- a/rules/magicmodules/google_spanner_instance_invalid_name.go +++ b/rules/magicmodules/google_spanner_instance_invalid_name.go @@ -15,12 +15,14 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleSpannerInstanceInvalidNameRule checks the pattern is valid type GoogleSpannerInstanceInvalidNameRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -44,7 +46,7 @@ func (r *GoogleSpannerInstanceInvalidNameRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleSpannerInstanceInvalidNameRule) Severity() string { +func (r *GoogleSpannerInstanceInvalidNameRule) Severity() tflint.Severity { return tflint.ERROR } @@ -55,18 +57,35 @@ func (r *GoogleSpannerInstanceInvalidNameRule) Link() string { // Check checks the pattern is valid func (r *GoogleSpannerInstanceInvalidNameRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validateRegexp(`^[a-z][-a-z0-9]*[a-z0-9]$`) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_sql_source_representation_instance_invalid_database_version.go b/rules/magicmodules/google_sql_source_representation_instance_invalid_database_version.go index 2af2b66..c032c44 100644 --- a/rules/magicmodules/google_sql_source_representation_instance_invalid_database_version.go +++ b/rules/magicmodules/google_sql_source_representation_instance_invalid_database_version.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleSqlSourceRepresentationInstanceInvalidDatabaseVersionRule checks the pattern is valid type GoogleSqlSourceRepresentationInstanceInvalidDatabaseVersionRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleSqlSourceRepresentationInstanceInvalidDatabaseVersionRule) Enable } // Severity returns the rule severity -func (r *GoogleSqlSourceRepresentationInstanceInvalidDatabaseVersionRule) Severity() string { +func (r *GoogleSqlSourceRepresentationInstanceInvalidDatabaseVersionRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleSqlSourceRepresentationInstanceInvalidDatabaseVersionRule) Link() // Check checks the pattern is valid func (r *GoogleSqlSourceRepresentationInstanceInvalidDatabaseVersionRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"MYSQL_5_5", "MYSQL_5_6", "MYSQL_5_7", "MYSQL_8_0"}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_storage_bucket_access_control_invalid_role.go b/rules/magicmodules/google_storage_bucket_access_control_invalid_role.go index 302b758..cf762af 100644 --- a/rules/magicmodules/google_storage_bucket_access_control_invalid_role.go +++ b/rules/magicmodules/google_storage_bucket_access_control_invalid_role.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleStorageBucketAccessControlInvalidRoleRule checks the pattern is valid type GoogleStorageBucketAccessControlInvalidRoleRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleStorageBucketAccessControlInvalidRoleRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleStorageBucketAccessControlInvalidRoleRule) Severity() string { +func (r *GoogleStorageBucketAccessControlInvalidRoleRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleStorageBucketAccessControlInvalidRoleRule) Link() string { // Check checks the pattern is valid func (r *GoogleStorageBucketAccessControlInvalidRoleRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"OWNER", "READER", "WRITER", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_storage_default_object_access_control_invalid_role.go b/rules/magicmodules/google_storage_default_object_access_control_invalid_role.go index e0e26b9..928f8d0 100644 --- a/rules/magicmodules/google_storage_default_object_access_control_invalid_role.go +++ b/rules/magicmodules/google_storage_default_object_access_control_invalid_role.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleStorageDefaultObjectAccessControlInvalidRoleRule checks the pattern is valid type GoogleStorageDefaultObjectAccessControlInvalidRoleRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleStorageDefaultObjectAccessControlInvalidRoleRule) Enabled() bool } // Severity returns the rule severity -func (r *GoogleStorageDefaultObjectAccessControlInvalidRoleRule) Severity() string { +func (r *GoogleStorageDefaultObjectAccessControlInvalidRoleRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleStorageDefaultObjectAccessControlInvalidRoleRule) Link() string { // Check checks the pattern is valid func (r *GoogleStorageDefaultObjectAccessControlInvalidRoleRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"OWNER", "READER"}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_storage_hmac_key_invalid_state.go b/rules/magicmodules/google_storage_hmac_key_invalid_state.go index bf351a4..62e9c3d 100644 --- a/rules/magicmodules/google_storage_hmac_key_invalid_state.go +++ b/rules/magicmodules/google_storage_hmac_key_invalid_state.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleStorageHmacKeyInvalidStateRule checks the pattern is valid type GoogleStorageHmacKeyInvalidStateRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleStorageHmacKeyInvalidStateRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleStorageHmacKeyInvalidStateRule) Severity() string { +func (r *GoogleStorageHmacKeyInvalidStateRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleStorageHmacKeyInvalidStateRule) Link() string { // Check checks the pattern is valid func (r *GoogleStorageHmacKeyInvalidStateRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"ACTIVE", "INACTIVE", ""}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/magicmodules/google_storage_object_access_control_invalid_role.go b/rules/magicmodules/google_storage_object_access_control_invalid_role.go index 3154237..00044cb 100644 --- a/rules/magicmodules/google_storage_object_access_control_invalid_role.go +++ b/rules/magicmodules/google_storage_object_access_control_invalid_role.go @@ -15,13 +15,15 @@ package magicmodules import ( - hcl "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-linters/tflint-plugin-sdk/hclext" "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // GoogleStorageObjectAccessControlInvalidRoleRule checks the pattern is valid type GoogleStorageObjectAccessControlInvalidRoleRule struct { + tflint.DefaultRule + resourceType string attributeName string } @@ -45,7 +47,7 @@ func (r *GoogleStorageObjectAccessControlInvalidRoleRule) Enabled() bool { } // Severity returns the rule severity -func (r *GoogleStorageObjectAccessControlInvalidRoleRule) Severity() string { +func (r *GoogleStorageObjectAccessControlInvalidRoleRule) Severity() tflint.Severity { return tflint.ERROR } @@ -56,18 +58,35 @@ func (r *GoogleStorageObjectAccessControlInvalidRoleRule) Link() string { // Check checks the pattern is valid func (r *GoogleStorageObjectAccessControlInvalidRoleRule) Check(runner tflint.Runner) error { - return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error { + resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{ + Attributes: []hclext.AttributeSchema{{Name: r.attributeName}}, + }, nil) + if err != nil { + return err + } + + for _, resource := range resources.Blocks { + attribute, exists := resource.Body.Attributes[r.attributeName] + if !exists { + continue + } + var val string err := runner.EvaluateExpr(attribute.Expr, &val, nil) validateFunc := validation.StringInSlice([]string{"OWNER", "READER"}, false) - return runner.EnsureNoError(err, func() error { + err = runner.EnsureNoError(err, func() error { _, errors := validateFunc(val, r.attributeName) for _, err := range errors { - runner.EmitIssueOnExpr(r, err.Error(), attribute.Expr) + runner.EmitIssue(r, err.Error(), attribute.Expr.Range()) } return nil }) - }) + if err != nil { + return err + } + } + + return nil } diff --git a/rules/provider.go b/rules/provider.go index b8b5517..fd06dc1 100644 --- a/rules/provider.go +++ b/rules/provider.go @@ -2,21 +2,34 @@ package rules import ( "github.com/terraform-linters/tflint-plugin-sdk/tflint" + "github.com/terraform-linters/tflint-ruleset-google/rules/api" "github.com/terraform-linters/tflint-ruleset-google/rules/magicmodules" ) +var rules = [][]tflint.Rule{ + { + NewGoogleComposerEnvironmentInvalidMachineTypeRule(), + NewGoogleComputeInstanceInvalidMachineTypeRule(), + NewGoogleComputeReservationInvalidMachineTypeRule(), + NewGoogleComputeInstanceTemplateInvalidMachineTypeRule(), + NewGoogleContainerClusterInvalidMachineTypeRule(), + NewGoogleContainerNodePoolInvalidMachineTypeRule(), + NewGoogleDataflowJobInvalidMachineTypeRule(), + NewGoogleComputeResourcePolicyInvalidNameRule(), + NewGoogleProjectIamMemberInvalidMemberRule(), + NewGoogleProjectIamAuditConfigInvalidMemberRule(), + NewGoogleProjectIamBindingInvalidMemberRule(), + NewGoogleProjectIamPolicyInvalidMemberRule(), + }, + magicmodules.Rules, + api.Rules, +} + // Rules is a list of all rules -var Rules = append([]tflint.Rule{ - NewGoogleComposerEnvironmentInvalidMachineTypeRule(), - NewGoogleComputeInstanceInvalidMachineTypeRule(), - NewGoogleComputeReservationInvalidMachineTypeRule(), - NewGoogleComputeInstanceTemplateInvalidMachineTypeRule(), - NewGoogleContainerClusterInvalidMachineTypeRule(), - NewGoogleContainerNodePoolInvalidMachineTypeRule(), - NewGoogleDataflowJobInvalidMachineTypeRule(), - NewGoogleComputeResourcePolicyInvalidNameRule(), - NewGoogleProjectIamMemberInvalidMemberRule(), - NewGoogleProjectIamAuditConfigInvalidMemberRule(), - NewGoogleProjectIamBindingInvalidMemberRule(), - NewGoogleProjectIamPolicyInvalidMemberRule(), -}, magicmodules.Rules...) +var Rules []tflint.Rule + +func init() { + for _, r := range rules { + Rules = append(Rules, r...) + } +}