require 'cloud/auth' require 'hws_settings' require 'json' require 'p4_error' require 'p4_util' require 'projects/methods' require 'sinatra/base' require 'auth' module Projects class App < Sinatra::Base include Projects::Methods # Sign into the "project server" depending upon the configuration of the # system. # # One implementation of this sits on top of a single p4d. Another uses # Helix Cloud. # # One subtle behavior of p4 login -p, is that it will reuse any token # lying in a p4ticket file. What this means, is that if you sign on, say, # using the p4 command line, this method will end up reusing and returning # that ticket. # # So, make sure you run your web services instance in a clean environment # that you don't do interactive work (like maintenance stuff) under. post '/projects/v1/login' do user = params[:user] password = params[:password] # may be a p4 ticket if user.nil? or password.nil? fail P4Error.default_error('user or password is not set') end ticket = nil if Cloud::Settings.cloud_enabled? ticket = Cloud::Auth::login(user, password) else env['p4'] = P4Util.open_from_env(env) p4 = env['p4'] p4.user = user p4.password = password p4.connect ticket = Auth.ticket_from_login(p4) end halt 403 if ticket.nil? if request.env['HTTP_ACCEPT'] =~ /application\/json/ content_type 'application/json' return { ticket: ticket }.to_json else content_type 'text/plain' return ticket end end # Query parameters: # # - `details` [Boolean] # - `extension` [String] get '/projects/v1' do require_p4 unless Cloud::Settings.cloud_enabled? options = {} options[:details] = params['details'] if params.key?('details') options[:extension] = params['extension'] if params.key?('extension') projects = list(options) projects.to_json if projects end get '/projects/v1/:project_id' do |project_id| require_p4 unless Cloud::Settings.cloud_enabled? project = fetch(project_id) halt 404 unless project project.to_json end end end