Skip to content

Commit

Permalink
Do not version private API methods
Browse files Browse the repository at this point in the history
  • Loading branch information
themylogin committed Feb 4, 2025
1 parent a28fdc6 commit b2db0cb
Show file tree
Hide file tree
Showing 42 changed files with 299 additions and 530 deletions.
27 changes: 27 additions & 0 deletions src/middlewared/middlewared/api/base/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ def api_method(
if list(returns.model_fields.keys()) != ["result"]:
raise TypeError("`returns` model must only have one field called `result`")

check_model_module(accepts, private)
check_model_module(returns, private)

def wrapper(func):
if pass_app:
# Pass the application instance as parameter to the method
Expand Down Expand Up @@ -129,3 +132,27 @@ def wrapped(*args):
return wrapped

return wrapper


def check_model_module(model: type[BaseModel], private: bool):
module_name = model.__module__

if module_name in ["middlewared.service.crud_service"]:
return

if private:
if model.__name__ == "QueryArgs" or model.__name__.endswith("QueryResult"):
# `filterable_api_method`
return

if module_name.startswith("middlewared.api."):
raise ValueError(
"Private methods must have their accepts/returns models defined in the corresponding plugin class. "
f"{model.__name__} is defined in {module_name}."
)
else:
if not module_name.startswith("middlewared.api."):
raise ValueError(
"Public methods must have their accepts/returns models defined in middlewared.api package. "
f"{model.__name__} is defined in {module_name}."
)
2 changes: 1 addition & 1 deletion src/middlewared/middlewared/api/base/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def single_argument_result(klass, klass_name=None):
model = create_model(
klass_name,
__base__=(BaseModel,),
__module__=inspect.getmodule(inspect.stack()[1][0]),
__module__=inspect.getmodule(inspect.stack()[1][0]).__name__,
result=typing.Annotated[klass, Field()],
)
if issubclass(klass, BaseModel):
Expand Down
2 changes: 0 additions & 2 deletions src/middlewared/middlewared/api/v25_04_0/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
from .iscsi_target_to_extent import * # noqa
from .keychain import * # noqa
from .k8s_to_docker import * # noqa
from .netdata import * # noqa
from .nfs import * # noqa
from .pool import * # noqa
from .pool_dataset_details import * # noqa
Expand All @@ -67,7 +66,6 @@
from .tn_connect import * # noqa
from .truenas import * # noqa
from .user import * # noqa
from .vendor import * # noqa
from .virt_device import * # noqa
from .virt_global import * # noqa
from .virt_instance import * # noqa
Expand Down
42 changes: 1 addition & 41 deletions src/middlewared/middlewared/api/v25_04_0/acl.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@
'AclTemplateCreateArgs', 'AclTemplateCreateResult',
'AclTemplateUpdateArgs', 'AclTemplateUpdateResult',
'AclTemplateDeleteArgs', 'AclTemplateDeleteResult',
'FilesystemAddToAclArgs', 'FilesystemAddToAclResult',
'FilesystemGetAclArgs', 'FilesystemGetAclResult',
'FilesystemSetAclArgs', 'FilesystemSetAclResult',
'FilesystemGetInheritedAclArgs', 'FilesystemGetInheritedAclResult'
'NFS4ACE', 'POSIXACE',
]

ACL_MAX_ID = 2 ** 32 // 2 - 1
Expand Down Expand Up @@ -331,42 +330,3 @@ class AclTemplateByPathArgs(BaseModel):

class AclTemplateByPathResult(BaseModel):
result: list[AclTemplateEntry]


class SimplifiedAclEntry(BaseModel):
id_type: Literal[NFS4ACE_Tag.USER, NFS4ACE_Tag.GROUP]
id: int
access: Literal[
NFS4ACE_MaskSimple.READ,
NFS4ACE_MaskSimple.MODIFY,
NFS4ACE_MaskSimple.FULL_CONTROL
]


class FilesystemAddToAclOptions(BaseModel):
force: bool = False


@single_argument_args('add_to_acl')
class FilesystemAddToAclArgs(BaseModel):
path: NonEmptyString
entries: list[SimplifiedAclEntry]
options: FilesystemAddToAclOptions = Field(default=FilesystemAddToAclOptions())


class FilesystemAddToAclResult(BaseModel):
result: bool


class FSGetInheritedAclOptions(BaseModel):
directory: bool = True


@single_argument_args('calculate_inherited_acl')
class FilesystemGetInheritedAclArgs(BaseModel):
path: NonEmptyString
options: FSGetInheritedAclOptions = Field(default=FSGetInheritedAclOptions())


class FilesystemGetInheritedAclResult(BaseModel):
result: list[NFS4ACE] | list[POSIXACE]
19 changes: 3 additions & 16 deletions src/middlewared/middlewared/api/v25_04_0/acme_dns_authenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@
from pydantic import BeforeValidator, ConfigDict, Field, FilePath, PlainSerializer, Secret

from middlewared.api.base import (
BaseModel, single_argument_args, ForUpdateMetaclass, LongString, NonEmptyString,
BaseModel, single_argument_args, ForUpdateMetaclass, NonEmptyString,
)


__all__ = [
'ACMEDNSAuthenticatorEntry', 'ACMEDNSAuthenticatorCreateArgs', 'ACMEDNSAuthenticatorCreateResult',
'ACMEDNSAuthenticatorUpdateArgs', 'ACMEDNSAuthenticatorUpdateResult', 'ACMEDNSAuthenticatorDeleteArgs',
'ACMEDNSAuthenticatorDeleteResult', 'ACMEDNSAuthenticatorSchemasArgs', 'ACMEDNSAuthenticatorSchemasResult',
'ACMEDNSAuthenticatorPerformChallengeArgs', 'ACMEDNSAuthenticatorPerformChallengeResult', 'Route53SchemaArgs',
'ACMECustomDNSAuthenticatorReturns', 'CloudFlareSchemaArgs', 'DigitalOceanSchemaArgs', 'OVHSchemaArgs', 'ShellSchemaArgs',
'TrueNASConnectSchemaArgs',
'Route53SchemaArgs', 'ACMECustomDNSAuthenticatorReturns', 'CloudFlareSchemaArgs', 'DigitalOceanSchemaArgs',
'OVHSchemaArgs', 'ShellSchemaArgs', 'TrueNASConnectSchemaArgs',
]


Expand Down Expand Up @@ -151,18 +150,6 @@ class ACMEDNSAuthenticatorDeleteResult(BaseModel):
result: bool


@single_argument_args('acme_dns_authenticator_performance_challenge')
class ACMEDNSAuthenticatorPerformChallengeArgs(BaseModel):
authenticator: int | None
key: LongString
domain: str
challenge: LongString


class ACMEDNSAuthenticatorPerformChallengeResult(BaseModel):
result: None


class ACMEDNSAuthenticatorAttributeSchema(BaseModel):
_name_: str
title: str
Expand Down
21 changes: 1 addition & 20 deletions src/middlewared/middlewared/api/v25_04_0/alert.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
__all__ = [
'AlertDismissArgs', 'AlertListArgs', 'AlertDismissResult', 'AlertListResult', 'AlertListCategoriesArgs',
'AlertListCategoriesResult', 'AlertListPoliciesArgs', 'AlertListPoliciesResult', 'AlertRestoreArgs',
'AlertRestoreResult', 'AlertOneshotCreateArgs', 'AlertOneshotCreateResult', 'AlertOneshotDeleteArgs',
'AlertOneshotDeleteResult', 'AlertClassesEntry', 'AlertClassesUpdateArgs', 'AlertClassesUpdateResult', 'Alert',
'AlertRestoreResult', 'AlertClassesEntry', 'AlertClassesUpdateArgs', 'AlertClassesUpdateResult', 'Alert',
'AlertListAddedEvent', 'AlertListChangedEvent', 'AlertListRemovedEvent',
]

Expand Down Expand Up @@ -88,24 +87,6 @@ class AlertListPoliciesResult(BaseModel):
result: list[str]


class AlertOneshotCreateArgs(BaseModel):
klass: str
args: Any


class AlertOneshotCreateResult(BaseModel):
result: None


class AlertOneshotDeleteArgs(BaseModel):
klass: str | list[str]
query: Any = None


class AlertOneshotDeleteResult(BaseModel):
result: None


class AlertRestoreArgs(BaseModel):
uuid: str

Expand Down
26 changes: 0 additions & 26 deletions src/middlewared/middlewared/api/v25_04_0/cloud_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,6 @@ class CloudCredentialEntry(BaseModel):
name: NonEmptyString
provider: CloudCredentialProvider

@classmethod
def from_previous(cls, value):
attributes = value.pop("attributes")
return {
**value,
"provider": {
"type": value["provider"],
**attributes,
}
}

@classmethod
def to_previous(cls, value):
value["attributes"] = value["provider"]
value["provider"] = value["attributes"].pop("type")
return value


class CloudCredentialCreate(CloudCredentialEntry):
id: Excluded = excluded_field()
Expand Down Expand Up @@ -73,15 +56,6 @@ class CloudCredentialDeleteResult(BaseModel):
class CloudCredentialVerifyArgs(BaseModel):
cloud_sync_credentials_create: CloudCredentialProvider

@classmethod
def from_previous(cls, value):
return {
"cloud_sync_credentials_create": {
"type": value["cloud_sync_credentials_create"]["provider"],
**value["cloud_sync_credentials_create"]["attributes"]
}
}


@single_argument_result
class CloudCredentialVerifyResult(BaseModel):
Expand Down
19 changes: 0 additions & 19 deletions src/middlewared/middlewared/api/v25_04_0/filesystem.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from middlewared.api.base import (
BaseModel,
LongNonEmptyString,
NonEmptyString,
UnixPerm,
single_argument_args,
Expand Down Expand Up @@ -28,7 +27,6 @@
'FilesystemGetZfsAttrsArgs', 'FilesystemGetZfsAttrsResult',
'FilesystemGetFileArgs', 'FilesystemGetFileResult',
'FilesystemPutFileArgs', 'FilesystemPutFileResult',
'FilesystemReceiveFileArgs', 'FilesystemReceiveFileResult',
]


Expand Down Expand Up @@ -369,20 +367,3 @@ class FilesystemPutFileArgs(BaseModel):

class FilesystemPutFileResult(BaseModel):
result: Literal[True]


class FilesystemReceiveFileOptions(BaseModel):
append: bool = False
mode: int = None
uid: int = ACL_UNDEFINED_ID
gid: int = ACL_UNDEFINED_ID


class FilesystemReceiveFileArgs(BaseModel):
path: NonEmptyString
content: LongNonEmptyString
options: FilesystemReceiveFileOptions = FilesystemReceiveFileOptions()


class FilesystemReceiveFileResult(BaseModel):
result: Literal[True]
11 changes: 1 addition & 10 deletions src/middlewared/middlewared/api/v25_04_0/initshutdownscript.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
__all__ = [
"InitShutdownScriptEntry", "InitShutdownScriptCreateArgs", "InitShutdownScriptCreateResult",
"InitShutdownScriptUpdateArgs", "InitShutdownScriptUpdateResult", "InitShutdownScriptDeleteArgs",
"InitShutdownScriptDeleteResult", "InitShutdownScriptExecuteInitTasksArgs",
"InitShutdownScriptExecuteInitTasksResult"
"InitShutdownScriptDeleteResult",
]


Expand Down Expand Up @@ -68,11 +67,3 @@ class InitShutdownScriptDeleteArgs(BaseModel):
class InitShutdownScriptDeleteResult(BaseModel):
result: Literal[True]
"""Always return `True`.""" # FIXME: Should return False or raise exception if no record was deleted.


class InitShutdownScriptExecuteInitTasksArgs(BaseModel):
when: Literal["PREINIT", "POSTINIT", "SHUTDOWN"]


class InitShutdownScriptExecuteInitTasksResult(BaseModel):
result: None
10 changes: 0 additions & 10 deletions src/middlewared/middlewared/api/v25_04_0/iscsi_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
"IscsiTargetUpdateResult",
"IscsiTargetDeleteArgs",
"IscsiTargetDeleteResult",
"IscsiTargetRemoveArgs",
"IscsiTargetRemoveResult"
]


Expand Down Expand Up @@ -96,11 +94,3 @@ class IscsiTargetDeleteArgs(BaseModel):

class IscsiTargetDeleteResult(BaseModel):
result: Literal[True]


class IscsiTargetRemoveArgs(BaseModel):
name: str


class IscsiTargetRemoveResult(BaseModel):
result: None
26 changes: 1 addition & 25 deletions src/middlewared/middlewared/api/v25_04_0/keychain.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@
from middlewared.api.base import (BaseModel, Excluded, excluded_field, ForUpdateMetaclass, HttpUrl, LongString,
NonEmptyString, single_argument_args, single_argument_result)

__all__ = ["KeychainCredentialEntry",
__all__ = ["KeychainCredentialEntry", "SSHKeyPairEntry", "SSHCredentialsEntry",
"KeychainCredentialCreateArgs", "KeychainCredentialCreateResult",
"KeychainCredentialUpdateArgs", "KeychainCredentialUpdateResult",
"KeychainCredentialDeleteArgs", "KeychainCredentialDeleteResult",
"KeychainCredentialUsedByArgs", "KeychainCredentialUsedByResult",
"KeychainCredentialGetOfTypeArgs", "KeychainCredentialGetOfTypeResult",
"KeychainCredentialGenerateSSHKeyPairArgs", "KeychainCredentialGenerateSSHKeyPairResult",
"KeychainCredentialRemoteSSHHostKeyScanArgs", "KeychainCredentialRemoteSSHHostKeyScanResult",
"KeychainCredentialRemoteSSHSemiautomaticSetupArgs", "KeychainCredentialRemoteSSHSemiautomaticSetupResult",
"KeychainCredentialSSHPairArgs", "KeychainCredentialSSHPairResult",
"KeychainCredentialSetupSSHConnectionArgs", "KeychainCredentialSetupSSHConnectionResult"]


Expand Down Expand Up @@ -121,15 +119,6 @@ class KeychainCredentialUsedByResult(BaseModel):
result: list[UsedKeychainCredential]


class KeychainCredentialGetOfTypeArgs(BaseModel):
id: int
type: Literal["SSH_KEY_PAIR", "SSH_CREDENTIALS"]


class KeychainCredentialGetOfTypeResult(BaseModel):
result: SSHKeyPairEntry | SSHCredentialsEntry


class KeychainCredentialGenerateSSHKeyPairArgs(BaseModel):
pass

Expand Down Expand Up @@ -173,19 +162,6 @@ class KeychainCredentialRemoteSSHSemiautomaticSetupResult(BaseModel):
result: SSHCredentialsEntry


@single_argument_args("keychain_ssh_pair")
class KeychainCredentialSSHPairArgs(BaseModel):
remote_hostname: NonEmptyString
username: str = "root"
public_key: NonEmptyString


@single_argument_result
class KeychainCredentialSSHPairResult(BaseModel):
port: int
host_key: LongString


class KeychainCredentialSetupSSHConnectionKeyNew(BaseModel):
generate_key: Literal[True] = True
name: NonEmptyString
Expand Down
25 changes: 0 additions & 25 deletions src/middlewared/middlewared/api/v25_04_0/netdata.py

This file was deleted.

Loading

0 comments on commit b2db0cb

Please sign in to comment.