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

Side by Side Diff: state/relation.go

Issue 6564054: state: make Refresh return NotFoundError
Patch Set: state: make Refresh return NotFoundError Created 11 years, 6 months 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
OLDNEW
1 package state 1 package state
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
5 "labix.org/v2/mgo"
5 "labix.org/v2/mgo/txn" 6 "labix.org/v2/mgo/txn"
6 "launchpad.net/juju-core/charm" 7 "launchpad.net/juju-core/charm"
7 "launchpad.net/juju-core/trivial" 8 "launchpad.net/juju-core/trivial"
8 "sort" 9 "sort"
9 "strconv" 10 "strconv"
10 "strings" 11 "strings"
11 ) 12 )
12 13
13 // RelationRole defines the role of a relation endpoint. 14 // RelationRole defines the role of a relation endpoint.
14 type RelationRole string 15 type RelationRole string
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 return &Relation{ 93 return &Relation{
93 st: st, 94 st: st,
94 doc: *doc, 95 doc: *doc,
95 } 96 }
96 } 97 }
97 98
98 func (r *Relation) String() string { 99 func (r *Relation) String() string {
99 return r.doc.Key 100 return r.doc.Key
100 } 101 }
101 102
103 // Refresh refreshes the contents of the relation from the underlying
104 // state. It returns a NotFoundError if the relation has been removed.
102 func (r *Relation) Refresh() error { 105 func (r *Relation) Refresh() error {
103 doc := relationDoc{} 106 doc := relationDoc{}
104 err := r.st.relations.FindId(r.doc.Key).One(&doc) 107 err := r.st.relations.FindId(r.doc.Key).One(&doc)
108 if err == mgo.ErrNotFound {
109 return notFound("relation %v", r)
niemeyer 2012/09/26 14:10:10 s/%v/%q/
rog 2012/09/26 14:21:56 Done.
110 }
105 if err != nil { 111 if err != nil {
106 return fmt.Errorf("cannot refresh relation %v: %v", r, err) 112 return fmt.Errorf("cannot refresh relation %v: %v", r, err)
107 } 113 }
108 r.doc = doc 114 r.doc = doc
109 return nil 115 return nil
110 } 116 }
111 117
118 // Life returns the relation's current life state.
112 func (r *Relation) Life() Life { 119 func (r *Relation) Life() Life {
113 return r.doc.Life 120 return r.doc.Life
114 } 121 }
115 122
116 // EnsureDying sets the relation lifecycle to Dying if it is Alive. 123 // EnsureDying sets the relation lifecycle to Dying if it is Alive.
117 // It does nothing otherwise. 124 // It does nothing otherwise.
118 func (r *Relation) EnsureDying() error { 125 func (r *Relation) EnsureDying() error {
119 err := ensureDying(r.st, r.st.relations, r.doc.Key, "relation") 126 err := ensureDying(r.st, r.st.relations, r.doc.Key, "relation")
120 if err != nil { 127 if err != nil {
121 return err 128 return err
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after
327 // relationScopeDoc represents a unit which is in a relation scope. 334 // relationScopeDoc represents a unit which is in a relation scope.
328 // The relation, container, role, and unit are all encoded in the key. 335 // The relation, container, role, and unit are all encoded in the key.
329 type relationScopeDoc struct { 336 type relationScopeDoc struct {
330 Key string `bson:"_id"` 337 Key string `bson:"_id"`
331 } 338 }
332 339
333 func (d *relationScopeDoc) unitName() string { 340 func (d *relationScopeDoc) unitName() string {
334 parts := strings.Split(d.Key, "#") 341 parts := strings.Split(d.Key, "#")
335 return parts[len(parts)-1] 342 return parts[len(parts)-1]
336 } 343 }
OLDNEW

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