Skip to content

Instantly share code, notes, and snippets.

@antigenius0910
Last active October 15, 2021 12:36
Show Gist options
  • Save antigenius0910/5e00e80cfadf48642acb44132acefb3a to your computer and use it in GitHub Desktop.
Save antigenius0910/5e00e80cfadf48642acb44132acefb3a to your computer and use it in GitHub Desktop.
Capture single resource from terraform config file (*.tf) by heading and balanced pairs of braces
def parse_segments(source, resource_heading, single_or_all):
"""
extract any substring enclosed in parenthesis
source should be a string
"""
unmatched_count = 0
start_pos = 0
opened = False
open_pos = 0
cur_pos = 0
finished = []
segments = []
heading = []
body = []
for character in source:
#scan for mismatched parenthesis:
if character == '{':
unmatched_count += 1
if not opened:
open_pos = cur_pos
opened = True
if character == '}':
unmatched_count -= 1
if opened and unmatched_count == 0:
segment = source[open_pos:cur_pos+1]
segments.append(segment)
# Heading
clean = source[start_pos:open_pos]
if clean:
heading.append(clean)
opened = False
start_pos = cur_pos+1
cur_pos += 1
assert unmatched_count == 0
for item in segments:
# get rid of front and end parentheses:
pruned = item[2:-1]
body.append(pruned)
clean_heading = []
# clean out leading new lines and ending space
for item in heading:
clean_heading.append(item.replace("\n\n", "").strip())
# matching haeding with body into 1:1 relationshop
dct = dict((a, b) for a, b in zip(clean_heading, body))
if single_or_all == "single":
return (match_single(dct, resource_heading))
elif single_or_all == "all":
return (match_all(dct, resource_heading))
def match_single(dct, resource_heading):
for k, v in dct.items():
# do reach on heading for full resource context
if k == resource_heading:
single_resource = k + " {" + "\n" + v + "}"
return single_resource
def match_all(dct, resource_heading):
all_sub_resource = ""
res = [key for key, val in dct.items() if resource_heading in key]
# find all heading which matches sub string
for k, v in dct.items():
if k in res and v != "":
all_sub_resource += k + " {" + "\n" + v + "}" + "\n" + "\n"
# for edge case like "resource "XXXX" "tfer--XXXX" {}"
elif k in res and v == "":
all_sub_resource += k + " {" + "}" + "\n" + "\n"
return all_sub_resource.rstrip('\n')
#"""
# return all match so no trailing double quote
with open("repository_collaborator.tf", 'r') as file:
lines = file.readlines()
file_context = "".join(lines)
# please be aware heading here can't have
resource_heading1 = 'resource "github_repository_collaborator" "tfer--zero_downtime_migrations'
print ("".join(parse_segments(file_context, resource_heading1, "all")))
#"""
#"""
# only return exactly match with heading
with open("repository.tf", 'r') as file:
lines = file.readlines()
file_context = "".join(lines)
resource_heading2 = 'resource "github_repository" "tfer--test-002D-plugin-002D-example"'
print ("".join(parse_segments(file_context, resource_heading2, "single")))
#"""
resource "github_repository" "tfer--test-002D-plugin-002D-everbridge" {
allow_merge_commit = "true"
allow_rebase_merge = "true"
allow_squash_merge = "true"
archived = "false"
default_branch = "master"
delete_branch_on_merge = "false"
has_downloads = "true"
has_issues = "true"
has_projects = "false"
has_wiki = "true"
is_template = "false"
name = "test-plugin-everbridge"
private = "true"
visibility = "private"
vulnerability_alerts = "true"
}
resource "github_repository" "tfer--test-002D-plugin-002D-example" {
allow_merge_commit = "true"
allow_rebase_merge = "true"
allow_squash_merge = "true"
archived = "true"
default_branch = "main"
delete_branch_on_merge = "false"
has_downloads = "true"
has_issues = "true"
has_projects = "false"
has_wiki = "true"
is_template = "false"
name = "test-plugin-example"
private = "true"
template {
owner = "test"
repository = "test-plugin-templattest-plugin-template"
}
visibility = "internal"
vulnerability_alerts = "false"
}
resource "github_repository" "tfer--test-002D-plugin-002D-example-002D-access-002D-control" {
allow_merge_commit = "true"
allow_rebase_merge = "true"
allow_squash_merge = "true"
archived = "false"
default_branch = "main"
delete_branch_on_merge = "false"
description = "This is an example Access Control integration with test's integration builder."
has_downloads = "true"
has_issues = "true"
has_projects = "false"
has_wiki = "true"
is_template = "false"
name = "test-plugin-example-access-control"
private = "false"
visibility = "public"
vulnerability_alerts = "true"
}
resource "github_repository_collaborator" "tfer--infra-002D-staging-002D-terraform-003A-user1" {
permission = "push"
repository = "${github_repository.tfer--infra-002D-staging-002D-terraform.name}"
username = "user1"
}
resource "github_repository_collaborator" "tfer--infra-002D-staging-002D-terraform-003A-user2" {
permission = "push"
repository = "${github_repository.tfer--infra-002D-staging-002D-terraform.name}"
username = "user2"
}
resource "github_repository_collaborator" "tfer--zero_downtime_migrations-003A-user1" {
permission = "push"
repository = "${github_repository.tfer--zero_downtime_migrations.name}"
username = "user1"
}
resource "github_repository_collaborator" "tfer--zero_downtime_migrations-003A-user2" {}
resource "github_repository_collaborator" "tfer--zero_downtime_migrations-003A-user3" {
permission = "push"
repository = "${github_repository.tfer--zero_downtime_migrations.name}"
username = "user3"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment