summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/services/iscsi_cli.py
blob: 71e6c9f3d9a2f33a0ae287054ed3aae96fabd65b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# -*- coding: utf-8 -*-
from __future__ import absolute_import

import errno
import json
from typing import Optional

from mgr_module import CLICheckNonemptyFileInput, CLIReadCommand, CLIWriteCommand

from ..rest_client import RequestException
from .iscsi_client import IscsiClient
from .iscsi_config import InvalidServiceUrl, IscsiGatewayAlreadyExists, \
    IscsiGatewayDoesNotExist, IscsiGatewaysConfig, \
    ManagedByOrchestratorException


@CLIReadCommand('dashboard iscsi-gateway-list')
def list_iscsi_gateways(_):
    '''
    List iSCSI gateways
    '''
    return 0, json.dumps(IscsiGatewaysConfig.get_gateways_config()), ''


@CLIWriteCommand('dashboard iscsi-gateway-add')
@CLICheckNonemptyFileInput(desc='iSCSI gateway configuration')
def add_iscsi_gateway(_, inbuf, name: Optional[str] = None):
    '''
    Add iSCSI gateway configuration. Gateway URL read from -i <file>
    '''
    service_url = inbuf
    try:
        IscsiGatewaysConfig.validate_service_url(service_url)
        if name is None:
            name = IscsiClient.instance(service_url=service_url).get_hostname()['data']
        IscsiGatewaysConfig.add_gateway(name, service_url)
        return 0, 'Success', ''
    except IscsiGatewayAlreadyExists as ex:
        return -errno.EEXIST, '', str(ex)
    except InvalidServiceUrl as ex:
        return -errno.EINVAL, '', str(ex)
    except ManagedByOrchestratorException as ex:
        return -errno.EINVAL, '', str(ex)
    except RequestException as ex:
        return -errno.EINVAL, '', str(ex)


@CLIWriteCommand('dashboard iscsi-gateway-rm')
def remove_iscsi_gateway(_, name: str):
    '''
    Remove iSCSI gateway configuration
    '''
    try:
        IscsiGatewaysConfig.remove_gateway(name)
        return 0, 'Success', ''
    except IscsiGatewayDoesNotExist as ex:
        return -errno.ENOENT, '', str(ex)
    except ManagedByOrchestratorException as ex:
        return -errno.EINVAL, '', str(ex)