Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backend: add support for pulp domains #3311

Merged
merged 1 commit into from
Jul 4, 2024
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
32 changes: 27 additions & 5 deletions backend/copr_backend/pulp.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import os
import tomllib
import requests
from six.moves.urllib.parse import urlencode


class PulpClient:
Expand Down Expand Up @@ -53,7 +52,20 @@ def url(self, endpoint):
"""
A fully qualified URL for a given API endpoint
"""
return self.config["base_url"] + self.config["api_root"] + endpoint
domain = self.config["domain"]
if domain == "default":
domain = ""

relative = os.path.normpath("/".join([
self.config["api_root"],
domain,
endpoint,
]))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we use os.path for urls already (sub-optimal), don't you want to use os.path.join()?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem with os.path.join is that it does something like this:

>>> os.path.join("/foo/", "/bar/", "/baz/")
'/baz/'

while the "/".join variant produces something more expected

ipdb> os.path.normpath("/".join(["/foo/", "/bar/", "/baz/"]))
'/foo/bar/baz'

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, OK


# Normpath removes the trailing slash. If it was there, put it back
if endpoint[-1] == "/":
relative += "/"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

os.path.join() preserves trailing slashes

return self.config["base_url"] + relative

@property
def request_params(self):
Expand All @@ -79,7 +91,7 @@ def get_repository(self, name):
# There is no endpoint for querying a single repository by its name,
# even Pulp CLI does this workaround
url = self.url("api/v3/repositories/rpm/rpm/?")
url += urlencode({"name": name, "offset": 0, "limit": 1})
url += self._urlencode({"name": name, "offset": 0, "limit": 1})
return requests.get(url, **self.request_params)

def get_distribution(self, name):
Expand All @@ -90,9 +102,19 @@ def get_distribution(self, name):
# There is no endpoint for querying a single repository by its name,
# even Pulp CLI does this workaround
url = self.url("api/v3/distributions/rpm/rpm/?")
url += urlencode({"name": name, "offset": 0, "limit": 1})
url += self._urlencode({"name": name, "offset": 0, "limit": 1})
return requests.get(url, **self.request_params)

def _urlencode(self, query):
"""
Join a dict into URL query string but don't encode special characters
https://docs.python.org/3/library/urllib.parse.html#urllib.parse.urlencode
Our repository names are e.g. frostyx/test-pulp/fedora-39-x86_64.
The standard urlencode would change the slashes to %2F making Pulp to
not find the project when filtering by name.
"""
return "&".join([f"{k}={v}" for k, v in query.items()])

def create_distribution(self, name, repository, basepath=None):
"""
Create an RPM distribution
Expand All @@ -111,7 +133,7 @@ def create_publication(self, repository):
Create an RPM publication
https://docs.pulpproject.org/pulp_rpm/restapi.html#tag/Publications:-Rpm/operation/publications_rpm_rpm_create
"""
url = self.url("api/v3/publications/rpm/rpm")
url = self.url("api/v3/publications/rpm/rpm/")
data = {"repository": repository}
return requests.post(url, json=data, **self.request_params)

Expand Down
39 changes: 39 additions & 0 deletions backend/tests/test_pulp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""
Test Pulp client
"""

# pylint: disable=attribute-defined-outside-init

from copr_backend.pulp import PulpClient


class TestPulp:

def setup_method(self, _method):
self.config = {
Fixed Show fixed Hide fixed
"api_root": "/pulp/",
"base_url": "http://pulp.fpo:24817",
"cert": "",
"domain": "default",
"dry_run": False,
"format": "json",
"key": "",
"password": "1234",
"timeout": 0,
"username": "admin",
"verbose": 0,
"verify_ssl": True,
}

def test_url(self):
client = PulpClient(self.config)
assert self.config["domain"] == "default"
assert client.url("api/v3/artifacts/")\
== "http://pulp.fpo:24817/pulp/api/v3/artifacts/"

assert client.url("api/v3/repositories/rpm/rpm/?")\
== "http://pulp.fpo:24817/pulp/api/v3/repositories/rpm/rpm/?"

self.config["domain"] = "copr"
assert client.url("api/v3/artifacts/")\
== "http://pulp.fpo:24817/pulp/copr/api/v3/artifacts/"