def post_blueprint(blueprint_id, revision_msg=None): """Add new version to existing blueprint. :param blueprint_id: Id of blueprint :type blueprint_id: :param revision_msg: Optional comment on submission :type revision_msg: str :rtype: Blueprint """ file = connexion.request.files['CSAR'] result, response = CSAR_db.add_revision(CSAR=file, revision_msg=revision_msg, blueprint_id=blueprint_id) if result is None: return f"Invalid CSAR: {response}", 406 blueprint_meta = BlueprintVersion.from_dict(result) # copy blueprint params from first revision blueprint_meta_old = BlueprintVersion.from_dict( PostgreSQL.get_blueprint_meta(blueprint_id, 'v1.0')) blueprint_meta.blueprint_name = blueprint_meta_old.blueprint_name blueprint_meta.project_domain = blueprint_meta_old.project_domain blueprint_meta.aadm_id = blueprint_meta_old.aadm_id blueprint_meta.username = blueprint_meta_old.username if not PostgreSQL.save_blueprint_meta(blueprint_meta): return f"Failed to save project data for blueprint_id={blueprint_id}", 500 PostgreSQL.save_git_transaction_data( blueprint_id=result['blueprint_id'], version_id=result['version_id'], revision_msg=f"Updated blueprint: {revision_msg}", job='update', git_backend=str(CSAR_db.connection.git_connector), repo_url=result['url'], commit_sha=result['commit_sha']) return blueprint_meta, 201
def post_new_blueprint(revision_msg=None, blueprint_name=None, aadm_id=None, username=None, project_domain=None): # noqa: E501 """Add new blueprint. :param revision_msg: Optional comment on submission :type revision_msg: str :param blueprint_name: Optional human-readable blueprint name :type blueprint_name: str :param aadm_id: End-to-end debugging id :type aadm_id: str :param username: End-to-end debugging id :type username: str :param project_domain: Optional project domain this blueprint belongs to :type project_domain: str :rtype: Blueprint """ # check roles if project_domain and not security_controller.check_roles(project_domain): return f"Unauthorized request for project: {project_domain}", 401 file = connexion.request.files['CSAR'] result, response = CSAR_db.add_revision(CSAR=file, revision_msg=revision_msg) if result is None: return f"Invalid CSAR: {response}", 406 blueprint_meta = BlueprintVersion.from_dict(result) blueprint_meta.blueprint_name = blueprint_name blueprint_meta.project_domain = project_domain blueprint_meta.aadm_id = aadm_id blueprint_meta.username = username if not PostgreSQL.save_blueprint_meta(blueprint_meta): blueprint_id = blueprint_meta.blueprint_id return f"Failed to save project data for blueprint_id={blueprint_id}", 500 PostgreSQL.save_git_transaction_data( blueprint_id=result['blueprint_id'], version_id=result['version_id'], revision_msg=f"Saved new blueprint: {revision_msg}", job='update', git_backend=str(CSAR_db.connection.git_connector), repo_url=result['url'], commit_sha=result['commit_sha']) return blueprint_meta, 201
def get_blueprint_meta(blueprint_id): # noqa: E501 """Get blueprint's metadata # noqa: E501 :param blueprint_id: Id of blueprint :type blueprint_id: :rtype: Blueprint """ data = PostgreSQL.get_blueprint_meta(blueprint_id) if not data: return "Blueprint meta not found", 404 return BlueprintVersion.from_dict(data), 200
def test_get_blueprint_meta(self, mocker, monkeypatch, caplog): # test set up caplog.set_level(logging.DEBUG, logger="opera.api.service.sqldb_service") mocker.patch('psycopg2.connect', new=FakePostgres) db = PostgreSQL() monkeypatch.setattr(FakePostgres, 'cursor', GetBlueprintMetaCursor) # test blueprint_meta: BlueprintVersion = self.blueprint_meta assert_that( BlueprintVersion.from_dict( db.get_blueprint_meta( blueprint_meta.blueprint_id))).is_equal_to(blueprint_meta)
def delete_blueprint_version(blueprint_id, version_id, force=None): """Delete version of blueprint. :param blueprint_id: Id of blueprint :type blueprint_id: :param version_id: Id of blueprint version :type version_id: str :param force: force delete blueprint :type force: bool :rtype: Blueprint """ if not force: if PostgreSQL.blueprint_used_in_deployment(blueprint_id, version_id): return "Cannot delete blueprint, deployment with this blueprint exists", 403 repo_url, _ = CSAR_db.get_repo_url(blueprint_id) rows_affected, status_code = CSAR_db.delete_blueprint( blueprint_id, version_id) logger.debug(f"Rows affected, status_code: {rows_affected} {status_code}") if status_code == 200: PostgreSQL.delete_blueprint_meta(blueprint_id, version_id) PostgreSQL.save_git_transaction_data( blueprint_id=blueprint_id, version_id=version_id, revision_msg=f"Deleted a version of blueprint", job='delete', git_backend=str(CSAR_db.connection.git_connector), repo_url=repo_url) return BlueprintVersion( blueprint_id=blueprint_id, version_id=version_id, url=repo_url, timestamp=timestamp_util.datetime_now_to_string()), 200 message = { 200: 'Successfully removed', 404: 'Blueprint version or blueprint not found', 500: 'Server error' } return message[status_code], status_code
def generic_blueprint_meta() -> BlueprintVersion: blueprint_meta = BlueprintVersion() blueprint_meta.blueprint_id = str(uuid.uuid4()) blueprint_meta.version_id = 'v1.0' blueprint_meta.blueprint_name = 'name' blueprint_meta.aadm_id = str(uuid.uuid4()) blueprint_meta.username = '******' blueprint_meta.project_domain = 'project_domain' blueprint_meta.url = 'https://github.com/torvalds/linux' blueprint_meta.commit_sha = 'd7c5303fbc8ac874ae3e597a5a0d3707dc0230b4' blueprint_meta.timestamp = timestamp_util.datetime_now_to_string() return blueprint_meta
class TestBlueprintMeta: def test_save_blueprint_meta(self, mocker, monkeypatch, caplog, generic_blueprint_meta): # test set up caplog.set_level(logging.DEBUG, logger="opera.api.service.sqldb_service") monkeypatch.setattr(FakePostgres, 'cursor', NoneCursor) mocker.patch('psycopg2.connect', new=FakePostgres) db = PostgreSQL() # testing blueprint_meta: BlueprintVersion = generic_blueprint_meta db.save_blueprint_meta(blueprint_meta) replacements = NoneCursor.get_replacements() assert_that(replacements).contains_only(*[ str(blueprint_meta.blueprint_id), blueprint_meta.version_id, blueprint_meta.blueprint_name, blueprint_meta.aadm_id, blueprint_meta.username, blueprint_meta.project_domain, blueprint_meta.url, blueprint_meta.commit_sha ]) assert_that(caplog.text).contains("Updated blueprint meta", str(blueprint_meta.blueprint_id)) def test_save_blueprint_meta_exception(self, mocker, caplog, monkeypatch, generic_blueprint_meta): # test set up caplog.set_level(logging.DEBUG, logger="opera.api.service.sqldb_service") mocker.patch('psycopg2.connect', new=FakePostgres) db = PostgreSQL() monkeypatch.setattr(FakePostgres, 'cursor', PsycopgErrorCursor) blueprint_meta: BlueprintVersion = generic_blueprint_meta db.save_blueprint_meta(blueprint_meta) assert_that(caplog.text).contains("Fail to update blueprint meta", str(blueprint_meta.blueprint_id)) blueprint_meta = BlueprintVersion( blueprint_id=str(uuid.uuid4()), version_id='v1.0', blueprint_name='a', aadm_id=str(uuid.uuid4()), username='******', project_domain='some_domain', url='www.google.com', timestamp=datetime.datetime.now(), commit_sha='d955c23e2771639202f52db9d40c633f6f732e55') def test_get_blueprint_meta(self, mocker, monkeypatch, caplog): # test set up caplog.set_level(logging.DEBUG, logger="opera.api.service.sqldb_service") mocker.patch('psycopg2.connect', new=FakePostgres) db = PostgreSQL() monkeypatch.setattr(FakePostgres, 'cursor', GetBlueprintMetaCursor) # test blueprint_meta: BlueprintVersion = self.blueprint_meta assert_that( BlueprintVersion.from_dict( db.get_blueprint_meta( blueprint_meta.blueprint_id))).is_equal_to(blueprint_meta) def test_get_blueprint_meta_version(self, mocker, monkeypatch, caplog): # test set up caplog.set_level(logging.DEBUG, logger="opera.api.service.sqldb_service") mocker.patch('psycopg2.connect', new=FakePostgres) db = PostgreSQL() monkeypatch.setattr(FakePostgres, 'cursor', GetBlueprintMetaCursor) # test blueprint_meta: BlueprintVersion = self.blueprint_meta assert_that( BlueprintVersion.from_dict( db.get_blueprint_meta( blueprint_id=blueprint_meta.blueprint_id, version_id=blueprint_meta.version_id))).is_equal_to( blueprint_meta) def test_get_project_blueprint_meta_missing(self, mocker, caplog, generic_blueprint_meta): # test set up caplog.set_level(logging.DEBUG, logger="opera.api.service.sqldb_service") mocker.patch('psycopg2.connect', new=FakePostgres) db = PostgreSQL() # test blueprint_meta: BlueprintVersion = generic_blueprint_meta assert db.get_blueprint_meta(blueprint_meta.blueprint_id) is None def test_get_project_domain(self, mocker, caplog, monkeypatch, generic_blueprint_meta): # test set up caplog.set_level(logging.DEBUG, logger="opera.api.service.sqldb_service") mocker.patch('psycopg2.connect', new=FakePostgres) db = PostgreSQL() monkeypatch.setattr(FakePostgres, 'cursor', GetBlueprintMetaCursor) # test blueprint_meta: BlueprintVersion = generic_blueprint_meta assert db.get_project_domain( blueprint_meta.blueprint_id) == blueprint_meta.project_domain def test_get_project_domain_missing(self, mocker, caplog, generic_blueprint_meta): # test set up caplog.set_level(logging.DEBUG, logger="opera.api.service.sqldb_service") mocker.patch('psycopg2.connect', new=FakePostgres) db = PostgreSQL() # test blueprint_meta: BlueprintVersion = generic_blueprint_meta assert db.get_project_domain(blueprint_meta.blueprint_id) is None def test_get_blueprint_name(self, mocker, caplog, monkeypatch, generic_blueprint_meta): # test set up caplog.set_level(logging.DEBUG, logger="opera.api.service.sqldb_service") mocker.patch('psycopg2.connect', new=FakePostgres) db = PostgreSQL() monkeypatch.setattr(FakePostgres, 'cursor', GetBlueprintMetaCursor) # test blueprint_meta: BlueprintVersion = generic_blueprint_meta assert db.get_blueprint_name( blueprint_meta.blueprint_id) == blueprint_meta.blueprint_name def test_save_blueprint_name(self, mocker, monkeypatch, caplog, generic_blueprint_meta): # test set up caplog.set_level(logging.DEBUG, logger="opera.api.service.sqldb_service") mocker.patch('psycopg2.connect', new=FakePostgres) db = PostgreSQL() monkeypatch.setattr(FakePostgres, 'cursor', NoneCursor) # test blueprint_meta: BlueprintVersion = generic_blueprint_meta new_name = 'new_name' db.update_blueprint_name(blueprint_meta.blueprint_id, new_name) command = NoneCursor.get_command() assert_that(command).contains(blueprint_meta.blueprint_name, new_name) assert_that(caplog.text).contains("Updated blueprint name", new_name, str(blueprint_meta.blueprint_id)) def test_save_blueprint_name_exception(self, mocker, monkeypatch, caplog, generic_blueprint_meta): # test set up caplog.set_level(logging.DEBUG, logger="opera.api.service.sqldb_service") mocker.patch('psycopg2.connect', new=FakePostgres) db = PostgreSQL() monkeypatch.setattr(FakePostgres, 'cursor', PsycopgErrorCursor) blueprint_meta: BlueprintVersion = generic_blueprint_meta new_name = 'new_name' db.update_blueprint_name(blueprint_meta.blueprint_id, new_name) assert_that(caplog.text).contains("Fail to update blueprint name", new_name, str(blueprint_meta.blueprint_id)) def test_get_blueprint_name_missing(self, mocker, caplog, generic_blueprint_meta): # test set up caplog.set_level(logging.DEBUG, logger="opera.api.service.sqldb_service") mocker.patch('psycopg2.connect', new=FakePostgres) db = PostgreSQL() # test blueprint_meta: BlueprintVersion = generic_blueprint_meta assert db.get_blueprint_name(blueprint_meta.blueprint_id) is None def test_delete_blueprint_meta(self, mocker, monkeypatch, caplog): # test set up caplog.set_level(logging.DEBUG, logger="opera.api.service.sqldb_service") mocker.patch('psycopg2.connect', new=FakePostgres) db = PostgreSQL() monkeypatch.setattr(FakePostgres, 'cursor', NoneCursor) # testing blueprint_id = uuid.uuid4() assert db.delete_blueprint_meta(blueprint_id) command = NoneCursor.get_command() assert_that(command).contains("delete") assert_that(caplog.text).contains("Deleted blueprint meta", str(blueprint_id)) def test_delete_blueprint_version_meta(self, mocker, monkeypatch, caplog): # test set up caplog.set_level(logging.DEBUG, logger="opera.api.service.sqldb_service") mocker.patch('psycopg2.connect', new=FakePostgres) db = PostgreSQL() monkeypatch.setattr(FakePostgres, 'cursor', NoneCursor) # testing blueprint_id = uuid.uuid4() version_id = 'v1.0' assert db.delete_blueprint_meta(blueprint_id, version_id) command = NoneCursor.get_command() assert_that(command).contains("delete") assert_that(caplog.text).contains("Deleted blueprint meta", str(blueprint_id), str(version_id)) def test_delete_blueprint_meta_fail(self, mocker, monkeypatch, caplog): # test set up caplog.set_level(logging.DEBUG, logger="opera.api.service.sqldb_service") mocker.patch('psycopg2.connect', new=FakePostgres) db = PostgreSQL() monkeypatch.setattr(FakePostgres, 'cursor', PsycopgErrorCursor) # testing blueprint_id = uuid.uuid4() assert not db.delete_blueprint_meta(blueprint_id) assert_that(caplog.text).contains( "Failed to delete blueprint metadata", str(blueprint_id)) inputs = {'foo': 'bar', 'foo2': 'bar2'} def test_get_inputs(self, monkeypatch, mocker): # test set up mocker.patch('psycopg2.connect', new=FakePostgres) db = PostgreSQL() monkeypatch.setattr(FakePostgres, 'cursor', GetInputsCursor) deployment_id = uuid.uuid4() assert_that(db.get_inputs(deployment_id=deployment_id)).is_equal_to( self.inputs) def test_get_inputs_missing(self, mocker): # test set up mocker.patch('psycopg2.connect', new=FakePostgres) db = PostgreSQL() deployment_id = uuid.uuid4() assert_that(db.get_inputs(deployment_id=deployment_id)).is_none() def test_get_inputs_exception(self, monkeypatch, mocker): mocker.patch('psycopg2.connect', new=FakePostgres) db = PostgreSQL() monkeypatch.setattr(FakePostgres, 'cursor', GetStringCursor) deployment_id = uuid.uuid4() assert_that(db.get_inputs(deployment_id=deployment_id)).is_none() deployment = { 'deployment_id': str(uuid.uuid4()), 'state': InvocationState.SUCCESS, 'operation': OperationType.DEPLOY_CONTINUE, 'timestamp': timestamp_util.datetime_now_to_string(), 'last_inputs': None, 'deployment_label': 'label' } def test_get_deployments(self, monkeypatch, mocker): # test set up mocker.patch('psycopg2.connect', new=FakePostgres) db = PostgreSQL() monkeypatch.setattr(FakePostgres, 'cursor', GetBlueprintMetaCursor) blueprint_id = uuid.uuid4() assert_that( db.get_deployments_for_blueprint( blueprint_id, active=False)).is_equal_to([self.deployment]) deployments = [{ "deployment_id": "71ceef1c-f169-4204-b180-e95948329108", "operation": OperationType.DEPLOY_FRESH, "state": InvocationState.SUCCESS, "timestamp": timestamp_util.datetime_now_to_string(), 'last_inputs': None, 'deployment_label': 'label' }, { "deployment_id": "30e143c9-e614-41bc-9b22-47c588f394e3", "operation": OperationType.UNDEPLOY, "state": InvocationState.SUCCESS, "timestamp": timestamp_util.datetime_to_str(datetime.datetime.fromtimestamp(1)), 'last_inputs': None, 'deployment_label': 'label' }] def test_get_active_deployments(self, monkeypatch, mocker): # test set up mocker.patch('psycopg2.connect', new=FakePostgres) db = PostgreSQL() monkeypatch.setattr(FakePostgres, 'cursor', GetDeploymentsCursor) blueprint_id = uuid.uuid4() assert_that( db.get_deployments_for_blueprint( blueprint_id, active=False)).is_equal_to(self.deployments) assert_that(db.get_deployments_for_blueprint( blueprint_id, active=True)).is_equal_to([self.deployments[0]])