2020-04-20 16:55:23 +00:00
|
|
|
package dbrp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"path"
|
2020-11-17 20:14:55 +00:00
|
|
|
"strconv"
|
2020-04-20 16:55:23 +00:00
|
|
|
|
|
|
|
"github.com/influxdata/influxdb/v2"
|
2021-09-13 19:12:35 +00:00
|
|
|
"github.com/influxdata/influxdb/v2/kit/platform"
|
2020-04-20 16:55:23 +00:00
|
|
|
"github.com/influxdata/influxdb/v2/kit/tracing"
|
|
|
|
"github.com/influxdata/influxdb/v2/pkg/httpc"
|
|
|
|
)
|
|
|
|
|
2021-08-30 22:27:11 +00:00
|
|
|
var _ influxdb.DBRPMappingService = (*Client)(nil)
|
2020-04-20 16:55:23 +00:00
|
|
|
|
|
|
|
// Client connects to Influx via HTTP using tokens to manage DBRPs.
|
|
|
|
type Client struct {
|
|
|
|
Client *httpc.Client
|
|
|
|
Prefix string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewClient(client *httpc.Client) *Client {
|
|
|
|
return &Client{
|
|
|
|
Client: client,
|
|
|
|
Prefix: PrefixDBRP,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-30 18:10:02 +00:00
|
|
|
func (c *Client) dbrpURL(id platform.ID) string {
|
2020-04-20 16:55:23 +00:00
|
|
|
return path.Join(c.Prefix, id.String())
|
|
|
|
}
|
|
|
|
|
2021-08-30 22:27:11 +00:00
|
|
|
func (c *Client) FindByID(ctx context.Context, orgID, id platform.ID) (*influxdb.DBRPMapping, error) {
|
2020-04-20 16:55:23 +00:00
|
|
|
span, _ := tracing.StartSpanFromContext(ctx)
|
|
|
|
defer span.Finish()
|
|
|
|
|
|
|
|
var resp getDBRPResponse
|
|
|
|
if err := c.Client.
|
|
|
|
Get(c.dbrpURL(id)).
|
|
|
|
QueryParams([2]string{"orgID", orgID.String()}).
|
|
|
|
DecodeJSON(&resp).
|
|
|
|
Do(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resp.Content, nil
|
|
|
|
}
|
|
|
|
|
2021-08-30 22:27:11 +00:00
|
|
|
func (c *Client) FindMany(ctx context.Context, filter influxdb.DBRPMappingFilter, opts ...influxdb.FindOptions) ([]*influxdb.DBRPMapping, int, error) {
|
2020-04-20 16:55:23 +00:00
|
|
|
span, _ := tracing.StartSpanFromContext(ctx)
|
|
|
|
defer span.Finish()
|
|
|
|
|
|
|
|
params := influxdb.FindOptionParams(opts...)
|
|
|
|
if filter.OrgID != nil {
|
|
|
|
params = append(params, [2]string{"orgID", filter.OrgID.String()})
|
|
|
|
} else {
|
|
|
|
return nil, 0, fmt.Errorf("please filter by orgID")
|
|
|
|
}
|
|
|
|
if filter.ID != nil {
|
|
|
|
params = append(params, [2]string{"id", filter.ID.String()})
|
|
|
|
}
|
|
|
|
if filter.BucketID != nil {
|
|
|
|
params = append(params, [2]string{"bucketID", filter.BucketID.String()})
|
|
|
|
}
|
|
|
|
if filter.Database != nil {
|
|
|
|
params = append(params, [2]string{"db", *filter.Database})
|
|
|
|
}
|
|
|
|
if filter.RetentionPolicy != nil {
|
|
|
|
params = append(params, [2]string{"rp", *filter.RetentionPolicy})
|
|
|
|
}
|
2020-11-17 20:14:55 +00:00
|
|
|
if filter.Default != nil {
|
|
|
|
params = append(params, [2]string{"default", strconv.FormatBool(*filter.Default)})
|
|
|
|
}
|
2020-04-20 16:55:23 +00:00
|
|
|
|
|
|
|
var resp getDBRPsResponse
|
|
|
|
if err := c.Client.
|
|
|
|
Get(c.Prefix).
|
|
|
|
QueryParams(params...).
|
|
|
|
DecodeJSON(&resp).
|
|
|
|
Do(ctx); err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
return resp.Content, len(resp.Content), nil
|
|
|
|
}
|
|
|
|
|
2021-08-30 22:27:11 +00:00
|
|
|
func (c *Client) Create(ctx context.Context, dbrp *influxdb.DBRPMapping) error {
|
2020-04-20 16:55:23 +00:00
|
|
|
span, _ := tracing.StartSpanFromContext(ctx)
|
|
|
|
defer span.Finish()
|
|
|
|
|
2021-08-30 22:27:11 +00:00
|
|
|
var newDBRP influxdb.DBRPMapping
|
2020-04-20 16:55:23 +00:00
|
|
|
if err := c.Client.
|
|
|
|
PostJSON(createDBRPRequest{
|
|
|
|
Database: dbrp.Database,
|
|
|
|
RetentionPolicy: dbrp.RetentionPolicy,
|
|
|
|
Default: dbrp.Default,
|
2021-01-07 17:08:06 +00:00
|
|
|
OrganizationID: dbrp.OrganizationID.String(),
|
|
|
|
BucketID: dbrp.BucketID.String(),
|
2020-04-20 16:55:23 +00:00
|
|
|
}, c.Prefix).
|
|
|
|
DecodeJSON(&newDBRP).
|
|
|
|
Do(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
dbrp.ID = newDBRP.ID
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-30 22:27:11 +00:00
|
|
|
func (c *Client) Update(ctx context.Context, dbrp *influxdb.DBRPMapping) error {
|
2020-04-20 16:55:23 +00:00
|
|
|
span, _ := tracing.StartSpanFromContext(ctx)
|
|
|
|
defer span.Finish()
|
|
|
|
|
|
|
|
if err := dbrp.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-08-30 22:27:11 +00:00
|
|
|
var newDBRP influxdb.DBRPMapping
|
2020-04-20 16:55:23 +00:00
|
|
|
if err := c.Client.
|
|
|
|
PatchJSON(dbrp, c.dbrpURL(dbrp.ID)).
|
|
|
|
QueryParams([2]string{"orgID", dbrp.OrganizationID.String()}).
|
|
|
|
DecodeJSON(&newDBRP).
|
|
|
|
Do(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*dbrp = newDBRP
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-30 18:10:02 +00:00
|
|
|
func (c *Client) Delete(ctx context.Context, orgID, id platform.ID) error {
|
2020-04-20 16:55:23 +00:00
|
|
|
span, _ := tracing.StartSpanFromContext(ctx)
|
|
|
|
defer span.Finish()
|
|
|
|
|
|
|
|
return c.Client.
|
|
|
|
Delete(c.dbrpURL(id)).
|
|
|
|
QueryParams([2]string{"orgID", orgID.String()}).
|
|
|
|
Do(ctx)
|
|
|
|
}
|