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')