]>
Commit | Line | Data |
---|---|---|
11fdf7f2 | 1 | # -*- coding: utf-8 -*- |
11fdf7f2 TL |
2 | |
3 | import errno | |
4 | import json | |
f67539c2 | 5 | from typing import Optional |
11fdf7f2 | 6 | |
cd265ab1 | 7 | from mgr_module import CLICheckNonemptyFileInput, CLIReadCommand, CLIWriteCommand |
11fdf7f2 | 8 | |
11fdf7f2 | 9 | from ..rest_client import RequestException |
f67539c2 TL |
10 | from .iscsi_client import IscsiClient |
11 | from .iscsi_config import InvalidServiceUrl, IscsiGatewayAlreadyExists, \ | |
12 | IscsiGatewayDoesNotExist, IscsiGatewaysConfig, \ | |
13 | ManagedByOrchestratorException | |
11fdf7f2 TL |
14 | |
15 | ||
f67539c2 | 16 | @CLIReadCommand('dashboard iscsi-gateway-list') |
11fdf7f2 | 17 | def list_iscsi_gateways(_): |
f67539c2 TL |
18 | ''' |
19 | List iSCSI gateways | |
20 | ''' | |
11fdf7f2 TL |
21 | return 0, json.dumps(IscsiGatewaysConfig.get_gateways_config()), '' |
22 | ||
23 | ||
f67539c2 | 24 | @CLIWriteCommand('dashboard iscsi-gateway-add') |
522d829b | 25 | @CLICheckNonemptyFileInput(desc='iSCSI gateway configuration') |
f67539c2 TL |
26 | def add_iscsi_gateway(_, inbuf, name: Optional[str] = None): |
27 | ''' | |
28 | Add iSCSI gateway configuration. Gateway URL read from -i <file> | |
29 | ''' | |
cd265ab1 | 30 | service_url = inbuf |
11fdf7f2 TL |
31 | try: |
32 | IscsiGatewaysConfig.validate_service_url(service_url) | |
e306af50 TL |
33 | if name is None: |
34 | name = IscsiClient.instance(service_url=service_url).get_hostname()['data'] | |
11fdf7f2 TL |
35 | IscsiGatewaysConfig.add_gateway(name, service_url) |
36 | return 0, 'Success', '' | |
37 | except IscsiGatewayAlreadyExists as ex: | |
38 | return -errno.EEXIST, '', str(ex) | |
39 | except InvalidServiceUrl as ex: | |
40 | return -errno.EINVAL, '', str(ex) | |
41 | except ManagedByOrchestratorException as ex: | |
42 | return -errno.EINVAL, '', str(ex) | |
43 | except RequestException as ex: | |
44 | return -errno.EINVAL, '', str(ex) | |
45 | ||
46 | ||
f67539c2 TL |
47 | @CLIWriteCommand('dashboard iscsi-gateway-rm') |
48 | def remove_iscsi_gateway(_, name: str): | |
49 | ''' | |
50 | Remove iSCSI gateway configuration | |
51 | ''' | |
11fdf7f2 | 52 | try: |
11fdf7f2 TL |
53 | IscsiGatewaysConfig.remove_gateway(name) |
54 | return 0, 'Success', '' | |
11fdf7f2 TL |
55 | except IscsiGatewayDoesNotExist as ex: |
56 | return -errno.ENOENT, '', str(ex) | |
57 | except ManagedByOrchestratorException as ex: | |
58 | return -errno.EINVAL, '', str(ex) |