module Connectors class Project < Base include Helpers::InsertCommand, Helpers::ShowCommand, Helpers::ListCommand, Helpers::DeleteCommand, Helpers::UpdateCommand def initialize(db) @collection = db.collection('projects') end def is_project_exists?(project) self.project(project.id) return true rescue RecordNotFound => e return false end def projects_all list end def projects ids=nil, type=nil, fields=[] query = {} query['_id'] = {'$in' => ids} if ids query['type'] = 'multi' if type == :multi list(query, fields: fields) end # names - array of project names def project_names_with_envs(names=nil) # db.projects.aggregate({$unwind:"$deploy_envs"}, {$project:{"deploy_envs.identifier":1}}, {$group:{_id:"$_id", envs: {$addToSet: "$deploy_envs.identifier"}}}) q = [] unless names.nil? q.push({ '$match' => { '_id' => { '$in' => names } } }) end q.push({ '$unwind' => '$deploy_envs' }) q.push({ '$project' => { 'deploy_envs.identifier' => 1 } }) q.push({ '$group' => { '_id' => '$_id', 'envs' => { '$addToSet' => '$deploy_envs.identifier' } } }) res = @collection.aggregate(q) r = {} res.each do |ar| r[ar['_id']] = ar['envs'] end return r end def projects_by_image(image) list('deploy_envs.image' => image) end def projects_by_user(user) list('deploy_envs.users' => user) end def check_project_auth(project_id, env, user_id) project = show(project_id) raise InvalidPrivileges.new("User '#{user_id}' unauthorized to work with project '#{project_id}'") unless project.check_authorization(user_id, env) project end private def model_from_bson(bson) ::Project.create_from_bson(bson) end end end