def delete(self, project_uuid): """Delete the specified project""" args = filename_parser.parse_args() log.info("DELETE to /projects/{}/files with args: {}".format( project_uuid, args)) # try to load the project project_path = os.path.join('projects', project_uuid) if not os.path.isdir(project_path): log.error( "No project found with name/UUID {}".format(project_uuid)) return { 'error_msg': "Project not found: {}".format(project_uuid) }, 404 project = cli_project.load_project(project_path) # check if file exists filename = args["filename"] if not os.path.isfile(os.path.join(project_path, filename)): log.error("File {} not found in project with UUID {}".format( filename, project_uuid)) return { "project_uuid": project_uuid, "error_msg": "File {} not found in project".format(filename) }, 404 # remove file from project manifest and delete it project.remove_file(os.path.join(project_path, filename)) os.remove(os.path.join(project_path, filename)) return { "project_uuid": project_uuid, "removed_file": filename, "error_msg": project.error_msg }
def post(self, project_uuid): """Upload and add a file to the specified project""" args = file_upload_parser.parse_args() log.info("POST to /projects/{}/files with args: {}".format( project_uuid, args)) # try to load the project project_path = os.path.join('projects', project_uuid) if not os.path.isdir(project_path): log.error( "No project found with name/UUID {}".format(project_uuid)) return { 'error_msg': "Project not found: {}".format(project_uuid) }, 404 project = cli_project.load_project(project_path) # check if file already exists file = args["file"] if os.path.isfile(os.path.join(project_path, file.filename)): log.warning("Overriding existing file {}".format(file.filename)) # save uploaded file to project and add to project manifest log.debug("Adding uploaded file {} to project with UUID {}".format( file.filename, project_uuid)) file.save(os.path.join(project_path, file.filename)) project.add_file(os.path.join(project_path, file.filename), args["file_type"]) return { "project_uuid": project_uuid, "filename": file.filename, "error_msg": project.error_msg }
def dispatch(args): if args is None: args = parse_args() # use specified workspace or default if args.workspace: ws_root = os.path.expanduser(args.workspace) else: ws_root = Workspace.DEFAULT_WORKSPACE_DIR ws = Workspace.load_workspace(ws_root) if not ws: log.error( "Could not find a 5GTANGO workspace at the specified location") exit(1) prj_root = os.path.expanduser(args.project) if args.add: # load project and add file to project.yml log.debug("Attempting to add file {}".format(args.add)) proj = Project.load_project(prj_root, ws) proj.add_file(args.add, type=args.type) elif args.remove: # load project and remove file from project.yml log.debug("Attempting to remove file {}".format(args.remove)) proj = Project.load_project(prj_root, ws) proj.remove_file(args.remove) elif args.status: # load project and show status log.debug("Attempting to show project status") proj = Project.load_project(prj_root, ws) proj.status() elif args.translate: proj = Project.load_project(prj_root, ws, translate=True) proj.translate() else: # create project log.debug("Attempting to create a new project") if args.vnfs != len(args.image_names): log.info("Number of VNFs and VNF image names don't match." " Using default image names if necessary.") if args.vnfs != len(args.image_types): log.info("Number of VNFs and VNF image types don't match." " Using default image types if necessary.") proj = Project(ws, prj_root) proj.create_prj(args) log.debug("Project created.") return proj
def test_load_example_project(self, capsys, workspace): ws = Workspace.load_workspace(workspace) project = Project.load_project('example-project', workspace=ws) project.status() # assert that the status is printed correctly stdout = capsys.readouterr().out assert all(x in stdout for x in ['Project:', 'Vendor:', 'Version:']) assert all(x in stdout for x in ['MIME type', 'Quantity'])
def test_example_project_descriptors(self, workspace): ws = Workspace.load_workspace(workspace) example_project = Project.load_project('example-project', workspace=ws) example_project.status() vnfds = example_project.get_vnfds() assert vnfds == ['tango_vnfd0.yml'] nsds = example_project.get_nsds() assert nsds == ['tango_nsd.yml']
def get(self, project_uuid): """Get a list of files in the specified project""" log.info("GET to /projects/{}/files".format(project_uuid)) project_path = os.path.join('projects', project_uuid) if not os.path.isdir(project_path): log.error("No project found with name/UUID {}".format(project_uuid)) return {'error_msg': "Project not found: {}".format(project_uuid)}, 404 project = cli_project.load_project(project_path) return {"project_uuid": project_uuid, "files": project.project_config["files"]}
def get(self, project_uuid): """Get the UUID and manifest of the specified project""" log.info("GET to /projects/{}".format(project_uuid)) project_path = os.path.join('projects', project_uuid) if not os.path.isdir(project_path): log.error("No project found with name/UUID {}".format(project_uuid)) return {'error_msg': "Project not found: {}".format(project_uuid)}, 404 project = cli_project.load_project(project_path) return {"project_uuid": project_uuid, "manifest": project.project_config, "error_msg": project.error_msg}
def validate_project(self, project): """ Validate a SONATA project. By default, it performs the following validations: syntax, integrity and network topology. :param project: SONATA project :return: True if all validations were successful, False otherwise """ if not self._assert_configuration(): return if project.endswith('/'): project_path = project else: project_path = project + '/' # consider cases when project is a path if not os.path.isdir(project): log.error("Incorrect path. Try again with a correct project path") return False if type(project) is not Project and os.path.isdir(project): if not self._workspace: log.error("Workspace not defined. Unable to validate project") return if self._workspace_path.endswith('/'): (self._workspace.config['projects_config']) = ( self._workspace_path + 'projects/config.yml') else: (self._workspace.config['projects_config']) = ( self._workspace_path + '/projects/config.yml') project = Project.__create_from_descriptor__( self._workspace, project) if type(project) is not Project: return log.info("Validating project '{0}'".format(project.project_root)) log.info("... syntax: {0}, integrity: {1}, topology: {2}".format( self._syntax, self._integrity, self._topology)) # retrieve project configuration self._dpath = [] vnfds_files = project.get_vnfds() for i in vnfds_files: self._dpath.append(project_path + i) self._dext = project.descriptor_extension # load all project descriptors present at source directory log.debug("Loading project service") nsd_file = Validator._load_project_service_file(project) if not nsd_file: return nsd_file = project_path + nsd_file return self.validate_service(nsd_file)