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

Unified Diff: state/state.go

Issue 12473043: names: add ParseTag
Patch Set: names: add ParseTag Created 10 years, 7 months ago
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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « state/apiserver/machine/agent.go ('k') | state/state_test.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: state/state.go
=== modified file 'state/state.go'
--- state/state.go 2013-08-05 14:39:06 +0000
+++ state/state.go 2013-08-07 12:55:15 +0000
@@ -657,42 +657,20 @@
}
// entity returns the entity for the given tag.
+//
+// The returned value can be of type *Machine, *Unit,
+// *User, *Service or *Environment, depending
+// on the tag.
func (st *State) entity(tag string) (interface{}, error) {
- i := strings.Index(tag, "-")
- if i <= 0 || i >= len(tag)-1 {
- return nil, fmt.Errorf("invalid entity tag %q", tag)
- }
- id := tag[i+1:]
- tagKind, err := names.TagKind(tag)
- if err != nil {
- return nil, fmt.Errorf("invalid entity tag %q", tag)
- }
- switch tagKind {
+ kind, id, err := names.ParseTag(tag, "")
+ switch kind {
case names.MachineTagKind:
- id, err := names.MachineFromTag(tag)
- if err != nil {
- return nil, fmt.Errorf("invalid entity tag %q", tag)
- }
- if !names.IsMachine(id) {
- return nil, fmt.Errorf("invalid entity tag %q", tag)
- }
return st.Machine(id)
case names.UnitTagKind:
- i := strings.LastIndex(id, "-")
- if i == -1 {
- return nil, fmt.Errorf("invalid entity tag %q", tag)
- }
- name := id[:i] + "/" + id[i+1:]
- if !names.IsUnit(name) {
- return nil, fmt.Errorf("invalid entity tag %q", tag)
- }
- return st.Unit(name)
+ return st.Unit(id)
case names.UserTagKind:
return st.User(id)
case names.ServiceTagKind:
- if !names.IsService(id) {
- return nil, fmt.Errorf("invalid entity tag %q", tag)
- }
return st.Service(id)
case names.EnvironTagKind:
conf, err := st.EnvironConfig()
@@ -702,11 +680,11 @@
// Return an invalid entity error if the requested environment is not
// the current one.
if id != conf.Name() {
- return nil, fmt.Errorf("invalid entity tag %q", tag)
+ return nil, errors.NotFoundf("environment %q", id)
}
return st.Environment()
}
- return nil, fmt.Errorf("invalid entity tag %q", tag)
+ return nil, err
}
// parseTag, given an entity tag, returns the collection name and id
« no previous file with comments | « state/apiserver/machine/agent.go ('k') | state/state_test.go » ('j') | no next file with comments »

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