|
| 1 | +"""Setup CLI configuration.""" |
| 2 | +# :license: MIT, see LICENSE for more details. |
| 3 | +import os.path |
| 4 | + |
| 5 | +import SoftLayer |
| 6 | +from SoftLayer import auth |
| 7 | +from SoftLayer.CLI import config |
| 8 | +from SoftLayer.CLI import environment |
| 9 | +from SoftLayer.CLI import exceptions |
| 10 | +from SoftLayer.CLI import formatting |
| 11 | +from SoftLayer import utils |
| 12 | + |
| 13 | +import click |
| 14 | + |
| 15 | + |
| 16 | +def get_api_key(client, username, secret, endpoint_url=None): |
| 17 | + """Attempts API-Key and password auth to get an API key. |
| 18 | +
|
| 19 | + This will also generate an API key if one doesn't exist |
| 20 | + """ |
| 21 | + |
| 22 | + client.endpoint_url = endpoint_url |
| 23 | + client.auth = None |
| 24 | + # Try to use a client with username/api key |
| 25 | + if len(secret) == 64: |
| 26 | + try: |
| 27 | + client.auth = auth.BasicAuthentication(username, secret) |
| 28 | + client['Account'].getCurrentUser() |
| 29 | + return secret |
| 30 | + except SoftLayer.SoftLayerAPIError as ex: |
| 31 | + if 'invalid api token' not in ex.faultString.lower(): |
| 32 | + raise |
| 33 | + else: |
| 34 | + # Try to use a client with username/password |
| 35 | + client.authenticate_with_password(username, secret) |
| 36 | + |
| 37 | + user_record = client['Account'].getCurrentUser( |
| 38 | + mask='id, apiAuthenticationKeys') |
| 39 | + api_keys = user_record['apiAuthenticationKeys'] |
| 40 | + if len(api_keys) == 0: |
| 41 | + return client['User_Customer'].addApiAuthenticationKey( |
| 42 | + id=user_record['id']) |
| 43 | + return api_keys[0]['authenticationKey'] |
| 44 | + |
| 45 | + |
| 46 | +@click.command() |
| 47 | +@environment.pass_env |
| 48 | +def cli(env): |
| 49 | + """Edit configuration.""" |
| 50 | + |
| 51 | + username, secret, endpoint_url, timeout = get_user_input(env) |
| 52 | + |
| 53 | + api_key = get_api_key(env.client, username, secret, |
| 54 | + endpoint_url=endpoint_url) |
| 55 | + |
| 56 | + path = '~/.softlayer' |
| 57 | + if env.config_file: |
| 58 | + path = env.config_file |
| 59 | + config_path = os.path.expanduser(path) |
| 60 | + |
| 61 | + env.out(env.fmt(config.config_table({'username': username, |
| 62 | + 'api_key': api_key, |
| 63 | + 'endpoint_url': endpoint_url, |
| 64 | + 'timeout': timeout}))) |
| 65 | + |
| 66 | + if not formatting.confirm('Are you sure you want to write settings ' |
| 67 | + 'to "%s"?' % config_path, default=True): |
| 68 | + raise exceptions.CLIAbort('Aborted.') |
| 69 | + |
| 70 | + # Persist the config file. Read the target config file in before |
| 71 | + # setting the values to avoid clobbering settings |
| 72 | + parsed_config = utils.configparser.RawConfigParser() |
| 73 | + parsed_config.read(config_path) |
| 74 | + try: |
| 75 | + parsed_config.add_section('softlayer') |
| 76 | + except utils.configparser.DuplicateSectionError: |
| 77 | + pass |
| 78 | + |
| 79 | + parsed_config.set('softlayer', 'username', username) |
| 80 | + parsed_config.set('softlayer', 'api_key', api_key) |
| 81 | + parsed_config.set('softlayer', 'endpoint_url', endpoint_url) |
| 82 | + |
| 83 | + config_fd = os.fdopen(os.open(config_path, |
| 84 | + (os.O_WRONLY | os.O_CREAT | os.O_TRUNC), |
| 85 | + 0o600), |
| 86 | + 'w') |
| 87 | + try: |
| 88 | + parsed_config.write(config_fd) |
| 89 | + finally: |
| 90 | + config_fd.close() |
| 91 | + |
| 92 | + return "Configuration Updated Successfully" |
| 93 | + |
| 94 | + |
| 95 | +def get_user_input(env): |
| 96 | + """Ask for username, secret (api_key or password) and endpoint_url.""" |
| 97 | + |
| 98 | + defaults = config.get_settings_from_client(env.client.real_client) |
| 99 | + timeout = defaults['timeout'] |
| 100 | + |
| 101 | + # Ask for username |
| 102 | + for _ in range(3): |
| 103 | + username = (env.input('Username [%s]: ' % defaults['username']) |
| 104 | + or defaults['username']) |
| 105 | + if username: |
| 106 | + break |
| 107 | + else: |
| 108 | + raise exceptions.CLIAbort('Aborted after 3 attempts') |
| 109 | + |
| 110 | + # Ask for 'secret' which can be api_key or their password |
| 111 | + for _ in range(3): |
| 112 | + secret = (env.getpass('API Key or Password [%s]: ' |
| 113 | + % defaults['api_key']) |
| 114 | + or defaults['api_key']) |
| 115 | + if secret: |
| 116 | + break |
| 117 | + else: |
| 118 | + raise exceptions.CLIAbort('Aborted after 3 attempts') |
| 119 | + |
| 120 | + # Ask for which endpoint they want to use |
| 121 | + for _ in range(3): |
| 122 | + endpoint_type = env.input( |
| 123 | + 'Endpoint (public|private|custom): ') |
| 124 | + endpoint_type = endpoint_type.lower() |
| 125 | + if not endpoint_type: |
| 126 | + endpoint_url = SoftLayer.API_PUBLIC_ENDPOINT |
| 127 | + break |
| 128 | + if endpoint_type == 'public': |
| 129 | + endpoint_url = SoftLayer.API_PUBLIC_ENDPOINT |
| 130 | + break |
| 131 | + elif endpoint_type == 'private': |
| 132 | + endpoint_url = SoftLayer.API_PRIVATE_ENDPOINT |
| 133 | + break |
| 134 | + elif endpoint_type == 'custom': |
| 135 | + endpoint_url = env.input( |
| 136 | + 'Endpoint URL [%s]: ' % defaults['endpoint_url'] |
| 137 | + ) or defaults['endpoint_url'] |
| 138 | + break |
| 139 | + else: |
| 140 | + raise exceptions.CLIAbort('Aborted after 3 attempts') |
| 141 | + |
| 142 | + return username, secret, endpoint_url, timeout |
0 commit comments