def post_channel_mirror( request: Request, mirror: rest_models.ChannelMirrorBase, channel_name: str, channel: db_models.Channel = Depends(get_channel_or_fail), auth: authorization.Rules = Depends(get_rules), dao: Dao = Depends(get_dao), remote_session: requests.Session = Depends(get_remote_session), ): auth.assert_register_mirror(channel_name) logger.debug(f"registering mirror {mirror.url}") if not mirror.api_endpoint: mirror.api_endpoint = mirror.url.replace("get", "api/channels") if not mirror.metrics_endpoint: mirror.metrics_endpoint = mirror.url.replace("get", "metrics/channels") # check api response response = remote_session.get(mirror.api_endpoint) if response.status_code != 200: raise HTTPException( status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=f"could not connect to remote repository {mirror.url}", ) response_data = response.json() try: mirrored_server = response_data["mirror_channel_url"] except KeyError: raise HTTPException( status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail="mirror server is not quetz server", ) if not mirrored_server: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail=f"{mirror.url} is not a mirror server", ) dao.create_channel_mirror(channel_name, mirror.url, mirror.api_endpoint, mirror.metrics_endpoint) logger.info(f"successfully registered mirror {mirror.url}")
def channel_mirror(public_channel, dao: Dao): mirror_url = "http://mirror_server/get/my-mirror" api_endpoint = "http://mirror_server/api/my-mirror" metrics_endpoint = "http://mirror_server/metrics/channels/my-mirror" return dao.create_channel_mirror( public_channel.name, mirror_url, api_endpoint, metrics_endpoint )