]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
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 | ||
494da23a TL |
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 | ||
11fdf7f2 | 117 | @RestClient.api_put('/api/disk/{pool}/{image}') |
eafe8130 | 118 | def create_disk(self, pool, image, backstore, wwn, request=None): |
11fdf7f2 TL |
119 | logger.debug("iSCSI[%s] Creating disk: %s/%s", self.gateway_name, pool, image) |
120 | return request({ | |
121 | 'mode': 'create', | |
eafe8130 TL |
122 | 'backstore': backstore, |
123 | 'wwn': wwn | |
11fdf7f2 TL |
124 | }) |
125 | ||
126 | @RestClient.api_delete('/api/disk/{pool}/{image}') | |
127 | def delete_disk(self, pool, image, request=None): | |
128 | logger.debug("iSCSI[%s] Deleting disk: %s/%s", self.gateway_name, pool, image) | |
129 | return request({ | |
130 | 'preserve_image': 'true' | |
131 | }) | |
132 | ||
133 | @RestClient.api_put('/api/disk/{pool}/{image}') | |
134 | def reconfigure_disk(self, pool, image, controls, request=None): | |
135 | logger.debug("iSCSI[%s] Reconfiguring disk: %s/%s", self.gateway_name, pool, image) | |
136 | return request({ | |
137 | 'controls': json.dumps(controls), | |
138 | 'mode': 'reconfigure' | |
139 | }) | |
140 | ||
141 | @RestClient.api_put('/api/targetlun/{target_iqn}') | |
eafe8130 | 142 | def create_target_lun(self, target_iqn, image_id, lun, request=None): |
11fdf7f2 TL |
143 | logger.debug("iSCSI[%s] Creating target lun: %s/%s", self.gateway_name, target_iqn, |
144 | image_id) | |
145 | return request({ | |
eafe8130 TL |
146 | 'disk': image_id, |
147 | 'lun_id': lun | |
11fdf7f2 TL |
148 | }) |
149 | ||
150 | @RestClient.api_delete('/api/targetlun/{target_iqn}') | |
151 | def delete_target_lun(self, target_iqn, image_id, request=None): | |
152 | logger.debug("iSCSI[%s] Deleting target lun: %s/%s", self.gateway_name, target_iqn, | |
153 | image_id) | |
154 | return request({ | |
155 | 'disk': image_id | |
156 | }) | |
157 | ||
158 | @RestClient.api_put('/api/client/{target_iqn}/{client_iqn}') | |
159 | def create_client(self, target_iqn, client_iqn, request=None): | |
160 | logger.debug("iSCSI[%s] Creating client: %s/%s", self.gateway_name, target_iqn, client_iqn) | |
161 | return request() | |
162 | ||
163 | @RestClient.api_delete('/api/client/{target_iqn}/{client_iqn}') | |
164 | def delete_client(self, target_iqn, client_iqn, request=None): | |
165 | logger.debug("iSCSI[%s] Deleting client: %s/%s", self.gateway_name, target_iqn, client_iqn) | |
166 | return request() | |
167 | ||
168 | @RestClient.api_put('/api/clientlun/{target_iqn}/{client_iqn}') | |
169 | def create_client_lun(self, target_iqn, client_iqn, image_id, request=None): | |
170 | logger.debug("iSCSI[%s] Creating client lun: %s/%s", self.gateway_name, target_iqn, | |
171 | client_iqn) | |
172 | return request({ | |
173 | 'disk': image_id | |
174 | }) | |
175 | ||
eafe8130 TL |
176 | @RestClient.api_delete('/api/clientlun/{target_iqn}/{client_iqn}') |
177 | def delete_client_lun(self, target_iqn, client_iqn, image_id, request=None): | |
178 | logger.debug("iSCSI[%s] Deleting client lun: %s/%s", self.gateway_name, target_iqn, | |
179 | client_iqn) | |
180 | return request({ | |
181 | 'disk': image_id | |
182 | }) | |
183 | ||
11fdf7f2 TL |
184 | @RestClient.api_put('/api/clientauth/{target_iqn}/{client_iqn}') |
185 | def create_client_auth(self, target_iqn, client_iqn, username, password, mutual_username, | |
186 | mutual_password, request=None): | |
187 | logger.debug("iSCSI[%s] Creating client auth: %s/%s/%s/%s/%s/%s", | |
188 | self.gateway_name, target_iqn, client_iqn, username, password, mutual_username, | |
189 | mutual_password) | |
190 | return request({ | |
191 | 'username': username, | |
192 | 'password': password, | |
193 | 'mutual_username': mutual_username, | |
194 | 'mutual_password': mutual_password | |
195 | }) | |
196 | ||
197 | @RestClient.api_put('/api/hostgroup/{target_iqn}/{group_name}') | |
198 | def create_group(self, target_iqn, group_name, members, image_ids, request=None): | |
199 | logger.debug("iSCSI[%s] Creating group: %s/%s", self.gateway_name, target_iqn, group_name) | |
200 | return request({ | |
201 | 'members': ','.join(members), | |
202 | 'disks': ','.join(image_ids) | |
203 | }) | |
204 | ||
205 | @RestClient.api_delete('/api/hostgroup/{target_iqn}/{group_name}') | |
206 | def delete_group(self, target_iqn, group_name, request=None): | |
207 | logger.debug("iSCSI[%s] Deleting group: %s/%s", self.gateway_name, target_iqn, group_name) | |
208 | return request() | |
209 | ||
210 | @RestClient.api_put('/api/discoveryauth') | |
211 | def update_discoveryauth(self, user, password, mutual_user, mutual_password, request=None): | |
212 | logger.debug("iSCSI[%s] Updating discoveryauth: %s/%s/%s/%s", self.gateway_name, user, | |
213 | password, mutual_user, mutual_password) | |
214 | return request({ | |
215 | 'username': user, | |
216 | 'password': password, | |
217 | 'mutual_username': mutual_user, | |
218 | 'mutual_password': mutual_password | |
219 | }) | |
220 | ||
221 | @RestClient.api_put('/api/targetauth/{target_iqn}') | |
eafe8130 TL |
222 | def update_targetacl(self, target_iqn, action, request=None): |
223 | logger.debug("iSCSI[%s] Updating targetacl: %s/%s", self.gateway_name, target_iqn, action) | |
11fdf7f2 TL |
224 | return request({ |
225 | 'action': action | |
226 | }) | |
227 | ||
eafe8130 TL |
228 | @RestClient.api_put('/api/targetauth/{target_iqn}') |
229 | def update_targetauth(self, target_iqn, user, password, mutual_user, mutual_password, | |
230 | request=None): | |
231 | logger.debug("iSCSI[%s] Updating targetauth: %s/%s/%s/%s/%s", self.gateway_name, | |
232 | target_iqn, user, password, mutual_user, mutual_password) | |
233 | return request({ | |
234 | 'username': user, | |
235 | 'password': password, | |
236 | 'mutual_username': mutual_user, | |
237 | 'mutual_password': mutual_password | |
238 | }) | |
239 | ||
11fdf7f2 TL |
240 | @RestClient.api_get('/api/targetinfo/{target_iqn}') |
241 | def get_targetinfo(self, target_iqn, request=None): | |
81eedcae | 242 | # pylint: disable=unused-argument |
11fdf7f2 | 243 | return request() |
494da23a TL |
244 | |
245 | @RestClient.api_get('/api/clientinfo/{target_iqn}/{client_iqn}') | |
246 | def get_clientinfo(self, target_iqn, client_iqn, request=None): | |
247 | # pylint: disable=unused-argument | |
248 | return request() |