Skip to content

Commit

Permalink
datastructure refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
aerickson committed Jul 29, 2024
1 parent e623c68 commit aca657c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
6 changes: 6 additions & 0 deletions test_files/non-comment/test.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# terraform_authoritative_scanner_ok
resource "non_authoritative" "google_deployment_accounts_compute_admin_google_project_iam_binding" {
project = "fxci-production-level3-workers"
role = "roles/compute.admin"
member = "serviceAccount:test3333"
}
32 changes: 22 additions & 10 deletions tf_authoritative_scanner/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,16 @@ def check_file_for_authoritative_resources(self, file_path):
if not self.exception_comment_pattern.search(line) and not self.exception_comment_pattern.search(
previous_line
):
authoritative_lines.append((line_number, stripped_line))
authoritative_lines.append({"line_number": line_number, "line": stripped_line})
non_authoritative = False
previous_line = stripped_line

return authoritative_lines, non_authoritative
return {"authoritative_lines": authoritative_lines, "non_authoritative": non_authoritative}

def check_directory_for_authoritative_resources(self):
all_authoritative_lines = []
non_authoritative_files = []
result = {"all_authoritative_lines": [], "non_authoritative_files": []}
total_files = 0
for root, dirs, files in os.walk(self.directory):
if not self.include_dotdirs:
Expand All @@ -79,23 +80,34 @@ def check_directory_for_authoritative_resources(self):
if file.endswith(".tf"):
total_files += 1
file_path = os.path.join(root, file)
authoritative_lines, non_authoritative = self.check_file_for_authoritative_resources(file_path)
result = self.check_file_for_authoritative_resources(file_path)
authoritative_lines = result["authoritative_lines"]
non_authoritative = result["non_authoritative"]
if authoritative_lines:
all_authoritative_lines.append((file_path, authoritative_lines))
all_authoritative_lines.append(
{"file_path": file_path, "authoritative_lines": authoritative_lines}
)
if non_authoritative:
non_authoritative_files.append(file_path)
non_authoritative_files.append({"file_path": file_path})

return all_authoritative_lines, total_files, non_authoritative_files
return {
"all_authoritative_lines": all_authoritative_lines,
"total_files": total_files,
"non_authoritative_files": non_authoritative_files,
}

def run(self):
all_authoritative_lines, total_files, non_authoritative_files = (
self.check_directory_for_authoritative_resources()
)
result = self.check_directory_for_authoritative_resources()
all_authoritative_lines = result["all_authoritative_lines"]
total_files = result["total_files"]
non_authoritative_files = result["non_authoritative_files"]
if self.verbosity:
for file_path in non_authoritative_files:
print(f"OK: {file_path}")
if all_authoritative_lines:
for file_path, lines in all_authoritative_lines:
for item in all_authoritative_lines:
file_path = item["file_path"]
lines = item["authoritative_lines"]
for line_number, line in lines:
print(f"AUTHORITATIVE: {file_path}:{line_number}: {line}")
authoritative_files = len(all_authoritative_lines)
Expand Down
13 changes: 10 additions & 3 deletions tf_authoritative_scanner/scanner_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@ def test_check_file_for_authoritative_resources(temp_tf_file):

def test_check_directory_for_authoritative_resources(temp_tf_dir):
scanner = TFAuthoritativeScanner(temp_tf_dir, include_dotdirs=False)
all_authoritative_lines, total_files, _ = scanner.check_directory_for_authoritative_resources()
result = scanner.check_directory_for_authoritative_resources()
all_authoritative_lines = result.get("all_authoritative_lines")
non_authoritative_files = result.get("non_authoritative_files")
total_files = len(all_authoritative_lines) + len(non_authoritative_files)

assert total_files == 1
assert len(all_authoritative_lines) > 0

Expand Down Expand Up @@ -146,13 +150,16 @@ def test_run_verbose_level_2(temp_non_authoritative_tf_file, capsys):

def test_exception_comment_same_line(temp_tf_file_with_exception_same_line):
scanner = TFAuthoritativeScanner(temp_tf_file_with_exception_same_line, include_dotdirs=False)
authoritative_lines, _ = scanner.check_file_for_authoritative_resources(temp_tf_file_with_exception_same_line)
result = scanner.check_file_for_authoritative_resources(temp_tf_file_with_exception_same_line)
authoritative_lines = result["authoritative_lines"]
assert len(authoritative_lines) == 0


def test_exception_comment_previous_line(temp_tf_file_with_exception_previous_line):
scanner = TFAuthoritativeScanner(temp_tf_file_with_exception_previous_line, include_dotdirs=False)
authoritative_lines, _ = scanner.check_file_for_authoritative_resources(temp_tf_file_with_exception_previous_line)
result = scanner.check_file_for_authoritative_resources(temp_tf_file_with_exception_previous_line)
authoritative_lines = result["authoritative_lines"]
_non_authoritative = result["non_authoritative"]
assert len(authoritative_lines) == 0


Expand Down

0 comments on commit aca657c

Please sign in to comment.