import uuid
from peewee import CharField, TextField, FloatField, Model, AutoField, Proxy
import uuid
from peewee import CharField, TextField, FloatField, Model, AutoField, Proxy
db.initialize(connect(config.get('sql', 'database_uri')))
def create_tables(self) -> None:
db.initialize(connect(config.get('sql', 'database_uri')))
def create_tables(self) -> None:
def save_credentials(self, vim_credentials) -> VimCredentials:
"""Saves vim credentials. If a record with same uuid exists, overwrite it."""
def save_credentials(self, vim_credentials) -> VimCredentials:
"""Saves vim credentials. If a record with same uuid exists, overwrite it."""
- with db.atomic():
- exists = VimCredentials.get_or_none(VimCredentials.uuid == vim_credentials.uuid)
- if exists:
- vim_credentials.id = exists.id
- vim_credentials.save()
- return vim_credentials
+ db.connect()
+ try:
+ with db.atomic():
+ exists = VimCredentials.get_or_none(VimCredentials.uuid == vim_credentials.uuid)
+ if exists:
+ vim_credentials.id = exists.id
+ vim_credentials.save()
+ return vim_credentials
+ finally:
+ db.close()
def save_alarm(self, name, threshold, operation, severity, statistic, metric_name, vdur_name,
vnf_member_index, nsr_id) -> Alarm:
"""Saves alarm."""
# TODO: Add uuid optional param and check if exists to handle updates (see self.save_credentials)
def save_alarm(self, name, threshold, operation, severity, statistic, metric_name, vdur_name,
vnf_member_index, nsr_id) -> Alarm:
"""Saves alarm."""
# TODO: Add uuid optional param and check if exists to handle updates (see self.save_credentials)
- with db.atomic():
- alarm = Alarm()
- alarm.uuid = str(uuid.uuid4())
- alarm.name = name
- alarm.threshold = threshold
- alarm.operation = operation
- alarm.severity = severity
- alarm.statistic = statistic
- alarm.monitoring_param = metric_name
- alarm.vdur_name = vdur_name
- alarm.vnf_member_index = vnf_member_index
- alarm.nsr_id = nsr_id
- alarm.save()
- return alarm
+ db.connect()
+ try:
+ with db.atomic():
+ alarm = Alarm()
+ alarm.uuid = str(uuid.uuid4())
+ alarm.name = name
+ alarm.threshold = threshold
+ alarm.operation = operation
+ alarm.severity = severity
+ alarm.statistic = statistic
+ alarm.monitoring_param = metric_name
+ alarm.vdur_name = vdur_name
+ alarm.vnf_member_index = vnf_member_index
+ alarm.nsr_id = nsr_id
+ alarm.save()
+ return alarm
+ finally:
+ db.close()
def get_vim_type(self, vim_account_id) -> str:
"""Get the vim type that is required by the message."""
credentials = self.get_credentials(vim_account_id)
def get_vim_type(self, vim_account_id) -> str:
"""Get the vim type that is required by the message."""
credentials = self.get_credentials(vim_account_id)
- return str(credentials.type)
+ config = json.loads(credentials.config)
+ if 'vim_type' in config:
+ vim_type = config['vim_type']
+ return str(vim_type.lower())
+ else:
+ return str(credentials.type)