def __init__(self, token: str) -> None: """ :param token: CloudGenix API authentication token """ self.token = token self.sdk = API() # Authenticate to the CloudGenix API self.login()
def cgx_auth(auth_token): try: from cloudgenix import API sdk = API() if (sdk.interactive.use_token(auth_token)): return sdk else: return False except: return False
def authenticate(CLIARGS): print("AUTHENTICATING...") user_email = None user_password = None sdk = API() ##First attempt to use an AuthTOKEN if defined if CLIARGS['token']: #Check if AuthToken is in the CLI ARG CLOUDGENIX_AUTH_TOKEN = CLIARGS['token'] print(" ", "Authenticating using Auth-Token in from CLI ARGS") elif CLIARGS['authtokenfile']: #Next: Check if an AuthToken file is used tokenfile = open(CLIARGS['authtokenfile']) CLOUDGENIX_AUTH_TOKEN = tokenfile.read().strip() print(" ", "Authenticating using Auth-token from file", CLIARGS['authtokenfile']) elif "X_AUTH_TOKEN" in os.environ: #Next: Check if an AuthToken is defined in the OS as X_AUTH_TOKEN CLOUDGENIX_AUTH_TOKEN = os.environ.get('X_AUTH_TOKEN') print(" ", "Authenticating using environment variable X_AUTH_TOKEN") elif "AUTH_TOKEN" in os.environ: #Next: Check if an AuthToken is defined in the OS as AUTH_TOKEN CLOUDGENIX_AUTH_TOKEN = os.environ.get('AUTH_TOKEN') print(" ", "Authenticating using environment variable AUTH_TOKEN") else: #Next: If we are not using an AUTH TOKEN, set it to NULL CLOUDGENIX_AUTH_TOKEN = None print(" ", "Authenticating using interactive login") ##ATTEMPT AUTHENTICATION if CLOUDGENIX_AUTH_TOKEN: sdk.interactive.use_token(CLOUDGENIX_AUTH_TOKEN) if sdk.tenant_id is None: print(" ", "ERROR: AUTH_TOKEN login failure, please check token.") sys.exit() else: while sdk.tenant_id is None: sdk.interactive.login(user_email, user_password) # clear after one failed login, force relogin. if not sdk.tenant_id: user_email = None user_password = None print(" ", "SUCCESS: Authentication Complete") return sdk
#!/usr/bin/env python PROGRAM_NAME = "cg-csv-element-inventory.py" PROGRAM_DESCRIPTION = """ CloudGenix script --------------------------------------- """ from cloudgenix import API, jd import os import sys import argparse import csv CLIARGS = {} cgx_session = API() #Instantiate a new CG API Session for AUTH def parse_arguments(): parser = argparse.ArgumentParser( prog=PROGRAM_NAME, formatter_class=argparse.RawDescriptionHelpFormatter, description=PROGRAM_DESCRIPTION) parser.add_argument( '--token', '-t', metavar='"MYTOKEN"', type=str, help='specify an authtoken to use for CloudGenix authentication') parser.add_argument('--authtokenfile', '-f',
from random import randint from os import system from platform import system as platform import os import yaml import csv import sys import argparse import re import requests import yaml import jinja2 from yaml.loader import SafeLoader from cloudgenix import API sdk = API() ##############GLOBALS############## ####Common params takes the dict format of YML_KEY: Depth. Where 0 is any depth. Used to ####specifically match duplicate key names like state which exists both as a key in ####elements (state: bound) and the site address (state: CA). common_yml_params = { ###These are the common YML Keys used for the common params checkbox 'city': 0, 'country': 0, 'post_code': 0, 'state': 6, ###Match only the state under the address at depth of 6 'street': 0, 'street2': 0, } policy_yml_params = { ###These are the common Policy Keys used for the policy params checkbox