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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
16 from dss.api import dss_plugin
17 from dss.api import dss_error
24 class dssetcd(dss_plugin.DSSPlugin):
26 Read the ini file. The structure is as follows
31 def __init__(self, config_file):
32 super(dssetcd, self).__init__(config_file)
35 self.connected = False
37 config = ConfigParser.ConfigParser()
38 config.read([config_file])
39 self.host = config.get('etcd', 'host')
40 self.port = int(config.get('etcd', 'port'))
42 except Exception as exp:
48 self.client = etcd.Client(self.host, self.port)
50 except Exception as exp:
51 self.connected = False
52 raise dss_error.Error(exp)
54 def get(self, domain, name):
55 if not self.connected:
59 value = self.client.read('/'+domain+'/'+name)
61 except Exception as exp:
62 raise dss_error.Error(exp)
64 def get_domain(self, domain):
65 if not self.connected:
70 values = self.client.read('/'+domain)
71 for value in values._children:
72 if 'dir' not in value and 'key' in value:
78 except Exception as exp:
79 raise dss_error.Error(exp)
81 def get_domains(self):
82 if not self.connected:
86 domains = self.client.read('/')
87 for domain in domains._children:
88 if 'dir' in domain and domain['dir'] and 'key' in domain:
93 except Exception as exp:
94 raise dss_error.Error(exp)
96 def set(self, domain, name, value):
97 if not self.connected:
101 self.client.write('/'+domain+'/'+name, value)
102 except Exception as exp:
103 raise dss_error.Error(exp)
105 def delete(self, domain, name):
106 if not self.connected:
110 self.client.delete('/'+domain+'/'+name)
111 except Exception as exp:
112 raise dss_error.Error(exp)
114 def delete_domain(self, domain):
115 if not self.connected:
119 self.client.delete('/'+domain, recursive=True, dir=True)
120 except Exception as exp:
121 raise dss_error.Error(exp)