def _convert_hcl_to_json(self, file_name): """ Method to convert file HCL to JSON format. """ try: with open(file_name, 'rb') as outfile: json_object = hcl.dumps(hcl.load(outfile)) outfile.close() self._create_json_file(json_object, file_name) os.remove(file_name) # Remove .tf file except: self.logger.exception( "Failed to Convert file %s from HCL to JSON format." % file_name)
#print json.dumps(resources, indent=2) #sys.exit(1) for rtype in resources['resource']: for rname in resources['resource'][rtype]: if 'default' in rname: pass elif searchstr in rname: print rname del output['resource'][rtype][rname] elif 'tags' in resources['resource'][rtype][rname]: if 'Project' in resources['resource'][rtype][rname]['tags']: if searchstr in resources['resource'][rtype][rname]['tags']['Project']: print rname del output['resource'][rtype][rname] elif 'Name' in resources['resource'][rtype][rname]['tags']: if searchstr in resources['resource'][rtype][rname]['tags']['Name']: print rname del output['resource'][rtype][rname] # Delete empty resource types outdata = deepcopy(output) for rtype in output['resource']: if not output['resource'][rtype]: del outdata['resource'][rtype] #print json.dumps(outdata, indent=2) outfile = open(outtf, 'w') outfile.write(hcl.dumps(outdata, indent=2, sort_keys=True)) outfile.close() os.rename(cleantf, cleantf + ".sav")
if regionName == s3client.get_bucket_location( Bucket=bucketName)['LocationConstraint']: # Were done print("Bucket: %s already exists in AWS account & region" % bucketName) print(" Enabling versioning on Bucket: %s" % bucketName) s3client.put_bucket_versioning( Bucket=bucketName, VersioningConfiguration={'Status': 'Enabled'}) bucketOk = True if bucketOk == False: # The bucket does not yet exist (in this account) print("Bucket: %s does not exist in this AWS account & region." % bucketName) # Create a bucket. Try to use the bucket Name in the config file first while createBucket(bucketName, regionName) != 0: # If we are still here, we need to try a new bucket name bucketName = "%s-%s" % (bucketName, str(uuid.uuid1())[:8]) print(" Picking a different bucket name: %s" % bucketName) # Write out a new config file print("Updating config file %s" % s3StateCfgFile) data['terraform']['backend']['s3']['bucket'] = bucketName data['terraform']['backend']['s3']['key'] = keyName data['terraform']['backend']['s3']['region'] = regionName cfgFile = open(s3StateCfgFile, 'w') cfgFile.write(hcl.dumps(data, indent=4, sort_keys=True)) cfgFile.close()