Skip to content

Commit

Permalink
Allow API Version to be explicitly specified in env var or cmd options
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Goodman <pete@petegoo.com>
  • Loading branch information
PeteGoo committed Sep 16, 2018
1 parent b833540 commit 541b2c3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
4 changes: 3 additions & 1 deletion anchorecli/cli/__init__.py
Expand Up @@ -16,20 +16,22 @@
@click.option('--u', help='Username (or use environment variable ANCHORE_CLI_USER)')
@click.option('--p', help='Password (or use environment variable ANCHORE_CLI_PASS)')
@click.option('--url', help='Service URL (or use environment variable ANCHORE_CLI_URL)')
@click.option('--api-version', help='Explicitly specify the API version to skip checking. Useful when swagger endpoint is inaccessible')
@click.option('--insecure', is_flag=True, help='Skip SSL cert checks (or use environment variable ANCHORE_CLI_SSL_VERIFY=<y/n>)')
@click.option('--json', is_flag=True, help='Output raw API JSON')

@click.version_option(version=version.version)
@click.pass_context
#@extended_help_option(extended_help="extended help")
def main_entry(ctx, debug, u, p, url, insecure, json):
def main_entry(ctx, debug, u, p, url, api_version, insecure, json):
if debug:
logging.basicConfig(level=logging.DEBUG)

cli_opts = {
'u': u,
'p': p,
'url': url,
'api-version': api_version,
'insecure': insecure,
'json': json,
'debug': debug
Expand Down
10 changes: 8 additions & 2 deletions anchorecli/cli/utils.py
Expand Up @@ -26,6 +26,7 @@ def setup_config(cli_opts):
'user':None,
'pass':None,
'url':"http://localhost:8228/v1",
'api-version': None,
'ssl_verify':True,
'jsonmode':False,
'debug':False,
Expand All @@ -50,7 +51,7 @@ def setup_config(cli_opts):
raise Exception("invalid credentials file format")

default_creds = ydata.get('default', {})
for e in ['ANCHORE_CLI_USER', 'ANCHORE_CLI_PASS', 'ANCHORE_CLI_URL', 'ANCHORE_CLI_SSL_VERIFY']:
for e in ['ANCHORE_CLI_USER', 'ANCHORE_CLI_PASS', 'ANCHORE_CLI_URL', 'ANCHORE_CLI_API_VERSION', 'ANCHORE_CLI_SSL_VERIFY']:
if e in default_creds:
settings[e] = default_creds[e]
except Exception as err:
Expand All @@ -61,7 +62,7 @@ def setup_config(cli_opts):

# load environment if present
try:
for e in ['ANCHORE_CLI_USER', 'ANCHORE_CLI_PASS', 'ANCHORE_CLI_URL', 'ANCHORE_CLI_SSL_VERIFY', 'ANCHORE_CLI_JSON', 'ANCHORE_CLI_DEBUG']:
for e in ['ANCHORE_CLI_USER', 'ANCHORE_CLI_PASS', 'ANCHORE_CLI_URL', 'ANCHORE_CLI_API_VERSION', 'ANCHORE_CLI_SSL_VERIFY', 'ANCHORE_CLI_JSON', 'ANCHORE_CLI_DEBUG']:
if e in os.environ:
settings[e] = os.environ[e]
except Exception as err:
Expand All @@ -78,6 +79,9 @@ def setup_config(cli_opts):
if cli_opts['url']:
settings['ANCHORE_CLI_URL'] = cli_opts['url']

if cli_opts['api-version']:
settings['ANCHORE_CLI_API_VERSION'] = cli_opts['api-version']

if cli_opts['insecure']:
settings['ANCHORE_CLI_SSL_VERIFY'] = "n"

Expand All @@ -98,6 +102,8 @@ def setup_config(cli_opts):
if 'ANCHORE_CLI_URL' in settings:
ret['url'] = settings['ANCHORE_CLI_URL']

if 'ANCHORE_CLI_API_VERSION' in settings:
ret['api-version'] = settings['ANCHORE_CLI_API_VERSION']
if 'ANCHORE_CLI_SSL_VERIFY' in settings:
if settings['ANCHORE_CLI_SSL_VERIFY'].lower() == 'n':
ret['ssl_verify'] = False
Expand Down
2 changes: 2 additions & 0 deletions anchorecli/clients/apiexternal.py
Expand Up @@ -163,6 +163,8 @@ def detect_api_version(config):
:param config:
:return: tuple of ints
"""
if config['api-version']:
return tuple([int(x) for x in config['api-version'].split('.')])

url = urlparse(config['url'])
url = urlunparse((url.scheme, url.netloc, '/swagger.json', url.params, url.query, url.fragment))
Expand Down

0 comments on commit 541b2c3

Please sign in to comment.