diff --git a/src/middlewared/middlewared/alembic/versions/25.04/2024-12-16_12-49_enclosure_label.py b/src/middlewared/middlewared/alembic/versions/25.04/2024-12-16_12-49_enclosure_label.py new file mode 100644 index 0000000000000..d6c9de2de0af8 --- /dev/null +++ b/src/middlewared/middlewared/alembic/versions/25.04/2024-12-16_12-49_enclosure_label.py @@ -0,0 +1,28 @@ +"""Rename `enclosure_label` table + +Revision ID: 19cdc9f2d2df +Revises: b44c092bfa30 +Create Date: 2024-12-16 12:49:19.950812+00:00 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '19cdc9f2d2df' +down_revision = 'b44c092bfa30' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.execute("ALTER TABLE truenas_enclosurelabel RENAME TO enclosure_label") + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + pass + # ### end Alembic commands ### diff --git a/src/middlewared/middlewared/api/v25_04_0/__init__.py b/src/middlewared/middlewared/api/v25_04_0/__init__.py index 5b9fca4d24fb7..c0459d8e556da 100644 --- a/src/middlewared/middlewared/api/v25_04_0/__init__.py +++ b/src/middlewared/middlewared/api/v25_04_0/__init__.py @@ -16,6 +16,7 @@ from .disk import * # noqa from .docker import * # noqa from .docker_network import * # noqa +from .enclosure_label import * # noqa from .failover_reboot import * # noqa from .fc_host import * # noqa from .fcport import * # noqa diff --git a/src/middlewared/middlewared/api/v25_04_0/enclosure_label.py b/src/middlewared/middlewared/api/v25_04_0/enclosure_label.py new file mode 100644 index 0000000000000..cd4f7e5ee3546 --- /dev/null +++ b/src/middlewared/middlewared/api/v25_04_0/enclosure_label.py @@ -0,0 +1,12 @@ +from middlewared.api.base import BaseModel + +__all__ = ["EnclosureLabelSetArgs", "EnclosureLabelUpdateResult"] + + +class EnclosureLabelSetArgs(BaseModel): + id: str + label: str + + +class EnclosureLabelUpdateResult(BaseModel): + result: None diff --git a/src/middlewared/middlewared/plugins/enclosure.py b/src/middlewared/middlewared/plugins/enclosure.py deleted file mode 100644 index 6a12f5a126b1d..0000000000000 --- a/src/middlewared/middlewared/plugins/enclosure.py +++ /dev/null @@ -1,37 +0,0 @@ -import middlewared.sqlalchemy as sa - -from middlewared.schema import accepts, Dict, Str -from middlewared.service import CRUDService - - -class EnclosureLabelModel(sa.Model): - __tablename__ = "truenas_enclosurelabel" - - id = sa.Column(sa.Integer(), primary_key=True) - encid = sa.Column(sa.String(200), unique=True) - label = sa.Column(sa.String(200)) - - -class EnclosureService(CRUDService): - class Config: - cli_namespace = "storage.enclosure" - - @accepts( - Str("id"), - Dict( - "enclosure_update", - Str("label"), - update=True, - ), - ) - async def do_update(self, id_, data): - if "label" in data: - await self.middleware.call( - "datastore.delete", "truenas.enclosurelabel", [["encid", "=", id_]] - ) - await self.middleware.call( - "datastore.insert", - "truenas.enclosurelabel", - {"encid": id_, "label": data["label"]}, - ) - return await self.get_instance(id_) diff --git a/src/middlewared/middlewared/plugins/enclosure_/enclosure2.py b/src/middlewared/middlewared/plugins/enclosure_/enclosure2.py index 7e32f94dd7689..e9479982c1437 100644 --- a/src/middlewared/middlewared/plugins/enclosure_/enclosure2.py +++ b/src/middlewared/middlewared/plugins/enclosure_/enclosure2.py @@ -139,10 +139,7 @@ def query(self, filters, options): # this feature is only available on hardware that ix sells return enclosures - labels = { - label['encid']: label['label'] - for label in self.middleware.call_sync('datastore.query', 'truenas.enclosurelabel') - } + labels = self.middleware.call_sync('enclosure.label.get_all') for i in self.get_ses_enclosures() + self.map_nvme() + self.middleware.call_sync('enclosure2.map_jbof'): if i.pop('should_ignore'): continue diff --git a/src/middlewared/middlewared/plugins/enclosure_/enclosure_label.py b/src/middlewared/middlewared/plugins/enclosure_/enclosure_label.py new file mode 100644 index 0000000000000..f3402751b0174 --- /dev/null +++ b/src/middlewared/middlewared/plugins/enclosure_/enclosure_label.py @@ -0,0 +1,37 @@ +import middlewared.sqlalchemy as sa + +from middlewared.api import api_method +from middlewared.api.current import EnclosureLabelSetArgs, EnclosureLabelUpdateResult +from middlewared.service import private, Service + + +class EnclosureLabelModel(sa.Model): + __tablename__ = "enclosure_label" + + id = sa.Column(sa.Integer(), primary_key=True) + encid = sa.Column(sa.String(200), unique=True) + label = sa.Column(sa.String(200)) + + +class EnclosureService(Service): + class Config: + namespace = "enclosure.label" + cli_namespace = "storage.enclosure.label" + + @private + async def get_all(self): + return { + label["encid"]: label["label"] + for label in await self.middleware.call("datastore.query", "enclosure.label") + } + + @api_method(EnclosureLabelSetArgs, EnclosureLabelUpdateResult) + async def set(self, id_, label): + await self.middleware.call( + "datastore.delete", "enclosure.label", [["encid", "=", id_]] + ) + await self.middleware.call( + "datastore.insert", + "enclosure.label", + {"encid": id_, "label": label}, + ) diff --git a/src/middlewared/middlewared/pytest/unit/plugins/enclosure/test_enclosure2_query.py b/src/middlewared/middlewared/pytest/unit/plugins/enclosure/test_enclosure2_query.py index 5fed380fcf247..ba7878f058e7e 100644 --- a/src/middlewared/middlewared/pytest/unit/plugins/enclosure/test_enclosure2_query.py +++ b/src/middlewared/middlewared/pytest/unit/plugins/enclosure/test_enclosure2_query.py @@ -73,7 +73,7 @@ def test_enclosure2_query(enc2_data): e.middleware._resolve_methods([Enclosure2Service], []) e.middleware['truenas.get_chassis_hardware'] = Mock(return_value=enc2_mocked.chassis) e.middleware['truenas.is_ix_hardware'] = Mock(return_value=True) - e.middleware['datastore.query'] = Mock(return_value=enc2_mocked.labels) + e.middleware['enclosure.label.get_all'] = Mock(return_value=enc2_mocked.labels) e.middleware['system.dmidecode_info'] = Mock(return_value=enc2_mocked.dmi) e.middleware['jbof.query'] = Mock(return_value=[]) e.middleware['enclosure2.map_jbof'] = Mock(return_value=[])