Skip to content
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

Merged
merged 225 commits into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from 218 commits
Commits
Show all changes
225 commits
Select commit Hold shift + click to select a range
23ce1a1
some quality of life suggestions
Jan 18, 2023
bfcfd93
added basic skeleton for relation
Jan 18, 2023
8b9fe45
fix data_interfaces name
Jan 18, 2023
5758fed
updated copyright string, and added test shell
Jan 18, 2023
f8c7d3f
format
Jan 18, 2023
bf2aaca
added testing infra
Jan 18, 2023
1b3f8f3
lint
Jan 18, 2023
d623669
update copyright notice
Jan 18, 2023
f9d7842
testing
Jan 18, 2023
ba1f465
lint
Jan 18, 2023
1ffc8d6
small fixes, setting up testing
Jan 18, 2023
9ad5f4c
learning about opensearch py lib
Jan 18, 2023
061f9b8
merge
Jan 18, 2023
ea140de
Delete juju-crashdump-5a62e3a0-5d70-4cb7-8d6b-07b04b94f45d.tar.xz
WRFitch Jan 19, 2023
bb91a53
removed unnecessary updates to operator charm
Jan 19, 2023
cc788f2
Merge branch 'dev/add-relation' of https://github.com/canonical/opens…
Jan 19, 2023
4d016de
update to match the rest of the charm
Jan 19, 2023
ca90489
fix tests
Jan 20, 2023
b9befd9
Setting up for client relation (#26)
WRFitch Jan 20, 2023
74a48d7
Merge branch 'feature/client-relation' of https://github.com/canonica…
Jan 20, 2023
c1e40ee
added some prelim relation data
Jan 23, 2023
fdcb7fd
removed superfluous PR pipeline
Jan 23, 2023
d7e2311
made peer relation name a constant to prevent circular import between…
Jan 23, 2023
a52bbab
added user management
Jan 24, 2023
3482c03
added relation teardown
Jan 25, 2023
84cf77d
fixing up test charm
Jan 25, 2023
ec1ad5f
unit testing opensearch_users.py
Jan 25, 2023
76f7e62
finished unit tests
Jan 27, 2023
9ec6fa1
fixed users, updated tests
Jan 31, 2023
1734415
added small preliminary changes to clean up major relation PR
Jan 31, 2023
f754abb
update to test charm
Jan 31, 2023
ba277c6
updated user creation
Feb 1, 2023
a043f41
updated version function
Feb 2, 2023
abdd8c6
fixed lint
Feb 2, 2023
b85ee0e
added some basic user management code for client relation
Feb 2, 2023
4858b37
lint
WRFitch Feb 2, 2023
996af8a
Merge branch 'dev/prelim' of https://github.com/canonical/opensearch-…
Feb 2, 2023
f53a553
docs
Feb 2, 2023
d4e9b20
Cleaning up major relation PR (#27)
WRFitch Feb 2, 2023
c41ff91
fixed permissions in charm
Feb 2, 2023
aadad85
merge
Feb 2, 2023
3d21e42
fix version access
Feb 2, 2023
98f6e81
objectified opensearch users
Feb 2, 2023
14c9452
removing unnecessary logging
Feb 2, 2023
0135fe7
cleaning up PR
Feb 2, 2023
c9c14bf
removed unnecessary TODO, since it's been done
Feb 2, 2023
742b700
Merge branch 'dev/users' into dev/add-relation
WRFitch Feb 2, 2023
0530682
fixed unit tests and user mgmt
Feb 2, 2023
539d85e
removed copy-paste errors
Feb 2, 2023
22028d7
updated CI
Feb 2, 2023
1fa2b40
Added user management code for client relation (#28)
WRFitch Feb 3, 2023
8d0de7d
added template tests
Feb 3, 2023
c170df9
fixed user mgmt
Feb 3, 2023
bd86744
merge users
Feb 3, 2023
16287a9
removed wait between relations
Feb 3, 2023
f63a665
added endpoint checking test
Feb 3, 2023
67a1498
added version test
Feb 3, 2023
359121a
added more tests
Feb 3, 2023
211e981
cleaning up low-hanging fruit
Feb 6, 2023
0b47f1f
PR updates:
Feb 6, 2023
293eaba
updated user failure handling
Feb 6, 2023
dda51ca
Merge branch 'dev/add-relation' of https://github.com/canonical/opens…
Feb 6, 2023
d0e0222
added more error logging
Feb 6, 2023
35e3320
more error logging
Feb 6, 2023
1d2f2b3
fixed zombie hook
Feb 7, 2023
c431a59
removed zombie term, and wrangled lint into line
Feb 7, 2023
8cb0226
removing unnecessary constants
Feb 7, 2023
cfa6997
merge
Feb 7, 2023
ff2a7af
fixed up test material
Feb 7, 2023
c2cbcf0
fixed up test material
Feb 7, 2023
4827f57
updated test data so we're not doing copyright infringement
Feb 7, 2023
aa213ed
working on bulk and teardown tests
Feb 7, 2023
a9e8d4b
fixed 404 error
Feb 8, 2023
0384735
updated test requests to get around json nightmare
Feb 8, 2023
982bf7f
fixing yaml
Feb 8, 2023
391febd
reinstated relation-broken test
Feb 8, 2023
7ba470f
fixed single put action
Feb 8, 2023
30279db
fixing up tests, and adding http status code to httperror
Feb 8, 2023
d093e48
added opensearch-provider lib
Feb 8, 2023
7f8357d
swapping to opensearchprovides
Feb 8, 2023
e06dfc8
removed superfluous code
Feb 9, 2023
3276763
updating request to maybe work properly
Feb 9, 2023
54aa2a8
updated terminology in test charm
Feb 9, 2023
da00061
Add basic client relation and testing infrastructure (#25)
WRFitch Feb 9, 2023
046dace
Merge branch 'feature/client-relation' into dev/client-relation-tests
WRFitch Feb 9, 2023
c6eb751
Merge branch 'feature/client-relation' into dev/add-lib
WRFitch Feb 9, 2023
a6fe66f
fix lint and unit
Feb 9, 2023
d9f7984
fixing lint after merge
Feb 9, 2023
b5dc2ec
made charm.py executable again
Feb 9, 2023
9182ed0
making test charm executable again
Feb 9, 2023
51e05f8
fixing relation-id variable
Feb 9, 2023
158a367
updated events
Feb 9, 2023
a321c58
fixed results
Feb 9, 2023
00960ef
merging in new tests
Feb 9, 2023
6c91b5a
user check
Feb 9, 2023
d06d4b9
fixed json
Feb 9, 2023
3c36bf1
updating topic
Feb 9, 2023
099fb36
added ip
Feb 9, 2023
d0573b2
remove user test
WRFitch Feb 10, 2023
fc5ff1d
lint
WRFitch Feb 10, 2023
69fd3ac
lint, but properly
Feb 10, 2023
613996e
Merge branch 'dev/client-relation-tests' of https://github.com/canoni…
Feb 10, 2023
00aebe8
removed more hosts
Feb 10, 2023
8b5b373
still removing topics and endpoints
Feb 10, 2023
2e0c42c
updating endpoints once more
Feb 10, 2023
d14a304
fixing up relation access
Feb 10, 2023
0182c36
added user check
Feb 10, 2023
2ea912b
updating lib and ops versions
Feb 10, 2023
a2438bb
updated ops in test charm
Feb 13, 2023
17817b9
removed hosts
Feb 13, 2023
9a9573e
fixed endpoint update
Feb 13, 2023
77ecb78
added endpoint management
Feb 14, 2023
48b0d46
another commit to trigger CI
Feb 14, 2023
1eb2445
PR feedback
Feb 17, 2023
87223eb
Merge branch 'dev/client-relation-tests' into dev/add-lib
WRFitch Feb 17, 2023
b1f2a27
Update test_opensearch_provider.py
WRFitch Feb 17, 2023
180fdff
deleting old functions properly
Feb 17, 2023
97ae728
minor pr feedback
Feb 17, 2023
7112f4d
Merge branch 'dev/client-relation-tests' into dev/add-lib
WRFitch Feb 17, 2023
105517c
updated action variable names
Feb 17, 2023
5fc2ff2
Merge branch 'dev/add-lib' of https://github.com/canonical/opensearch…
Feb 17, 2023
3239ddb
updated json parsing in test actions
Feb 17, 2023
56f2845
moved logging in test
Feb 17, 2023
fc05a37
Update actions.yaml
WRFitch Feb 17, 2023
2a76d28
updated CI to use fewer runners
Feb 17, 2023
c4b04dd
Merge branch 'dev/client-relation-tests' of https://github.com/canoni…
Feb 17, 2023
522fb42
removed old actions
Feb 17, 2023
90641b2
Update helpers.py
WRFitch Feb 17, 2023
10ff318
fixed relation_id again
Feb 20, 2023
d5e5e6d
resolving some PR feedback
Feb 20, 2023
4514299
handling exceptions better
Feb 20, 2023
c5d82a6
PR nits
Feb 21, 2023
117c467
Merge branch 'dev/client-relation-tests' into dev/add-lib
WRFitch Feb 21, 2023
4baa522
fixing databag access
Feb 21, 2023
775cc59
Update charm.py
WRFitch Feb 21, 2023
8761fb0
fixed first_index
WRFitch Feb 21, 2023
c679f11
Removed lingering user code (#32)
WRFitch Feb 21, 2023
364c7a6
Client Relation Tests (#29)
WRFitch Feb 21, 2023
6e41f35
Merge branch 'feature/client-relation' into dev/add-lib
WRFitch Feb 21, 2023
34528c1
lint
Feb 21, 2023
02371fc
fixing merge
Feb 21, 2023
309cc06
Merge branch 'main' of https://github.com/canonical/opensearch-operat…
Feb 21, 2023
f5d0c4a
merging main
Feb 21, 2023
86595bc
updating user role check
Feb 22, 2023
1599e2f
Update ci.yaml
WRFitch Feb 22, 2023
31c4b38
fixing extra user roles
Feb 22, 2023
d7050e7
merge
Feb 22, 2023
c81d459
updated request
Feb 22, 2023
a3913f1
merge
Feb 22, 2023
fbf0425
fixed payloads
Feb 22, 2023
cc382e5
merge
Feb 23, 2023
e1069d0
updating events on relation change
Feb 23, 2023
f8c271a
running tests on PR
Feb 23, 2023
9f29434
added a simple scaling test
Feb 24, 2023
fc517d5
fixed internal funcs
Feb 24, 2023
ebbcb17
checking correct number of opensearch units
Feb 24, 2023
6863fad
updated sysctl command
Feb 24, 2023
41887ab
merge in main
Feb 24, 2023
de621b9
cleaning up merge
Feb 27, 2023
28244cb
Merge branch 'main' into dev/add-lib
WRFitch Feb 27, 2023
2d6b418
added TLS CA to charm client relation (#33)
WRFitch Feb 27, 2023
24e17f3
added libid etc
Feb 27, 2023
17b7447
added updates to tests
Feb 27, 2023
710f8ac
Merge branch 'dev/add-lib' of https://github.com/canonical/opensearch…
Feb 27, 2023
ddea28b
increasing sleeps
Feb 27, 2023
58f7b8a
added correct test marker
Feb 28, 2023
4203eb3
Merge branch 'dev/add-lib' of https://github.com/canonical/opensearch…
Feb 28, 2023
0f97333
removed ffwd
Feb 28, 2023
300cbf7
lint
Feb 28, 2023
e1aca97
removing ffwd seems to fix the service operation bug, removing timeouts
Feb 28, 2023
a286edf
increasing timeout
Feb 28, 2023
2caf2d5
added libid for new libs
Feb 28, 2023
8959941
updating docs to trigger a proper test rerun
Feb 28, 2023
2433eac
Merge branch 'dev/add-lib' of https://github.com/canonical/opensearch…
Feb 28, 2023
ba83d3f
experimenting with yellow state
Feb 28, 2023
e8881bc
added leader-elected hook to rolling-ops
Feb 28, 2023
67f5599
added leader-elected hook to rolling-ops
Feb 28, 2023
28ca17f
wait for opensearch app to be active
Feb 28, 2023
1ca1b2c
lint
Feb 28, 2023
b7df1fd
updated blocking
Mar 1, 2023
d3e4a43
Merge branch 'main' of https://github.com/canonical/opensearch-operat…
Mar 1, 2023
483b872
Merge branch 'main' of https://github.com/canonical/opensearch-operat…
Mar 1, 2023
736276d
printing application status throughout test
Mar 1, 2023
ded5a9d
added default case for nodes
Mar 1, 2023
6ef1e4b
skipping second relation
Mar 1, 2023
0cb96f0
updating status on correct host
Mar 1, 2023
923a88a
logging shard state
Mar 1, 2023
b016b00
merge
Mar 1, 2023
669ef72
merge
Mar 2, 2023
cd31a28
merge
Mar 2, 2023
d72eb3d
Merge branch 'dev/add-lib' of https://github.com/canonical/opensearch…
Mar 2, 2023
9f7d9ee
lint
Mar 2, 2023
9db7fa1
merge
Mar 2, 2023
57ab3a4
fixed yaml
Mar 2, 2023
39459a6
remove unit from endpoints if this unit is departing
Mar 2, 2023
b99f9b1
fine tuning test fixes
Mar 2, 2023
54a5c21
removing ip updates
Mar 2, 2023
edbfe0b
removing ip updates
Mar 2, 2023
be8beea
Update opensearch_distro.py
WRFitch Mar 3, 2023
844252b
Update test_opensearch_provider.py
WRFitch Mar 3, 2023
f32a964
switching scaling around to go easier on the CI
Mar 3, 2023
cc308b9
Merge branch 'dev/add-lib' of https://github.com/canonical/opensearch…
Mar 3, 2023
d16b8e0
updating tests and fixing regressions
Mar 3, 2023
2352a0b
added new test
Mar 3, 2023
4e26c02
fixed await
Mar 3, 2023
7eceac9
updating endpoints everywhere
Mar 3, 2023
a3af2e9
lint
Mar 3, 2023
6af639e
removing endpoint updates
WRFitch Mar 6, 2023
bfd387a
Update test_opensearch_provider.py
WRFitch Mar 6, 2023
3e75fe6
lint
Mar 6, 2023
487b0fb
fixed endpoint assignment, and triggered it during relation-departed
Mar 6, 2023
9b22c52
lib and unit test fixes
Mar 6, 2023
a0b3347
adding endpoint updates to status hook
Mar 6, 2023
6d72368
small fixes
Mar 6, 2023
9e8da66
committing again to verify tests aren't totally transient
Mar 6, 2023
333fc7c
get nodes using _get_nodes
Mar 6, 2023
9edb2f4
reverting rollingops changes to check if they're necessary
Mar 7, 2023
ce1ded6
added index functions to provider
Mar 7, 2023
6d36e89
Merge branch 'main' into dev/add-lib
WRFitch Mar 7, 2023
7795fa5
lint. I have to stop using the github merge tool, lint never works qu…
Mar 7, 2023
2ffd925
reset rolling ops
WRFitch Mar 7, 2023
91c1d7a
Update test_opensearch_tls.py
WRFitch Mar 8, 2023
6ec371c
final cleanup for PR
Mar 8, 2023
6917f42
Update opensearch_relation_provider.py
WRFitch Mar 8, 2023
1c1dd66
lint
Mar 8, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ jobs:
- name: Run integration tests
run: |
# set sysctl values in case the cloudinit-userdata not applied
sudo sysctl -w vm.max_map_count=262144
sudo sysctl -w vm.swappiness=0
sudo sysctl -w net.ipv4.tcp_retries2=5
sudo sysctl -w vm.max_map_count=262144 vm.swappiness=0 net.ipv4.tcp_retries2=5

tox run -e ${{ matrix.tox-environments }} -- -m '${{ steps.select-tests.outputs.mark_expression }}'
env:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ __pycache__/
.vscode

*.tar.gz
*.tar.xz
cloudinit-userdata.yaml
/.pytest_cache/
8 changes: 3 additions & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,12 @@ cloudinit-userdata: |
- [ 'sysctl', '-w', 'fs.file-max=1048576' ]
EOF
```

or in a single machine:
```
sudo sysctl -w vm.max_map_count=262144
sudo sysctl -w vm.swappiness=0
sudo sysctl -w net.ipv4.tcp_retries2=5
sudo sysctl -w vm.max_map_count=262144 vm.swappiness=0 net.ipv4.tcp_retries2=5
```


Then create a new model and set the previously generated file in it.
```bash
# Create a model
Expand Down Expand Up @@ -101,7 +99,7 @@ juju deploy -n 1 ./opensearch_ubuntu-22.04-amd64.charm --series jammy --show-log
juju relate tls-certificates-operator opensearch
```

**Note:** The TLS settings shown here are for self-signed-certificates, which are not recommended for production clusters. The TLS Certificates Operator offers a variety of configurations. Read more on the TLS Certificates Operator [here](https://charmhub.io/tls-certificates-operator).
**Note:** The TLS settings shown here are for self-signed-certificates, which are not recommended for production clusters. The TLS Certificates Operator offers a variety of configurations. Read more on the TLS Certificates Operator [here](https://charmhub.io/tls-certificates-operator).


## Canonical Contributor Agreement
Expand Down
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Bootstrap a [lxd controller](https://juju.is/docs/olm/lxd#heading--create-a-cont
juju add-model opensearch
```

Configure the system settings required by [OpenSearch](https://opensearch.org/docs/2.3/opensearch/install/important-settings/),
Configure the system settings required by [OpenSearch](https://opensearch.org/docs/2.3/opensearch/install/important-settings/),
we'll do that by creating and setting a [`cloudinit-userdata.yaml` file](https://juju.is/docs/olm/juju-model-config) on the model.
```
cat <<EOF > cloudinit-userdata.yaml
Expand All @@ -34,9 +34,7 @@ juju model-config ./cloudinit-userdata.yaml
```
or in a single machine:
```
sudo sysctl -w vm.max_map_count=262144
sudo sysctl -w vm.swappiness=0
sudo sysctl -w net.ipv4.tcp_retries2=5
sudo sysctl -w vm.max_map_count=262144 vm.swappiness=0 net.ipv4.tcp_retries2=5
```

### Basic Usage
Expand All @@ -55,10 +53,10 @@ Supported [relations](https://juju.is/docs/olm/relations):
The Charmed OpenSearch Operator also supports TLS encryption on the HTTP and Transport layers. TLS is enabled by default:

```shell
# Deploy the TLS Certificates Operator.
# Deploy the TLS Certificates Operator.
juju deploy tls-certificates-operator --channel=edge
# Add the necessary configurations for TLS.
juju config tls-certificates-operator generate-self-signed-certificates="true" ca-common-name="Test CA"
juju config tls-certificates-operator generate-self-signed-certificates="true" ca-common-name="Test CA"
# Enable TLS via relation.
juju relate opensearch tls-certificates-operator
# Disable TLS by removing relation.
Expand Down
171 changes: 166 additions & 5 deletions lib/charms/data_platform_libs/v0/data_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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})

Expand Down Expand Up @@ -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).
Expand All @@ -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
Copy link
Contributor

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?

Copy link
Contributor Author

@WRFitch WRFitch Feb 13, 2023

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.



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})
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the provider side, you will need to include the index parameter: canonical/charm-relation-interfaces#42.
In this case you will need to add a new function set_index in the provider.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 :(

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
16 changes: 11 additions & 5 deletions lib/charms/opensearch/v0/opensearch_base_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ def _on_opensearch_data_storage_detaching(self, _: StorageDetachingEvent):
def _on_update_status(self, event: UpdateStatusEvent):
"""On update status event.

We want to periodically check for 3 things:
We want to periodically check for the following:
1- Do we have users that need to be deleted, and if so we need to delete them.
2- The system requirements are still met
3- every 6 hours check if certs are expiring soon (in 7 days),
Expand All @@ -355,6 +355,9 @@ def _on_update_status(self, event: UpdateStatusEvent):
self.opensearch_exclusions.cleanup()
self._apply_cluster_health()

for relation in self.model.relations.get(ClientRelationName, []):
self.opensearch_provider.update_endpoints(relation)

self.user_manager.remove_users_and_roles()

# If relation broken - leave
Expand Down Expand Up @@ -472,8 +475,11 @@ def _is_tls_fully_configured(self) -> bool:
def _start_opensearch(self, event: EventBase) -> None: # noqa: C901
"""Start OpenSearch, with a generated or passed conf, if all resources configured."""
if self.opensearch.is_started():
self._post_start_init()
self.status.clear(WaitingToStart)
try:
self._post_start_init()
self.status.clear(WaitingToStart)
except OpenSearchHttpError:
event.defer()
return

if not self._can_service_start():
Expand Down Expand Up @@ -842,8 +848,8 @@ def _check_certs_expiration(self, event: UpdateStatusEvent) -> None:
certs = self.secrets.get_unit_certificates()

# keep certificates that are expiring in less than 24h
for cert_type, cert in certs.items():
hours = cert_expiration_remaining_hours(cert)
for cert_type in list(certs.keys()):
hours = cert_expiration_remaining_hours(certs[cert_type])
if hours > 24 * 7:
del certs[cert_type]

Expand Down
Loading