]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/services/iscsi_client.py
import 14.2.4 nautilus point release
[ceph.git] / ceph / src / pybind / mgr / dashboard / services / iscsi_client.py
1 # -*- coding: utf-8 -*-
2 # pylint: disable=too-many-public-methods
3 from __future__ import absolute_import
4
5 import json
6
7 from requests.auth import HTTPBasicAuth
8
9 try:
10 from urlparse import urlparse
11 except ImportError:
12 from urllib.parse import urlparse
13
14 from .iscsi_config import IscsiGatewaysConfig
15 from .. import logger
16 from ..settings import Settings
17 from ..rest_client import RestClient
18
19
20 class IscsiClient(RestClient):
21 _CLIENT_NAME = 'iscsi'
22 _instances = {}
23
24 service_url = None
25 gateway_name = None
26
27 @classmethod
28 def instance(cls, gateway_name=None, service_url=None):
29 if not service_url:
30 if not gateway_name:
31 gateway_name = list(IscsiGatewaysConfig.get_gateways_config()['gateways'].keys())[0]
32 gateways_config = IscsiGatewaysConfig.get_gateway_config(gateway_name)
33 service_url = gateways_config['service_url']
34
35 instance = cls._instances.get(gateway_name)
36 if not instance or service_url != instance.service_url or \
37 instance.session.verify != Settings.ISCSI_API_SSL_VERIFICATION:
38 url = urlparse(service_url)
39 ssl = url.scheme == 'https'
40 host = url.hostname
41 port = url.port
42 username = url.username
43 password = url.password
44 if not port:
45 port = 443 if ssl else 80
46
47 auth = HTTPBasicAuth(username, password)
48 instance = IscsiClient(host, port, IscsiClient._CLIENT_NAME, ssl,
49 auth, Settings.ISCSI_API_SSL_VERIFICATION)
50 instance.service_url = service_url
51 instance.gateway_name = gateway_name
52 if gateway_name:
53 cls._instances[gateway_name] = instance
54
55 return instance
56
57 @RestClient.api_get('/api/_ping')
58 def ping(self, request=None):
59 return request()
60
61 @RestClient.api_get('/api/settings')
62 def get_settings(self, request=None):
63 return request()
64
65 @RestClient.api_get('/api/sysinfo/ip_addresses')
66 def get_ip_addresses(self, request=None):
67 return request()
68
69 @RestClient.api_get('/api/sysinfo/hostname')
70 def get_hostname(self, request=None):
71 return request()
72
73 @RestClient.api_get('/api/config')
74 def get_config(self, request=None):
75 return request({
76 'decrypt_passwords': True
77 })
78
79 @RestClient.api_put('/api/target/{target_iqn}')
80 def create_target(self, target_iqn, target_controls, request=None):
81 logger.debug("iSCSI[%s] Creating target: %s", self.gateway_name, target_iqn)
82 return request({
83 'controls': json.dumps(target_controls)
84 })
85
86 @RestClient.api_delete('/api/target/{target_iqn}')
87 def delete_target(self, target_iqn, request=None):
88 logger.debug("iSCSI[%s] Deleting target: %s", self.gateway_name, target_iqn)
89 return request()
90
91 @RestClient.api_put('/api/target/{target_iqn}')
92 def reconfigure_target(self, target_iqn, target_controls, request=None):
93 logger.debug("iSCSI[%s] Reconfiguring target: %s", self.gateway_name, target_iqn)
94 return request({
95 'mode': 'reconfigure',
96 'controls': json.dumps(target_controls)
97 })
98
99 @RestClient.api_put('/api/gateway/{target_iqn}/{gateway_name}')
100 def create_gateway(self, target_iqn, gateway_name, ip_address, request=None):
101 logger.debug("iSCSI[%s] Creating gateway: %s/%s", self.gateway_name, target_iqn,
102 gateway_name)
103 return request({
104 'ip_address': ','.join(ip_address),
105 'skipchecks': 'true'
106 })
107
108 @RestClient.api_get('/api/gatewayinfo')
109 def get_gatewayinfo(self, request=None):
110 return request()
111
112 @RestClient.api_delete('/api/gateway/{target_iqn}/{gateway_name}')
113 def delete_gateway(self, target_iqn, gateway_name, request=None):
114 logger.debug("iSCSI: Deleting gateway: %s/%s", target_iqn, gateway_name)
115 return request()
116
117 @RestClient.api_put('/api/disk/{pool}/{image}')
118 def create_disk(self, pool, image, backstore, request=None):
119 logger.debug("iSCSI[%s] Creating disk: %s/%s", self.gateway_name, pool, image)
120 return request({
121 'mode': 'create',
122 'backstore': backstore
123 })
124
125 @RestClient.api_delete('/api/disk/{pool}/{image}')
126 def delete_disk(self, pool, image, request=None):
127 logger.debug("iSCSI[%s] Deleting disk: %s/%s", self.gateway_name, pool, image)
128 return request({
129 'preserve_image': 'true'
130 })
131
132 @RestClient.api_put('/api/disk/{pool}/{image}')
133 def reconfigure_disk(self, pool, image, controls, request=None):
134 logger.debug("iSCSI[%s] Reconfiguring disk: %s/%s", self.gateway_name, pool, image)
135 return request({
136 'controls': json.dumps(controls),
137 'mode': 'reconfigure'
138 })
139
140 @RestClient.api_put('/api/targetlun/{target_iqn}')
141 def create_target_lun(self, target_iqn, image_id, request=None):
142 logger.debug("iSCSI[%s] Creating target lun: %s/%s", self.gateway_name, target_iqn,
143 image_id)
144 return request({
145 'disk': image_id
146 })
147
148 @RestClient.api_delete('/api/targetlun/{target_iqn}')
149 def delete_target_lun(self, target_iqn, image_id, request=None):
150 logger.debug("iSCSI[%s] Deleting target lun: %s/%s", self.gateway_name, target_iqn,
151 image_id)
152 return request({
153 'disk': image_id
154 })
155
156 @RestClient.api_put('/api/client/{target_iqn}/{client_iqn}')
157 def create_client(self, target_iqn, client_iqn, request=None):
158 logger.debug("iSCSI[%s] Creating client: %s/%s", self.gateway_name, target_iqn, client_iqn)
159 return request()
160
161 @RestClient.api_delete('/api/client/{target_iqn}/{client_iqn}')
162 def delete_client(self, target_iqn, client_iqn, request=None):
163 logger.debug("iSCSI[%s] Deleting client: %s/%s", self.gateway_name, target_iqn, client_iqn)
164 return request()
165
166 @RestClient.api_put('/api/clientlun/{target_iqn}/{client_iqn}')
167 def create_client_lun(self, target_iqn, client_iqn, image_id, request=None):
168 logger.debug("iSCSI[%s] Creating client lun: %s/%s", self.gateway_name, target_iqn,
169 client_iqn)
170 return request({
171 'disk': image_id
172 })
173
174 @RestClient.api_put('/api/clientauth/{target_iqn}/{client_iqn}')
175 def create_client_auth(self, target_iqn, client_iqn, username, password, mutual_username,
176 mutual_password, request=None):
177 logger.debug("iSCSI[%s] Creating client auth: %s/%s/%s/%s/%s/%s",
178 self.gateway_name, target_iqn, client_iqn, username, password, mutual_username,
179 mutual_password)
180 return request({
181 'username': username,
182 'password': password,
183 'mutual_username': mutual_username,
184 'mutual_password': mutual_password
185 })
186
187 @RestClient.api_put('/api/hostgroup/{target_iqn}/{group_name}')
188 def create_group(self, target_iqn, group_name, members, image_ids, request=None):
189 logger.debug("iSCSI[%s] Creating group: %s/%s", self.gateway_name, target_iqn, group_name)
190 return request({
191 'members': ','.join(members),
192 'disks': ','.join(image_ids)
193 })
194
195 @RestClient.api_delete('/api/hostgroup/{target_iqn}/{group_name}')
196 def delete_group(self, target_iqn, group_name, request=None):
197 logger.debug("iSCSI[%s] Deleting group: %s/%s", self.gateway_name, target_iqn, group_name)
198 return request()
199
200 @RestClient.api_put('/api/discoveryauth')
201 def update_discoveryauth(self, user, password, mutual_user, mutual_password, request=None):
202 logger.debug("iSCSI[%s] Updating discoveryauth: %s/%s/%s/%s", self.gateway_name, user,
203 password, mutual_user, mutual_password)
204 return request({
205 'username': user,
206 'password': password,
207 'mutual_username': mutual_user,
208 'mutual_password': mutual_password
209 })
210
211 @RestClient.api_put('/api/targetauth/{target_iqn}')
212 def update_targetauth(self, target_iqn, action, request=None):
213 logger.debug("iSCSI[%s] Updating targetauth: %s/%s", self.gateway_name, target_iqn, action)
214 return request({
215 'action': action
216 })
217
218 @RestClient.api_get('/api/targetinfo/{target_iqn}')
219 def get_targetinfo(self, target_iqn, request=None):
220 # pylint: disable=unused-argument
221 return request()
222
223 @RestClient.api_get('/api/clientinfo/{target_iqn}/{client_iqn}')
224 def get_clientinfo(self, target_iqn, client_iqn, request=None):
225 # pylint: disable=unused-argument
226 return request()