Switch back logic of valid names within DBRP mappings

pull/10616/head
Leonardo Di Donato 2018-07-31 03:36:22 +02:00 committed by Leonardo Di Donato
parent 43ebb0251f
commit fe4c3199ce
4 changed files with 27 additions and 78 deletions

View File

@ -3,6 +3,8 @@ package platform
import (
"context"
"errors"
"strings"
"unicode"
)
// DBRPMappingService provides a mapping of cluster, database and retention policy to an organization ID and bucket ID.
@ -22,9 +24,9 @@ type DBRPMappingService interface {
// DBRPMapping represents a mapping of a cluster, database and retention policy to an organization ID and bucket ID.
type DBRPMapping struct {
Cluster Name `json:"cluster"`
Database Name `json:"database"`
RetentionPolicy Name `json:"retention_policy"`
Cluster string `json:"cluster"`
Database string `json:"database"`
RetentionPolicy string `json:"retention_policy"`
// Default indicates if this mapping is the default for the cluster and database.
Default bool `json:"default"`
@ -35,13 +37,13 @@ type DBRPMapping struct {
// Validate reports any validation errors for the mapping.
func (m DBRPMapping) Validate() error {
if !m.Cluster.Valid() {
if !validName(m.Cluster) {
return errors.New("Cluster must contain at least one character and only be letters, numbers, '_', '-', and '.'")
}
if !m.Database.Valid() {
if !validName(m.Database) {
return errors.New("Database must contain at least one character and only be letters, numbers, '_', '-', and '.'")
}
if !m.RetentionPolicy.Valid() {
if !validName(m.RetentionPolicy) {
return errors.New("RetentionPolicy must contain at least one character and only be letters, numbers, '_', '-', and '.'")
}
if !m.OrganizationID.Valid() {
@ -53,6 +55,19 @@ func (m DBRPMapping) Validate() error {
return nil
}
// validName checks to see if the given name can would be valid for DB/RP name
func validName(name string) bool {
for _, r := range name {
if !unicode.IsPrint(r) {
return false
}
}
return name != "" &&
name != "." &&
name != ".." &&
!strings.ContainsAny(name, `/\`)
}
// Equal checks if the two mappings are identical.
func (m *DBRPMapping) Equal(o *DBRPMapping) bool {
if m == o {

View File

@ -9,9 +9,9 @@ import (
func TestDBRPMapping_Validate(t *testing.T) {
type fields struct {
Cluster platform.Name
Database platform.Name
RetentionPolicy platform.Name
Cluster string
Database string
RetentionPolicy string
Default bool
OrganizationID platform.ID
BucketID platform.ID
@ -67,7 +67,7 @@ func TestDBRPMapping_Validate(t *testing.T) {
{
name: "cluster name cannot have non-printable characters.",
fields: fields{
Cluster: platform.Name([]byte{0x0D}),
Cluster: string([]byte{0x0D}),
},
wantErr: true,
},
@ -75,7 +75,7 @@ func TestDBRPMapping_Validate(t *testing.T) {
name: "db cannot have non-letters/numbers/_/./-",
fields: fields{
Cluster: "12345_.",
Database: platform.Name([]byte{0x0D}),
Database: string([]byte{0x0D}),
},
wantErr: true,
},
@ -84,7 +84,7 @@ func TestDBRPMapping_Validate(t *testing.T) {
fields: fields{
Cluster: "12345",
Database: "telegraf",
RetentionPolicy: platform.Name([]byte{0x0D}),
RetentionPolicy: string([]byte{0x0D}),
},
wantErr: true,
},

23
name.go
View File

@ -1,23 +0,0 @@
package platform
import (
"strings"
"unicode"
)
// Name represents a DB or a RP name.
type Name string
// Valid tells whether it is a accepted name or not.
func (n Name) Valid() bool {
for _, r := range n {
if !unicode.IsPrint(r) {
return false
}
}
return n != "" &&
n != "." &&
n != ".." &&
!strings.ContainsAny(string(n), `/\`)
}

View File

@ -1,43 +0,0 @@
package platform_test
import (
"testing"
"github.com/influxdata/platform"
)
func TestValidName(t *testing.T) {
tests := []struct {
arg platform.Name
name string
want bool
}{
{
name: "names cannot have unprintable characters",
arg: platform.Name([]byte{0x0D}),
want: false,
},
{
name: "names cannot have .",
arg: ".",
want: false,
},
{
name: "names cannot have ..",
arg: "..",
want: false,
},
{
name: "names cannot have /",
arg: "/",
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.arg.Valid(); got != tt.want {
t.Errorf("validName() = %v, want %v", got, tt.want)
}
})
}
}