db: object,
fs: object,
log: object = None,
- loop: object = None,
on_update_db=None,
):
"""
:param: db: Database object from osm_common
:param: fs: Filesystem object from osm_common
:param: log: Logger
- :param: loop: Asyncio loop
:param: on_update_db: Callback function to be called for updating the database.
"""
# parent class constructor
- N2VCConnector.__init__(
- self,
- db=db,
- fs=fs,
- log=log,
- loop=loop,
- on_update_db=on_update_db,
- )
+ N2VCConnector.__init__(self, db=db, fs=fs, log=log, on_update_db=on_update_db)
# silence websocket traffic log
logging.getLogger("websockets.protocol").setLevel(logging.INFO)
db_uri = EnvironConfig(prefixes=["OSMLCM_", "OSMMON_"]).get("database_uri")
self._store = MotorStore(db_uri)
- self.loading_libjuju = asyncio.Lock(loop=self.loop)
+ self.loading_libjuju = asyncio.Lock()
self.delete_namespace_locks = {}
self.log.info("N2VC juju connector initialized")
# create or reuse a new juju machine
try:
if not await libjuju.model_exists(model_name):
- await libjuju.add_model(
- model_name,
- libjuju.vca_connection.lxd_cloud,
- )
+ await libjuju.add_model(model_name, libjuju.vca_connection.lxd_cloud)
machine, new = await libjuju.create_machine(
model_name=model_name,
machine_id=machine_id,
raise N2VCException(message=message)
# new machine credentials
- credentials = {
- "hostname": machine.dns_name,
- }
+ credentials = {"hostname": machine.dns_name}
self.log.info(
"Execution environment created. ee_id: {}, credentials: {}".format(
# register machine on juju
try:
if not await libjuju.model_exists(model_name):
- await libjuju.add_model(
- model_name,
- libjuju.vca_connection.lxd_cloud,
- )
+ await libjuju.add_model(model_name, libjuju.vca_connection.lxd_cloud)
machine_id = await libjuju.provision_machine(
model_name=model_name,
hostname=hostname,
_, ns_id, _, _, _ = self._get_namespace_components(namespace=namespace)
model_name = "{}-k8s".format(ns_id)
if not await libjuju.model_exists(model_name):
- await libjuju.add_model(
- model_name,
- libjuju.vca_connection.k8s_cloud,
- )
+ await libjuju.add_model(model_name, libjuju.vca_connection.k8s_cloud)
application_name = self._get_application_name(namespace)
try:
self.log.info("K8s proxy charm installed")
ee_id = N2VCJujuConnector._build_ee_id(
- model_name=model_name,
- application_name=application_name,
- machine_id="k8s",
+ model_name=model_name, application_name=application_name, machine_id="k8s"
)
self._write_ee_id_db(db_dict=db_dict, ee_id=ee_id)
return await libjuju.get_metrics(model_name, application_name)
async def add_relation(
- self,
- provider: RelationEndpoint,
- requirer: RelationEndpoint,
+ self, provider: RelationEndpoint, requirer: RelationEndpoint
):
"""
Add relation between two charmed endpoints
self.log.debug(f"adding new relation between {provider} and {requirer}")
cross_model_relation = (
provider.model_name != requirer.model_name
- or requirer.vca_id != requirer.vca_id
+ or provider.vca_id != requirer.vca_id
)
try:
if cross_model_relation:
requirer.model_name, offer, provider_libjuju
)
await requirer_libjuju.add_relation(
- requirer.model_name,
- requirer.endpoint,
- saas_name,
+ requirer.model_name, requirer.endpoint, saas_name
)
else:
# Standard relation
self.log.info("Deleting namespace={}".format(namespace))
will_not_delete = False
if namespace not in self.delete_namespace_locks:
- self.delete_namespace_locks[namespace] = asyncio.Lock(loop=self.loop)
+ self.delete_namespace_locks[namespace] = asyncio.Lock()
delete_lock = self.delete_namespace_locks[namespace]
while delete_lock.locked():
if not scaling_in:
# destroy the model
await libjuju.destroy_model(
- model_name=model_name,
- total_timeout=total_timeout,
+ model_name=model_name, total_timeout=total_timeout
)
elif vca_type == "native_charm" and scaling_in:
# destroy the unit in the application
config=params_dict,
)
actions = await libjuju.get_actions(
- application_name=application_name,
- model_name=model_name,
+ application_name=application_name, model_name=model_name
)
self.log.debug(
"Application {} has these actions: {}".format(
)
try:
-
await libjuju.upgrade_charm(
application_name=application_name,
path=path,
if not self.libjuju:
async with self.loading_libjuju:
vca_connection = await get_connection(self._store)
- self.libjuju = Libjuju(vca_connection, loop=self.loop, log=self.log)
+ self.libjuju = Libjuju(vca_connection, log=self.log)
return self.libjuju
else:
vca_connection = await get_connection(self._store, vca_id)
- return Libjuju(
- vca_connection,
- loop=self.loop,
- log=self.log,
- n2vc=self,
- )
+ return Libjuju(vca_connection, log=self.log, n2vc=self)
def _write_ee_id_db(self, db_dict: dict, ee_id: str):
-
# write ee_id to database: _admin.deployed.VCA.x
try:
the_table = db_dict["collection"]
)
return application_name
+ @staticmethod
+ def _get_vca_record(search_key: str, vca_records: list, vdu_id: str) -> dict:
+ """Get the correct VCA record dict depending on the search key
+
+ Args:
+ search_key (str): keyword to find the correct VCA record
+ vca_records (list): All VCA records as list
+ vdu_id (str): VDU ID
+
+ Returns:
+ vca_record (dict): Dictionary which includes the correct VCA record
+
+ """
+ return next(
+ filter(lambda record: record[search_key] == vdu_id, vca_records), {}
+ )
+
@staticmethod
def _generate_application_name(
charm_level: str,
# to first 12 characters.
if not vdu_id:
raise N2VCException(message="vdu-id should be provided.")
- vca_record = next(
- filter(lambda record: record["vdu_id"] == vdu_id, vca_records), {}
+
+ vca_record = N2VCJujuConnector._get_vca_record(
+ "vdu_id", vca_records, vdu_id
)
+
+ if not vca_record:
+ vca_record = N2VCJujuConnector._get_vca_record(
+ "kdu_name", vca_records, vdu_id
+ )
+
application_name = (
vca_record["ee_descriptor_id"][:12]
+ "-"
:param: vca_id: VCA ID
"""
vca_connection = await get_connection(self._store, vca_id=vca_id)
- libjuju = Libjuju(vca_connection, loop=self.loop, log=self.log, n2vc=self)
+ libjuju = Libjuju(vca_connection, log=self.log, n2vc=self)
controller = await libjuju.get_controller()
await libjuju.disconnect_controller(controller)