forked from juju-solutions/interface-elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
requires.py
51 lines (44 loc) · 2.05 KB
/
requires.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/bin/env python3
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# pylint: disable=c0111,c0103,c0301
from charms.reactive import RelationBase
from charms.reactive import hook
from charms.reactive import scopes
class ElasticSearchClient(RelationBase):
# Existing elasticsearch client interface listed here:
# https://api.jujucharms.com/charmstore/v5/trusty/elasticsearch-13/archive/playbook.yaml
scope = scopes.UNIT
@hook('{requires:elasticsearch}-relation-{joined,changed}')
def changed(self):
self.set_state('{relation_name}.connected')
conv = self.conversation()
# assume we have all the data if we have the port
if conv.get_remote('port'):
conv.set_state('{relation_name}.available')
@hook('{requires:elasticsearch}-relation-{departed,broken}')
def departed(self):
conv = self.conversation()
conv.remove_state('{relation_name}.connected')
conv.remove_state('{relation_name}.available')
conv.set_state('{relation_name}.broken')
def list_unit_data(self):
''' Iterate through all ElasticSearch conversations and return the data
for each cached conversation. This allows us to build a cluster string
directly from the relation data. eg:
for unit in elasticsearch.list_data():
print(unit['cluster_name'])
'''
for conv in self.conversations():
yield {'cluster_name': conv.get_remote('cluster_name'),
'host': conv.get_remote('private-address'),
'port': conv.get_remote('port')}