github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/model/project_vars.go (about) 1 package model 2 3 import ( 4 "github.com/evergreen-ci/evergreen/db" 5 "github.com/evergreen-ci/evergreen/db/bsonutil" 6 "gopkg.in/mgo.v2" 7 "gopkg.in/mgo.v2/bson" 8 ) 9 10 var ( 11 ProjectVarIdKey = bsonutil.MustHaveTag(ProjectVars{}, "Id") 12 ProjectVarsMapKey = bsonutil.MustHaveTag(ProjectVars{}, "Vars") 13 PrivateVarsMapKey = bsonutil.MustHaveTag(ProjectVars{}, "PrivateVars") 14 ) 15 16 const ( 17 ProjectVarsCollection = "project_vars" 18 ) 19 20 //ProjectVars holds a map of variables specific to a given project. 21 //They can be fetched at run time by the agent, so that settings which are 22 //sensitive or subject to frequent change don't need to be hard-coded into 23 //yml files. 24 type ProjectVars struct { 25 26 //Should match the _id in the project it refers to 27 Id string `bson:"_id" json:"_id"` 28 29 //The actual mapping of variables for this project 30 Vars map[string]string `bson:"vars" json:"vars"` 31 32 //PrivateVars keeps track of which variables are private and should therefore not 33 //be returned to the UI server. 34 PrivateVars map[string]bool `bson:"private_vars" json:"private_vars"` 35 } 36 37 func FindOneProjectVars(projectId string) (*ProjectVars, error) { 38 projectVars := &ProjectVars{} 39 err := db.FindOne( 40 ProjectVarsCollection, 41 bson.M{ 42 ProjectVarIdKey: projectId, 43 }, 44 db.NoProjection, 45 db.NoSort, 46 projectVars, 47 ) 48 if err == mgo.ErrNotFound { 49 return nil, nil 50 } 51 if err != nil { 52 return nil, err 53 } 54 return projectVars, nil 55 } 56 57 func (projectVars *ProjectVars) Upsert() (*mgo.ChangeInfo, error) { 58 return db.Upsert( 59 ProjectVarsCollection, 60 bson.M{ 61 ProjectVarIdKey: projectVars.Id, 62 }, 63 bson.M{ 64 "$set": bson.M{ 65 ProjectVarsMapKey: projectVars.Vars, 66 PrivateVarsMapKey: projectVars.PrivateVars, 67 }, 68 }, 69 ) 70 } 71 72 func (projectVars *ProjectVars) RedactPrivateVars() { 73 if projectVars != nil && 74 projectVars.Vars != nil && 75 projectVars.PrivateVars != nil { 76 // Redact private variables 77 for k := range projectVars.Vars { 78 if _, ok := projectVars.PrivateVars[k]; ok { 79 projectVars.Vars[k] = "" 80 } 81 } 82 } 83 }