Skip to content

Commit

Permalink
Prompt for 'region' in 'config create --full' flow
Browse files Browse the repository at this point in the history
  • Loading branch information
randomir committed Oct 18, 2021
1 parent 140a6a4 commit 893b46e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 35 deletions.
22 changes: 13 additions & 9 deletions dwave/cloud/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from functools import partial
from timeit import default_timer as timer

from typing import Dict
from typing import Dict, List
from configparser import ConfigParser

import click
Expand All @@ -44,6 +44,7 @@
load_profile_from_files, load_config_from_files, get_default_config,
get_configfile_path, get_default_configfile_path,
get_configfile_paths)
from dwave.cloud.api.constants import DEFAULT_METADATA_API_ENDPOINT


def enable_logging(ctx, param, value):
Expand Down Expand Up @@ -201,15 +202,16 @@ def create(config_file, profile, ask_full):

def _input_config_variables(config: ConfigParser,
profile: str,
prompts: Dict[str, str]) -> ConfigParser:
prompts: Dict[str, Dict[str, str]]) -> ConfigParser:
"""Update config variables in place with user-provided values."""

for var, prompt in prompts.items():
default_val = config.get(profile, var, fallback=None)
val = default_text_input(prompt, default_val)
prompt.setdefault('default', default_val)
val = default_text_input(**prompt)
if val:
val = os.path.expandvars(val)
if val != default_val:
if val and val != default_val:
config.set(profile, var, val)
return config

Expand Down Expand Up @@ -241,15 +243,17 @@ def _config_create(config_file, profile, ask_full=False):
"""Full/simplified dwave create flows."""

if ask_full:
rs = Client._fetch_available_regions(DEFAULT_METADATA_API_ENDPOINT)
prompts = dict(
endpoint="Solver API endpoint URL",
token="Authentication token",
client="Client class",
solver="Solver")
region=dict(prompt="Solver API region", choices=[r.code for r in rs]),
endpoint=dict(prompt="Solver API endpoint URL (overwrites 'region')"),
token=dict(prompt="Authentication token"),
client=dict(prompt="Client class", choices='base qpu sw hybrid'.split()),
solver=dict(prompt="Solver"))

else:
prompts = dict(
token="Authentication token")
token=dict(prompt="Authentication token"))

click.echo("Using the simplified configuration flow.\n"
"Try 'dwave config create --full' for more options.\n")
Expand Down
1 change: 1 addition & 0 deletions dwave/cloud/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ def wrapper(*args, **kwargs):
key = self.argshash(args, kwargs)
data = self.cache.get(key, {})

logger.trace("cached: refresh=%r, key=%r, data=%r", refresh_, key, data)
if not refresh_ and data.get('expires', 0) > now:
val = data.get('val')
else:
Expand Down
53 changes: 27 additions & 26 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,31 @@ def touch(path):
class TestConfigCreate(unittest.TestCase):

@parameterized.expand([
("simple", [], ["token"]),
("full", ["--full"], "endpoint token client solver".split()),
("simple", [], dict(token="token")),
("full", ["--full"], dict(region="na-west-1", endpoint="endpoint",
token="token", client=None, solver=None)),
])
def test_create(self, name, extra_opts, inputs):
config_file = 'path/to/dwave.conf'
profile = 'profile'

runner = CliRunner(mix_stderr=False)
with runner.isolated_filesystem():
with mock.patch("dwave.cloud.utils.input", side_effect=inputs):
result = runner.invoke(cli, [
'config', 'create', '--config-file', config_file, '--profile', profile
] + extra_opts, input='\n'.join(inputs))
self.assertEqual(result.exit_code, 0)
result = runner.invoke(cli, [
'config', 'create', '--config-file', config_file, '--profile', profile
] + extra_opts, input='\n'.join('' if v is None else v for v in inputs.values()))
self.assertEqual(result.exit_code, 0)

# load and verify config
with isolated_environ(remove_dwave=True):
config = load_config(config_file, profile=profile)
for val in inputs:
self.assertEqual(config.get(val), val)
for var, val in inputs.items():
self.assertEqual(config.get(var), val)

@parameterized.expand([
("simple", [], ["", "token"]),
("full", ["--full"], ["", ""] + "endpoint token client solver".split()),
("simple", [], dict(config_file=None, token="token")),
("full", ["--full"], dict(config_file=None, profile=None, region="na-west-1",
endpoint="endpoint", token="token", client=None, solver=None)),
])
def test_default_flows(self, name, extra_opts, inputs):
runner = CliRunner(mix_stderr=False)
Expand All @@ -70,19 +71,20 @@ def test_default_flows(self, name, extra_opts, inputs):
with mock.patch("dwave.cloud.utils.input", side_effect=inputs):
result = runner.invoke(cli, [
'config', 'create'
] + extra_opts, input='\n'.join(inputs))
] + extra_opts, input='\n'.join('' if v is None else v for v in inputs.values()))
self.assertEqual(result.exit_code, 0)

# load and verify config
with isolated_environ(remove_dwave=True):
config = load_config()
for val in inputs:
for var, val in inputs.items():
if val: # skip empty default confirmations
self.assertEqual(config.get(val), val)
self.assertEqual(config.get(var), val)

@parameterized.expand([
("simple", [], ["token"]),
("full", ["--full"], "endpoint token client solver".split()),
("simple", [], dict(token="token")),
("full", ["--full"], dict(region="na-west-1", endpoint="endpoint",
token="token", client="base", solver="solver")),
])
def test_update(self, name, extra_opts, inputs):
config_file = 'dwave.conf'
Expand All @@ -91,28 +93,27 @@ def test_update(self, name, extra_opts, inputs):
runner = CliRunner(mix_stderr=False)
with runner.isolated_filesystem():
# create config
config_body = '\n'.join(f"{v} = old-{v}" for v in inputs)
config_body = '\n'.join(f"{k} = old-{v}" for k,v in inputs.items())
with open(config_file, 'w') as fp:
fp.write(f"[{profile}]\n{config_body}")

# verify config before update
with isolated_environ(remove_dwave=True):
config = load_config(config_file=config_file)
for val in inputs:
self.assertEqual(config.get(val), f"old-{val}")
for var, val in inputs.items():
self.assertEqual(config.get(var), f"old-{val}")

# update config
with mock.patch("dwave.cloud.utils.input", side_effect=inputs):
result = runner.invoke(cli, [
'config', 'create', '-f', config_file, '-p', profile,
] + extra_opts, input='\n'.join(inputs))
self.assertEqual(result.exit_code, 0)
result = runner.invoke(cli, [
'config', 'create', '-f', config_file, '-p', profile,
] + extra_opts, input='\n'.join('' if v is None else v for v in inputs.values()))
self.assertEqual(result.exit_code, 0)

# verify config updated
with isolated_environ(remove_dwave=True):
config = load_config(config_file=config_file)
for val in inputs:
self.assertEqual(config.get(val), val)
for var, val in inputs.items():
self.assertEqual(config.get(var), val)


class TestCli(unittest.TestCase):
Expand Down

0 comments on commit 893b46e

Please sign in to comment.