github.com/influxdata/influxdb/v2@v2.7.6/dbrp_mapping.go (about)

     1  package influxdb
     2  
     3  import (
     4  	"context"
     5  	"strconv"
     6  	"strings"
     7  	"unicode"
     8  
     9  	"github.com/influxdata/influxdb/v2/kit/platform"
    10  	"github.com/influxdata/influxdb/v2/kit/platform/errors"
    11  )
    12  
    13  // DBRPMappingService provides CRUD to DBRPMappingV2s.
    14  type DBRPMappingService interface {
    15  	// FindBy returns the dbrp mapping for the specified ID.
    16  	// Requires orgID because every resource will be org-scoped.
    17  	FindByID(ctx context.Context, orgID, id platform.ID) (*DBRPMapping, error)
    18  	// FindMany returns a list of dbrp mappings that match filter and the total count of matching dbrp mappings.
    19  	FindMany(ctx context.Context, dbrp DBRPMappingFilter, opts ...FindOptions) ([]*DBRPMapping, int, error)
    20  	// Create creates a new dbrp mapping, if a different mapping exists an error is returned.
    21  	Create(ctx context.Context, dbrp *DBRPMapping) error
    22  	// Update a new dbrp mapping
    23  	Update(ctx context.Context, dbrp *DBRPMapping) error
    24  	// Delete removes a dbrp mapping.
    25  	// Deleting a mapping that does not exists is not an error.
    26  	// Requires orgID because every resource will be org-scoped.
    27  	Delete(ctx context.Context, orgID, id platform.ID) error
    28  }
    29  
    30  // DBRPMapping represents a mapping of a database and retention policy to an organization ID and bucket ID.
    31  type DBRPMapping struct {
    32  	ID              platform.ID `json:"id"`
    33  	Database        string      `json:"database"`
    34  	RetentionPolicy string      `json:"retention_policy"`
    35  
    36  	// Default indicates if this mapping is the default for the cluster and database.
    37  	Default bool `json:"default"`
    38  	// Virtual indicates if this is a virtual mapping (tied to bucket name) or physical
    39  	Virtual bool `json:"virtual"`
    40  
    41  	OrganizationID platform.ID `json:"orgID"`
    42  	BucketID       platform.ID `json:"bucketID"`
    43  }
    44  
    45  // Validate reports any validation errors for the mapping.
    46  func (m DBRPMapping) Validate() error {
    47  	if !validName(m.Database) {
    48  		return &errors.Error{
    49  			Code: errors.EInvalid,
    50  			Msg:  "database must contain at least one character and only be letters, numbers, '_', '-', and '.'",
    51  		}
    52  	}
    53  	if !validName(m.RetentionPolicy) {
    54  		return &errors.Error{
    55  			Code: errors.EInvalid,
    56  			Msg:  "retentionPolicy must contain at least one character and only be letters, numbers, '_', '-', and '.'",
    57  		}
    58  	}
    59  	if !m.OrganizationID.Valid() {
    60  		return &errors.Error{
    61  			Code: errors.EInvalid,
    62  			Msg:  "organizationID is required",
    63  		}
    64  	}
    65  	if !m.BucketID.Valid() {
    66  		return &errors.Error{
    67  			Code: errors.EInvalid,
    68  			Msg:  "bucketID is required",
    69  		}
    70  	}
    71  	return nil
    72  }
    73  
    74  // Equal checks if the two mappings are identical.
    75  func (m *DBRPMapping) Equal(o *DBRPMapping) bool {
    76  	if m == o {
    77  		return true
    78  	}
    79  	if m == nil || o == nil {
    80  		return false
    81  	}
    82  	return m.Database == o.Database &&
    83  		m.RetentionPolicy == o.RetentionPolicy &&
    84  		m.Default == o.Default &&
    85  		m.OrganizationID.Valid() &&
    86  		o.OrganizationID.Valid() &&
    87  		m.BucketID.Valid() &&
    88  		o.BucketID.Valid() &&
    89  		o.ID.Valid() &&
    90  		m.ID == o.ID &&
    91  		m.OrganizationID == o.OrganizationID &&
    92  		m.BucketID == o.BucketID
    93  }
    94  
    95  // DBRPMappingFilter represents a set of filters that restrict the returned results.
    96  type DBRPMappingFilter struct {
    97  	ID       *platform.ID
    98  	OrgID    *platform.ID
    99  	BucketID *platform.ID
   100  
   101  	Database        *string
   102  	RetentionPolicy *string
   103  	Default         *bool
   104  	Virtual         *bool
   105  }
   106  
   107  func (f DBRPMappingFilter) String() string {
   108  	var s strings.Builder
   109  
   110  	s.WriteString("{ id:")
   111  	if f.ID != nil {
   112  		s.WriteString(f.ID.String())
   113  	} else {
   114  		s.WriteString("<nil>")
   115  	}
   116  
   117  	s.WriteString(" org_id:")
   118  	if f.ID != nil {
   119  		s.WriteString(f.OrgID.String())
   120  	} else {
   121  		s.WriteString("<nil>")
   122  	}
   123  
   124  	s.WriteString(" bucket_id:")
   125  	if f.ID != nil {
   126  		s.WriteString(f.OrgID.String())
   127  	} else {
   128  		s.WriteString("<nil>")
   129  	}
   130  
   131  	s.WriteString(" db:")
   132  	if f.Database != nil {
   133  		s.WriteString(*f.Database)
   134  	} else {
   135  		s.WriteString("<nil>")
   136  	}
   137  
   138  	s.WriteString(" rp:")
   139  	if f.RetentionPolicy != nil {
   140  		s.WriteString(*f.RetentionPolicy)
   141  	} else {
   142  		s.WriteString("<nil>")
   143  	}
   144  
   145  	s.WriteString(" default:")
   146  	if f.Default != nil {
   147  		s.WriteString(strconv.FormatBool(*f.Default))
   148  	} else {
   149  		s.WriteString("<nil>")
   150  	}
   151  	s.WriteString("}")
   152  	return s.String()
   153  }
   154  
   155  // validName checks to see if the given name can would be valid for DB/RP name
   156  func validName(name string) bool {
   157  	for _, r := range name {
   158  		if !unicode.IsPrint(r) {
   159  			return false
   160  		}
   161  	}
   162  	return name != "" &&
   163  		name != "." &&
   164  		name != ".." &&
   165  		!strings.ContainsAny(name, `/\`)
   166  }