From 55f6b897135140100f5c2c557110ba430d543e22 Mon Sep 17 00:00:00 2001 From: Sandhya1874 <39799075+Sandhya1874@users.noreply.github.com> Date: Thu, 22 Aug 2024 17:28:10 +0100 Subject: [PATCH] Updated terraform discovery code for pingdom (#9) * Updated terraform discovery code for pingdom * Updated for pingdom * Working version with no pingdom.tf check * Added check to verify pingdom resource exists in namespace or not * Updated to comment json import and print statements * Update terraform_discovery.py Addressed Matt's comment * Update terraform_discovery.py Corrected SC ID variable for elasticache and pingdom * Updated lambda variables for elasticache and pingdom * Update terraform_discovery.py Corrected variable name for elasticache --- terraform_discovery.py | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) mode change 100644 => 100755 terraform_discovery.py diff --git a/terraform_discovery.py b/terraform_discovery.py old mode 100644 new mode 100755 index e8d98e9..49ac26b --- a/terraform_discovery.py +++ b/terraform_discovery.py @@ -151,14 +151,43 @@ def process_repo(**component): # If there are any rds instances in the existing SC data if sc_data[0]["attributes"]["elasticache_cluster"]: # Find the elasticache cluster SC ID that matches - rds_id = list(filter(lambda rds: rds['tf_path'] == elasticache_cluster['__tfmeta']['path'], sc_data[0]["attributes"]["elasticache_cluster"]))[0]['id'] - elasticache_cluster.update({'id': rds_id}) + elasticache_id = list(filter(lambda elasticache: elasticache['tf_path'] == elasticache_cluster['__tfmeta']['path'], sc_data[0]["attributes"]["elasticache_cluster"]))[0]['id'] + elasticache_cluster.update({'id': elasticache_id}) except (IndexError,KeyError): pass # Clean up field not used in post to SC del elasticache_cluster['__tfmeta'] data.update({"elasticache_cluster": [elasticache_cluster]}) + + if 'pingdom_check' in parsed.keys() : + for r in parsed['pingdom_check']: + # Look for RDS instances. + if "http" in r['type']: + pingdom_check = r + # Delete ID that is generated by tfparse + del pingdom_check['id'] + # Process fields + pingdom_check.update({'tf_label': pingdom_check['__tfmeta']['label']}) + pingdom_check.update({'tf_filename': pingdom_check['__tfmeta']['filename']}) + pingdom_check.update({'tf_path': pingdom_check['__tfmeta']['path']}) + pingdom_check.update({'tf_line_end': pingdom_check['__tfmeta']['line_end']}) + pingdom_check.update({'tf_line_start': pingdom_check['__tfmeta']['line_start']}) + # pingdom_check.update({'tf_mod_version': tf_mod_version}) + # Check for existing instance in SC and update same ID if so. + try: + # If there are any rds instances in the existing SC data + if sc_data[0]["attributes"]["pingdom_check"]: + # Find the Pingdom check SC ID that matches + pingdom_id = list(filter(lambda pingdom: pingdom['tf_path'] == pingdom_check['__tfmeta']['path'], sc_data[0]["attributes"]["pingdom_check"]))[0]['id'] + pingdom_check.update({'id': pingdom_id}) + except IndexError: + pass + + # Clean up field not used in post to SC + del pingdom_check['__tfmeta'] + data.update({"pingdom_check": [pingdom_check]}) + print(data) update_sc_namespace(namespace_id, data)