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

     1  package influxdb
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  
     7  	"github.com/influxdata/influxdb/v2/kit/platform"
     8  	"github.com/influxdata/influxdb/v2/kit/platform/errors"
     9  	"github.com/influxdata/influxdb/v2/query/fluxlang"
    10  )
    11  
    12  // consts for checks config.
    13  const (
    14  	CheckDefaultPageSize = 100
    15  	CheckMaxPageSize     = 500
    16  )
    17  
    18  // Check represents the information required to generate a periodic check task.
    19  type Check interface {
    20  	Valid(lang fluxlang.FluxLanguageService) error
    21  	Type() string
    22  	ClearPrivateData()
    23  	SetTaskID(platform.ID)
    24  	GetTaskID() platform.ID
    25  	GetOwnerID() platform.ID
    26  	SetOwnerID(platform.ID)
    27  	GenerateFlux(lang fluxlang.FluxLanguageService) (string, error)
    28  	json.Marshaler
    29  
    30  	CRUDLogSetter
    31  	SetID(id platform.ID)
    32  	SetOrgID(id platform.ID)
    33  	SetName(name string)
    34  	SetDescription(description string)
    35  
    36  	GetID() platform.ID
    37  	GetCRUDLog() CRUDLog
    38  	GetOrgID() platform.ID
    39  	GetName() string
    40  	GetDescription() string
    41  }
    42  
    43  // ops for checks error
    44  var (
    45  	OpFindCheckByID = "FindCheckByID"
    46  	OpFindCheck     = "FindCheck"
    47  	OpFindChecks    = "FindChecks"
    48  	OpCreateCheck   = "CreateCheck"
    49  	OpUpdateCheck   = "UpdateCheck"
    50  	OpDeleteCheck   = "DeleteCheck"
    51  )
    52  
    53  // CheckService represents a service for managing checks.
    54  type CheckService interface {
    55  	// FindCheckByID returns a single check by ID.
    56  	FindCheckByID(ctx context.Context, id platform.ID) (Check, error)
    57  
    58  	// FindCheck returns the first check that matches filter.
    59  	FindCheck(ctx context.Context, filter CheckFilter) (Check, error)
    60  
    61  	// FindChecks returns a list of checks that match filter and the total count of matching checks.
    62  	// Additional options provide pagination & sorting.
    63  	FindChecks(ctx context.Context, filter CheckFilter, opt ...FindOptions) ([]Check, int, error)
    64  
    65  	// CreateCheck creates a new check and sets b.ID with the new identifier.
    66  	CreateCheck(ctx context.Context, c CheckCreate, userID platform.ID) error
    67  
    68  	// UpdateCheck updates the whole check.
    69  	// Returns the new check state after update.
    70  	UpdateCheck(ctx context.Context, id platform.ID, c CheckCreate) (Check, error)
    71  
    72  	// PatchCheck updates a single bucket with changeset.
    73  	// Returns the new check state after update.
    74  	PatchCheck(ctx context.Context, id platform.ID, upd CheckUpdate) (Check, error)
    75  
    76  	// DeleteCheck will delete the check by id.
    77  	DeleteCheck(ctx context.Context, id platform.ID) error
    78  }
    79  
    80  // CheckUpdate are properties than can be updated on a check
    81  type CheckUpdate struct {
    82  	Name        *string `json:"name,omitempty"`
    83  	Status      *Status `json:"status,omitempty"`
    84  	Description *string `json:"description,omitempty"`
    85  }
    86  
    87  // CheckCreate represent data to create a new Check
    88  type CheckCreate struct {
    89  	Check
    90  	Status Status `json:"status"`
    91  }
    92  
    93  // Valid returns err is the update is invalid.
    94  func (n *CheckUpdate) Valid() error {
    95  	if n.Name != nil && *n.Name == "" {
    96  		return &errors.Error{
    97  			Code: errors.EInvalid,
    98  			Msg:  "Check Name can't be empty",
    99  		}
   100  	}
   101  
   102  	if n.Description != nil && *n.Description == "" {
   103  		return &errors.Error{
   104  			Code: errors.EInvalid,
   105  			Msg:  "Check Description can't be empty",
   106  		}
   107  	}
   108  
   109  	if n.Status != nil {
   110  		if err := n.Status.Valid(); err != nil {
   111  			return err
   112  		}
   113  	}
   114  
   115  	return nil
   116  }
   117  
   118  // CheckFilter represents a set of filters that restrict the returned results.
   119  type CheckFilter struct {
   120  	ID    *platform.ID
   121  	Name  *string
   122  	OrgID *platform.ID
   123  	Org   *string
   124  	UserResourceMappingFilter
   125  }
   126  
   127  // QueryParams Converts CheckFilter fields to url query params.
   128  func (f CheckFilter) QueryParams() map[string][]string {
   129  	qp := map[string][]string{}
   130  
   131  	if f.ID != nil {
   132  		qp["id"] = []string{f.ID.String()}
   133  	}
   134  
   135  	if f.Name != nil {
   136  		qp["name"] = []string{*f.Name}
   137  	}
   138  
   139  	if f.OrgID != nil {
   140  		qp["orgID"] = []string{f.OrgID.String()}
   141  	}
   142  
   143  	if f.Org != nil {
   144  		qp["org"] = []string{*f.Org}
   145  	}
   146  
   147  	return qp
   148  }