1 # -*- coding: utf-8 -*-
2 from __future__
import absolute_import
6 from orchestrator
import OrchestratorError
9 from urlparse
import urlparse
11 from urllib
.parse
import urlparse
13 from mgr_util
import merge_dicts
14 from .orchestrator
import OrchClient
18 class IscsiGatewayAlreadyExists(Exception):
19 def __init__(self
, gateway_name
):
20 super(IscsiGatewayAlreadyExists
, self
).__init
__(
21 "iSCSI gateway '{}' already exists".format(gateway_name
))
24 class IscsiGatewayDoesNotExist(Exception):
25 def __init__(self
, hostname
):
26 super(IscsiGatewayDoesNotExist
, self
).__init
__(
27 "iSCSI gateway '{}' does not exist".format(hostname
))
30 class IscsiGatewayInUse(Exception):
31 def __init__(self
, hostname
):
32 super(IscsiGatewayInUse
, self
).__init
__(
33 "iSCSI gateway '{}' is in use".format(hostname
))
36 class InvalidServiceUrl(Exception):
37 def __init__(self
, service_url
):
38 super(InvalidServiceUrl
, self
).__init
__(
39 "Invalid service URL '{}'. "
40 "Valid format: '<scheme>://<username>:<password>@<host>[:port]'.".format(service_url
))
43 class ManagedByOrchestratorException(Exception):
45 super(ManagedByOrchestratorException
, self
).__init
__(
46 "iSCSI configuration is managed by the orchestrator")
49 _ISCSI_STORE_KEY
= "_iscsi_config"
52 class IscsiGatewaysConfig(object):
54 def _load_config_from_store(cls
):
55 json_db
= mgr
.get_store(_ISCSI_STORE_KEY
,
57 return json
.loads(json_db
)
60 def _load_config_from_orchestrator():
61 config
= {'gateways': {}}
63 instances
= OrchClient().list_service_info("iscsi")
64 for instance
in instances
:
65 config
['gateways'][instance
.nodename
] = {
66 'service_url': instance
.service_url
68 except (RuntimeError, OrchestratorError
, ImportError):
73 def _save_config(cls
, config
):
74 mgr
.set_store(_ISCSI_STORE_KEY
, json
.dumps(config
))
77 def validate_service_url(cls
, service_url
):
78 url
= urlparse(service_url
)
79 if not url
.scheme
or not url
.hostname
or not url
.username
or not url
.password
:
80 raise InvalidServiceUrl(service_url
)
83 def add_gateway(cls
, name
, service_url
):
84 config
= cls
.get_gateways_config()
86 raise IscsiGatewayAlreadyExists(name
)
87 IscsiGatewaysConfig
.validate_service_url(service_url
)
88 config
['gateways'][name
] = {'service_url': service_url
}
89 cls
._save
_config
(config
)
92 def remove_gateway(cls
, name
):
93 if name
in cls
._load
_config
_from
_orchestrator
()['gateways']:
94 raise ManagedByOrchestratorException()
96 config
= cls
._load
_config
_from
_store
()
97 if name
not in config
['gateways']:
98 raise IscsiGatewayDoesNotExist(name
)
100 del config
['gateways'][name
]
101 cls
._save
_config
(config
)
104 def get_gateways_config(cls
):
105 orch_config
= cls
._load
_config
_from
_orchestrator
()
106 local_config
= cls
._load
_config
_from
_store
()
108 return {'gateways': merge_dicts(orch_config
['gateways'], local_config
['gateways'])}
111 def get_gateway_config(cls
, name
):
112 config
= IscsiGatewaysConfig
.get_gateways_config()
113 if name
not in config
['gateways']:
114 raise IscsiGatewayDoesNotExist(name
)
115 return config
['gateways'][name
]