Example #1
0
def create_rest_client(**kwargs):
    # Doing it with kwargs instead of arguments with default values to allow
    # not passing args (which will then use the default values), or explicitly
    # passing None (or False) which will then be passed as-is to the Client

    username = kwargs.get('username', get_manager_username())
    password = kwargs.get('password', get_manager_password())
    tenant = kwargs.get('tenant', get_manager_tenant())
    token = kwargs.get('token')
    rest_port = kwargs.get('rest_port',
                           os.environ.get(constants.CLOUDIFY_REST_PORT, 80))
    rest_protocol = kwargs.get('rest_protocol',
                               'https' if rest_port == '443' else 'http')
    cert_path = kwargs.get('cert_path', cli_env.get_ssl_cert())
    trust_all = kwargs.get('trust_all', cli_env.get_ssl_trust_all())

    headers = create_auth_header(username, password, token, tenant)

    return CloudifyClient(
            host=get_manager_ip(),
            port=rest_port,
            protocol=rest_protocol,
            headers=headers,
            trust_all=trust_all,
            cert=cert_path)
Example #2
0
def _cfy_cli_inner(cmdline, shell=False, capture_stdout=False):
    """
    Lowest layer of calling the Cloudify CLI.
    Typically, you would want to call "_cfy_cli" instead of this one.
    """
    env = dict(os.environ)
    if CLOUDIFY_TENANT_ENV not in env:
        env[CLOUDIFY_TENANT_ENV] = DEFAULT_TENANT_NAME
    # If "trust all" is in effect, then disable this warning and
    # assume the user knows what they're doing.
    ignored_warnings = ["Python 2 is no longer supported"]
    if _use_ssl() and get_ssl_trust_all():
        ignored_warnings.append("Unverified HTTPS request")
    if ignored_warnings:
        env['PYTHONWARNINGS'] = ','.join(
            ["ignore:{}".format(x) for x in ignored_warnings])
    if shell:
        full_cmdline = "cfy {}".format(cmdline)
    else:
        full_cmdline = ['cfy']
        full_cmdline.extend(cmdline)
    logger.info("Running: %s", full_cmdline)
    if capture_stdout:
        stdout_contents = subprocess.check_output(full_cmdline,
                                                  env=env,
                                                  shell=shell)
    else:
        subprocess.check_call(full_cmdline, env=env, shell=shell)
        stdout_contents = None
    return stdout_contents
Example #3
0
def create_rest_client(**kwargs):
    # Doing it with kwargs instead of arguments with default values to allow
    # not passing args (which will then use the default values), or explicitly
    # passing None (or False) which will then be passed as-is to the Client

    username = kwargs.get('username', get_manager_username())
    password = kwargs.get('password', get_manager_password())
    tenant = kwargs.get('tenant', get_manager_tenant())
    token = kwargs.get('token')
    rest_port = kwargs.get('rest_port',
                           os.environ.get(constants.CLOUDIFY_REST_PORT, 80))
    rest_protocol = kwargs.get('rest_protocol',
                               'https' if rest_port == '443' else 'http')
    cert_path = kwargs.get('cert_path', cli_env.get_ssl_cert())
    trust_all = kwargs.get('trust_all', cli_env.get_ssl_trust_all())

    headers = create_auth_header(username, password, token, tenant)

    return CloudifyClient(
            host=get_manager_ip(),
            port=rest_port,
            protocol=rest_protocol,
            headers=headers,
            trust_all=trust_all,
            cert=cert_path)
Example #4
0
 def wrapper(*args, **kwargs):
     manager_host = os.environ[CLOUDIFY_HOST_ENV]
     manager_user = os.environ[CLOUDIFY_USERNAME_ENV]
     manager_password = os.environ[CLOUDIFY_PASSWORD_ENV]
     manager_tenant = os.environ.get(CLOUDIFY_TENANT_ENV,
                                     DEFAULT_TENANT_NAME)
     use_ssl = _use_ssl()
     ssl_trust_all = get_ssl_trust_all()
     # If user wants to trust all certificates, then disable the warning
     # about it and assume (and hope) they know what they're doing.
     if use_ssl and ssl_trust_all:
         urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
     client = CloudifyClient(
         host=manager_host,
         username=manager_user,
         password=manager_password,
         tenant=manager_tenant,
         protocol=SECURED_PROTOCOL if use_ssl else DEFAULT_PROTOCOL,
         trust_all=ssl_trust_all)
     kwargs['client'] = client
     return func(*args, **kwargs)