2018-07-10 21:56:57 +00:00
|
|
|
package platform
|
|
|
|
|
|
|
|
import (
|
2018-08-01 18:54:32 +00:00
|
|
|
"bytes"
|
2018-07-10 21:56:57 +00:00
|
|
|
"context"
|
|
|
|
"errors"
|
2018-07-31 01:36:22 +00:00
|
|
|
"strings"
|
|
|
|
"unicode"
|
2018-07-10 21:56:57 +00:00
|
|
|
)
|
|
|
|
|
2018-07-12 19:27:03 +00:00
|
|
|
// DBRPMappingService provides a mapping of cluster, database and retention policy to an organization ID and bucket ID.
|
|
|
|
type DBRPMappingService interface {
|
2018-07-10 21:56:57 +00:00
|
|
|
// FindBy returns the dbrp mapping the for cluster, db and rp.
|
|
|
|
FindBy(ctx context.Context, cluster, db, rp string) (*DBRPMapping, error)
|
|
|
|
// Find returns the first dbrp mapping the matches the filter.
|
|
|
|
Find(ctx context.Context, filter DBRPMappingFilter) (*DBRPMapping, error)
|
|
|
|
// FindMany returns a list of dbrp mappings that match filter and the total count of matching dbrp mappings.
|
|
|
|
FindMany(ctx context.Context, filter DBRPMappingFilter, opt ...FindOptions) ([]*DBRPMapping, int, error)
|
|
|
|
// Create creates a new dbrp mapping, if a different mapping exists an error is returned.
|
|
|
|
Create(ctx context.Context, dbrpMap *DBRPMapping) error
|
|
|
|
// Delete removes a dbrp mapping.
|
|
|
|
// Deleting a mapping that does not exists is not an error.
|
|
|
|
Delete(ctx context.Context, cluster, db, rp string) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// DBRPMapping represents a mapping of a cluster, database and retention policy to an organization ID and bucket ID.
|
|
|
|
type DBRPMapping struct {
|
2018-07-31 01:36:22 +00:00
|
|
|
Cluster string `json:"cluster"`
|
|
|
|
Database string `json:"database"`
|
|
|
|
RetentionPolicy string `json:"retention_policy"`
|
2018-07-10 21:56:57 +00:00
|
|
|
|
|
|
|
// Default indicates if this mapping is the default for the cluster and database.
|
|
|
|
Default bool `json:"default"`
|
|
|
|
|
2018-07-30 14:29:52 +00:00
|
|
|
OrganizationID ID `json:"organization_id"`
|
|
|
|
BucketID ID `json:"bucket_id"`
|
2018-07-10 21:56:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate reports any validation errors for the mapping.
|
|
|
|
func (m DBRPMapping) Validate() error {
|
2018-07-31 01:36:22 +00:00
|
|
|
if !validName(m.Cluster) {
|
2018-07-19 16:15:14 +00:00
|
|
|
return errors.New("Cluster must contain at least one character and only be letters, numbers, '_', '-', and '.'")
|
2018-07-10 21:56:57 +00:00
|
|
|
}
|
2018-07-31 01:36:22 +00:00
|
|
|
if !validName(m.Database) {
|
2018-07-19 16:15:14 +00:00
|
|
|
return errors.New("Database must contain at least one character and only be letters, numbers, '_', '-', and '.'")
|
2018-07-10 21:56:57 +00:00
|
|
|
}
|
2018-07-31 01:36:22 +00:00
|
|
|
if !validName(m.RetentionPolicy) {
|
2018-07-19 16:15:14 +00:00
|
|
|
return errors.New("RetentionPolicy must contain at least one character and only be letters, numbers, '_', '-', and '.'")
|
2018-07-10 21:56:57 +00:00
|
|
|
}
|
2018-08-01 18:54:32 +00:00
|
|
|
if len(m.OrganizationID) == 0 {
|
2018-07-10 21:56:57 +00:00
|
|
|
return errors.New("OrganizationID is required")
|
|
|
|
}
|
2018-08-01 18:54:32 +00:00
|
|
|
if len(m.BucketID) == 0 {
|
2018-07-10 21:56:57 +00:00
|
|
|
return errors.New("BucketID is required")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-31 01:36:22 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
2018-08-01 18:54:32 +00:00
|
|
|
|
2018-07-31 01:36:22 +00:00
|
|
|
return name != "" &&
|
|
|
|
name != "." &&
|
|
|
|
name != ".." &&
|
|
|
|
!strings.ContainsAny(name, `/\`)
|
|
|
|
}
|
|
|
|
|
2018-07-19 15:21:06 +00:00
|
|
|
// Equal checks if the two mappings are identical.
|
2018-07-12 19:27:03 +00:00
|
|
|
func (m *DBRPMapping) Equal(o *DBRPMapping) bool {
|
|
|
|
if m == o {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if m == nil || o == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return m.Cluster == o.Cluster &&
|
|
|
|
m.Database == o.Database &&
|
|
|
|
m.RetentionPolicy == o.RetentionPolicy &&
|
|
|
|
m.Default == o.Default &&
|
2018-08-01 18:54:32 +00:00
|
|
|
bytes.Equal(m.OrganizationID, o.OrganizationID) &&
|
|
|
|
bytes.Equal(m.BucketID, o.BucketID)
|
2018-07-12 19:27:03 +00:00
|
|
|
}
|
|
|
|
|
2018-07-10 21:56:57 +00:00
|
|
|
// DBRPMappingFilter represents a set of filters that restrict the returned results by cluster, database and retention policy.
|
|
|
|
type DBRPMappingFilter struct {
|
|
|
|
Cluster *string
|
|
|
|
Database *string
|
|
|
|
RetentionPolicy *string
|
|
|
|
Default *bool
|
|
|
|
}
|