summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/services/iscsi_client.py
blob: 9cc2600e1217b720cbae4de136e7d6e026c16f10 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# -*- coding: utf-8 -*-
# pylint: disable=too-many-public-methods
from __future__ import absolute_import

import json
import logging

from requests.auth import HTTPBasicAuth

try:
    from urlparse import urlparse
except ImportError:
    from urllib.parse import urlparse

from ..rest_client import RestClient
from ..settings import Settings
from .iscsi_config import IscsiGatewaysConfig

logger = logging.getLogger('iscsi_client')


class IscsiClient(RestClient):
    _CLIENT_NAME = 'iscsi'
    _instances = {}  # type: dict

    service_url = None
    gateway_name = None

    @classmethod
    def instance(cls, gateway_name=None, service_url=None):
        if not service_url:
            if not gateway_name:
                gateway_name = list(IscsiGatewaysConfig.get_gateways_config()['gateways'].keys())[0]
            gateways_config = IscsiGatewaysConfig.get_gateway_config(gateway_name)
            service_url = gateways_config['service_url']

        instance = cls._instances.get(gateway_name)
        if not instance or service_url != instance.service_url or \
                instance.session.verify != Settings.ISCSI_API_SSL_VERIFICATION:
            url = urlparse(service_url)
            ssl = url.scheme == 'https'
            host = url.hostname
            port = url.port
            username = url.username
            password = url.password
            if not port:
                port = 443 if ssl else 80

            auth = HTTPBasicAuth(username, password)
            instance = IscsiClient(host, port, IscsiClient._CLIENT_NAME, ssl,
                                   auth, Settings.ISCSI_API_SSL_VERIFICATION)
            instance.service_url = service_url
            instance.gateway_name = gateway_name
            if gateway_name:
                cls._instances[gateway_name] = instance

        return instance

    @RestClient.api_get('/api/_ping')
    def ping(self, request=None):
        return request()

    @RestClient.api_get('/api/settings')
    def get_settings(self, request=None):
        return request()

    @RestClient.api_get('/api/sysinfo/ip_addresses')
    def get_ip_addresses(self, request=None):
        return request()

    @RestClient.api_get('/api/sysinfo/hostname')
    def get_hostname(self, request=None):
        return request()

    @RestClient.api_get('/api/config')
    def get_config(self, request=None):
        return request({
            'decrypt_passwords': True
        })

    @RestClient.api_put('/api/target/{target_iqn}')
    def create_target(self, target_iqn, target_controls, request=None):
        logger.debug("[%s] Creating target: %s", self.gateway_name, target_iqn)
        return request({
            'controls': json.dumps(target_controls)
        })

    @RestClient.api_delete('/api/target/{target_iqn}')
    def delete_target(self, target_iqn, request=None):
        logger.debug("[%s] Deleting target: %s", self.gateway_name, target_iqn)
        return request()

    @RestClient.api_put('/api/target/{target_iqn}')
    def reconfigure_target(self, target_iqn, target_controls, request=None):
        logger.debug("[%s] Reconfiguring target: %s", self.gateway_name, target_iqn)
        return request({
            'mode': 'reconfigure',
            'controls': json.dumps(target_controls)
        })

    @RestClient.api_put('/api/gateway/{target_iqn}/{gateway_name}')
    def create_gateway(self, target_iqn, gateway_name, ip_address, request=None):
        logger.debug("[%s] Creating gateway: %s/%s", self.gateway_name, target_iqn,
                     gateway_name)
        return request({
            'ip_address': ','.join(ip_address),
            'skipchecks': 'true'
        })

    @RestClient.api_get('/api/gatewayinfo')
    def get_gatewayinfo(self, request=None):
        return request()

    @RestClient.api_delete('/api/gateway/{target_iqn}/{gateway_name}')
    def delete_gateway(self, target_iqn, gateway_name, request=None):
        logger.debug("Deleting gateway: %s/%s", target_iqn, gateway_name)
        return request()

    @RestClient.api_put('/api/disk/{pool}/{image}')
    def create_disk(self, pool, image, backstore, wwn, request=None):
        logger.debug("[%s] Creating disk: %s/%s", self.gateway_name, pool, image)
        return request({
            'mode': 'create',
            'backstore': backstore,
            'wwn': wwn
        })

    @RestClient.api_delete('/api/disk/{pool}/{image}')
    def delete_disk(self, pool, image, request=None):
        logger.debug("[%s] Deleting disk: %s/%s", self.gateway_name, pool, image)
        return request({
            'preserve_image': 'true'
        })

    @RestClient.api_put('/api/disk/{pool}/{image}')
    def reconfigure_disk(self, pool, image, controls, request=None):
        logger.debug("[%s] Reconfiguring disk: %s/%s", self.gateway_name, pool, image)
        return request({
            'controls': json.dumps(controls),
            'mode': 'reconfigure'
        })

    @RestClient.api_put('/api/targetlun/{target_iqn}')
    def create_target_lun(self, target_iqn, image_id, lun, request=None):
        logger.debug("[%s] Creating target lun: %s/%s", self.gateway_name, target_iqn,
                     image_id)
        return request({
            'disk': image_id,
            'lun_id': lun
        })

    @RestClient.api_delete('/api/targetlun/{target_iqn}')
    def delete_target_lun(self, target_iqn, image_id, request=None):
        logger.debug("[%s] Deleting target lun: %s/%s", self.gateway_name, target_iqn,
                     image_id)
        return request({
            'disk': image_id
        })

    @RestClient.api_put('/api/client/{target_iqn}/{client_iqn}')
    def create_client(self, target_iqn, client_iqn, request=None):
        logger.debug("[%s] Creating client: %s/%s", self.gateway_name, target_iqn, client_iqn)
        return request()

    @RestClient.api_delete('/api/client/{target_iqn}/{client_iqn}')
    def delete_client(self, target_iqn, client_iqn, request=None):
        logger.debug("[%s] Deleting client: %s/%s", self.gateway_name, target_iqn, client_iqn)
        return request()

    @RestClient.api_put('/api/clientlun/{target_iqn}/{client_iqn}')
    def create_client_lun(self, target_iqn, client_iqn, image_id, request=None):
        logger.debug("[%s] Creating client lun: %s/%s", self.gateway_name, target_iqn,
                     client_iqn)
        return request({
            'disk': image_id
        })

    @RestClient.api_delete('/api/clientlun/{target_iqn}/{client_iqn}')
    def delete_client_lun(self, target_iqn, client_iqn, image_id, request=None):
        logger.debug("iSCSI[%s] Deleting client lun: %s/%s", self.gateway_name, target_iqn,
                     client_iqn)
        return request({
            'disk': image_id
        })

    @RestClient.api_put('/api/clientauth/{target_iqn}/{client_iqn}')
    def create_client_auth(self, target_iqn, client_iqn, username, password, mutual_username,
                           mutual_password, request=None):
        logger.debug("[%s] Creating client auth: %s/%s/%s/%s/%s/%s",
                     self.gateway_name, target_iqn, client_iqn, username, password, mutual_username,
                     mutual_password)
        return request({
            'username': username,
            'password': password,
            'mutual_username': mutual_username,
            'mutual_password': mutual_password
        })

    @RestClient.api_put('/api/hostgroup/{target_iqn}/{group_name}')
    def create_group(self, target_iqn, group_name, members, image_ids, request=None):
        logger.debug("[%s] Creating group: %s/%s", self.gateway_name, target_iqn, group_name)
        return request({
            'members': ','.join(members),
            'disks': ','.join(image_ids)
        })

    @RestClient.api_put('/api/hostgroup/{target_iqn}/{group_name}')
    def update_group(self, target_iqn, group_name, members, image_ids, request=None):
        logger.debug("iSCSI[%s] Updating group: %s/%s", self.gateway_name, target_iqn, group_name)
        return request({
            'action': 'remove',
            'members': ','.join(members),
            'disks': ','.join(image_ids)
        })

    @RestClient.api_delete('/api/hostgroup/{target_iqn}/{group_name}')
    def delete_group(self, target_iqn, group_name, request=None):
        logger.debug("[%s] Deleting group: %s/%s", self.gateway_name, target_iqn, group_name)
        return request()

    @RestClient.api_put('/api/discoveryauth')
    def update_discoveryauth(self, user, password, mutual_user, mutual_password, request=None):
        logger.debug("[%s] Updating discoveryauth: %s/%s/%s/%s", self.gateway_name, user,
                     password, mutual_user, mutual_password)
        return request({
            'username': user,
            'password': password,
            'mutual_username': mutual_user,
            'mutual_password': mutual_password
        })

    @RestClient.api_put('/api/targetauth/{target_iqn}')
    def update_targetacl(self, target_iqn, action, request=None):
        logger.debug("[%s] Updating targetacl: %s/%s", self.gateway_name, target_iqn, action)
        return request({
            'action': action
        })

    @RestClient.api_put('/api/targetauth/{target_iqn}')
    def update_targetauth(self, target_iqn, user, password, mutual_user, mutual_password,
                          request=None):
        logger.debug("[%s] Updating targetauth: %s/%s/%s/%s/%s", self.gateway_name,
                     target_iqn, user, password, mutual_user, mutual_password)
        return request({
            'username': user,
            'password': password,
            'mutual_username': mutual_user,
            'mutual_password': mutual_password
        })

    @RestClient.api_get('/api/targetinfo/{target_iqn}')
    def get_targetinfo(self, target_iqn, request=None):
        # pylint: disable=unused-argument
        return request()

    @RestClient.api_get('/api/clientinfo/{target_iqn}/{client_iqn}')
    def get_clientinfo(self, target_iqn, client_iqn, request=None):
        # pylint: disable=unused-argument
        return request()