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

     1  package influxdb
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/influxdata/influxdb/v2/kit/platform"
     7  	"github.com/influxdata/influxdb/v2/kit/platform/errors"
     8  )
     9  
    10  // ErrLabelNotFound is the error for a missing Label.
    11  const ErrLabelNotFound = "label not found"
    12  
    13  const (
    14  	OpFindLabels         = "FindLabels"
    15  	OpFindLabelByID      = "FindLabelByID"
    16  	OpFindLabelMapping   = "FindLabelMapping"
    17  	OpCreateLabel        = "CreateLabel"
    18  	OpCreateLabelMapping = "CreateLabelMapping"
    19  	OpUpdateLabel        = "UpdateLabel"
    20  	OpDeleteLabel        = "DeleteLabel"
    21  	OpDeleteLabelMapping = "DeleteLabelMapping"
    22  )
    23  
    24  // errors on label
    25  var (
    26  	// ErrLabelNameisEmpty is error when org name is empty
    27  	ErrLabelNameisEmpty = &errors.Error{
    28  		Code: errors.EInvalid,
    29  		Msg:  "label name is empty",
    30  	}
    31  
    32  	// ErrLabelExistsOnResource is used when attempting to add a label to a resource
    33  	// when that label already exists on the resource
    34  	ErrLabelExistsOnResource = &errors.Error{
    35  		Code: errors.EConflict,
    36  		Msg:  "Cannot add label, label already exists on resource",
    37  	}
    38  )
    39  
    40  // LabelService represents a service for managing resource labels
    41  type LabelService interface {
    42  	// FindLabelByID a single label by ID.
    43  	FindLabelByID(ctx context.Context, id platform.ID) (*Label, error)
    44  
    45  	// FindLabels returns a list of labels that match a filter
    46  	FindLabels(ctx context.Context, filter LabelFilter, opt ...FindOptions) ([]*Label, error)
    47  
    48  	// FindResourceLabels returns a list of labels that belong to a resource
    49  	FindResourceLabels(ctx context.Context, filter LabelMappingFilter) ([]*Label, error)
    50  
    51  	// CreateLabel creates a new label
    52  	CreateLabel(ctx context.Context, l *Label) error
    53  
    54  	// CreateLabelMapping maps a resource to an existing label
    55  	CreateLabelMapping(ctx context.Context, m *LabelMapping) error
    56  
    57  	// UpdateLabel updates a label with a changeset.
    58  	UpdateLabel(ctx context.Context, id platform.ID, upd LabelUpdate) (*Label, error)
    59  
    60  	// DeleteLabel deletes a label
    61  	DeleteLabel(ctx context.Context, id platform.ID) error
    62  
    63  	// DeleteLabelMapping deletes a label mapping
    64  	DeleteLabelMapping(ctx context.Context, m *LabelMapping) error
    65  }
    66  
    67  // Label is a tag set on a resource, typically used for filtering on a UI.
    68  type Label struct {
    69  	ID         platform.ID       `json:"id,omitempty"`
    70  	OrgID      platform.ID       `json:"orgID,omitempty"`
    71  	Name       string            `json:"name"`
    72  	Properties map[string]string `json:"properties,omitempty"`
    73  }
    74  
    75  // Validate returns an error if the label is invalid.
    76  func (l *Label) Validate() error {
    77  	if l.Name == "" {
    78  		return &errors.Error{
    79  			Code: errors.EInvalid,
    80  			Msg:  "label name is required",
    81  		}
    82  	}
    83  
    84  	if !l.OrgID.Valid() {
    85  		return &errors.Error{
    86  			Code: errors.EInvalid,
    87  			Msg:  "orgID is required",
    88  		}
    89  	}
    90  
    91  	return nil
    92  }
    93  
    94  // LabelMapping is used to map resource to its labels.
    95  // It should not be shared directly over the HTTP API.
    96  type LabelMapping struct {
    97  	LabelID      platform.ID `json:"labelID"`
    98  	ResourceID   platform.ID `json:"resourceID,omitempty"`
    99  	ResourceType `json:"resourceType"`
   100  }
   101  
   102  // Validate returns an error if the mapping is invalid.
   103  func (l *LabelMapping) Validate() error {
   104  	if !l.LabelID.Valid() {
   105  		return &errors.Error{
   106  			Code: errors.EInvalid,
   107  			Msg:  "label id is required",
   108  		}
   109  	}
   110  	if !l.ResourceID.Valid() {
   111  		return &errors.Error{
   112  			Code: errors.EInvalid,
   113  			Msg:  "resource id is required",
   114  		}
   115  	}
   116  	if err := l.ResourceType.Valid(); err != nil {
   117  		return &errors.Error{
   118  			Code: errors.EInvalid,
   119  			Err:  err,
   120  		}
   121  	}
   122  
   123  	return nil
   124  }
   125  
   126  // LabelUpdate represents a changeset for a label.
   127  // Only the properties specified are updated.
   128  type LabelUpdate struct {
   129  	Name       string            `json:"name,omitempty"`
   130  	Properties map[string]string `json:"properties,omitempty"`
   131  }
   132  
   133  // LabelFilter represents a set of filters that restrict the returned results.
   134  type LabelFilter struct {
   135  	Name  string
   136  	OrgID *platform.ID
   137  }
   138  
   139  // LabelMappingFilter represents a set of filters that restrict the returned results.
   140  type LabelMappingFilter struct {
   141  	ResourceID platform.ID
   142  	ResourceType
   143  }