Initial commit
[ta/distributed-state-server.git] / plugins / dssetcd.py
1 # Copyright 2019 Nokia
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 #
15
16 from dss.api import dss_plugin
17 from dss.api import dss_error
18
19 import ConfigParser
20 import logging
21 import os
22 import etcd
23
24 class dssetcd(dss_plugin.DSSPlugin):
25     """
26     Read the ini file. The structure is as follows
27     [etcd]
28     host = controller-1
29     port = 2380
30     """
31     def __init__(self, config_file):
32         super(dssetcd, self).__init__(config_file)
33         self.host = None
34         self.port = None
35         self.connected = False
36         try:
37             config = ConfigParser.ConfigParser()
38             config.read([config_file])
39             self.host = config.get('etcd', 'host')
40             self.port = int(config.get('etcd', 'port'))
41             self._connect()
42         except Exception as exp:
43             pass
44
45
46     def _connect(self):
47         try:
48             self.client = etcd.Client(self.host, self.port)
49             self.connected = True
50         except Exception as exp:
51             self.connected = False
52             raise dss_error.Error(exp)
53
54     def get(self, domain, name):
55         if not self.connected:
56             self._connect()
57
58         try:
59             value = self.client.read('/'+domain+'/'+name)
60             return value.value
61         except Exception as exp:
62             raise dss_error.Error(exp)
63
64     def get_domain(self, domain):
65         if not self.connected:
66             self._connect()
67
68         ret = {}
69         try:
70             values = self.client.read('/'+domain)
71             for value in values._children:
72                 if 'dir' not in value and 'key' in value:
73                     k = value['key']
74                     n = k.split('/')[2]
75                     v = value['value']
76                     ret[n] = v
77             return ret
78         except Exception as exp:
79             raise dss_error.Error(exp)
80
81     def get_domains(self):
82         if not self.connected:
83             self._connect()
84         ret = []
85         try:
86             domains = self.client.read('/')
87             for domain in domains._children:
88                 if 'dir' in domain and domain['dir'] and 'key' in domain:
89                     d = domain['key']
90                     v = d.split('/')[1]
91                     ret.append(v)
92             return ret
93         except Exception as exp:
94             raise dss_error.Error(exp)
95
96     def set(self, domain, name, value):
97         if not self.connected:
98             self._connect()
99
100         try:
101             self.client.write('/'+domain+'/'+name, value)
102         except Exception as exp:
103             raise dss_error.Error(exp)
104
105     def delete(self, domain, name):
106         if not self.connected:
107             self._connect()
108
109         try:
110             self.client.delete('/'+domain+'/'+name)
111         except Exception as exp:
112             raise dss_error.Error(exp)
113
114     def delete_domain(self, domain):
115         if not self.connected:
116             self._connect()
117
118         try:
119             self.client.delete('/'+domain, recursive=True, dir=True)
120         except Exception as exp:
121             raise dss_error.Error(exp)