Switch to using mysql-k8s for database relation

Depends-On: https://review.opendev.org/c/openstack/charm-ops-sunbeam/+/855308

Change-Id: I245a5e56ff56cfc42ce23c36ff3fcba509a2058e
This commit is contained in:
Samuel Walladge 2022-09-01 16:14:21 +09:30
parent 15cfd08385
commit cb80255363
9 changed files with 568 additions and 188 deletions

View File

@ -16,7 +16,7 @@ and [Sunbeam documentation](sunbeam-docs).
nova-k8s charm uses the ops\_sunbeam library and extends
OSBaseOperatorAPICharm from the library.
nova-k8s charm consumes shared-db relation to connect to database,
nova-k8s charm consumes database relation to connect to database,
identity-service to register the service endpoints to keystone
and ingress-internal/ingress-public relation to get exposed over
internal and public networks.

View File

@ -16,7 +16,9 @@ nova-k8s is deployed using below command:
Now connect the nova operator to existing database,
messaging and keystone identity operators:
juju relate mysql:database nova:shared-db
juju relate mysql:database nova:database
juju relate mysql:database nova:api-database
juju relate mysql:database nova:cell-database
juju relate rabbitmq:amqp nova:amqp
juju relate keystone:identity-service nova:identity-service
@ -38,7 +40,7 @@ deployed then see file `actions.yaml`.
nova-k8s requires the following relations:
`shared-db`: To connect to MySQL
`database`, `api-database`, `cell-database`: To connect to MySQL (nova requires 3 databases)
`amqp`: To connect to RabbitMQ
`identity-service`: To register endpoints in Keystone
`ingress-internal`: To expose service on underlying internal network

View File

@ -0,0 +1,496 @@
# Copyright 2022 Canonical Ltd.
#
# 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.
"""Relation 'requires' side abstraction for database relation.
This library is a uniform interface to a selection of common database
metadata, with added custom events that add convenience to database management,
and methods to consume the application related data.
Following an example of using the DatabaseCreatedEvent, in the context of the
application charm code:
```python
from charms.data_platform_libs.v0.database_requires import DatabaseRequires
class ApplicationCharm(CharmBase):
# Application charm that connects to database charms.
def __init__(self, *args):
super().__init__(*args)
# Charm events defined in the database requires charm library.
self.database = DatabaseRequires(self, relation_name="database", database_name="database")
self.framework.observe(self.database.on.database_created, self._on_database_created)
def _on_database_created(self, event: DatabaseCreatedEvent) -> None:
# Handle the created database
# Create configuration file for app
config_file = self._render_app_config_file(
event.username,
event.password,
event.endpoints,
)
# Start application with rendered configuration
self._start_application(config_file)
# Set active status
self.unit.status = ActiveStatus("received database credentials")
```
As shown above, the library provides some custom events to handle specific situations,
which are listed below:
database_created: event emitted when the requested database is created.
endpoints_changed: event emitted when the read/write endpoints of the database have changed.
read_only_endpoints_changed: event emitted when the read-only endpoints of the database
have changed. Event is not triggered if read/write endpoints changed too.
If it is needed to connect multiple database clusters to the same relation endpoint
the application charm can implement the same code as if it would connect to only
one database cluster (like the above code example).
To differentiate multiple clusters connected to the same relation endpoint
the application charm can use the name of the remote application:
```python
def _on_database_created(self, event: DatabaseCreatedEvent) -> None:
# Get the remote app name of the cluster that triggered this event
cluster = event.relation.app.name
```
It is also possible to provide an alias for each different database cluster/relation.
So, it is possible to differentiate the clusters in two ways.
The first is to use the remote application name, i.e., `event.relation.app.name`, as above.
The second way is to use different event handlers to handle each cluster events.
The implementation would be something like the following code:
```python
from charms.data_platform_libs.v0.database_requires import DatabaseRequires
class ApplicationCharm(CharmBase):
# Application charm that connects to database charms.
def __init__(self, *args):
super().__init__(*args)
# Define the cluster aliases and one handler for each cluster database created event.
self.database = DatabaseRequires(
self,
relation_name="database",
database_name="database",
relations_aliases = ["cluster1", "cluster2"],
)
self.framework.observe(
self.database.on.cluster1_database_created, self._on_cluster1_database_created
)
self.framework.observe(
self.database.on.cluster2_database_created, self._on_cluster2_database_created
)
def _on_cluster1_database_created(self, event: DatabaseCreatedEvent) -> None:
# Handle the created database on the cluster named cluster1
# Create configuration file for app
config_file = self._render_app_config_file(
event.username,
event.password,
event.endpoints,
)
...
def _on_cluster2_database_created(self, event: DatabaseCreatedEvent) -> None:
# Handle the created database on the cluster named cluster2
# Create configuration file for app
config_file = self._render_app_config_file(
event.username,
event.password,
event.endpoints,
)
...
```
"""
import json
import logging
from collections import namedtuple
from datetime import datetime
from typing import List, Optional
from ops.charm import (
CharmEvents,
RelationChangedEvent,
RelationEvent,
RelationJoinedEvent,
)
from ops.framework import EventSource, Object
from ops.model import Relation
# The unique Charmhub library identifier, never change it
LIBID = "0241e088ffa9440fb4e3126349b2fb62"
# Increment this major API version when introducing breaking changes
LIBAPI = 0
# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version.
LIBPATCH = 4
logger = logging.getLogger(__name__)
class DatabaseEvent(RelationEvent):
"""Base class for database events."""
@property
def endpoints(self) -> Optional[str]:
"""Returns a comma separated list of read/write endpoints."""
return self.relation.data[self.relation.app].get("endpoints")
@property
def password(self) -> Optional[str]:
"""Returns the password for the created user."""
return self.relation.data[self.relation.app].get("password")
@property
def read_only_endpoints(self) -> Optional[str]:
"""Returns a comma separated list of read only endpoints."""
return self.relation.data[self.relation.app].get("read-only-endpoints")
@property
def replset(self) -> Optional[str]:
"""Returns the replicaset name.
MongoDB only.
"""
return self.relation.data[self.relation.app].get("replset")
@property
def tls(self) -> Optional[str]:
"""Returns whether TLS is configured."""
return self.relation.data[self.relation.app].get("tls")
@property
def tls_ca(self) -> Optional[str]:
"""Returns TLS CA."""
return self.relation.data[self.relation.app].get("tls-ca")
@property
def uris(self) -> Optional[str]:
"""Returns the connection URIs.
MongoDB, Redis, OpenSearch and Kafka only.
"""
return self.relation.data[self.relation.app].get("uris")
@property
def username(self) -> Optional[str]:
"""Returns the created username."""
return self.relation.data[self.relation.app].get("username")
@property
def version(self) -> Optional[str]:
"""Returns the version of the database.
Version as informed by the database daemon.
"""
return self.relation.data[self.relation.app].get("version")
class DatabaseCreatedEvent(DatabaseEvent):
"""Event emitted when a new database is created for use on this relation."""
class DatabaseEndpointsChangedEvent(DatabaseEvent):
"""Event emitted when the read/write endpoints are changed."""
class DatabaseReadOnlyEndpointsChangedEvent(DatabaseEvent):
"""Event emitted when the read only endpoints are changed."""
class DatabaseEvents(CharmEvents):
"""Database events.
This class defines the events that the database can emit.
"""
database_created = EventSource(DatabaseCreatedEvent)
endpoints_changed = EventSource(DatabaseEndpointsChangedEvent)
read_only_endpoints_changed = EventSource(DatabaseReadOnlyEndpointsChangedEvent)
Diff = namedtuple("Diff", "added changed deleted")
Diff.__doc__ = """
A tuple for storing the diff between two data mappings.
added keys that were added.
changed keys that still exist but have new values.
deleted keys that were deleted.
"""
class DatabaseRequires(Object):
"""Requires-side of the database relation."""
on = DatabaseEvents()
def __init__(
self,
charm,
relation_name: str,
database_name: str,
extra_user_roles: str = None,
relations_aliases: List[str] = None,
):
"""Manager of database client relations."""
super().__init__(charm, relation_name)
self.charm = charm
self.database = database_name
self.extra_user_roles = extra_user_roles
self.local_app = self.charm.model.app
self.local_unit = self.charm.unit
self.relation_name = relation_name
self.relations_aliases = relations_aliases
self.framework.observe(
self.charm.on[relation_name].relation_joined, self._on_relation_joined_event
)
self.framework.observe(
self.charm.on[relation_name].relation_changed, self._on_relation_changed_event
)
# Define custom event names for each alias.
if relations_aliases:
# Ensure the number of aliases does not exceed the maximum
# of connections allowed in the specific relation.
relation_connection_limit = self.charm.meta.requires[relation_name].limit
if len(relations_aliases) != relation_connection_limit:
raise ValueError(
f"The number of aliases must match the maximum number of connections allowed in the relation. "
f"Expected {relation_connection_limit}, got {len(relations_aliases)}"
)
for relation_alias in relations_aliases:
self.on.define_event(f"{relation_alias}_database_created", DatabaseCreatedEvent)
self.on.define_event(
f"{relation_alias}_endpoints_changed", DatabaseEndpointsChangedEvent
)
self.on.define_event(
f"{relation_alias}_read_only_endpoints_changed",
DatabaseReadOnlyEndpointsChangedEvent,
)
def _assign_relation_alias(self, relation_id: int) -> None:
"""Assigns an alias to a relation.
This function writes in the unit data bag.
Args:
relation_id: the identifier for a particular relation.
"""
# If no aliases were provided, return immediately.
if not self.relations_aliases:
return
# Return if an alias was already assigned to this relation
# (like when there are more than one unit joining the relation).
if (
self.charm.model.get_relation(self.relation_name, relation_id)
.data[self.local_unit]
.get("alias")
):
return
# Retrieve the available aliases (the ones that weren't assigned to any relation).
available_aliases = self.relations_aliases[:]
for relation in self.charm.model.relations[self.relation_name]:
alias = relation.data[self.local_unit].get("alias")
if alias:
logger.debug("Alias %s was already assigned to relation %d", alias, relation.id)
available_aliases.remove(alias)
# Set the alias in the unit relation databag of the specific relation.
relation = self.charm.model.get_relation(self.relation_name, relation_id)
relation.data[self.local_unit].update({"alias": available_aliases[0]})
def _diff(self, event: RelationChangedEvent) -> Diff:
"""Retrieves the diff of the data in the relation changed databag.
Args:
event: relation changed event.
Returns:
a Diff instance containing the added, deleted and changed
keys from the event relation databag.
"""
# Retrieve the old data from the data key in the local unit relation databag.
old_data = json.loads(event.relation.data[self.local_unit].get("data", "{}"))
# Retrieve the new data from the event relation databag.
new_data = {
key: value for key, value in event.relation.data[event.app].items() if key != "data"
}
# These are the keys that were added to the databag and triggered this event.
added = new_data.keys() - old_data.keys()
# These are the keys that were removed from the databag and triggered this event.
deleted = old_data.keys() - new_data.keys()
# These are the keys that already existed in the databag,
# but had their values changed.
changed = {
key for key in old_data.keys() & new_data.keys() if old_data[key] != new_data[key]
}
# TODO: evaluate the possibility of losing the diff if some error
# happens in the charm before the diff is completely checked (DPE-412).
# Convert the new_data to a serializable format and save it for a next diff check.
event.relation.data[self.local_unit].update({"data": json.dumps(new_data)})
# Return the diff with all possible changes.
return Diff(added, changed, deleted)
def _emit_aliased_event(self, event: RelationChangedEvent, event_name: str) -> None:
"""Emit an aliased event to a particular relation if it has an alias.
Args:
event: the relation changed event that was received.
event_name: the name of the event to emit.
"""
alias = self._get_relation_alias(event.relation.id)
if alias:
getattr(self.on, f"{alias}_{event_name}").emit(
event.relation, app=event.app, unit=event.unit
)
def _get_relation_alias(self, relation_id: int) -> Optional[str]:
"""Returns the relation alias.
Args:
relation_id: the identifier for a particular relation.
Returns:
the relation alias or None if the relation was not found.
"""
for relation in self.charm.model.relations[self.relation_name]:
if relation.id == relation_id:
return relation.data[self.local_unit].get("alias")
return None
def fetch_relation_data(self) -> dict:
"""Retrieves data from relation.
This function can be used to retrieve data from a relation
in the charm code when outside an event callback.
Returns:
a dict of the values stored in the relation data bag
for all relation instances (indexed by the relation ID).
"""
data = {}
for relation in self.relations:
data[relation.id] = {
key: value for key, value in relation.data[relation.app].items() if key != "data"
}
return data
def _update_relation_data(self, relation_id: int, data: dict) -> None:
"""Updates a set of key-value pairs in the relation.
This function writes in the application data bag, therefore,
only the leader unit can call it.
Args:
relation_id: the identifier for a particular relation.
data: dict containing the key-value pairs
that should be updated in the relation.
"""
if self.local_unit.is_leader():
relation = self.charm.model.get_relation(self.relation_name, relation_id)
relation.data[self.local_app].update(data)
def _on_relation_joined_event(self, event: RelationJoinedEvent) -> None:
"""Event emitted when the application joins the database relation."""
# If relations aliases were provided, assign one to the relation.
self._assign_relation_alias(event.relation.id)
# Sets both database and extra user roles in the relation
# if the roles are provided. Otherwise, sets only the database.
if self.extra_user_roles:
self._update_relation_data(
event.relation.id,
{
"database": self.database,
"extra-user-roles": self.extra_user_roles,
},
)
else:
self._update_relation_data(event.relation.id, {"database": self.database})
def _on_relation_changed_event(self, event: RelationChangedEvent) -> None:
"""Event emitted when the database relation has changed."""
# Check which data has changed to emit customs events.
diff = self._diff(event)
# Check if the database is created
# (the database charm shared the credentials).
if "username" in diff.added and "password" in diff.added:
# Emit the default event (the one without an alias).
logger.info("database created at %s", datetime.now())
self.on.database_created.emit(event.relation, app=event.app, unit=event.unit)
# Emit the aliased event (if any).
self._emit_aliased_event(event, "database_created")
# To avoid unnecessary application restarts do not trigger
# “endpoints_changed“ event if “database_created“ is triggered.
return
# Emit an endpoints changed event if the database
# 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)
# Emit the aliased event (if any).
self._emit_aliased_event(event, "endpoints_changed")
# To avoid unnecessary application restarts do not trigger
# “read_only_endpoints_changed“ event if “endpoints_changed“ is triggered.
return
# Emit a read only endpoints changed event if the database
# added or changed this info in the relation databag.
if "read-only-endpoints" in diff.added or "read-only-endpoints" in diff.changed:
# Emit the default event (the one without an alias).
logger.info("read-only-endpoints changed on %s", datetime.now())
self.on.read_only_endpoints_changed.emit(
event.relation, app=event.app, unit=event.unit
)
# Emit the aliased event (if any).
self._emit_aliased_event(event, "read_only_endpoints_changed")
@property
def relations(self) -> List[Relation]:
"""The list of Relation instances associated with this relation_name."""
return list(self.charm.model.relations[self.relation_name])

View File

@ -1,164 +0,0 @@
"""
## Overview
This document explains how to integrate with the MySQL charm for the purposes of consuming a mysql database. It also explains how alternative implementations of the MySQL charm may maintain the same interface and be backward compatible with all currently integrated charms. Finally this document is the authoritative reference on the structure of relation data that is shared between MySQL charms and any other charm that intends to use the database.
## Consumer Library Usage
The MySQL charm library uses the [Provider and Consumer](https://ops.readthedocs.io/en/latest/#module-ops.relation) objects from the Operator Framework. Charms that would like to use a MySQL database must use the `MySQLConsumer` object from the charm library. Using the `MySQLConsumer` object requires instantiating it, typically in the constructor of your charm. The `MySQLConsumer` constructor requires the name of the relation over which a database will be used. This relation must use the `mysql_datastore` interface. In addition the constructor also requires a `consumes` specification, which is a dictionary with key `mysql` (also see Provider Library Usage below) and a value that represents the minimum acceptable version of MySQL. This version string can be in any format that is compatible with the Python [Semantic Version module](https://pypi.org/project/semantic-version/). For example, assuming your charm consumes a database over a rlation named "monitoring", you may instantiate `MySQLConsumer` as follows:
from charms.mysql_k8s.v0.mysql import MySQLConsumer
def __init__(self, *args):
super().__init__(*args)
...
self.mysql_consumer = MySQLConsumer(
self, "monitoring", {"mysql": ">=8"}
)
...
This example hard codes the consumes dictionary argument containing the minimal MySQL version required, however you may want to consider generating this dictionary by some other means, such as a `self.consumes` property in your charm. This is because the minimum required MySQL version may change when you upgrade your charm. Of course it is expected that you will keep this version string updated as you develop newer releases of your charm. If the version string can be determined at run time by inspecting the actual deployed version of your charmed application, this would be ideal.
An instantiated `MySQLConsumer` object may be used to request new databases using the `new_database()` method. This method requires no arguments unless you require multiple databases. If multiple databases are requested, you must provide a unique `name_suffix` argument. For example:
def _on_database_relation_joined(self, event):
self.mysql_consumer.new_database(name_suffix="db1")
self.mysql_consumer.new_database(name_suffix="db2")
The `address`, `port`, `databases`, and `credentials` methods can all be called
to get the relevant information from the relation data.
"""
# !/usr/bin/env python3
# Copyright 2021 Canonical Ltd.
# See LICENSE file for licensing details.
import json
import uuid
import logging
from ops.framework import (
StoredState,
EventBase,
ObjectEvents,
EventSource,
Object,
)
# The unique Charmhub library identifier, never change it
LIBID = "1fdc567d7095465990dc1f9be80461fd"
# Increment this major API version when introducing breaking changes
LIBAPI = 0
# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version
LIBPATCH = 2
logger = logging.getLogger(__name__)
class DatabaseConnectedEvent(EventBase):
"""Database connected Event."""
pass
class DatabaseReadyEvent(EventBase):
"""Database ready for use Event."""
pass
class DatabaseGoneAwayEvent(EventBase):
"""Database relation has gone-away Event"""
pass
class DatabaseServerEvents(ObjectEvents):
"""Events class for `on`"""
connected = EventSource(DatabaseConnectedEvent)
ready = EventSource(DatabaseReadyEvent)
goneaway = EventSource(DatabaseGoneAwayEvent)
class MySQLConsumer(Object):
"""
MySQLConsumer lib class
"""
on = DatabaseServerEvents()
def __init__(self, charm, relation_name: str, databases: list):
super().__init__(charm, relation_name)
self.charm = charm
self.relation_name = relation_name
self.request_databases = databases
self.framework.observe(
self.charm.on[relation_name].relation_joined,
self._on_database_relation_joined,
)
def _on_database_relation_joined(self, event):
"""AMQP relation joined."""
logging.debug("DatabaseRequires on_joined")
self.on.connected.emit()
self.request_access(self.request_databases)
def databases(self, rel_id=None) -> list:
"""
List of currently available databases
Returns:
list: list of database names
"""
rel = self.framework.model.get_relation(self.relation_name, rel_id)
relation_data = rel.data[rel.app]
dbs = relation_data.get("databases")
databases = json.loads(dbs) if dbs else []
return databases
def credentials(self, rel_id=None) -> dict:
"""
Dictionary of credential information to access databases
Returns:
dict: dictionary of credential information including username,
password and address
"""
rel = self.framework.model.get_relation(self.relation_name, rel_id)
relation_data = rel.data[rel.app]
data = relation_data.get("data")
data = json.loads(data) if data else {}
credentials = data.get("credentials")
return credentials
def new_database(self, rel_id=None, name_suffix=""):
"""
Request creation of an additional database
"""
if not self.charm.unit.is_leader():
return
rel = self.framework.model.get_relation(self.relation_name, rel_id)
if name_suffix:
name_suffix = "_{}".format(name_suffix)
rid = str(uuid.uuid4()).split("-")[-1]
db_name = "db_{}_{}_{}".format(rel.id, rid, name_suffix)
logger.debug("CLIENT REQUEST %s", db_name)
rel_data = rel.data[self.charm.app]
dbs = rel_data.get("databases")
dbs = json.loads(dbs) if dbs else []
dbs.append(db_name)
rel.data[self.charm.app]["databases"] = json.dumps(dbs)
def request_access(self, databases: list) -> None:
"""Request access to the AMQP server."""
if self.model.unit.is_leader():
logging.debug("Requesting AMQP user and vhost")
if databases:
rel = self.framework.model.get_relation(self.relation_name)
rel.data[self.charm.app]["databases"] = json.dumps(databases)

View File

@ -55,8 +55,14 @@ requires:
interface: ingress
optional: true
limit: 1
shared-db:
interface: mysql_datastore
database:
interface: mysql_client
limit: 1
api-database:
interface: mysql_client
limit: 1
cell-database:
interface: mysql_client
limit: 1
amqp:
interface: rabbitmq

View File

@ -6,8 +6,7 @@ This charm provide Nova services as part of an OpenStack deployment
import logging
import uuid
from typing import Callable
from typing import List
from typing import Callable, List, Mapping
import ops.framework
from ops.main import main
@ -166,9 +165,22 @@ class NovaOperatorCharm(sunbeam_charm.OSBaseOperatorAPICharm):
wsgi_public_script = '/usr/bin/nova-api-wsgi'
shared_metadata_secret_key = 'shared-metadata-secret'
db_sync_cmds = [
@property
def db_sync_cmds(self) -> List[List[str]]:
# we must provide the database connection for the cell database,
# because the database credentials are different to the main database.
# If we don't provide them:
# > If you don't specify --database_connection then nova-manage will
# > use the [database]/connection value from your config file,
# > and mangle the database name to have a _cell0 suffix.
# https://docs.openstack.org/nova/yoga/admin/cells.html#configuring-a-new-deployment
cell_database = self.dbs["cell-database"].context()["connection"]
return [
['sudo', '-u', 'nova', 'nova-manage', 'api_db', 'sync'],
['sudo', '-u', 'nova', 'nova-manage', 'cell_v2', 'map_cell0'],
[
'sudo', '-u', 'nova', 'nova-manage', 'cell_v2', 'map_cell0',
'--database_connection', cell_database
],
['sudo', '-u', 'nova', 'nova-manage', 'db', 'sync'],
['sudo', '-u', 'nova', 'nova-manage', 'cell_v2', 'create_cell',
'--name', 'cell1', '--verbose'],
@ -205,12 +217,17 @@ class NovaOperatorCharm(sunbeam_charm.OSBaseOperatorAPICharm):
return 8774
@property
def databases(self) -> List[str]:
def databases(self) -> Mapping[str, str]:
"""Databases needed to support this charm.
Need to override the default to specify three dbs.
Need to override the default
because we're registering multiple databases.
"""
return ["nova_api", "nova", "nova_cell0"]
return {
"database": "nova",
"api-database": "nova_api",
"cell-database": "nova_cell0",
}
def get_pebble_handlers(self):
pebble_handlers = super().get_pebble_handlers()

View File

@ -6,16 +6,16 @@ state_path = /var/lib/nova
transport_url = {{ amqp.transport_url }}
[api_database]
{% if shared_db.nova_api -%}
connection = {{ shared_db.nova_api.connection }}
{% if api_database.connection -%}
connection = {{ api_database.connection }}
{% else -%}
connection = sqlite:////var/lib/nova/nova_api.sqlite
{% endif -%}
connection_recycle_time = 200
[database]
{% if shared_db.nova -%}
connection = {{ shared_db.nova.connection }}
{% if database.connection -%}
connection = {{ database.connection }}
{% else -%}
connection = sqlite:////var/lib/nova/nova.sqlite
{% endif -%}

View File

@ -1,6 +1,6 @@
[database]
{% if shared_db.database_host -%}
connection = {{ shared_db.database_type }}://{{ shared_db.database_user }}:{{ shared_db.database_password }}@{{ shared_db.database_host }}/{{ shared_db.database }}{% if shared_db.database_ssl_ca %}?ssl_ca={{ shared_db.database_ssl_ca }}{% if shared_db.database_ssl_cert %}&ssl_cert={{ shared_db.database_ssl_cert }}&ssl_key={{ shared_db.database_ssl_key }}{% endif %}{% endif %}
{% if database.connection -%}
connection = {{ database.connection }}
{% else -%}
connection = sqlite:////var/lib/cinder/cinder.db
{% endif -%}

View File

@ -62,12 +62,24 @@ class TestNovaOperatorCharm(test_utils.CharmTestCase):
def test_all_relations(self):
self.harness.set_leader()
test_utils.set_all_pebbles_ready(self.harness)
# this adds all the default/common relations
test_utils.add_all_relations(self.harness)
# but nova has some extra db relations, so add them manually here
rel_id = add_db_relation(self.harness, "api-database")
test_utils.add_db_relation_credentials(self.harness, rel_id)
rel_id = add_db_relation(self.harness, "cell-database")
test_utils.add_db_relation_credentials(self.harness, rel_id)
setup_cmds = [
['a2ensite', 'wsgi-nova-api'],
['sudo', '-u', 'nova', 'nova-manage', 'api_db', 'sync'],
['sudo', '-u', 'nova', 'nova-manage', 'cell_v2', 'map_cell0'],
[
'sudo', '-u', 'nova', 'nova-manage', 'cell_v2', 'map_cell0',
'--database_connection',
# values originate in test_utils.add_db_relation_credentials()
'mysql+pymysql://foo:hardpassword@10.0.0.10/nova_cell0'
],
['sudo', '-u', 'nova', 'nova-manage', 'db', 'sync'],
['sudo', '-u', 'nova', 'nova-manage', 'cell_v2', 'create_cell',
'--name', 'cell1', '--verbose'],
@ -79,3 +91,14 @@ class TestNovaOperatorCharm(test_utils.CharmTestCase):
'/etc/nova/nova.conf']
for f in config_files:
self.check_file('nova-api', f)
def add_db_relation(harness, name) -> str:
"""Add db relation."""
rel_id = harness.add_relation(name, "mysql")
harness.add_relation_unit(rel_id, "mysql/0")
harness.add_relation_unit(rel_id, "mysql/0")
harness.update_relation_data(
rel_id, "mysql/0", {"ingress-address": "10.0.0.3"}
)
return rel_id