Skip to content

Commit

Permalink
Add ability to add new platforms to the product (#1008)
Browse files Browse the repository at this point in the history
  • Loading branch information
maccelf authored Oct 16, 2024
1 parent 8ffc847 commit 1ae8d04
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 49 deletions.
155 changes: 106 additions & 49 deletions alws/crud/products.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from alws.models import Build, Product, Repository, Team, UserRole
from alws.perms import actions
from alws.perms.authorization import can_perform
from alws.schemas.product_schema import ProductCreate
from alws.schemas.product_schema import ProductCreate, Platform
from alws.schemas.team_schema import TeamCreate
from alws.utils.copr import create_product_repo, create_product_sign_key_repo
from alws.utils.pulp_client import PulpClient
Expand All @@ -34,6 +34,63 @@
logger = logging.getLogger(__name__)


async def add_repos_to_product(
pulp_client: PulpClient,
product: models.Product,
owner: models.User,
platforms: List[models.Platform]
):
repos_to_insert = []
repo_tasks = []
for platform in platforms:
platform_name = platform.name.lower()
repo_tasks.extend((
create_product_repo(
pulp_client,
product.name,
owner.username,
platform_name,
arch,
is_debug,
)
for arch in platform.arch_list
for is_debug in (True, False)
))
repo_tasks.append(
create_product_repo(
pulp_client,
product.name,
owner.username,
platform_name,
'src',
False,
)
)
task_results = await asyncio.gather(*repo_tasks)

for (
repo_name,
repo_url,
arch,
pulp_href,
export_path,
is_debug,
) in task_results:
repo = models.Repository(
name=repo_name,
url=repo_url,
arch=arch,
pulp_href=pulp_href,
type=arch,
debug=is_debug,
production=True,
export_path=export_path,
)

repos_to_insert.append(repo)
return repos_to_insert


async def create_product(
db: AsyncSession,
payload: ProductCreate,
Expand All @@ -44,7 +101,6 @@ async def create_product(
settings.pulp_password,
)
items_to_insert = []
repo_tasks = []

owner = await get_user(db, user_id=payload.owner_id)
if not owner:
Expand Down Expand Up @@ -81,52 +137,15 @@ async def create_product(
.all()
)

for platform in product.platforms:
platform_name = platform.name.lower()
repo_tasks.extend((
create_product_repo(
pulp_client,
product.name,
owner.username,
platform_name,
arch,
is_debug,
)
for arch in platform.arch_list
for is_debug in (True, False)
))
repo_tasks.append(
create_product_repo(
pulp_client,
product.name,
owner.username,
platform_name,
'src',
False,
)
)
task_results = await asyncio.gather(*repo_tasks)
repos = await add_repos_to_product(
pulp_client=pulp_client,
product=product,
owner=owner,
platforms=product.platforms,
)
items_to_insert.extend(repos)
product.repositories.extend(repos)

for (
repo_name,
repo_url,
arch,
pulp_href,
export_path,
is_debug,
) in task_results:
repo = models.Repository(
name=repo_name,
url=repo_url,
arch=arch,
pulp_href=pulp_href,
type=arch,
debug=is_debug,
production=True,
export_path=export_path,
)
product.repositories.append(repo)
items_to_insert.append(repo)
# Create sign key repository if a product is community
if payload.is_community:
repo_name, repo_url, repo_href = await create_product_sign_key_repo(
Expand Down Expand Up @@ -367,8 +386,44 @@ async def modify_product(
perform_product_modification.send(db_build.id, db_product.id, modification)


async def add_platform_to_product(
session: AsyncSession,
product_id: int,
platforms: List[Platform],
):
pulp_client = PulpClient(
settings.pulp_host,
settings.pulp_user,
settings.pulp_password,
)
db_product = await get_products(session, product_id=product_id)
db_platforms = (
(
await session.execute(
select(models.Platform).where(
models.Platform.name.in_(
[platform.name for platform in platforms]
),
),
)
)
.scalars()
.all()
)
db_product.platforms.extend(db_platforms)
repos = await add_repos_to_product(
pulp_client=pulp_client,
owner=db_product.owner,
product=db_product,
platforms=db_platforms
)
db_product.repositories.extend(repos)
session.add_all(repos)


async def get_repo_product(
session: AsyncSession, repository: str
session: AsyncSession,
repository: str,
) -> Optional[Product]:
product_relationships = (
selectinload(Product.owner),
Expand All @@ -383,7 +438,9 @@ async def get_repo_product(
await session.execute(
select(Build)
.filter(
Build.repos.any(Repository.name.ilike(f'%{repository}'))
Build.repos.any(
Repository.name.ilike(f'%{repository}')
)
)
.options(
joinedload(Build.team)
Expand Down
18 changes: 18 additions & 0 deletions alws/routers/products.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,21 @@ async def create_gen_key_task(
'user_email': user.email,
'product_name': product.name,
}


@router.post(
'/{product_id}/add_platforms/',
status_code=201,
)
async def add_platforms(
product_id: int,
platforms: List[product_schema.Platform],
db: AsyncSession = Depends(AsyncSessionDependency(key=get_async_db_key())),
):
try:
await products.add_platform_to_product(db, product_id, platforms)
except Exception as exc:
raise HTTPException(
detail=str(exc),
status_code=status.HTTP_400_BAD_REQUEST,
)
20 changes: 20 additions & 0 deletions tests/fixtures/products.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,26 @@ def product_create_payload(request) -> dict:
}


@pytest.fixture
def add_platfroms_to_product_payload() -> list:
return [
{
"id": 1,
"name": "AlmaLinux-9",
"distr_type": "rhel",
"distr_version": "9",
"arch_list": [
"i686",
"x86_64",
"ppc64le",
"aarch64",
"s390x",
],
"modularity": {},
}
]


@pytest.fixture(
params=[
ADMIN_USER_ID,
Expand Down
14 changes: 14 additions & 0 deletions tests/test_api/test_products.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ async def test_product_create(
)
assert response.status_code == self.status_codes.HTTP_200_OK, message

async def test_add_platfroms_to_product(
self,
user_product: Product,
add_platfroms_to_product_payload,
):
endpoint = f"/api/v1/products/{user_product.id}/add_platforms/"
response = await self.make_request(
"post",
endpoint,
json=add_platfroms_to_product_payload,
)

assert response.status_code == self.status_codes.HTTP_201_CREATED

async def test_add_to_product(
self,
regular_build: Build,
Expand Down

0 comments on commit 1ae8d04

Please sign in to comment.