Skip to content

Commit

Permalink
Move common remote options to generic
Browse files Browse the repository at this point in the history
[noissue]
  • Loading branch information
mdellweg committed Apr 15, 2021
1 parent 4a6f11f commit 0158b85
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 290 deletions.
29 changes: 3 additions & 26 deletions pulpcore/cli/ansible/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,14 @@
from pulpcore.cli.common.context import (
EntityDefinition,
PulpEntityContext,
PulpRemoteContext,
PulpRepositoryContext,
PulpRepositoryVersionContext,
)

_ = gettext.gettext

# TODO Add Role and Collection Content contexts
remote_nullables = [
"ca_cert",
"client_cert",
"client_key",
"password",
"proxy_url",
"username",
]


class PulpAnsibleDistributionContext(PulpEntityContext):
Expand All @@ -38,7 +31,7 @@ def preprocess_body(self, body: EntityDefinition) -> EntityDefinition:
return body


class PulpAnsibleRoleRemoteContext(PulpEntityContext):
class PulpAnsibleRoleRemoteContext(PulpRemoteContext):
ENTITY = "role remote"
HREF = "ansible_role_remote_href"
LIST_ID = "remotes_ansible_role_list"
Expand All @@ -47,15 +40,8 @@ class PulpAnsibleRoleRemoteContext(PulpEntityContext):
UPDATE_ID = "remotes_ansible_role_partial_update"
DELETE_ID = "remotes_ansible_role_delete"

def preprocess_body(self, body: EntityDefinition) -> EntityDefinition:
body = super().preprocess_body(body)
for nullable in remote_nullables:
if body.get(nullable) == "":
body[nullable] = None
return body


class PulpAnsibleCollectionRemoteContext(PulpEntityContext):
class PulpAnsibleCollectionRemoteContext(PulpRemoteContext):
ENTITY = "collection remote"
HREF = "ansible_collection_remote_href"
LIST_ID = "remotes_ansible_collection_list"
Expand All @@ -69,9 +55,6 @@ def preprocess_body(self, body: EntityDefinition) -> EntityDefinition:
body = super().preprocess_body(body)
if "requirements" in body.keys():
body["requirements_file"] = body.pop("requirements")
for nullable in remote_nullables + self.collection_nullable:
if body.get(nullable) == "":
body[nullable] = None
return body


Expand All @@ -94,9 +77,3 @@ class PulpAnsibleRepositoryContext(PulpRepositoryContext):
SYNC_ID = "repositories_ansible_ansible_sync"
MODIFY_ID = "repositories_ansible_ansible_modify"
VERSION_CONTEXT = PulpAnsibleRepositoryVersionContext

def preprocess_body(self, body: EntityDefinition) -> EntityDefinition:
body = super().preprocess_body(body)
if body.get("description") == "":
body["description"] = None
return body
44 changes: 5 additions & 39 deletions pulpcore/cli/ansible/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
)
from pulpcore.cli.common.context import PulpContext, pass_pulp_context
from pulpcore.cli.common.generic import (
common_remote_create_options,
common_remote_update_options,
create_command,
destroy_command,
href_option,
Expand Down Expand Up @@ -59,38 +61,7 @@ def remote(ctx: click.Context, pulp_ctx: PulpContext, remote_type: str) -> None:

lookup_options = [href_option, name_option]
remote_options = [
click.option("--ca-cert", help=_("a PEM encoded CA certificate")),
click.option("--client-cert", help=_("a PEM encoded client certificate")),
click.option("--client-key", help=_("a PEM encode private key")),
click.option("--tls-validation", help=_("if True, TLS peer validation must be performed")),
click.option("--proxy-url", help=_("format: scheme//user:password@host:port")),
click.option("--username", help=_("used for authentication when syncing")),
click.option("--password"),
click.option(
"--download-concurrency", type=int, help=_("total number of simultaneous connections")
),
click.option("--policy", help=_("policy to use when downloading")),
click.option(
"--total-timeout",
type=float,
help=_("aiohttp.ClientTimeout.total for download connections"),
),
click.option(
"--connect-timeout",
type=float,
help=_("aiohttp.ClientTimeout.connect for download connections"),
),
click.option(
"--sock-connect-timeout",
type=float,
help=_("aiohttp.ClientTimeout.sock_connect for download connections"),
),
click.option(
"--sock-read-timeout",
type=float,
help=_("aiohttp.ClientTimeout.sock_read for download connections"),
),
click.option("--rate-limit", type=int, help=_("limit download rate in requests per second")),
]
collection_options = [
click.option(
Expand All @@ -116,17 +87,12 @@ def remote(ctx: click.Context, pulp_ctx: PulpContext, remote_type: str) -> None:
),
]
ansible_remote_options = remote_options + collection_options
create_options = [
click.option("--name", required=True),
click.option("--url", required=True),
] + ansible_remote_options
update_options = (
lookup_options + [click.option("--url", help=_("new url"))] + ansible_remote_options
)
create_options = common_remote_create_options + remote_options + ansible_remote_options
update_options = common_remote_update_options + remote_options + ansible_remote_options

remote.add_command(list_command(decorators=[label_select_option]))
remote.add_command(show_command(decorators=lookup_options))
remote.add_command(destroy_command(decorators=lookup_options))
remote.add_command(create_command(decorators=create_options))
remote.add_command(update_command(decorators=update_options))
remote.add_command(update_command(decorators=lookup_options + update_options))
remote.add_command(label_command())
22 changes: 20 additions & 2 deletions pulpcore/cli/common/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import json
import sys
import time
from typing import IO, Any, ClassVar, Dict, List, Optional, Tuple, Type, Union
from typing import IO, Any, ClassVar, Dict, List, Optional, Set, Tuple, Type, Union

import click
import yaml
Expand Down Expand Up @@ -232,7 +232,9 @@ class PulpEntityContext:
CREATE_ID: ClassVar[str]
UPDATE_ID: ClassVar[str]
DELETE_ID: ClassVar[str]
NULLABLES: ClassVar[Set[str]] = set()

# Hidden values for the lazy entity lookup
_entity: Optional[EntityDefinition]
_entity_lookup: EntityDefinition

Expand Down Expand Up @@ -317,9 +319,16 @@ def __init__(
else:
self.pulp_href = pulp_href

def _preprocess_value(self, key: str, value: Any) -> Any:
if key in self.NULLABLES and value == "":
return None
if isinstance(value, PulpEntityContext):
return value.pulp_href
return value

def preprocess_body(self, body: EntityDefinition) -> EntityDefinition:
return {
key: value.pulp_href if isinstance(value, PulpEntityContext) else value
key: self._preprocess_value(key, value)
for key, value in body.items()
if value is not None
}
Expand Down Expand Up @@ -459,6 +468,14 @@ class PulpRemoteContext(PulpEntityContext):

ENTITY = "remote"
ENTITIES = "remotes"
NULLABLES = {
"ca_cert",
"client_cert",
"client_key",
"password",
"proxy_url",
"username",
}

def preprocess_body(self, body: EntityDefinition) -> EntityDefinition:
body = super().preprocess_body(body)
Expand Down Expand Up @@ -525,6 +542,7 @@ class PulpRepositoryContext(PulpEntityContext):
SYNC_ID: ClassVar[str]
MODIFY_ID: ClassVar[str]
VERSION_CONTEXT: ClassVar[Type[PulpRepositoryVersionContext]]
NULLABLES = {"description"}

def get_version_context(self) -> PulpRepositoryVersionContext:
return self.VERSION_CONTEXT(self.pulp_ctx, self)
Expand Down
43 changes: 43 additions & 0 deletions pulpcore/cli/common/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,49 @@ def load_json_callback(
cls=PulpOption,
)

common_remote_create_options = [
click.option("--name", required=True),
click.option("--url", required=True),
click.option("--ca-cert", help=_("a PEM encoded CA certificate")),
click.option("--client-cert", help=_("a PEM encoded client certificate")),
click.option("--client-key", help=_("a PEM encode private key")),
click.option("--connect-timeout", type=float),
click.option(
"--download-concurrency", type=int, help=_("total number of simultaneous connections")
),
click.option("--password"),
click.option("--proxy-url"),
click.option("--proxy-username"),
click.option("--proxy-password"),
click.option("--rate-limit", type=int, help=_("limit download rate in requests per second")),
click.option("--sock-connect-timeout", type=float),
click.option("--sock-read-timeout", type=float),
click.option("--tls-validation", type=bool),
click.option("--total-timeout", type=float),
click.option("--username"),
]

common_remote_update_options = [
click.option("--url"),
click.option("--ca-cert", help=_("a PEM encoded CA certificate")),
click.option("--client-cert", help=_("a PEM encoded client certificate")),
click.option("--client-key", help=_("a PEM encode private key")),
click.option("--connect-timeout", type=float),
click.option(
"--download-concurrency", type=int, help=_("total number of simultaneous connections")
),
click.option("--password"),
click.option("--proxy-url"),
click.option("--proxy-username"),
click.option("--proxy-password"),
click.option("--rate-limit", type=int, help=_("limit download rate in requests per second")),
click.option("--sock-connect-timeout", type=float),
click.option("--sock-read-timeout", type=float),
click.option("--tls-validation", type=bool),
click.option("--total-timeout", type=float),
click.option("--username"),
]

##############################################################################
# Generic reusable commands

Expand Down
29 changes: 2 additions & 27 deletions pulpcore/cli/container/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pulpcore.cli.common.context import (
EntityDefinition,
PulpEntityContext,
PulpRemoteContext,
PulpRepositoryContext,
PulpRepositoryVersionContext,
)
Expand Down Expand Up @@ -55,7 +56,7 @@ def preprocess_body(self, body: EntityDefinition) -> EntityDefinition:
return body


class PulpContainerRemoteContext(PulpEntityContext):
class PulpContainerRemoteContext(PulpRemoteContext):
ENTITY = "container remote"
ENTITIES = "container remotes"
HREF = "container_container_remote_href"
Expand All @@ -64,20 +65,6 @@ class PulpContainerRemoteContext(PulpEntityContext):
UPDATE_ID = "remotes_container_container_partial_update"
DELETE_ID = "remotes_container_container_delete"

def preprocess_body(self, body: EntityDefinition) -> EntityDefinition:
body = super().preprocess_body(body)
for nullable in [
"ca_cert",
"client_cert",
"client_key",
"password",
"proxy_url",
"username",
]:
if body.get(nullable) == "":
body[nullable] = None
return body


class PulpContainerRepositoryVersionContext(PulpRepositoryVersionContext):
HREF = "container_container_repository_version_href"
Expand Down Expand Up @@ -105,12 +92,6 @@ class PulpContainerRepositoryContext(PulpRepositoryContext):
SYNC_ID = "repositories_container_container_sync"
VERSION_CONTEXT = PulpContainerRepositoryVersionContext

def preprocess_body(self, body: EntityDefinition) -> EntityDefinition:
body = super().preprocess_body(body)
if body.get("description") == "":
body["description"] = None
return body


class PulpContainerPushRepositoryContext(PulpRepositoryContext):
HREF = "container_container_push_repository_href"
Expand All @@ -123,9 +104,3 @@ class PulpContainerPushRepositoryContext(PulpRepositoryContext):
# TODO Incorporate into capabilities
# SYNC_ID = "repositories_container_container_push_sync"
VERSION_CONTEXT = PulpContainerPushRepositoryVersionContext

def preprocess_body(self, body: EntityDefinition) -> EntityDefinition:
body = super().preprocess_body(body)
if body.get("description") == "":
body["description"] = None
return body
48 changes: 7 additions & 41 deletions pulpcore/cli/container/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

from pulpcore.cli.common.context import PulpContext, pass_pulp_context
from pulpcore.cli.common.generic import (
common_remote_create_options,
common_remote_update_options,
create_command,
destroy_command,
href_option,
Expand Down Expand Up @@ -37,51 +39,15 @@ def remote(ctx: click.Context, pulp_ctx: PulpContext, remote_type: str) -> None:


lookup_options = [href_option, name_option]
create_options = [
click.option("--name", required=True),
click.option("--url", required=True),
click.option("--ca-cert"),
click.option("--client-cert"),
click.option("--client-key"),
click.option("--connect-timeout", type=float),
click.option("--download-concurrency", type=int),
click.option("--password"),
click.option(
"--policy", type=click.Choice(["immediate", "on_demand", "streamed"], case_sensitive=False)
),
click.option("--proxy-url"),
click.option("--sock-connect-timeout", type=float),
click.option("--sock-read-timeout", type=float),
click.option("--tls-validation", type=bool),
click.option("--total-timeout", type=float),
click.option("--username"),
# Container specific
remote_options = [
click.option("--upstream-name", required=True),
]
update_options = [
click.option("--url"),
click.option("--ca-cert"),
click.option("--client-cert"),
click.option("--client-key"),
click.option("--connect-timeout", type=float),
click.option("--download-concurrency", type=int),
click.option("--password"),
click.option(
"--policy", type=click.Choice(["immediate", "on_demand", "streamed"], case_sensitive=False)
),
click.option("--proxy-url"),
click.option("--sock-connect-timeout", type=float),
click.option("--sock-read-timeout", type=float),
click.option("--tls-validation", type=bool),
click.option("--total-timeout", type=float),
click.option("--username"),
# Container specific
click.option("--upstream-name"),
]

remote.add_command(list_command(decorators=[label_select_option]))
remote.add_command(show_command(decorators=lookup_options))
remote.add_command(create_command(decorators=create_options))
remote.add_command(update_command(decorators=lookup_options + update_options))
remote.add_command(create_command(decorators=common_remote_create_options + remote_options))
remote.add_command(
update_command(decorators=lookup_options + common_remote_update_options + remote_options)
)
remote.add_command(destroy_command(decorators=lookup_options))
remote.add_command(label_command())
Loading

0 comments on commit 0158b85

Please sign in to comment.