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_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
)
117 'backstore': backstore
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
)
124 'preserve_image': 'true'
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
)
131 'controls': json
.dumps(controls
),
132 'mode': 'reconfigure'
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
,
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
,
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
)
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
)
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
,
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
,
176 'username': username
,
177 'password': password
,
178 'mutual_username': mutual_username
,
179 'mutual_password': mutual_password
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
)
186 'members': ','.join(members
),
187 'disks': ','.join(image_ids
)
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
)
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
)
201 'password': password
,
202 'mutual_username': mutual_user
,
203 'mutual_password': mutual_password
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
)
213 @RestClient.api_get('/api/targetinfo/{target_iqn}')
214 def get_targetinfo(self
, target_iqn
, request
=None):
215 # pylint: disable=unused-argument