def user_handling_by_name(client: JfrogAPI, username: str, email: str, password: str, is_deletion: bool = False) -> dict: try: if is_deletion: endpoint = endpoints.DELETE_USER else: endpoint = endpoints.CREATE_USER data = {"email": email, "password": password} res = client.request(endpoint['method'].value, endpoint['url'].format(username), data) if res['success']: if res['ok']: return {"succeed": True} else: return { "error": res['response'].status_code, "message": res['message'] } else: return {"error": res['error']} except Exception as e: return {"error": "unknown exception"}
def user_handling_by_name(client: JfrogAPI, username: str, email: str, password: str, is_deletion: bool = False) -> str: try: if is_deletion: endpoint = endpoints.DELETE_USER else: endpoint = endpoints.CREATE_USER data = {"email": email, "password": password} res = client.request(endpoint['method'].value, endpoint['url'].format(username), data) if res['success']: if res['ok']: if is_deletion: return "Deleted successfully !" else: return "Created successfully !" else: return "Error creating user : "******" - " + res['message'] else: return "Error creating user: "******"Error creating user: unknown exception"
def get_health_ping(client: JfrogAPI) -> str: try: endpoint = endpoints.HEALTH_PING res = client.request(endpoint['method'].value, endpoint['url']) if res['success']: if res['ok']: return "Artifactory is Alive !" else: return "Artifactory might be Down - " + str( res['response'].status_code) + " - " + res['message'] else: return "Artifactory might be Down - " + res['error'] except Exception as e: return "Artifactory might be Down - Unknown exception"
def get_storage_info(client: JfrogAPI) -> str: try: endpoint = endpoints.STORAGE_INFO res = client.request(endpoint['method'].value, endpoint['url']) if res['success']: if res['ok']: storage_info_json = json.loads(res['response'].content) return json.dumps(storage_info_json, indent=1, sort_keys=True) else: return "Error retrieving storage info - " + str( res['response'].status_code) + " - " + res['message'] else: return "Error retrieving storage info - " + res['error'] except Exception as e: return "Error retrieving storage info - unknown exception"
def get_health_ping(client: JfrogAPI) -> dict: try: endpoint = endpoints.HEALTH_PING res = client.request(endpoint['method'].value, endpoint['url']) if res['success']: if res['ok']: return {"jfrog_alive": True} else: return { "jfrog_alive": False, "error": res['response'].status_code, "message": res['message'] } else: return {"jfrog_alive": False, "error": res['error']} except Exception as e: return {"jfrog_alive": False, "error": "unknown exception"}
def get_system_version(client: JfrogAPI) -> str: try: endpoint = endpoints.SYSTEM_VERSION res = client.request(endpoint['method'].value, endpoint['url']) if res['success']: if res['ok']: response_json = json.loads(res['response'].content) if "version" in response_json.keys(): return "Artifactory Version: " + response_json['version'] else: return "Error finding version : No version" else: return "Error finding version : " + str( res['response'].status_code) + " - " + res['message'] else: return "Error finding version" + res['error'] except Exception as e: return "Error - Unknown exception"
def get_storage_info(client: JfrogAPI) -> dict: try: endpoint = endpoints.STORAGE_INFO res = client.request(endpoint['method'].value, endpoint['url']) if res['success']: if res['ok']: return { "succeed": True, "data": json.loads(res['response'].content) } else: return { "error": res['response'].status_code, "message": res['message'] } else: return {"error": res['error']} except Exception as e: return {"error": "unknown exception"}
def get_system_version(client: JfrogAPI) -> dict: try: endpoint = endpoints.SYSTEM_VERSION res = client.request(endpoint['method'].value, endpoint['url']) if res['success']: if res['ok']: response_json = json.loads(res['response'].content) if "version" in response_json.keys(): return {"version": response_json['version']} else: return {"error": "no version"} else: return { "error": res['response'].status_code, "message": res['message'] } else: return {"error": res['error']} except Exception as e: return {"error": "unknown exception"}
def health(url: str, username: str, password: str): """ Retrieving the Artifactory instance health status. """ client = JfrogAPI(url, username, password) typer.echo(get_health_ping(client))
def storage_info(url: str, username: str, password: str): """ Prints the storage info of the Artifactory instance. """ client = JfrogAPI(url, username, password) typer.echo(get_storage_info(client))
def delete_user(url: str, username: str, password: str, new_username: str, new_user_password: str, email: str): """ Deletes Artifactory user, using username, password and email. """ client = JfrogAPI(url, username, password) typer.echo(user_handling_by_name(client, new_username, email, new_user_password, is_deletion=True))
def create_user(url: str, username: str, password: str, new_username: str, new_user_password: str, email: str): """ Creates Artifactory user, using username, password and email. """ client = JfrogAPI(url, username, password) typer.echo(user_handling_by_name(client, new_username, email, new_user_password))
def version(url: str, username: str, password: str): """ Retrieving the Artifactory instance version. """ client = JfrogAPI(url, username, password) typer.echo(get_system_version(client))
from fastapi import Depends, FastAPI from fastapi.security import OAuth2PasswordBearer from controllers.artifactory import get_health_ping, get_system_version, user_handling_by_name, get_storage_info from interfaces.jfrog_api import JfrogAPI from utils.tokens import set_token_to_client from configs.envs import ARTIFACTORY_URL app = FastAPI() oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token") client = JfrogAPI(ARTIFACTORY_URL) @app.get("/") def read_root(): return {"Info": "JFrog Artifactory API, feel free to go /docs for more information about the API !"} @app.get("/artifactory_health") def artifactory_health(token: str = Depends(oauth2_scheme)): tokenized_client = set_token_to_client(client, token) return get_health_ping(tokenized_client) @app.get("/artifactory_version") def artifactory_version(token: str = Depends(oauth2_scheme)): tokenized_client = set_token_to_client(client, token) return get_system_version(tokenized_client) @app.put("/users/{username}")