github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/state/constraints.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package state
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/juju/errors"
    10  	"gopkg.in/mgo.v2"
    11  	"gopkg.in/mgo.v2/bson"
    12  	"gopkg.in/mgo.v2/txn"
    13  
    14  	"github.com/juju/juju/constraints"
    15  	"github.com/juju/juju/instance"
    16  )
    17  
    18  // constraintsDoc is the mongodb representation of a constraints.Value.
    19  type constraintsDoc struct {
    20  	ModelUUID    string `bson:"model-uuid"`
    21  	Arch         *string
    22  	CpuCores     *uint64
    23  	CpuPower     *uint64
    24  	Mem          *uint64
    25  	RootDisk     *uint64
    26  	InstanceType *string
    27  	Container    *instance.ContainerType
    28  	Tags         *[]string
    29  	Spaces       *[]string
    30  }
    31  
    32  func (doc constraintsDoc) value() constraints.Value {
    33  	return constraints.Value{
    34  		Arch:         doc.Arch,
    35  		CpuCores:     doc.CpuCores,
    36  		CpuPower:     doc.CpuPower,
    37  		Mem:          doc.Mem,
    38  		RootDisk:     doc.RootDisk,
    39  		InstanceType: doc.InstanceType,
    40  		Container:    doc.Container,
    41  		Tags:         doc.Tags,
    42  		Spaces:       doc.Spaces,
    43  	}
    44  }
    45  
    46  func newConstraintsDoc(st *State, cons constraints.Value) constraintsDoc {
    47  	return constraintsDoc{
    48  		Arch:         cons.Arch,
    49  		CpuCores:     cons.CpuCores,
    50  		CpuPower:     cons.CpuPower,
    51  		Mem:          cons.Mem,
    52  		RootDisk:     cons.RootDisk,
    53  		InstanceType: cons.InstanceType,
    54  		Container:    cons.Container,
    55  		Tags:         cons.Tags,
    56  		Spaces:       cons.Spaces,
    57  	}
    58  }
    59  
    60  func createConstraintsOp(st *State, id string, cons constraints.Value) txn.Op {
    61  	return txn.Op{
    62  		C:      constraintsC,
    63  		Id:     id,
    64  		Assert: txn.DocMissing,
    65  		Insert: newConstraintsDoc(st, cons),
    66  	}
    67  }
    68  
    69  func setConstraintsOp(st *State, id string, cons constraints.Value) txn.Op {
    70  	return txn.Op{
    71  		C:      constraintsC,
    72  		Id:     id,
    73  		Assert: txn.DocExists,
    74  		Update: bson.D{{"$set", newConstraintsDoc(st, cons)}},
    75  	}
    76  }
    77  
    78  func removeConstraintsOp(st *State, id string) txn.Op {
    79  	return txn.Op{
    80  		C:      constraintsC,
    81  		Id:     id,
    82  		Remove: true,
    83  	}
    84  }
    85  
    86  func readConstraints(st *State, id string) (constraints.Value, error) {
    87  	constraintsCollection, closer := st.getCollection(constraintsC)
    88  	defer closer()
    89  
    90  	doc := constraintsDoc{}
    91  	if err := constraintsCollection.FindId(id).One(&doc); err == mgo.ErrNotFound {
    92  		return constraints.Value{}, errors.NotFoundf("constraints")
    93  	} else if err != nil {
    94  		return constraints.Value{}, err
    95  	}
    96  	return doc.value(), nil
    97  }
    98  
    99  func writeConstraints(st *State, id string, cons constraints.Value) error {
   100  	ops := []txn.Op{setConstraintsOp(st, id, cons)}
   101  	if err := st.runTransaction(ops); err != nil {
   102  		return fmt.Errorf("cannot set constraints: %v", err)
   103  	}
   104  	return nil
   105  }