def create_or_update(cluster, token_name, token_fields, action): """Creates (or updates) the given token on the given cluster""" cluster_name = cluster['name'] cluster_url = cluster['url'] existing_token_data, existing_token_etag = get_token(cluster, token_name) try: print_info( f'Attempting to {action} token on {terminal.bold(cluster_name)}...' ) json_body = existing_token_data if existing_token_data and action.should_patch( ) else {} json_body.update(token_fields) headers = {'If-Match': existing_token_etag or ''} resp = http_util.post(cluster, 'token', json_body, params={'token': token_name}, headers=headers) process_post_result(resp) return 0 except requests.exceptions.ReadTimeout as rt: logging.exception(rt) print_info( terminal.failed( f'Encountered read timeout with {cluster_name} ({cluster_url}). Your post may have completed.' )) return 1 except IOError as ioe: logging.exception(ioe) reason = f'Cannot connect to {cluster_name} ({cluster_url})' message = post_failed_message(cluster_name, reason) print_info(f'{message}\n')
def _update_token(cluster, token_name, existing_token_etag, body): cluster_name = cluster['name'] cluster_url = cluster['url'] headers = {'If-Match': existing_token_etag} params = {'token': token_name} try: resp = http_util.post(cluster, 'token', body, params=params, headers=headers) process_post_result(resp) return 0 except requests.exceptions.ReadTimeout as rt: logging.exception(rt) print_info( terminal.failed( f'Encountered read timeout with {cluster_name} ({cluster_url}). The operation may have completed.' )) return 1 except IOError as ioe: logging.exception(ioe) reason = f'Cannot connect to {cluster_name} ({cluster_url})' message = post_failed_message(cluster_name, reason) print_info(f'{message}\n')
def format_status(status): """Formats service status""" if status == 'Running': return terminal.running(status) elif status == 'Inactive': return terminal.inactive(status) elif status == 'Failing': return terminal.failed(status) elif status == 'Starting': return terminal.starting(status) else: return status
def no_data_message(clusters): """Returns a message indicating that no data was found in the given clusters""" clusters_text = ' / '.join([c['name'] for c in clusters]) message = terminal.failed(f'No matching data found in {clusters_text}.') message = f'{message}\nDo you need to add another cluster to your configuration?' return message
def print_error(text): """Prints text to stderr, colored as a failure""" print(terminal.failed(text), file=sys.stderr)