Skip to content

Instantly share code, notes, and snippets.

@dacort
Created October 31, 2022 18:36
Show Gist options
  • Save dacort/0784505bf11580c50bc640ec655a528d to your computer and use it in GitHub Desktop.
Save dacort/0784505bf11580c50bc640ec655a528d to your computer and use it in GitHub Desktop.
Terraform base36 external data source
import json
import sys
BS = "0123456789abcdefghijklmnopqrstuvwxyz"
def parse_input():
input = sys.stdin.read()
return json.loads(input)
def to_base(n, b):
return "0" if not n else to_base(n//b, b).lstrip("0") + BS[n%b]
def b36_encode(s):
"""
Converts string to base16 integer, then to base36 string
"""
n = int("".join([hex(ord(c))[2:] for c in s]), base=16)
return to_base(n, 36)
if __name__ == "__main__":
input = parse_input()
result = b36_encode(input.get('input'))
print(json.dumps(
{"val": result}
))
data "external" "base36" {
program = ["python3", "base36.py"]
query = {
input = "damon"
}
}
output "base36_val" {
value = data.external.base36.result.val
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment