Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 14 additions & 32 deletions alws/crud/products.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
Dict,
List,
Optional,
Tuple,
Union,
)

Expand Down Expand Up @@ -46,37 +45,14 @@ async def add_repos_to_product(
product: models.Product,
owner: models.User,
platforms: List[models.Platform],
ignore_errors: bool = False,
):
repos_to_insert = []
repo_tasks = []

async def _create_repo(
pulp_client: PulpClient,
product_name: str,
owner_name: str,
platform_name: str,
arch: str,
is_debug: bool,
) -> Tuple[str, str, str, str, str, bool]:
try:
return await create_product_repo(
pulp_client=pulp_client,
product_name=product_name,
ownername=owner_name,
platform_name=platform_name,
arch=arch,
is_debug=is_debug,
)
except Exception:
if ignore_errors:
return '', '', '', '', '', False
raise

for platform in platforms:
platform_name = platform.name.lower()
repo_tasks.extend((
_create_repo(
create_product_repo(
pulp_client,
product.name,
owner.username,
Expand All @@ -88,7 +64,7 @@ async def _create_repo(
for is_debug in (True, False)
))
repo_tasks.append(
_create_repo(
create_product_repo(
pulp_client,
product.name,
owner.username,
Expand All @@ -107,8 +83,6 @@ async def _create_repo(
export_path,
is_debug,
) in task_results:
if not any((repo_name, repo_url, pulp_href)):
continue
repo = models.Repository(
name=repo_name,
url=repo_url,
Expand Down Expand Up @@ -424,7 +398,6 @@ async def add_platform_to_product(
product_id: int,
platforms: List[Platform],
user_id: int,
ignore_errors: bool = False,
):
pulp_client = PulpClient(
settings.pulp_host,
Expand All @@ -435,6 +408,8 @@ async def add_platform_to_product(
db_user = await get_user(session, user_id=user_id)
if not db_user:
raise DataNotFoundError(f"User={user_id} doesn't exist")
if not db_product:
raise DataNotFoundError(f"Product={product_id} doesn't exist")
if not can_perform(db_product, db_user, actions.UpdateProduct.name):
raise PermissionDenied(
f'User has no permissions to modify the product "{db_product.name}"'
Expand All @@ -458,10 +433,17 @@ async def add_platform_to_product(
owner=db_product.owner,
product=db_product,
platforms=db_platforms,
ignore_errors=ignore_errors,
)
db_product.repositories.extend(repos)
session.add_all(repos)
existing_repo_names = {repo.name for repo in db_product.repositories}
new_repos = [
repo
for repo in repos
if repo.name not in existing_repo_names
]
if not new_repos:
return
db_product.repositories.extend(new_repos)
session.add_all(new_repos)


async def get_repo_product(
Expand Down
2 changes: 0 additions & 2 deletions alws/routers/products.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ async def create_gen_key_task(
async def add_platforms(
product_id: int,
platforms: List[product_schema.Platform],
ignore_errors: bool = False,
session: AsyncSession = Depends(AsyncSessionDependency(key=get_async_db_key())),
user: User = Depends(get_current_user),
):
Expand All @@ -197,7 +196,6 @@ async def add_platforms(
product_id=product_id,
platforms=platforms,
user_id=user.id,
ignore_errors=ignore_errors,
)
except Exception as exc:
raise HTTPException(
Expand Down
18 changes: 15 additions & 3 deletions alws/utils/copr.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,27 @@ async def create_product_repo(
repo_name = (
f'{ownername}-{product_name}-{platform_name}-{arch}{debug_suffix}-dr'
)
export_path = (
f"{product_name}/{platform_name}/{'debug/' if is_debug else ''}{arch}/"
)
pulp_repo = await pulp_client.get_rpm_repository(repo_name)
if pulp_repo:
repo_href = pulp_repo['pulp_href']
distro = await pulp_client.get_rpm_distro(f'{repo_name}-distro')
distro_url = distro['base_url'] if distro else ''
if not distro_url:
distro_url = await pulp_client.create_rpm_distro(
name=repo_name,
repository=repo_href,
base_path_start='copr',
)
return repo_name, distro_url, arch, repo_href, export_path, is_debug
repo_url, repo_href = await pulp_client.create_rpm_repository(
repo_name,
auto_publish=False,
create_publication=True,
base_path_start='copr',
)
export_path = (
f"{product_name}/{platform_name}/{'debug/' if is_debug else ''}{arch}/"
)
return repo_name, repo_url, arch, repo_href, export_path, is_debug


Expand Down
6 changes: 5 additions & 1 deletion tests/fixtures/products.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ def user_product_create_payload(request) -> dict:

@pytest.fixture
async def base_product(
async_session: AsyncSession, product_create_payload: dict, create_repo
async_session: AsyncSession,
product_create_payload: dict,
get_rpm_repository,
create_repo,
) -> AsyncIterable[Product]:
product = (
(
Expand Down Expand Up @@ -151,6 +154,7 @@ async def user_product(
user_product_create_payload: dict,
create_repo,
create_file_repository,
get_rpm_repository,
) -> AsyncIterable[Product]:
product = (
(
Expand Down
7 changes: 7 additions & 0 deletions tests/fixtures/pulp.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,13 @@ async def func(*args, **kwargs):
monkeypatch.setattr(PulpClient, "get_rpm_repository_by_params", func)


@pytest.fixture
def get_rpm_repository(monkeypatch):
async def func(*args, **kwargs):
return None
monkeypatch.setattr(PulpClient, "get_rpm_repository", func)


@pytest.fixture
def create_log_repo(monkeypatch):
async def func(*args, **kwargs):
Expand Down
2 changes: 2 additions & 0 deletions tests/test_api/test_products.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ async def test_product_create(
self,
product_create_payload,
create_file_repository,
get_rpm_repository,
):
response = await self.make_request(
"post",
Expand All @@ -33,6 +34,7 @@ async def test_add_platfroms_to_product(
self,
user_product: Product,
add_platfroms_to_product_payload,
get_rpm_repository,
):
endpoint = f"/api/v1/products/{user_product.id}/add-platforms/"
response = await self.make_request(
Expand Down
Loading