Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(4)

Side by Side Diff: state/state.go

Issue 7945044: statecmd: don't use juju.Conn unnecessarily
Patch Set: statecmd: don't use juju.Conn unnecessarily Created 12 years ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « state/machine_test.go ('k') | state/statecmd/destroyunit.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // The state package enables reading, observing, and changing 1 // The state package enables reading, observing, and changing
2 // the state stored in MongoDB of a whole environment 2 // the state stored in MongoDB of a whole environment
3 // managed by juju. 3 // managed by juju.
4 package state 4 package state
5 5
6 import ( 6 import (
7 "fmt" 7 "fmt"
8 "labix.org/v2/mgo" 8 "labix.org/v2/mgo"
9 "labix.org/v2/mgo/bson" 9 "labix.org/v2/mgo/bson"
10 "labix.org/v2/mgo/txn" 10 "labix.org/v2/mgo/txn"
(...skipping 734 matching lines...) Expand 10 before | Expand all | Expand 10 after
745 err := st.units.FindId(name).One(&doc) 745 err := st.units.FindId(name).One(&doc)
746 if err == mgo.ErrNotFound { 746 if err == mgo.ErrNotFound {
747 return nil, NotFoundf("unit %q", name) 747 return nil, NotFoundf("unit %q", name)
748 } 748 }
749 if err != nil { 749 if err != nil {
750 return nil, fmt.Errorf("cannot get unit %q: %v", name, err) 750 return nil, fmt.Errorf("cannot get unit %q: %v", name, err)
751 } 751 }
752 return newUnit(st, &doc), nil 752 return newUnit(st, &doc), nil
753 } 753 }
754 754
755 // DestroyUnits destroys the units with the specified names.
756 func (st *State) DestroyUnits(names ...string) (err error) {
757 // TODO(rog) make this a transaction?
758 var errs []string
759 for _, name := range names {
760 unit, err := st.Unit(name)
761 switch {
762 case IsNotFound(err):
763 err = fmt.Errorf("unit %q does not exist", name)
764 case err != nil:
765 case unit.Life() != Alive:
766 continue
767 case unit.IsPrincipal():
768 err = unit.Destroy()
769 default:
770 err = fmt.Errorf("unit %q is a subordinate", name)
771 }
772 if err != nil {
773 errs = append(errs, err.Error())
774 }
775 }
776 return destroyErr("units", names, errs)
777 }
778
779 // DestroyMachines destroys the machines with the specified ids.
780 func (st *State) DestroyMachines(ids ...string) (err error) {
781 var errs []string
782 for _, id := range ids {
783 machine, err := st.Machine(id)
784 switch {
785 case IsNotFound(err):
786 err = fmt.Errorf("machine %s does not exist", id)
787 case err != nil:
788 case machine.Life() != Alive:
789 continue
790 default:
791 err = machine.Destroy()
792 }
793 if err != nil {
794 errs = append(errs, err.Error())
795 }
796 }
797 return destroyErr("machines", ids, errs)
798 }
799
800 func destroyErr(desc string, ids, errs []string) error {
801 if len(errs) == 0 {
802 return nil
803 }
804 msg := "some %s were not destroyed"
805 if len(errs) == len(ids) {
806 msg = "no %s were destroyed"
807 }
808 msg = fmt.Sprintf(msg, desc)
809 return fmt.Errorf("%s: %s", msg, strings.Join(errs, "; "))
810 }
811
755 // AssignUnit places the unit on a machine. Depending on the policy, and the 812 // AssignUnit places the unit on a machine. Depending on the policy, and the
756 // state of the environment, this may lead to new instances being launched 813 // state of the environment, this may lead to new instances being launched
757 // within the environment. 814 // within the environment.
758 func (st *State) AssignUnit(u *Unit, policy AssignmentPolicy) (err error) { 815 func (st *State) AssignUnit(u *Unit, policy AssignmentPolicy) (err error) {
759 if !u.IsPrincipal() { 816 if !u.IsPrincipal() {
760 return fmt.Errorf("subordinate unit %q cannot be assigned direct ly to a machine", u) 817 return fmt.Errorf("subordinate unit %q cannot be assigned direct ly to a machine", u)
761 } 818 }
762 defer trivial.ErrorContextf(&err, "cannot assign unit %q to machine", u) 819 defer trivial.ErrorContextf(&err, "cannot assign unit %q to machine", u)
763 var m *Machine 820 var m *Machine
764 switch policy { 821 switch policy {
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
879 }} 936 }}
880 if err := st.runner.Run(ops, "", nil); err != nil { 937 if err := st.runner.Run(ops, "", nil); err != nil {
881 return fmt.Errorf("cannot remove empty cleanup document: %v", err) 938 return fmt.Errorf("cannot remove empty cleanup document: %v", err)
882 } 939 }
883 } 940 }
884 if err := iter.Err(); err != nil { 941 if err := iter.Err(); err != nil {
885 return fmt.Errorf("cannot read cleanup document: %v", err) 942 return fmt.Errorf("cannot read cleanup document: %v", err)
886 } 943 }
887 return nil 944 return nil
888 } 945 }
OLDNEW
« no previous file with comments | « state/machine_test.go ('k') | state/statecmd/destroyunit.go » ('j') | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b