Skip to content

Instantly share code, notes, and snippets.

@EscVector
Last active May 12, 2021 21:30
Show Gist options
  • Save EscVector/173f5c0a252f4cbd3bcc153728fcf779 to your computer and use it in GitHub Desktop.
Save EscVector/173f5c0a252f4cbd3bcc153728fcf779 to your computer and use it in GitHub Desktop.
#
#
# Todo: Jinja2 template
#
#
import oci
# from jinja2 import Template
import re
from datetime import datetime
region = "us-ashburn-1"
print("Using region:", region)
ts = str(datetime.utcnow().timestamp())[:10]
tenancy_id = ""
config = oci.config.from_file()
client = oci.identity.IdentityClient(config)
regions = client.list_regions().data
# for r in regions:
# region_key = r.key
# region_name = r.name
# print(r.name + ' ' + r.key)
# subscriptions = client.list_region_subscriptions(tenancy_id).data
provider_file = open("providers_"+ ts +".tfx",mode="w",encoding="utf-8")
header = str("#"*49) + "\n"
header = header + "### Terraform Provider File ###\n"
header = header + "### Auto Generated " + str(datetime.now()) + " ###\n"
header = header + str("#"*49)
header = header + "\n\n"
header = header + "# Default Provider" + "\n"
header = header + "provider \"oci\" { " + "\n"
header = header + " region =\"" + region + "\"\n"
header = header + "} \n\n"
provider_file.write(header)
print(header)
for region in regions:
region_key = region.key
region_name = region.name
alias = region_name.replace("eu-","").replace("ap-","").replace("uk-","").replace("us-","").replace("sa-","").replace("me-","").replace("ca-","").replace("-","")
alias = alias.strip("1").strip("2").strip("3")
full_name = region_name
region_name = region_name.replace(")","").lower()
region_name = region_name.replace("n. ","").replace(" ","")
region_name = re.sub(r'^.*?\(', '', region_name)
provider = "# RegionKey:" + region_key + " - " + full_name + "\n"
provider = provider + "\n\n"
provider = provider + "provider \"oci\" {\n"
provider = provider + " region = \""+region_name+"\" \n"
provider = provider + " alias = \""+alias+"\" \n"
provider = provider + " # tenancy_ocid = \"\" \n"
provider = provider + " # user_ocid = \n"
provider = provider + " # private_key = \n"
provider = provider + " # private_key_path = \n"
provider = provider + " # private_key_password = \n"
provider = provider + " # fingerprint = \n"
provider = provider + " # config_file_profile = \n"
provider = provider + "}\n\n"
print (str("#"*45)+"\n"+provider)
provider_file.write(str("#"*45)+"\n")
provider_file.write(provider)
# tenancy_ocid - OCID of your tenancy. To get the value, see Required Keys and OCIDs #Tenancy's OCID.
# user_ocid - OCID of the user calling the API. To get the value, see Required Keys and OCIDs #User's OCID.
# private_key - The contents of the private key file, required if private_key_path is not defined, takes precedence over private_key_path if both are defined. For details on how to create and configure keys see Required Keys and OCIDs #How to Upload the Public Key.
# private_key_path - The path (including filename) of the private key stored on your computer, required if private_key is not defined. For details on how to create and configure keys see Required Keys and OCIDs #How to Upload the Public Key.
# private_key_password - (Optional) Passphrase used for the key, if it is encrypted.
# fingerprint - Fingerprint for the key pair being used. To get the value, see Required Keys and OCIDs #How to Get the Key's Fingerprint.
# region - An Oracle Cloud Infrastructure region. See Regions and Availability Domains.
# config_file_profile - Profile Name if you would like to use custom profile for oci standard config file for credentials
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment