-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update data_interfaces lib to include opensearch provider #30
Changes from 218 commits
23ce1a1
bfcfd93
8b9fe45
5758fed
f8c7d3f
bf2aaca
1b3f8f3
d623669
f9d7842
ba1f465
1ffc8d6
9ad5f4c
061f9b8
ea140de
bb91a53
cc788f2
4d016de
ca90489
b9befd9
74a48d7
c1e40ee
fdcb7fd
d7e2311
a52bbab
3482c03
84cf77d
ec1ad5f
76f7e62
9ec6fa1
1734415
f754abb
ba277c6
a043f41
abdd8c6
b85ee0e
4858b37
996af8a
f53a553
d4e9b20
c41ff91
aadad85
3d21e42
98f6e81
14c9452
0135fe7
c9c14bf
742b700
0530682
539d85e
22028d7
1fa2b40
8d0de7d
c170df9
bd86744
16287a9
f63a665
67a1498
359121a
211e981
0b47f1f
293eaba
dda51ca
d0e0222
35e3320
1d2f2b3
c431a59
8cb0226
cfa6997
ff2a7af
c2cbcf0
4827f57
aa213ed
a9e8d4b
0384735
982bf7f
391febd
7ba470f
30279db
d093e48
7f8357d
e06dfc8
3276763
54aa2a8
da00061
046dace
c6eb751
a6fe66f
d9f7984
b5dc2ec
9182ed0
51e05f8
158a367
a321c58
00960ef
6c91b5a
d06d4b9
3c36bf1
099fb36
d0573b2
fc5ff1d
69fd3ac
613996e
00aebe8
8b5b373
2e0c42c
d14a304
0182c36
2ea912b
a2438bb
17817b9
9a9573e
77ecb78
48b0d46
1eb2445
87223eb
b1f2a27
180fdff
97ae728
7112f4d
105517c
5fc2ff2
3239ddb
56f2845
fc05a37
2a76d28
c4b04dd
522fb42
90641b2
10ff318
d5e5e6d
4514299
c5d82a6
117c467
4baa522
775cc59
8761fb0
c679f11
364c7a6
6e41f35
34528c1
02371fc
309cc06
f5d0c4a
86595bc
1599e2f
31c4b38
d7050e7
c81d459
a3913f1
fbf0425
cc382e5
e1069d0
f8c271a
9f29434
fc517d5
ebbcb17
6863fad
41887ab
de621b9
28244cb
2d6b418
24e17f3
17b7447
710f8ac
ddea28b
58f7b8a
4203eb3
0f97333
300cbf7
e1aca97
a286edf
2caf2d5
8959941
2433eac
ba83d3f
e8881bc
67f5599
28ca17f
1ca1b2c
b7df1fd
d3e4a43
483b872
736276d
ded5a9d
6ef1e4b
0cb96f0
923a88a
b016b00
669ef72
cd31a28
d72eb3d
9f7d9ee
9db7fa1
57ab3a4
39459a6
b99f9b1
54a5c21
edbfe0b
be8beea
844252b
f32a964
cc308b9
d16b8e0
2352a0b
4e26c02
7eceac9
a3af2e9
6af639e
bfd387a
3e75fe6
487b0fb
9b22c52
a0b3347
6d72368
9e8da66
333fc7c
9edb2f4
ce1ded6
6d36e89
7795fa5
2ffd925
91c1d7a
6ec371c
6917f42
1c1dd66
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,5 +9,6 @@ __pycache__/ | |
.vscode | ||
|
||
*.tar.gz | ||
*.tar.xz | ||
cloudinit-userdata.yaml | ||
/.pytest_cache/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -304,7 +304,7 @@ def _on_topic_requested(self, event: TopicRequestedEvent): | |
|
||
# Increment this PATCH version before using `charmcraft publish-lib` or reset | ||
# to 0 if you are raising the major API version | ||
LIBPATCH = 4 | ||
LIBPATCH = 6 | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
@@ -659,7 +659,7 @@ def replset(self) -> Optional[str]: | |
def uris(self) -> Optional[str]: | ||
"""Returns the connection URIs. | ||
|
||
MongoDB, Redis, OpenSearch. | ||
MongoDB, Redis. | ||
""" | ||
return self.relation.data[self.relation.app].get("uris") | ||
|
||
|
@@ -969,7 +969,7 @@ class KafkaRequiresEvent(RelationEvent): | |
|
||
@property | ||
def bootstrap_server(self) -> Optional[str]: | ||
"""Returns a a comma-seperated list of broker uris.""" | ||
"""Returns a a comma-separated list of broker uris.""" | ||
return self.relation.data[self.relation.app].get("endpoints") | ||
|
||
@property | ||
|
@@ -1049,7 +1049,7 @@ def set_zookeeper_uris(self, relation_id: int, zookeeper_uris: str) -> None: | |
|
||
Args: | ||
relation_id: the identifier for a particular relation. | ||
zookeeper_uris: comma-seperated list of ZooKeeper server uris. | ||
zookeeper_uris: comma-separated list of ZooKeeper server uris. | ||
""" | ||
self._update_relation_data(relation_id, {"zookeeper-uris": zookeeper_uris}) | ||
|
||
|
@@ -1096,7 +1096,7 @@ def _on_relation_changed_event(self, event: RelationChangedEvent) -> None: | |
# “endpoints_changed“ event if “topic_created“ is triggered. | ||
return | ||
|
||
# Emit an endpoints (bootstap-server) changed event if the Kakfa endpoints | ||
# Emit an endpoints (bootstrap-server) changed event if the Kakfa endpoints | ||
# added or changed this info in the relation databag. | ||
if "endpoints" in diff.added or "endpoints" in diff.changed: | ||
# Emit the default event (the one without an alias). | ||
|
@@ -1105,3 +1105,164 @@ def _on_relation_changed_event(self, event: RelationChangedEvent) -> None: | |
event.relation, app=event.app, unit=event.unit | ||
) # here check if this is the right design | ||
return | ||
|
||
|
||
# Opensearch related events | ||
|
||
|
||
class OpenSearchProvidesEvent(RelationEvent): | ||
"""Base class for OpenSearch events.""" | ||
|
||
@property | ||
def index(self) -> Optional[str]: | ||
"""Returns the index that was requested.""" | ||
return self.relation.data[self.relation.app].get("index") | ||
|
||
|
||
class IndexRequestedEvent(OpenSearchProvidesEvent, ExtraRoleEvent): | ||
"""Event emitted when a new index is requested for use on this relation.""" | ||
|
||
|
||
class OpenSearchProvidesEvents(CharmEvents): | ||
"""OpenSearch events. | ||
|
||
This class defines the events that OpenSearch can emit. | ||
""" | ||
|
||
index_requested = EventSource(IndexRequestedEvent) | ||
|
||
|
||
class OpenSearchRequiresEvent(DatabaseRequiresEvent): | ||
"""Base class for OpenSearch requirer events.""" | ||
|
||
|
||
class IndexCreatedEvent(AuthenticationEvent, OpenSearchRequiresEvent): | ||
"""Event emitted when a new index is created for use on this relation.""" | ||
|
||
|
||
class OpenSearchRequiresEvents(CharmEvents): | ||
"""OpenSearch events. | ||
|
||
This class defines the events that the opensearch requirer can emit. | ||
""" | ||
|
||
index_created = EventSource(IndexCreatedEvent) | ||
endpoints_changed = EventSource(DatabaseEndpointsChangedEvent) | ||
authentication_updated = EventSource(AuthenticationEvent) | ||
|
||
|
||
# OpenSearch Provides and Requires Objects | ||
|
||
|
||
class OpenSearchProvides(DataProvides): | ||
"""Provider-side of the OpenSearch relation.""" | ||
|
||
on = OpenSearchProvidesEvents() | ||
|
||
def __init__(self, charm: CharmBase, relation_name: str) -> None: | ||
super().__init__(charm, relation_name) | ||
|
||
def _on_relation_changed(self, event: RelationChangedEvent) -> None: | ||
"""Event emitted when the relation has changed.""" | ||
# Only the leader should handle this event. | ||
if not self.local_unit.is_leader(): | ||
return | ||
|
||
# Check which data has changed to emit customs events. | ||
diff = self._diff(event) | ||
|
||
# Emit an index requested event if the setup key (index name and optional extra user roles) | ||
# have been added to the relation databag by the application. | ||
if "index" in diff.added: | ||
self.on.index_requested.emit(event.relation, app=event.app, unit=event.unit) | ||
|
||
def set_index(self, relation_id: int, index: str) -> None: | ||
"""Set the index in the application relation databag. | ||
|
||
Args: | ||
relation_id: the identifier for a particular relation. | ||
index: the index as it is _created_ on the provider charm. This needn't match the | ||
requested index, and can be used to present a different index name if, for example, | ||
the requested index is invalid. | ||
""" | ||
self._update_relation_data(relation_id, {"index": index}) | ||
|
||
def set_endpoints(self, relation_id: int, endpoints: str) -> None: | ||
"""Set the endpoints in the application relation databag. | ||
|
||
Args: | ||
relation_id: the identifier for a particular relation. | ||
endpoints: the endpoint addresses for opensearch nodes. | ||
""" | ||
self._update_relation_data(relation_id, {"endpoints": endpoints}) | ||
|
||
def set_version(self, relation_id: int, version: str) -> None: | ||
"""Set the database version in the application relation databag. | ||
|
||
Args: | ||
relation_id: the identifier for a particular relation. | ||
version: database version. | ||
""" | ||
self._update_relation_data(relation_id, {"version": version}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the provider side, you will need to include the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks for letting me know, I've also opened a PR in that repo since the opensearch spec didn't get the update :( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this has been added in ce1ded6 |
||
|
||
|
||
class OpenSearchRequires(DataRequires): | ||
"""Requires-side of the OpenSearch relation.""" | ||
|
||
on = OpenSearchRequiresEvents() | ||
|
||
def __init__( | ||
self, charm, relation_name: str, index: str, extra_user_roles: Optional[str] = None | ||
): | ||
"""Manager of OpenSearch client relations.""" | ||
super().__init__(charm, relation_name, extra_user_roles) | ||
self.charm = charm | ||
self.index = index | ||
|
||
def _on_relation_joined_event(self, event: RelationJoinedEvent) -> None: | ||
"""Event emitted when the application joins the OpenSearch relation.""" | ||
# Sets both index and extra user roles in the relation if the roles are provided. | ||
# Otherwise, sets only the index. | ||
data = {"index": self.index} | ||
if self.extra_user_roles: | ||
data["extra-user-roles"] = self.extra_user_roles | ||
|
||
self._update_relation_data(event.relation.id, data) | ||
|
||
def _on_relation_changed_event(self, event: RelationChangedEvent) -> None: | ||
"""Event emitted when the OpenSearch relation has changed. | ||
|
||
This event triggers individual custom events depending on the changing relation. | ||
""" | ||
# Check which data has changed to emit customs events. | ||
diff = self._diff(event) | ||
|
||
# Check if the index is created | ||
# (the OpenSearch charm shares the credentials). | ||
if "username" in diff.added and "password" in diff.added: | ||
# Emit the default event (the one without an alias). | ||
logger.info("index created at: %s", datetime.now()) | ||
self.on.index_created.emit(event.relation, app=event.app, unit=event.unit) | ||
|
||
# To avoid unnecessary application restarts do not trigger | ||
# “endpoints_changed“ or "authentication_updated" event if “index_created“ is | ||
# triggered. | ||
return | ||
|
||
# Check if authentication has updated, emit event if so | ||
updates = {"password", "tls", "tls-ca"} | ||
if len(set(diff._asdict().keys()) - updates) < len(diff): | ||
logger.info("authentication updated at: %s", datetime.now()) | ||
self.on.authentication_updated.emit(event.relation, app=event.app, unit=event.unit) | ||
|
||
return | ||
|
||
# Emit a endpoints changed event if the OpenSearch application added or changed this info | ||
# in the relation databag. | ||
if "endpoints" in diff.added or "endpoints" in diff.changed: | ||
# Emit the default event (the one without an alias). | ||
logger.info("endpoints changed on %s", datetime.now()) | ||
self.on.endpoints_changed.emit( | ||
event.relation, app=event.app, unit=event.unit | ||
) # here check if this is the right design | ||
return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uhm... this should not really be here, but one should rather commit these modifications in the dataplatform-lib here. We should really never modify the libs within the projects but rather submit PR to the "upstream"
Although it may make sense to have custom events/classes, one thing that we were trying to promote was the use (whenever possible) of same classes across databases. I'm wondering whether one could use the already existing classes, just for the sake of avoiding to create too many granular classes.
Also, classes there should follow the requirements that are outlined in https://github.com/canonical/charm-relation-interfaces
I understand the process there may be a bit convoluted (requiring other people to review the interface spec), but I suppose the idea is to try to make interfaces and data-platform libs somewhat more opinionated and controlled.
If you want to get this merged, maybe it could be better to move these classes in the opensearch library for the time being, while starting the approval/review process for the interface
@delgod what is your point of view here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These changes were actually proposed by Mykola so we can have an interface that's more specific to opensearch; I'm aware that changes to this library should be proposed in the data-platform-lib repo, but I wanted to implement and test them in this repo first so we can ensure the library is fit for purpose before publishing. The same is true of PR #31 - I'll document this better in the PR description.