github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/model/distro/db.go (about)

     1  package distro
     2  
     3  import (
     4  	"github.com/evergreen-ci/evergreen/db"
     5  	"github.com/evergreen-ci/evergreen/db/bsonutil"
     6  	"gopkg.in/mgo.v2/bson"
     7  )
     8  
     9  var (
    10  	// bson fields for the Distro struct
    11  	IdKey               = bsonutil.MustHaveTag(Distro{}, "Id")
    12  	ArchKey             = bsonutil.MustHaveTag(Distro{}, "Arch")
    13  	PoolSizeKey         = bsonutil.MustHaveTag(Distro{}, "PoolSize")
    14  	ProviderKey         = bsonutil.MustHaveTag(Distro{}, "Provider")
    15  	ProviderSettingsKey = bsonutil.MustHaveTag(Distro{}, "ProviderSettings")
    16  	SetupAsSudoKey      = bsonutil.MustHaveTag(Distro{}, "SetupAsSudo")
    17  	SetupKey            = bsonutil.MustHaveTag(Distro{}, "Setup")
    18  	UserKey             = bsonutil.MustHaveTag(Distro{}, "User")
    19  	SSHKeyKey           = bsonutil.MustHaveTag(Distro{}, "SSHKey")
    20  	SSHOptionsKey       = bsonutil.MustHaveTag(Distro{}, "SSHOptions")
    21  	WorkDirKey          = bsonutil.MustHaveTag(Distro{}, "WorkDir")
    22  
    23  	UserDataKey = bsonutil.MustHaveTag(Distro{}, "UserData")
    24  
    25  	SpawnAllowedKey = bsonutil.MustHaveTag(Distro{}, "SpawnAllowed")
    26  	ExpansionsKey   = bsonutil.MustHaveTag(Distro{}, "Expansions")
    27  
    28  	// bson fields for the UserData struct
    29  	UserDataFileKey     = bsonutil.MustHaveTag(UserData{}, "File")
    30  	UserDataValidateKey = bsonutil.MustHaveTag(UserData{}, "Validate")
    31  )
    32  
    33  const Collection = "distro"
    34  
    35  // All is a query that returns all distros.
    36  var All = db.Query(nil)
    37  
    38  // FindOne gets one Distro for the given query.
    39  func FindOne(query db.Q) (*Distro, error) {
    40  	d := &Distro{}
    41  	return d, db.FindOneQ(Collection, query, d)
    42  }
    43  
    44  // Find gets every Distro matching the given query.
    45  func Find(query db.Q) ([]Distro, error) {
    46  	distros := []Distro{}
    47  	err := db.FindAllQ(Collection, query, &distros)
    48  	return distros, err
    49  }
    50  
    51  // Insert writes the distro to the database.
    52  func (d *Distro) Insert() error {
    53  	return db.Insert(Collection, d)
    54  }
    55  
    56  // Update updates one distro.
    57  func (d *Distro) Update() error {
    58  	return db.UpdateId(Collection, d.Id, d)
    59  }
    60  
    61  // Remove removes one distro.
    62  func Remove(id string) error {
    63  	return db.Remove(Collection, bson.D{{IdKey, id}})
    64  }
    65  
    66  // ById returns a query that contains an Id selector on the string, id.
    67  func ById(id string) db.Q {
    68  	return db.Query(bson.D{{IdKey, id}})
    69  }
    70  
    71  // ByProvider returns a query that contains a Provider selector on the string, p.
    72  func ByProvider(p string) db.Q {
    73  	return db.Query(bson.D{{ProviderKey, p}})
    74  }
    75  
    76  // BySpawnAllowed returns a query that contains the SpawnAllowed selector.
    77  func BySpawnAllowed() db.Q {
    78  	return db.Query(bson.D{{SpawnAllowedKey, true}})
    79  }