1 # -*- coding: utf-8 -*-
2 # pylint: disable=too-many-public-methods
3 from __future__
import absolute_import
7 from requests
.auth
import HTTPBasicAuth
10 from urlparse
import urlparse
12 from urllib
.parse
import urlparse
14 from .iscsi_config
import IscsiGatewaysConfig
16 from ..settings
import Settings
17 from ..rest_client
import RestClient
20 class IscsiClient(RestClient
):
21 _CLIENT_NAME
= 'iscsi'
28 def instance(cls
, gateway_name
=None, service_url
=None):
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']
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'
42 username
= url
.username
43 password
= url
.password
45 port
= 443 if ssl
else 80
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
53 cls
._instances
[gateway_name
] = instance
57 @RestClient.api_get('/api/_ping')
58 def ping(self
, request
=None):
61 @RestClient.api_get('/api/settings')
62 def get_settings(self
, request
=None):
65 @RestClient.api_get('/api/sysinfo/ip_addresses')
66 def get_ip_addresses(self
, request
=None):
69 @RestClient.api_get('/api/sysinfo/hostname')
70 def get_hostname(self
, request
=None):
73 @RestClient.api_get('/api/config')
74 def get_config(self
, request
=None):
76 'decrypt_passwords': True
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
)
83 'controls': json
.dumps(target_controls
)
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
)
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
)
95 'mode': 'reconfigure',
96 'controls': json
.dumps(target_controls
)
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
,
104 'ip_address': ','.join(ip_address
),
108 @RestClient.api_get('/api/gatewayinfo')
109 def get_gatewayinfo(self
, request
=None):
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
)
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
)
122 'backstore': backstore
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
)
129 'preserve_image': 'true'
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
)
136 'controls': json
.dumps(controls
),
137 'mode': 'reconfigure'
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
,
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
,
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
)
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
)
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
,
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
,
181 'username': username
,
182 'password': password
,
183 'mutual_username': mutual_username
,
184 'mutual_password': mutual_password
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
)
191 'members': ','.join(members
),
192 'disks': ','.join(image_ids
)
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
)
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
)
206 'password': password
,
207 'mutual_username': mutual_user
,
208 'mutual_password': mutual_password
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
)
218 @RestClient.api_get('/api/targetinfo/{target_iqn}')
219 def get_targetinfo(self
, target_iqn
, request
=None):
220 # pylint: disable=unused-argument
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