require 'json' require 'p4_web_api_client' require 'p4_web_services_auth' require 'p4_project_services/helpers' require 'p4_project_services/models' require 'p4_project_services/project_dao' require 'p4_project_services/version' require 'p4_project_services_data' require 'rack/parser' require 'sinatra/base' require 'sinatra/json' module P4ProjectServices class App < Sinatra::Base # Automatically parse content as JSON use Rack::Parser, content_types: { 'application/json' => proc { |body| ::MultiJson.decode body } } use P4WebServicesAuth::AuthMiddleware, settings: settings before do content_type 'application/json' end configure do set(:p4, 'host' => 'localhost', 'port' => '1666') set(:token_path, '/tmp/p4_project_services/tokens') set(:p4_web_api_url, 'http://localhost:4567') set(:p4_web_api_prefix, nil) set :raise_errors, :environment == :test set :dump_errors, :environment == :development set :show_exceptions, :environment == :development set :port, 4568, :environment == :development set :p4, {'host' => 'localhost', 'port' => '1666', 'charset' => 'auto'}, :environment == :development enable :logging end helpers P4ProjectServices::Helpers error do err = env['sinatra.error'] puts "error: #{err.to_s}:\n#{err.backtrace.join('\n')}" msg = err.message if ENV['RACK_ENV'] == 'development' msg += "\n#{err.backtrace.join('\n')}" end e = P4WebServicesAuth::P4Error.default_error(msg) return { MessageCode: e.message_code, MessageSeverity: e.message_severity, MessageText: e.message_text }.to_json end # List all projects in the system. # # Each 'project ID' should actually be the sub-path between the '//.project/' # depot start and the '/.project.json' end. # # If the first response is actually an error with message code 6244, it's # likely that the '//.project' depot doesn't actually exist. We'll just # return an empty list at this point. # # We actually provide a series of 'filtering' operations to make it easier # for client applications to say a few things: # # - owner # - name # - extensions # # TODO define a safe name 'matching' scheme that allows for wildcards get '/v1/projects' do res = nil args = {} args[:owners] = params['owners'] if params.key?('owners') args[:names] = params['names'] if params.key?('names') args[:extensions] = params['extensions'] if params.key?('extensions') projects = nil open_client do |c| dao = ProjectDao.new(c) projects = dao.find(args) end projects.to_json end # Create a new project. post '/v1/projects' do project = P4ProjectServicesData::Project.json_create(params) open_client do |client| dao = ProjectDao.new(client) dao.create(project) end project.to_json end # Return a project get '/v1/projects/*' do id = params[:splat].join('/') project = nil open_client do |client| dao = ProjectDao.new(client) project = dao.find_by_id(id) end project.to_json end # Update a part of the project patch '/v1/projects/*' do id = params[:splat].join('/') open_client do |client| # TODO investigate path encoding/mapping depotPath = "//.project/#{id}/project.json" project = nil file = client.file(depotPath) if file.is_a?(P4WebApiClient::Models::File) project = P4ProjectServicesData::Project.new(JSON.parse(file.content)) end halt 404 unless project new_fields = models::Project.new(params) new_fields.each_pair do |field, value| project[field] = value end json = project.to_json client.upload_file({ depot_file: depotPath, content: json }) end '' end # Update the entire project spec put '/v1/projects/*' do id = params[:splat].join('/') open_client do |client| # TODO investigate path encoding/mapping depotPath = "//.project/#{id}/project.json" project = nil file = client.file(depotPath) if file.is_a?(P4WebApiClient::Models::File) project = P4ProjectServicesData::Project.new(JSON.parse(file.content)) end halt 404 unless project new_proj = models::Project.new(params) json = new_proj.to_json client.upload_file({ depot_file: depotPath, content: json }) end '' end # Delete the project delete '/v1/projects/*' do id = params[:splat].join('/') open_client do |client| # TODO investigate path encoding/mapping depotPath = "//.project/#{id}/project.json" client.delete_file(depotPath) end '' end end end
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#7 | 13972 | tjuricek |
Removing old microservice implementations. The system is now mostly a monolith. Eventually there will be a websocket service. |
||
#6 | 13789 | tjuricek |
Basic create/read methods for hooks on project services. Not connected to the main project handler yet. |
||
#5 | 13528 | tjuricek |
Moved rack and app server configuration to be managed via Salt. Also, only using a single value "url" to configure how the p4_project_services instance references the p4_web_api. And, removing the Docker setup, since that won't work for a production system. |
||
#4 | 13481 | tjuricek |
Tests for the p4 web api and p4 project services now pass against a development setup both in and out of the docker cluster. Note that configuration *has not* been finalized, so conventions to dealing with development vs production need to be organized a bit. |
||
#3 | 13462 | tjuricek |
Created a preliminary caching schema and basic database models for the p4 project services. This may break some tests momentarily until I implement caching. |
||
#2 | 13459 | tjuricek |
Return the created project when creating a new project, since default values will often be filled out. Allow new projects to be created only with names set. We'll generate an ID at the moment, when I have better indexing, I'll double check for uniqueness. |
||
#1 | 13412 | tjuricek |
Initial version of the web-services mainline. This is a collection of several projects, that will likely often get released together, though many of them may not always be relevant. See the README for more information. |