Skip to content

Commit

Permalink
Convert iscsi.global.* to new API (#15429)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmeagherix authored Jan 18, 2025
1 parent 30d28e3 commit 7a96892
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 34 deletions.
1 change: 1 addition & 0 deletions src/middlewared/middlewared/api/v25_04_0/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from .initshutdownscript import * # noqa
from .iscsi_auth import * # noqa
from .iscsi_extent import * # noqa
from .iscsi_global import * # noqa
from .iscsi_initiator import * # noqa
from .iscsi_portal import * # noqa
from .iscsi_target import * # noqa
Expand Down
77 changes: 77 additions & 0 deletions src/middlewared/middlewared/api/v25_04_0/iscsi_global.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
from pydantic import Field

from middlewared.api.base import BaseModel, Excluded, excluded_field, ForUpdateMetaclass, single_argument_args
from .common import QueryFilters, QueryOptions

__all__ = [
"IscsiGlobalEntry",
"IscsiGlobalUpdateArgs",
"IscsiGlobalUpdateResult",
"IscsiGlobalAluaEnabledArgs",
"IscsiGlobalAluaEnabledResult",
"IscsiGlobalClientCountArgs",
"IscsiGlobalClientCountResult",
"IscsiGlobalSessionsArgs",
"IscsiGlobalSessionsResult"
]


class IscsiGlobalEntry(BaseModel):
id: int
basename: str
isns_servers: list[str]
listen_port: int = Field(ge=1025, le=65535, default=3260)
pool_avail_threshold: int | None = Field(ge=1, le=99, default=None)
alua: bool


@single_argument_args('iscsi_update')
class IscsiGlobalUpdateArgs(IscsiGlobalEntry, metaclass=ForUpdateMetaclass):
id: Excluded = excluded_field()


class IscsiGlobalUpdateResult(BaseModel):
result: IscsiGlobalEntry


class IscsiGlobalAluaEnabledArgs(BaseModel):
pass


class IscsiGlobalAluaEnabledResult(BaseModel):
result: bool


class IscsiGlobalClientCountArgs(BaseModel):
pass


class IscsiGlobalClientCountResult(BaseModel):
result: int


class IscsiSession(BaseModel):
initiator: str
initiator_addr: str
initiator_alias: str | None
target: str
target_alias: str
header_digest: str | None
data_digest: str | None
max_data_segment_length: int | None
max_receive_data_segment_length: int | None
max_xmit_data_segment_length: int | None
max_burst_length: int | None
first_burst_length: int | None
immediate_data: bool
iser: bool
offload: bool


class IscsiGlobalSessionsArgs(BaseModel):
query_filters: QueryFilters = []
query_options: QueryOptions = QueryOptions()


class IscsiGlobalSessionsResult(BaseModel):
result: list[IscsiSession]
28 changes: 8 additions & 20 deletions src/middlewared/middlewared/plugins/iscsi_/global_linux.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import glob
import os

from middlewared.schema import Bool, Dict, Int, Str
from middlewared.service import filterable, filterable_returns, private, Service
from middlewared.api import api_method
from middlewared.api.current import IscsiGlobalSessionsArgs, IscsiGlobalSessionsResult
from middlewared.service import private, Service
from middlewared.service_exception import MatchNotFound
from middlewared.utils import filter_list, run

Expand All @@ -12,24 +13,11 @@ class ISCSIGlobalService(Service):
class Config:
namespace = 'iscsi.global'

@filterable(roles=['SHARING_ISCSI_GLOBAL_READ'])
@filterable_returns(Dict(
'session',
Str('initiator'),
Str('initiator_addr'),
Str('initiator_alias', null=True),
Str('target'),
Str('target_alias'),
Str('header_digest', null=True),
Str('data_digest', null=True),
Int('max_data_segment_length', null=True),
Int('max_receive_data_segment_length', null=True),
Int('max_burst_length', null=True),
Int('first_burst_length', null=True),
Bool('immediate_data'),
Bool('iser'),
Bool('offload'),
))
@api_method(
IscsiGlobalSessionsArgs,
IscsiGlobalSessionsResult,
roles=['SHARING_ISCSI_GLOBAL_READ']
)
def sessions(self, filters, options):
"""
Get a list of currently running iSCSI sessions. This includes initiator and target names
Expand Down
27 changes: 15 additions & 12 deletions src/middlewared/middlewared/plugins/iscsi_/iscsi_global.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import socket

import middlewared.sqlalchemy as sa
from middlewared.api import api_method
from middlewared.api.current import (IscsiGlobalAluaEnabledArgs, IscsiGlobalAluaEnabledResult, IscsiGlobalEntry,
IscsiGlobalUpdateArgs, IscsiGlobalUpdateResult)
from middlewared.async_validators import validate_port
from middlewared.schema import Bool, Dict, Int, List, Str, accepts
from middlewared.service import SystemServiceService, ValidationErrors, private
from middlewared.utils import run
from middlewared.validators import IpAddress, Port, Range
from middlewared.validators import IpAddress, Port

RE_IP_PORT = re.compile(r'^(.+?)(:[0-9]+)?$')

Expand All @@ -33,6 +35,7 @@ class Config:
namespace = 'iscsi.global'
cli_namespace = 'sharing.iscsi.global'
role_prefix = 'SHARING_ISCSI_GLOBAL'
entry = IscsiGlobalEntry

@private
def port_is_listening(self, host, port, timeout=5):
Expand Down Expand Up @@ -98,15 +101,11 @@ def config_extend(self, data):
data['isns_servers'] = data['isns_servers'].split()
return data

@accepts(Dict(
'iscsiglobal_update',
Str('basename'),
List('isns_servers', items=[Str('server')]),
Int('listen_port', validators=[Range(min_=1025, max_=65535)], default=3260),
Int('pool_avail_threshold', validators=[Range(min_=1, max_=99)], null=True),
Bool('alua'),
update=True
), audit='Update iSCSI')
@api_method(
IscsiGlobalUpdateArgs,
IscsiGlobalUpdateResult,
audit='Update iSCSI'
)
async def do_update(self, data):
"""
`alua` is a no-op for FreeNAS.
Expand Down Expand Up @@ -183,7 +182,11 @@ async def stop_active_isns(self):
if cp.returncode:
self.logger.warning('Failed to stop active iSNS: %s', cp.stderr.decode())

@accepts(roles=['SHARING_ISCSI_GLOBAL_READ'])
@api_method(
IscsiGlobalAluaEnabledArgs,
IscsiGlobalAluaEnabledResult,
roles=['SHARING_ISCSI_GLOBAL_READ']
)
async def alua_enabled(self):
"""
Returns whether iSCSI ALUA is enabled or not.
Expand Down
10 changes: 8 additions & 2 deletions src/middlewared/middlewared/plugins/iscsi_/status.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from middlewared.service import accepts, Service
from middlewared.api import api_method
from middlewared.api.current import IscsiGlobalClientCountArgs, IscsiGlobalClientCountResult
from middlewared.service import Service


class ISCSIGlobalService(Service):
Expand All @@ -7,7 +9,11 @@ class Config:
namespace = 'iscsi.global'
cli_namespace = 'sharing.iscsi.global'

@accepts(roles=['SHARING_ISCSI_GLOBAL_READ'])
@api_method(
IscsiGlobalClientCountArgs,
IscsiGlobalClientCountResult,
roles=['SHARING_ISCSI_GLOBAL_READ']
)
async def client_count(self):
"""
Return currently connected clients count.
Expand Down

0 comments on commit 7a96892

Please sign in to comment.