OLD | NEW |
1 // Copyright 2012, 2013 Canonical Ltd. | 1 // Copyright 2012, 2013 Canonical Ltd. |
2 // Licensed under the AGPLv3, see LICENCE file for details. | 2 // Licensed under the AGPLv3, see LICENCE file for details. |
3 | 3 |
4 // Package state enables reading, observing, and changing | 4 // Package state enables reading, observing, and changing |
5 // the state stored in MongoDB of a whole environment | 5 // the state stored in MongoDB of a whole environment |
6 // managed by juju. | 6 // managed by juju. |
7 package state | 7 package state |
8 | 8 |
9 import ( | 9 import ( |
10 "fmt" | 10 "fmt" |
(...skipping 1390 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1401 err := st.actions.FindId(id).One(&doc) | 1401 err := st.actions.FindId(id).One(&doc) |
1402 if err == mgo.ErrNotFound { | 1402 if err == mgo.ErrNotFound { |
1403 return nil, errors.NotFoundf("action %q", id) | 1403 return nil, errors.NotFoundf("action %q", id) |
1404 } | 1404 } |
1405 if err != nil { | 1405 if err != nil { |
1406 return nil, fmt.Errorf("cannot get action %q: %v", id, err) | 1406 return nil, fmt.Errorf("cannot get action %q: %v", id, err) |
1407 } | 1407 } |
1408 return newAction(st, doc), nil | 1408 return newAction(st, doc), nil |
1409 } | 1409 } |
1410 | 1410 |
| 1411 // UnitActions returns a list of pending actions for a unit named name |
| 1412 func (st *State) UnitActions(name string) ([]*Action, error) { |
| 1413 actions := []*Action{} |
| 1414 prefix := actionPrefix(unitGlobalKey(name)) |
| 1415 sel := bson.D{{"_id", bson.D{{"$regex", "^" + prefix}}}} |
| 1416 iter := st.actions.Find(sel).Iter() |
| 1417 doc := actionDoc{} |
| 1418 for iter.Next(&doc) { |
| 1419 actions = append(actions, newAction(st, doc)) |
| 1420 } |
| 1421 if err := iter.Err(); err != nil { |
| 1422 return actions, err |
| 1423 } |
| 1424 return actions, nil |
| 1425 } |
| 1426 |
1411 // Unit returns a unit by name. | 1427 // Unit returns a unit by name. |
1412 func (st *State) Unit(name string) (*Unit, error) { | 1428 func (st *State) Unit(name string) (*Unit, error) { |
1413 if !names.IsUnit(name) { | 1429 if !names.IsUnit(name) { |
1414 return nil, fmt.Errorf("%q is not a valid unit name", name) | 1430 return nil, fmt.Errorf("%q is not a valid unit name", name) |
1415 } | 1431 } |
1416 doc := unitDoc{} | 1432 doc := unitDoc{} |
1417 err := st.units.FindId(name).One(&doc) | 1433 err := st.units.FindId(name).One(&doc) |
1418 if err == mgo.ErrNotFound { | 1434 if err == mgo.ErrNotFound { |
1419 return nil, errors.NotFoundf("unit %q", name) | 1435 return nil, errors.NotFoundf("unit %q", name) |
1420 } | 1436 } |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1583 func tagForGlobalKey(key string) (string, bool) { | 1599 func tagForGlobalKey(key string) (string, bool) { |
1584 if len(key) < 3 || key[1] != '#' { | 1600 if len(key) < 3 || key[1] != '#' { |
1585 return "", false | 1601 return "", false |
1586 } | 1602 } |
1587 p, ok := tagPrefix[key[0]] | 1603 p, ok := tagPrefix[key[0]] |
1588 if !ok { | 1604 if !ok { |
1589 return "", false | 1605 return "", false |
1590 } | 1606 } |
1591 return p + key[2:], true | 1607 return p + key[2:], true |
1592 } | 1608 } |
OLD | NEW |