Last active
April 27, 2025 04:12
-
-
Save manasmbellani/de10285b45491988434b103e9aebcf1f to your computer and use it in GitHub Desktop.
terraform-universe-example: An example of using terraform universe provider which is located here: https://github.com/aellwein/terraform-provider-universe
This file contains hidden or 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
| # terraform init | |
| # TERRAFORM_UNIVERSE_RESOURCETYPES='json_file' terraform plan | |
| terraform { | |
| required_providers { | |
| universe = { | |
| source = "aellwein/universe" | |
| version = "0.1.1" | |
| } | |
| } | |
| } | |
| provider "universe" { | |
| executor = "python3" | |
| script = "json_file.py" | |
| id_key = "filename" | |
| } | |
| resource "universe_json_file" "h" { | |
| provider = universe | |
| config = jsonencode({ | |
| "name": "Don't Step On My Blue Suede Shoes", | |
| "created-by" : "Elvis Presley2", | |
| "where" : "Gracelands2" | |
| "hit" : "Gold2" | |
| "@created": null | |
| }) | |
| } | |
| resource "universe_json_file" "hp" { | |
| provider = universe | |
| config = jsonencode({ | |
| "name": "Another strange resource", | |
| "main-character" : "Harry Potter", | |
| "nemesis" : "Tom Riddle", | |
| "likes" : [ | |
| "Ginny Weasley", | |
| "Ron Weasley" | |
| ], | |
| "@created": 23 | |
| }) | |
| } | |
| resource "universe_json_file" "i" { | |
| provider = universe | |
| executor = "python3" | |
| script = "json_file.py" | |
| id_key = "filename" | |
| config = jsonencode({ | |
| "name": "Fake strange resource" | |
| }) | |
| } | |
| output "hp_name" { | |
| value = jsondecode(universe_json_file.hp.config)["name"] | |
| } | |
| output "hp_created" { | |
| value = jsondecode(universe_json_file.hp.config)["@created"] | |
| } | |
This file contains hidden or 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 os | |
| import sys | |
| import json | |
| import tempfile | |
| from datetime import datetime | |
| if __name__ == '__main__': | |
| result = None | |
| event = sys.argv[1] # create, read, update or delete, maybe exists too | |
| id = os.environ.get("filename") # Get the id if present else None | |
| script = os.environ.get("script") | |
| if event == "exists": | |
| # ignore stdin | |
| # Is file there? | |
| if id is None: | |
| result = False | |
| else: | |
| result = os.path.isfile(id) | |
| print('true' if result else 'false') | |
| exit(0) | |
| elif event == "delete": | |
| # Delete the file | |
| os.remove(id) | |
| exit(0) | |
| # Read the JSON from standard input | |
| input = sys.stdin.read() | |
| input_dict = json.loads(input) | |
| if event == "create": | |
| # Create a unique file /tmp/json-file.pyXXXX and write the data to it | |
| ff = tempfile.NamedTemporaryFile(mode='w+', prefix=script, delete=False) | |
| input_dict["@created"] = datetime.now().strftime("%d/%m/%Y %H:%M:%S") | |
| ff.write(json.dumps(input_dict)) | |
| ff.close() | |
| input_dict.update({"filename": ff.name}) # Give the ID back to Terraform - it's the filename | |
| result = input_dict | |
| elif event == "read": | |
| # Open the file given by the id and return the data | |
| fr = open(id, mode='r+') | |
| data = fr.read() | |
| fr.close() | |
| if len(data) > 0: | |
| result = json.loads(data) | |
| else: | |
| result = {} | |
| elif event == "update": | |
| # First get the creation date if we can - need to save it again | |
| fr = open(id, mode='r+') | |
| old_data = fr.read() | |
| fr.close() | |
| if len(old_data) > 0: | |
| oldinfo = json.loads(old_data) | |
| input_dict['@created'] = oldinfo.get('@created', "unknown") | |
| # write the data out to the file given by the Id | |
| fu = open(id, mode='w+') | |
| fu.write(json.dumps(input_dict)) | |
| fu.close() | |
| result = input_dict | |
| print(json.dumps(result)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment