Skip to content

Commit

Permalink
add basic system-generated tags (#375)
Browse files Browse the repository at this point in the history
  • Loading branch information
edaniszewski committed Mar 30, 2020
1 parent 25717be commit 7c0bfff
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 7 deletions.
5 changes: 5 additions & 0 deletions sdk/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ func NewDeviceFromConfig(proto *config.DeviceProto, instance *config.DeviceInsta
if instance.Type != "" {
deviceType = instance.Type
}
// We require devices to have a type; error if there is none set.
if deviceType == "" {
// fixme: err message
return nil, fmt.Errorf("device requires type")
}

// Override write timeout, if set.
if instance.WriteTimeout != 0 {
Expand Down
3 changes: 3 additions & 0 deletions sdk/device_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@ func (manager *deviceManager) AddDevice(device *Device) error {
return fmt.Errorf("device id exists")
}

// Update the device with the SDK auto-generated tags.
device.Tags = append(device.Tags, newIDTag(device.id), newTypeTag(device.Type))

// Add the device to the manager.
manager.devices[device.id] = device

Expand Down
52 changes: 45 additions & 7 deletions sdk/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,20 @@ import (
"regexp"
"strings"

log "github.com/Sirupsen/logrus"
synse "github.com/vapor-ware/synse-server-grpc/go"
)

const (
// Tag namespace constants
TagNamespaceDefault = "default"
TagNamespaceSystem = "system"

// Tag annotation constants
TagAnnotationID = "id"
TagAnnotationType = "type"
)

// Tag represents a group identifier which a Synse device can belong to.
type Tag struct {
Namespace string
Expand Down Expand Up @@ -76,7 +87,7 @@ func NewTag(tag string) (*Tag, error) {

// If no namespace is specified, use the default namespace.
if namespace == "" {
namespace = "default"
namespace = TagNamespaceDefault
}

return &Tag{
Expand Down Expand Up @@ -268,18 +279,45 @@ func (cache *TagCache) GetDevicesFromTags(tags ...*Tag) []*Device {
return deviceSet.Results()
}

// newIDTag creates a new Tag for a device ID. These tags are auto-generated
// by the SDK and are considered system-wide tags.
func newIDTag(deviceID string) *Tag {
return &Tag{
Namespace: TagNamespaceSystem,
Annotation: TagAnnotationID,
Label: deviceID,
string: fmt.Sprintf("%s/%s:%s", TagNamespaceSystem, TagAnnotationID, deviceID),
}
}

// newTypeTag creates a new Tag for a device Type. These tags are auto-generated
// by the SDK and are considered system-wide tags.
func newTypeTag(deviceType string) *Tag {
return &Tag{
Namespace: TagNamespaceSystem,
Annotation: TagAnnotationType,
Label: deviceType,
string: fmt.Sprintf("%s/%s:%s", TagNamespaceSystem, TagAnnotationType, deviceType),
}
}

// DeviceSelectorToTags is a utility that converts a gRPC device selector message
// into its corresponding tags.
func DeviceSelectorToTags(selector *synse.V3DeviceSelector) []*Tag {
var tags []*Tag
if selector.Id != "" {
if len(selector.Tags) > 0 {
log.WithFields(log.Fields{
"id": selector.Id,
"tags": selector.Tags,
}).Warn("[tags] device selector specifies id and tags; only using id (tags ignored)")
}

return []*Tag{newIDTag(selector.Id)}
}

var tags []*Tag
for _, t := range selector.Tags {
tags = append(tags, NewTagFromGRPC(t))
}

// TODO (etd):
// 1) check if id is set, if so only create a tag for ID.
// 2) figure out how ID tags are represented internally.

return tags
}

0 comments on commit 7c0bfff

Please sign in to comment.