]>
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 | ||
112 | @RestClient.api_put('/api/disk/{pool}/{image}') | |
113 | def create_disk(self, pool, image, backstore, request=None): | |
114 | logger.debug("iSCSI[%s] Creating disk: %s/%s", self.gateway_name, pool, image) | |
115 | return request({ | |
116 | 'mode': 'create', | |
117 | 'backstore': backstore | |
118 | }) | |
119 | ||
120 | @RestClient.api_delete('/api/disk/{pool}/{image}') | |
121 | def delete_disk(self, pool, image, request=None): | |
122 | logger.debug("iSCSI[%s] Deleting disk: %s/%s", self.gateway_name, pool, image) | |
123 | return request({ | |
124 | 'preserve_image': 'true' | |
125 | }) | |
126 | ||
127 | @RestClient.api_put('/api/disk/{pool}/{image}') | |
128 | def reconfigure_disk(self, pool, image, controls, request=None): | |
129 | logger.debug("iSCSI[%s] Reconfiguring disk: %s/%s", self.gateway_name, pool, image) | |
130 | return request({ | |
131 | 'controls': json.dumps(controls), | |
132 | 'mode': 'reconfigure' | |
133 | }) | |
134 | ||
135 | @RestClient.api_put('/api/targetlun/{target_iqn}') | |
136 | def create_target_lun(self, target_iqn, image_id, request=None): | |
137 | logger.debug("iSCSI[%s] Creating target lun: %s/%s", self.gateway_name, target_iqn, | |
138 | image_id) | |
139 | return request({ | |
140 | 'disk': image_id | |
141 | }) | |
142 | ||
143 | @RestClient.api_delete('/api/targetlun/{target_iqn}') | |
144 | def delete_target_lun(self, target_iqn, image_id, request=None): | |
145 | logger.debug("iSCSI[%s] Deleting target lun: %s/%s", self.gateway_name, target_iqn, | |
146 | image_id) | |
147 | return request({ | |
148 | 'disk': image_id | |
149 | }) | |
150 | ||
151 | @RestClient.api_put('/api/client/{target_iqn}/{client_iqn}') | |
152 | def create_client(self, target_iqn, client_iqn, request=None): | |
153 | logger.debug("iSCSI[%s] Creating client: %s/%s", self.gateway_name, target_iqn, client_iqn) | |
154 | return request() | |
155 | ||
156 | @RestClient.api_delete('/api/client/{target_iqn}/{client_iqn}') | |
157 | def delete_client(self, target_iqn, client_iqn, request=None): | |
158 | logger.debug("iSCSI[%s] Deleting client: %s/%s", self.gateway_name, target_iqn, client_iqn) | |
159 | return request() | |
160 | ||
161 | @RestClient.api_put('/api/clientlun/{target_iqn}/{client_iqn}') | |
162 | def create_client_lun(self, target_iqn, client_iqn, image_id, request=None): | |
163 | logger.debug("iSCSI[%s] Creating client lun: %s/%s", self.gateway_name, target_iqn, | |
164 | client_iqn) | |
165 | return request({ | |
166 | 'disk': image_id | |
167 | }) | |
168 | ||
169 | @RestClient.api_put('/api/clientauth/{target_iqn}/{client_iqn}') | |
170 | def create_client_auth(self, target_iqn, client_iqn, username, password, mutual_username, | |
171 | mutual_password, request=None): | |
172 | logger.debug("iSCSI[%s] Creating client auth: %s/%s/%s/%s/%s/%s", | |
173 | self.gateway_name, target_iqn, client_iqn, username, password, mutual_username, | |
174 | mutual_password) | |
175 | return request({ | |
176 | 'username': username, | |
177 | 'password': password, | |
178 | 'mutual_username': mutual_username, | |
179 | 'mutual_password': mutual_password | |
180 | }) | |
181 | ||
182 | @RestClient.api_put('/api/hostgroup/{target_iqn}/{group_name}') | |
183 | def create_group(self, target_iqn, group_name, members, image_ids, request=None): | |
184 | logger.debug("iSCSI[%s] Creating group: %s/%s", self.gateway_name, target_iqn, group_name) | |
185 | return request({ | |
186 | 'members': ','.join(members), | |
187 | 'disks': ','.join(image_ids) | |
188 | }) | |
189 | ||
190 | @RestClient.api_delete('/api/hostgroup/{target_iqn}/{group_name}') | |
191 | def delete_group(self, target_iqn, group_name, request=None): | |
192 | logger.debug("iSCSI[%s] Deleting group: %s/%s", self.gateway_name, target_iqn, group_name) | |
193 | return request() | |
194 | ||
195 | @RestClient.api_put('/api/discoveryauth') | |
196 | def update_discoveryauth(self, user, password, mutual_user, mutual_password, request=None): | |
197 | logger.debug("iSCSI[%s] Updating discoveryauth: %s/%s/%s/%s", self.gateway_name, user, | |
198 | password, mutual_user, mutual_password) | |
199 | return request({ | |
200 | 'username': user, | |
201 | 'password': password, | |
202 | 'mutual_username': mutual_user, | |
203 | 'mutual_password': mutual_password | |
204 | }) | |
205 | ||
206 | @RestClient.api_put('/api/targetauth/{target_iqn}') | |
207 | def update_targetauth(self, target_iqn, action, request=None): | |
208 | logger.debug("iSCSI[%s] Updating targetauth: %s/%s", self.gateway_name, target_iqn, action) | |
209 | return request({ | |
210 | 'action': action | |
211 | }) | |
212 | ||
213 | @RestClient.api_get('/api/targetinfo/{target_iqn}') | |
214 | def get_targetinfo(self, target_iqn, request=None): | |
215 | logger.debug("iSCSI: Getting targetinfo: %s", target_iqn) | |
216 | return request() |