Skip to content

Commit

Permalink
Preserve the entity tag in LogStream.Next().
Browse files Browse the repository at this point in the history
  • Loading branch information
ericsnowcurrently committed Jun 21, 2016
1 parent 569e108 commit 51cb389
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
11 changes: 11 additions & 0 deletions api/logstream/logstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/juju/errors"
"github.com/juju/loggo"
"github.com/juju/version"
"gopkg.in/juju/names.v2"

"github.com/juju/juju/api/base"
"github.com/juju/juju/api/common/stream"
Expand Down Expand Up @@ -128,6 +129,16 @@ func api2record(apiRec params.LogStreamRecord, controllerUUID string) (logfwd.Re
Message: apiRec.Message,
}

entity, err := names.ParseTag(apiRec.Entity)
if err != nil {
return rec, errors.Annotate(err, "invalid entity")
}
rec.Origin.Type, err = logfwd.ParseOriginType(entity.Kind())
if err != nil {
return rec, errors.Annotate(err, "invalid entity")
}
rec.Origin.Name = entity.Id()

ver, err := version.Parse(apiRec.Version)
if err != nil {
return rec, errors.Annotatef(err, "invalid version %q", apiRec.Version)
Expand Down
3 changes: 3 additions & 0 deletions api/logstream/logstream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func (s *LogReaderSuite) TestNextOneRecord(c *gc.C) {
ts := time.Now()
apiRec := params.LogStreamRecord{
ModelUUID: "deadbeef-2f18-4fd2-967d-db9663db7bea",
Entity: "machine-99",
Version: version.Current.String(),
Timestamp: ts,
Module: "api.logstream.test",
Expand Down Expand Up @@ -104,6 +105,8 @@ func (s *LogReaderSuite) TestNextOneRecord(c *gc.C) {
Origin: logfwd.Origin{
ControllerUUID: cUUID,
ModelUUID: "deadbeef-2f18-4fd2-967d-db9663db7bea",
Type: logfwd.OriginTypeMachine,
Name: "99",
JujuVersion: version.Current,
},
Timestamp: ts,
Expand Down
12 changes: 12 additions & 0 deletions logfwd/origin.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@ const canonicalPEN = 28978
const (
OriginTypeUnknown OriginType = 0
OriginTypeUser = iota
OriginTypeMachine
OriginTypeUnit
)

var originTypes = map[OriginType]string{
OriginTypeUnknown: "unknown",
OriginTypeUser: names.UserTagKind,
OriginTypeMachine: names.MachineTagKind,
OriginTypeUnit: names.UnitTagKind,
}

// OriginType is the "enum" type for the different kinds of log record
Expand Down Expand Up @@ -73,6 +77,14 @@ func (ot OriginType) ValidateName(name string) error {
if !names.IsValidUser(name) {
return errors.NewNotValid(nil, "bad user name")
}
case OriginTypeMachine:
if !names.IsValidMachine(name) {
return errors.NewNotValid(nil, "bad machine name")
}
case OriginTypeUnit:
if !names.IsValidUnit(name) {
return errors.NewNotValid(nil, "bad unit name")
}
}
return nil
}
Expand Down
16 changes: 16 additions & 0 deletions logfwd/origin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ func (s *OriginTypeSuite) TestParseOriginTypeValid(c *gc.C) {
tests := map[string]logfwd.OriginType{
"unknown": logfwd.OriginTypeUnknown,
"user": logfwd.OriginTypeUser,
"machine": logfwd.OriginTypeMachine,
"unit": logfwd.OriginTypeUnit,
}
for str, expected := range tests {
c.Logf("trying %q", str)
Expand Down Expand Up @@ -56,6 +58,8 @@ func (s *OriginTypeSuite) TestString(c *gc.C) {
tests := map[logfwd.OriginType]string{
logfwd.OriginTypeUnknown: "unknown",
logfwd.OriginTypeUser: "user",
logfwd.OriginTypeMachine: "machine",
logfwd.OriginTypeUnit: "unit",
}
for ot, expected := range tests {
c.Logf("trying %q", ot)
Expand All @@ -70,6 +74,8 @@ func (s *OriginTypeSuite) TestValidateValid(c *gc.C) {
tests := []logfwd.OriginType{
logfwd.OriginTypeUnknown,
logfwd.OriginTypeUser,
logfwd.OriginTypeMachine,
logfwd.OriginTypeUnit,
}
for _, ot := range tests {
c.Logf("trying %q", ot)
Expand Down Expand Up @@ -101,6 +107,8 @@ func (s *OriginTypeSuite) TestValidateNameValid(c *gc.C) {
tests := map[logfwd.OriginType]string{
logfwd.OriginTypeUnknown: "",
logfwd.OriginTypeUser: "a-user",
logfwd.OriginTypeMachine: "99",
logfwd.OriginTypeUnit: "svc-a/0",
}
for ot, name := range tests {
c.Logf("trying %q + %q", ot, name)
Expand All @@ -124,6 +132,14 @@ func (s *OriginTypeSuite) TestValidateNameInvalid(c *gc.C) {
ot: logfwd.OriginTypeUser,
name: "...",
err: `bad user name`,
}, {
ot: logfwd.OriginTypeMachine,
name: "...",
err: `bad machine name`,
}, {
ot: logfwd.OriginTypeUnit,
name: "...",
err: `bad unit name`,
}}
for _, test := range tests {
c.Logf("trying %q + %q", test.ot, test.name)
Expand Down

0 comments on commit 51cb389

Please sign in to comment.