|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +import json |
| 3 | +from typing import Optional |
| 4 | + |
| 5 | +from ..security import Scope |
| 6 | +from . import APIDoc, APIRouter, CreatePermission, DeletePermission, Endpoint, \ |
| 7 | + EndpointDoc, ReadPermission, RESTController |
| 8 | + |
| 9 | +try: |
| 10 | + from google.protobuf.json_format import MessageToJson |
| 11 | + |
| 12 | + from ..services.nvmeof_client import NVMeoFClient |
| 13 | +except ImportError: |
| 14 | + MessageToJson = None |
| 15 | +else: |
| 16 | + @APIRouter('/nvmeof/namespace', Scope.NVME_OF) |
| 17 | + @APIDoc('NVMe-oF Namespace Management API', 'NVMe-oF') |
| 18 | + class NvmeofNamespace(RESTController): |
| 19 | + @ReadPermission |
| 20 | + def list(self, subsystem_nqn: str): |
| 21 | + """ |
| 22 | + List all NVMeoF namespaces |
| 23 | + """ |
| 24 | + response = MessageToJson(NVMeoFClient().list_namespaces(subsystem_nqn)) |
| 25 | + return json.loads(response) |
| 26 | + |
| 27 | + @CreatePermission |
| 28 | + def create(self, rbd_pool: str, rbd_image: str, subsystem_nqn: str, |
| 29 | + create_image: Optional[bool] = True, image_size: Optional[int] = 1024, |
| 30 | + block_size: int = 512, nsid: Optional[int] = 1, |
| 31 | + uuid: Optional[str] = None, anagrpid: Optional[int] = 1): |
| 32 | + """ |
| 33 | + Create a new NVMeoF namespace |
| 34 | + :param rbd_pool: RBD pool name |
| 35 | + :param rbd_image: RBD image name |
| 36 | + :param subsystem_nqn: NVMeoF subsystem NQN |
| 37 | + :param create_image: Create RBD image |
| 38 | + :param image_size: RBD image size |
| 39 | + :param block_size: NVMeoF namespace block size |
| 40 | + :param nsid: NVMeoF namespace ID |
| 41 | + :param uuid: NVMeoF namespace UUID |
| 42 | + :param anagrpid: NVMeoF namespace ANA group ID |
| 43 | + """ |
| 44 | + response = NVMeoFClient().create_namespace(rbd_pool, rbd_image, |
| 45 | + subsystem_nqn, block_size, |
| 46 | + nsid, uuid, anagrpid, |
| 47 | + create_image, image_size) |
| 48 | + return json.loads(MessageToJson(response)) |
| 49 | + |
| 50 | + @Endpoint('DELETE', path='{subsystem_nqn}') |
| 51 | + def delete(self, subsystem_nqn: str, nsid: int): |
| 52 | + """ |
| 53 | + Delete an existing NVMeoF namespace |
| 54 | + :param subsystem_nqn: NVMeoF subsystem NQN |
| 55 | + :param nsid: NVMeoF namespace ID |
| 56 | + """ |
| 57 | + response = NVMeoFClient().delete_namespace(subsystem_nqn, nsid) |
| 58 | + return json.loads(MessageToJson(response)) |
| 59 | + |
| 60 | + @APIRouter('/nvmeof/subsystem', Scope.NVME_OF) |
| 61 | + @APIDoc('NVMe-oF Subsystem Management API', 'NVMe-oF') |
| 62 | + class NvmeofSubsystem(RESTController): |
| 63 | + @ReadPermission |
| 64 | + @EndpointDoc("List all NVMeoF gateways", |
| 65 | + parameters={ |
| 66 | + 'subsystem_nqn': (str, 'NVMeoF subsystem NQN'), |
| 67 | + 'serial_number': (str, 'NVMeoF subsystem serial number') |
| 68 | + }) |
| 69 | + def list(self, subsystem_nqn: Optional[str] = None, serial_number: Optional[str] = None): |
| 70 | + response = MessageToJson(NVMeoFClient().list_subsystems( |
| 71 | + subsystem_nqn=subsystem_nqn, serial_number=serial_number)) |
| 72 | + |
| 73 | + return json.loads(response) |
| 74 | + |
| 75 | + @CreatePermission |
| 76 | + def create(self, subsystem_nqn: str, serial_number: Optional[str] = None, |
| 77 | + max_namespaces: Optional[int] = 256, ana_reporting: Optional[bool] = False, |
| 78 | + enable_ha: Optional[bool] = False): |
| 79 | + """ |
| 80 | + Create a new NVMeoF subsystem |
| 81 | +
|
| 82 | + :param subsystem_nqn: NVMeoF subsystem NQN |
| 83 | + :param serial_number: NVMeoF subsystem serial number |
| 84 | + :param max_namespaces: NVMeoF subsystem maximum namespaces |
| 85 | + :param ana_reporting: NVMeoF subsystem ANA reporting |
| 86 | + :param enable_ha: NVMeoF subsystem enable HA |
| 87 | + """ |
| 88 | + response = NVMeoFClient().create_subsystem(subsystem_nqn, serial_number, max_namespaces, |
| 89 | + ana_reporting, enable_ha) |
| 90 | + return json.loads(MessageToJson(response)) |
| 91 | + |
| 92 | + @DeletePermission |
| 93 | + @Endpoint('DELETE', path='{subsystem_nqn}') |
| 94 | + def delete(self, subsystem_nqn: str): |
| 95 | + """ |
| 96 | + Delete an existing NVMeoF subsystem |
| 97 | + :param subsystem_nqn: NVMeoF subsystem NQN |
| 98 | + """ |
| 99 | + response = NVMeoFClient().delete_subsystem(subsystem_nqn) |
| 100 | + return json.loads(MessageToJson(response)) |
| 101 | + |
| 102 | + @APIRouter('/nvmeof/hosts', Scope.NVME_OF) |
| 103 | + @APIDoc('NVMe-oF Host Management API', 'NVMe-oF') |
| 104 | + class NvmeofHost(RESTController): |
| 105 | + @ReadPermission |
| 106 | + def list(self, subsystem_nqn: str): |
| 107 | + """ |
| 108 | + List all NVMeoF hosts |
| 109 | + :param subsystem_nqn: NVMeoF subsystem NQN |
| 110 | + """ |
| 111 | + response = MessageToJson(NVMeoFClient().list_hosts(subsystem_nqn)) |
| 112 | + return json.loads(response) |
| 113 | + |
| 114 | + @CreatePermission |
| 115 | + def create(self, subsystem_nqn: str, host_nqn: str): |
| 116 | + """ |
| 117 | + Create a new NVMeoF host |
| 118 | + :param subsystem_nqn: NVMeoF subsystem NQN |
| 119 | + :param host_nqn: NVMeoF host NQN |
| 120 | + """ |
| 121 | + response = NVMeoFClient().add_host(subsystem_nqn, host_nqn) |
| 122 | + return json.loads(MessageToJson(response)) |
| 123 | + |
| 124 | + @DeletePermission |
| 125 | + def delete(self, subsystem_nqn: str, host_nqn: str): |
| 126 | + """ |
| 127 | + Delete an existing NVMeoF host |
| 128 | + :param subsystem_nqn: NVMeoF subsystem NQN |
| 129 | + :param host_nqn: NVMeoF host NQN |
| 130 | + """ |
| 131 | + response = NVMeoFClient().remove_host(subsystem_nqn, host_nqn) |
| 132 | + return json.loads(MessageToJson(response)) |
| 133 | + |
| 134 | + @APIRouter('/nvmeof/listener', Scope.NVME_OF) |
| 135 | + @APIDoc('NVMe-oF Listener Management API', 'NVMe-oF') |
| 136 | + class NvmeofListener(RESTController): |
| 137 | + @ReadPermission |
| 138 | + def list(self, subsystem_nqn: str): |
| 139 | + """ |
| 140 | + List all NVMeoF listeners |
| 141 | + :param nqn: NVMeoF subsystem NQN |
| 142 | + """ |
| 143 | + response = MessageToJson(NVMeoFClient().list_listeners(subsystem_nqn)) |
| 144 | + return json.loads(response) |
| 145 | + |
| 146 | + @CreatePermission |
| 147 | + def create(self, nqn: str, gateway: str, traddr: Optional[str] = None, |
| 148 | + trtype: Optional[str] = 'TCP', adrfam: Optional[str] = 'IPV4', |
| 149 | + trsvcid: Optional[int] = 4420, |
| 150 | + auto_ha_state: Optional[str] = 'AUTO_HA_UNSET'): |
| 151 | + """ |
| 152 | + Create a new NVMeoF listener |
| 153 | + :param nqn: NVMeoF subsystem NQN |
| 154 | + :param gateway: NVMeoF gateway |
| 155 | + :param traddr: NVMeoF transport address |
| 156 | + :param trtype: NVMeoF transport type |
| 157 | + :param adrfam: NVMeoF address family |
| 158 | + :param trsvcid: NVMeoF transport service ID |
| 159 | + :param auto_ha_state: NVMeoF auto HA state |
| 160 | + """ |
| 161 | + response = NVMeoFClient().create_listener(nqn, gateway, traddr, |
| 162 | + trtype, adrfam, trsvcid, auto_ha_state) |
| 163 | + return json.loads(MessageToJson(response)) |
| 164 | + |
| 165 | + @DeletePermission |
| 166 | + def delete(self, nqn: str, gateway: str, traddr: Optional[str] = None, |
| 167 | + transport_type: Optional[str] = 'TCP', addr_family: Optional[str] = 'IPV4', |
| 168 | + transport_svc_id: Optional[int] = 4420): |
| 169 | + """ |
| 170 | + Delete an existing NVMeoF listener |
| 171 | + :param nqn: NVMeoF subsystem NQN |
| 172 | + :param gateway: NVMeoF gateway |
| 173 | + :param traddr: NVMeoF transport address |
| 174 | + :param transport_type: NVMeoF transport type |
| 175 | + :param addr_family: NVMeoF address family |
| 176 | + :param transport_svc_id: NVMeoF transport service ID |
| 177 | + """ |
| 178 | + response = NVMeoFClient().delete_listener(nqn, gateway, traddr, transport_type, |
| 179 | + addr_family, transport_svc_id) |
| 180 | + return json.loads(MessageToJson(response)) |
| 181 | + |
| 182 | + @APIRouter('/nvmeof/gateway', Scope.NVME_OF) |
| 183 | + @APIDoc('NVMe-oF Gateway Management API', 'NVMe-oF') |
| 184 | + class NvmeofGateway(RESTController): |
| 185 | + @ReadPermission |
| 186 | + @Endpoint() |
| 187 | + def info(self): |
| 188 | + """ |
| 189 | + Get NVMeoF gateway information |
| 190 | + """ |
| 191 | + response = MessageToJson(NVMeoFClient().gateway_info()) |
| 192 | + return json.loads(response) |
0 commit comments