LEFT | RIGHT |
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 // The state package enables reading, observing, and changing | 4 // The state package 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 456 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
467 return newMachine(st, mdoc), nil | 467 return newMachine(st, mdoc), nil |
468 } | 468 } |
469 | 469 |
470 // FindEntity returns the entity with the given tag. | 470 // FindEntity returns the entity with the given tag. |
471 // | 471 // |
472 // The returned value can be of type *Machine, *Unit, | 472 // The returned value can be of type *Machine, *Unit, |
473 // *User, *Service or *Environment, depending | 473 // *User, *Service or *Environment, depending |
474 // on the tag. | 474 // on the tag. |
475 func (st *State) FindEntity(tag string) (Entity, error) { | 475 func (st *State) FindEntity(tag string) (Entity, error) { |
476 kind, id, err := names.ParseTag(tag, "") | 476 kind, id, err := names.ParseTag(tag, "") |
477 // TODO(fwereade): when lp:1199352 (relation lacks Tag) is fixed, add | |
478 // support for relation entities here. | |
479 switch kind { | 477 switch kind { |
480 case names.MachineTagKind: | 478 case names.MachineTagKind: |
481 return st.Machine(id) | 479 return st.Machine(id) |
482 case names.UnitTagKind: | 480 case names.UnitTagKind: |
483 return st.Unit(id) | 481 return st.Unit(id) |
484 case names.UserTagKind: | 482 case names.UserTagKind: |
485 return st.User(id) | 483 return st.User(id) |
486 case names.ServiceTagKind: | 484 case names.ServiceTagKind: |
487 return st.Service(id) | 485 return st.Service(id) |
488 case names.EnvironTagKind: | 486 case names.EnvironTagKind: |
489 conf, err := st.EnvironConfig() | 487 conf, err := st.EnvironConfig() |
490 if err != nil { | 488 if err != nil { |
491 return nil, err | 489 return nil, err |
492 } | 490 } |
493 // Return an invalid entity error if the requested environment i
s not | 491 // Return an invalid entity error if the requested environment i
s not |
494 // the current one. | 492 // the current one. |
495 if id != conf.Name() { | 493 if id != conf.Name() { |
496 return nil, errors.NotFoundf("environment %q", id) | 494 return nil, errors.NotFoundf("environment %q", id) |
497 } | 495 } |
498 return st.Environment() | 496 return st.Environment() |
499 case names.RelationTagKind: | 497 case names.RelationTagKind: |
500 » » relId, err := strconv.Atoi(id) | 498 » » return st.KeyRelation(id) |
501 » » if err != nil { | |
502 » » » return nil, errors.NotFoundf("relation %s", id) | |
503 » » } | |
504 » » return st.Relation(relId) | |
505 } | 499 } |
506 return nil, err | 500 return nil, err |
507 } | 501 } |
508 | 502 |
509 // parseTag, given an entity tag, returns the collection name and id | 503 // parseTag, given an entity tag, returns the collection name and id |
510 // of the entity document. | 504 // of the entity document. |
511 func (st *State) parseTag(tag string) (coll string, id string, err error) { | 505 func (st *State) parseTag(tag string) (coll string, id string, err error) { |
512 kind, id, err := names.ParseTag(tag, "") | 506 kind, id, err := names.ParseTag(tag, "") |
513 if err != nil { | 507 if err != nil { |
514 return "", "", err | 508 return "", "", err |
(...skipping 677 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1192 func tagForGlobalKey(key string) (string, bool) { | 1186 func tagForGlobalKey(key string) (string, bool) { |
1193 if len(key) < 3 || key[1] != '#' { | 1187 if len(key) < 3 || key[1] != '#' { |
1194 return "", false | 1188 return "", false |
1195 } | 1189 } |
1196 p, ok := tagPrefix[key[0]] | 1190 p, ok := tagPrefix[key[0]] |
1197 if !ok { | 1191 if !ok { |
1198 return "", false | 1192 return "", false |
1199 } | 1193 } |
1200 return p + key[2:], true | 1194 return p + key[2:], true |
1201 } | 1195 } |
LEFT | RIGHT |