def get_manifest(manifest_id: str = None, plan_id: str = None) -> List[Manifest]: if manifest_id and plan_id: raise Exception('Query Manifests only by manifest_id OR plan_id') if plan_id: manifests = ManifestSQL.where( 'plan_id_name', '=', '{}'.format(plan_id)).get().serialize() if len(manifests) > 1: raise Exception( 'Too many manifests {mani_len} returned for the Plan: {plan_id}' .format(mani_len=len(manifests), plan_id=plan_id)) return [Manifest.from_dict(manifests[0])] if manifest_id: if ManifestAdapter.exists_in_db(manifest_id): model_sql = ManifestAdapter.find_by_id_name(manifest_id) model = ManifestAdapter.model_sql_to_model(model_sql) model.manifest_content = model.manifest_content.replace( '</br>', '\n') return [model] else: return [] else: manifests = ManifestAdapter.get_all() for manifest in manifests: manifest.manifest_content = manifest.manifest_content.replace( '</br>', '\n') return manifests
def test_manifest_deletion(self): _, result = SQLStore.delete_manifest(self.test_model.id) self.assertEqual(self.result, 200, msg='Assert Manifest Deleted') exists = ManifestAdapter.exists_in_db(self.test_model.id) self.assertFalse(exists, msg='Assert manifest does NOT Exist.') manifest_sql = ManifestAdapter.find_by_id_name(self.test_model.id) self.assertIsNone(manifest_sql)
def test_sample_model_with_plans(self): self.assertIsInstance(self.test_model, Manifest) model_sql = ManifestAdapter.find_by_id_name(self.test_model.id) self.assertIsInstance(model_sql, ManifestSQL) ''' query associated service ''' service = model_sql.service self.assertIsInstance(service, ServiceTypeSQL) ''' verify relations ''' plans = service.plans plan = model_sql.plan self.assertTrue(plan in plans) ''' manifest also deleted ''' PlanAdapter.delete(plan.id_name) self.assertFalse(ManifestAdapter.exists_in_db(model_sql.id_name)) ''' service updated (has to be re-query-ed) ''' ''' this is not correct! >> service = model_sql.service ''' service = ServiceTypeAdapter.find_by_id_name(service.id_name) plans = service.plans ''' verify plans reduced ''' self.assertEqual(len(plans), 1) ''' verify service has no manifest now ''' self.assertTrue(service.manifests.is_empty())
def set_up(wait_time=10): connection = SQLStore.get_connection() count = 3 while not connection and count: LOG.debug('Retrying to connect to Database \'{}\'...'.format( Helper.database)) count = count - 1 time.sleep(wait_time) connection = SQLStore.get_connection() if connection: # Create DB connection.cursor().execute( 'CREATE DATABASE IF NOT EXISTS {}'.format(Helper.database)) LOG.debug('Successfully connected to Database \'{}\'...'.format( Helper.database)) # Create tables PlanAdapter.create_table() ServiceTypeAdapter.create_table() PlanServiceTypeAdapter.create_table() ManifestAdapter.create_table() ServiceInstanceAdapter.create_table() LastOperationAdapter.create_table() connection.close() else: raise Exception('Could not connect to the DB')
def delete_manifest(manifest_id: str = None): # -> None: if manifest_id: if ManifestAdapter.exists_in_db(manifest_id): ManifestAdapter.delete(manifest_id) return 'Manifest Deleted', 200 else: return 'Manifest ID not found', 500 else: ManifestAdapter.delete_all() return 'Deleted all Manifests', 200
def setUp(self): """PREREQUISITES""" PlanAdapter.create_table() ServiceTypeAdapter.create_table() PlanServiceTypeAdapter.create_table() self.service = ServiceTypeAdapter.sample_model('manifest1') ServiceTypeAdapter.save(self.service) ManifestAdapter.create_table() self.test_model = ManifestAdapter.sample_model('manifest1') _, self.result = SQLStore.add_manifest(self.test_model)
def add_manifest(manifest) -> tuple: if ManifestAdapter.exists_in_db(manifest.id): return 'The Manifest already exists in the catalog.', 409 ''' Attempt to Create Table ''' PlanAdapter.create_table() ServiceTypeAdapter.create_table() if ServiceTypeAdapter.exists_in_db( manifest.service_id) and PlanAdapter.exists_in_db( manifest.plan_id): ManifestAdapter.save(manifest) else: return 'Could not save the Manifest in the DB, Plan and Service don\'t exist', 500 if ManifestAdapter.exists_in_db(manifest.id): return 'Manifest added successfully', 200 else: return 'Could not save the Manifest in the DB', 500
def delete_service(service_id: str = None) -> tuple: if service_id: if ServiceTypeAdapter.exists_in_db(service_id): ServiceTypeAdapter.delete(service_id) return 'Service Deleted', 200 else: return 'Service ID not found', 500 else: PlanServiceTypeAdapter.delete_all() LastOperationAdapter.delete_all() ServiceInstanceAdapter.delete_all() ManifestAdapter.delete_all() ServiceTypeAdapter.delete_all() PlanAdapter.delete_all() return 'Deleted all Services', 200
def add_service_instance(instance: ServiceInstance) -> tuple: id_name = ServiceInstanceAdapter.get_id(instance) if ServiceInstanceAdapter.exists_in_db(id_name): LOG.warning( 'An existing instance was attempted to be saved. Updating it...' ) SQLStore.delete_service_instance(id_name) # return 'The Instance already exists in the catalog.', 409 ''' Attempt to Create Table ''' PlanAdapter.create_table() ServiceTypeAdapter.create_table() ManifestAdapter.create_table() ServiceInstanceAdapter.create_table() LastOperationAdapter.create_table() ServiceInstanceAdapter.save(instance) id_name = ServiceInstanceAdapter.get_id(instance) if ServiceInstanceAdapter.exists_in_db(id_name): LOG.warning('Instance added successfully...') return 'Instance added successfully', 200 else: LOG.warning('Could not save the Instance in the DB...') return 'Could not save the Instance in the DB', 500
def test_adapter_save_to_update(self): self.test_model.name = 'new-name' model_sql = ManifestAdapter.save(self.test_model) exists = ManifestAdapter.exists_in_db(model_sql.id_name) self.assertTrue(exists)
def test_adapter_delete(self): with self.assertRaises(Exception): ManifestAdapter.delete(id_name='')
def test_manifest_created(self): self.assertEqual(self.result, 200, msg='Assert Successful Add') exists = ManifestAdapter.exists_in_db(self.test_model.id) self.assertTrue(exists, msg='Assert manifest exists.') manifest_sql = ManifestAdapter.find_by_id_name(self.test_model.id) self.assertIsInstance(manifest_sql, ManifestSQL)