Created
October 31, 2022 18:36
-
-
Save dacort/0784505bf11580c50bc640ec655a528d to your computer and use it in GitHub Desktop.
Terraform base36 external data source
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | |
)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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