]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | # -*- coding: utf-8 -*- |
2 | from __future__ import absolute_import | |
3 | ||
4 | import json | |
5 | ||
6 | try: | |
7 | from urlparse import urlparse | |
8 | except ImportError: | |
9 | from urllib.parse import urlparse | |
10 | ||
11 | from .orchestrator import OrchClient | |
12 | from .. import mgr | |
13 | ||
14 | ||
15 | class IscsiGatewayAlreadyExists(Exception): | |
16 | def __init__(self, gateway_name): | |
17 | super(IscsiGatewayAlreadyExists, self).__init__( | |
18 | "iSCSI gateway '{}' already exists".format(gateway_name)) | |
19 | ||
20 | ||
21 | class IscsiGatewayDoesNotExist(Exception): | |
22 | def __init__(self, hostname): | |
23 | super(IscsiGatewayDoesNotExist, self).__init__( | |
24 | "iSCSI gateway '{}' does not exist".format(hostname)) | |
25 | ||
26 | ||
27 | class IscsiGatewayInUse(Exception): | |
28 | def __init__(self, hostname): | |
29 | super(IscsiGatewayInUse, self).__init__( | |
30 | "iSCSI gateway '{}' is in use".format(hostname)) | |
31 | ||
32 | ||
33 | class InvalidServiceUrl(Exception): | |
34 | def __init__(self, service_url): | |
35 | super(InvalidServiceUrl, self).__init__( | |
36 | "Invalid service URL '{}'. " | |
37 | "Valid format: '<scheme>://<username>:<password>@<host>[:port]'.".format(service_url)) | |
38 | ||
39 | ||
40 | class ManagedByOrchestratorException(Exception): | |
41 | def __init__(self): | |
42 | super(ManagedByOrchestratorException, self).__init__( | |
43 | "iSCSI configuration is managed by the orchestrator") | |
44 | ||
45 | ||
46 | _ISCSI_STORE_KEY = "_iscsi_config" | |
47 | ||
48 | ||
49 | class IscsiGatewaysConfig(object): | |
50 | @classmethod | |
51 | def _load_config(cls): | |
52 | if OrchClient.instance().available(): | |
53 | raise ManagedByOrchestratorException() | |
54 | json_db = mgr.get_store(_ISCSI_STORE_KEY, | |
55 | '{"gateways": {}}') | |
56 | return json.loads(json_db) | |
57 | ||
58 | @classmethod | |
59 | def _save_config(cls, config): | |
60 | mgr.set_store(_ISCSI_STORE_KEY, json.dumps(config)) | |
61 | ||
62 | @classmethod | |
63 | def validate_service_url(cls, service_url): | |
64 | url = urlparse(service_url) | |
65 | if not url.scheme or not url.hostname or not url.username or not url.password: | |
66 | raise InvalidServiceUrl(service_url) | |
67 | ||
68 | @classmethod | |
69 | def add_gateway(cls, name, service_url): | |
70 | config = cls._load_config() | |
71 | if name in config: | |
72 | raise IscsiGatewayAlreadyExists(name) | |
73 | IscsiGatewaysConfig.validate_service_url(service_url) | |
74 | config['gateways'][name] = {'service_url': service_url} | |
75 | cls._save_config(config) | |
76 | ||
77 | @classmethod | |
78 | def remove_gateway(cls, name): | |
79 | config = cls._load_config() | |
80 | if name not in config['gateways']: | |
81 | raise IscsiGatewayDoesNotExist(name) | |
82 | ||
83 | del config['gateways'][name] | |
84 | cls._save_config(config) | |
85 | ||
86 | @classmethod | |
87 | def get_gateways_config(cls): | |
88 | try: | |
89 | config = cls._load_config() | |
90 | except ManagedByOrchestratorException: | |
91 | config = {'gateways': {}} | |
92 | instances = OrchClient.instance().list_service_info("iscsi") | |
93 | for instance in instances: | |
94 | config['gateways'][instance.nodename] = { | |
95 | 'service_url': instance.service_url | |
96 | } | |
97 | return config | |
98 | ||
99 | @classmethod | |
100 | def get_gateway_config(cls, name): | |
101 | config = IscsiGatewaysConfig.get_gateways_config() | |
102 | if name not in config['gateways']: | |
103 | raise IscsiGatewayDoesNotExist(name) | |
104 | return config['gateways'][name] |