feat: Add bucket schema management commands (#52)
* feat: update generated client to include schema-management APIs * feat: implement interfaces to decode flags and CSV * feat: implement decoders for different measurement schema column formats * feat: extend bucket CLI commands to support schema type property * feat: add CLI commands to manage measurement schema * test: add unit tests for bucket schema create, update and list commands
This commit is contained in:
@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/influxdata/influx-cli/v2/internal"
|
||||
"github.com/influxdata/influx-cli/v2/internal/api"
|
||||
"github.com/influxdata/influx-cli/v2/pkg/cli/middleware"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
@ -44,7 +45,9 @@ func newBucketCmd() *cli.Command {
|
||||
}
|
||||
|
||||
func newBucketCreateCmd() *cli.Command {
|
||||
var params internal.BucketsCreateParams
|
||||
params := internal.BucketsCreateParams{
|
||||
SchemaType: api.SCHEMATYPE_IMPLICIT,
|
||||
}
|
||||
return &cli.Command{
|
||||
Name: "create",
|
||||
Usage: "Create bucket",
|
||||
@ -91,6 +94,12 @@ func newBucketCreateCmd() *cli.Command {
|
||||
EnvVars: []string{"INFLUX_ORG"},
|
||||
Destination: ¶ms.OrgName,
|
||||
},
|
||||
&cli.GenericFlag{
|
||||
Name: "schema-type",
|
||||
Usage: "The schema type (implicit, explicit)",
|
||||
DefaultText: "implicit",
|
||||
Value: ¶ms.SchemaType,
|
||||
},
|
||||
),
|
||||
Action: func(ctx *cli.Context) error {
|
||||
clients := getBucketsClient(ctx)
|
||||
|
189
cmd/influx/bucket_schema.go
Normal file
189
cmd/influx/bucket_schema.go
Normal file
@ -0,0 +1,189 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/influxdata/influx-cli/v2/internal"
|
||||
"github.com/influxdata/influx-cli/v2/internal/cmd/bucket_schema"
|
||||
"github.com/influxdata/influx-cli/v2/pkg/cli/middleware"
|
||||
"github.com/influxdata/influx-cli/v2/pkg/influxid"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
func withBucketSchemaClient() cli.BeforeFunc {
|
||||
return middleware.WithBeforeFns(
|
||||
withCli(),
|
||||
withApi(true),
|
||||
func(ctx *cli.Context) error {
|
||||
c := getCLI(ctx)
|
||||
client := getAPI(ctx)
|
||||
ctx.App.Metadata["measurement_schema"] = bucket_schema.Client{
|
||||
BucketApi: client.BucketsApi,
|
||||
BucketSchemasApi: client.BucketSchemasApi,
|
||||
CLI: c,
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func getBucketSchemaClient(ctx *cli.Context) bucket_schema.Client {
|
||||
i, ok := ctx.App.Metadata["measurement_schema"].(bucket_schema.Client)
|
||||
if !ok {
|
||||
panic("missing measurement schema client")
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
func newBucketSchemaCmd() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "bucket-schema",
|
||||
Usage: "Bucket schema management commands",
|
||||
Subcommands: []*cli.Command{
|
||||
newBucketSchemaCreateCmd(),
|
||||
newBucketSchemaUpdateCmd(),
|
||||
newBucketSchemaListCmd(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func newBucketSchemaCreateCmd() *cli.Command {
|
||||
var params struct {
|
||||
internal.OrgBucketParams
|
||||
Name string
|
||||
ColumnsFile string
|
||||
ColumnsFormat bucket_schema.ColumnsFormat
|
||||
ExtendedOutput bool
|
||||
}
|
||||
return &cli.Command{
|
||||
Name: "create",
|
||||
Usage: "Create a measurement schema for a bucket",
|
||||
Before: withBucketSchemaClient(),
|
||||
Flags: append(
|
||||
commonFlags,
|
||||
append(
|
||||
getOrgBucketFlags(¶ms.OrgBucketParams),
|
||||
&cli.StringFlag{
|
||||
Name: "name",
|
||||
Usage: "Name of the measurement",
|
||||
Destination: ¶ms.Name,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "columns-file",
|
||||
Usage: "A path referring to list of column definitions",
|
||||
Destination: ¶ms.ColumnsFile,
|
||||
},
|
||||
&cli.GenericFlag{
|
||||
Name: "columns-format",
|
||||
Usage: "The format of the columns file. \"auto\" will attempt to guess the format.",
|
||||
DefaultText: "auto",
|
||||
Value: ¶ms.ColumnsFormat,
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: "extended-output",
|
||||
Usage: "Print column information for each measurement",
|
||||
Aliases: []string{"x"},
|
||||
Destination: ¶ms.ExtendedOutput,
|
||||
},
|
||||
)...,
|
||||
),
|
||||
Action: func(ctx *cli.Context) error {
|
||||
return getBucketSchemaClient(ctx).
|
||||
Create(ctx.Context, bucket_schema.CreateParams{
|
||||
OrgBucketParams: params.OrgBucketParams,
|
||||
Name: params.Name,
|
||||
Stdin: ctx.App.Reader,
|
||||
ColumnsFile: params.ColumnsFile,
|
||||
ColumnsFormat: params.ColumnsFormat,
|
||||
ExtendedOutput: params.ExtendedOutput,
|
||||
})
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func newBucketSchemaUpdateCmd() *cli.Command {
|
||||
var params struct {
|
||||
internal.OrgBucketParams
|
||||
ID influxid.ID
|
||||
Name string
|
||||
ColumnsFile string
|
||||
ColumnsFormat bucket_schema.ColumnsFormat
|
||||
ExtendedOutput bool
|
||||
}
|
||||
return &cli.Command{
|
||||
Name: "update",
|
||||
Usage: "Update a measurement schema for a bucket",
|
||||
Before: withBucketSchemaClient(),
|
||||
Flags: append(
|
||||
commonFlags,
|
||||
append(
|
||||
getOrgBucketFlags(¶ms.OrgBucketParams),
|
||||
&cli.GenericFlag{
|
||||
Name: "id",
|
||||
Usage: "ID of the measurement",
|
||||
Value: ¶ms.ID,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "name",
|
||||
Usage: "Name of the measurement",
|
||||
Destination: ¶ms.Name,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "columns-file",
|
||||
Usage: "A path referring to list of column definitions",
|
||||
Destination: ¶ms.ColumnsFile,
|
||||
},
|
||||
&cli.GenericFlag{
|
||||
Name: "columns-format",
|
||||
Usage: "The format of the columns file. \"auto\" will attempt to guess the format.",
|
||||
DefaultText: "auto",
|
||||
Value: ¶ms.ColumnsFormat,
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: "extended-output",
|
||||
Usage: "Print column information for each measurement",
|
||||
Aliases: []string{"x"},
|
||||
Destination: ¶ms.ExtendedOutput,
|
||||
},
|
||||
)...,
|
||||
),
|
||||
Action: func(ctx *cli.Context) error {
|
||||
return getBucketSchemaClient(ctx).
|
||||
Update(ctx.Context, bucket_schema.UpdateParams{
|
||||
OrgBucketParams: params.OrgBucketParams,
|
||||
ID: params.ID.String(),
|
||||
Name: params.Name,
|
||||
Stdin: ctx.App.Reader,
|
||||
ColumnsFile: params.ColumnsFile,
|
||||
ColumnsFormat: params.ColumnsFormat,
|
||||
ExtendedOutput: params.ExtendedOutput,
|
||||
})
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func newBucketSchemaListCmd() *cli.Command {
|
||||
var params bucket_schema.ListParams
|
||||
return &cli.Command{
|
||||
Name: "list",
|
||||
Usage: "List schemas for a bucket",
|
||||
Before: withBucketSchemaClient(),
|
||||
Flags: append(
|
||||
commonFlags,
|
||||
append(
|
||||
getOrgBucketFlags(¶ms.OrgBucketParams),
|
||||
&cli.StringFlag{
|
||||
Name: "name",
|
||||
Usage: "Name of single measurement to find",
|
||||
Destination: ¶ms.Name,
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: "extended-output",
|
||||
Usage: "Print column information for each measurement",
|
||||
Aliases: []string{"x"},
|
||||
Destination: ¶ms.ExtendedOutput,
|
||||
},
|
||||
)...,
|
||||
),
|
||||
Action: func(ctx *cli.Context) error {
|
||||
return getBucketSchemaClient(ctx).List(ctx.Context, params)
|
||||
},
|
||||
}
|
||||
}
|
@ -198,6 +198,7 @@ var app = cli.App{
|
||||
newWriteCmd(),
|
||||
newBucketCmd(),
|
||||
newCompletionCmd(),
|
||||
newBucketSchemaCmd(),
|
||||
},
|
||||
}
|
||||
|
||||
|
36
cmd/influx/params.go
Normal file
36
cmd/influx/params.go
Normal file
@ -0,0 +1,36 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/influxdata/influx-cli/v2/internal"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
func getOrgBucketFlags(c *internal.OrgBucketParams) []cli.Flag {
|
||||
return []cli.Flag{
|
||||
&cli.GenericFlag{
|
||||
Name: "bucket-id",
|
||||
Usage: "The bucket ID, required if name isn't provided",
|
||||
Aliases: []string{"i"},
|
||||
Value: &c.BucketID,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "bucket",
|
||||
Usage: "The bucket name, org or org-id will be required by choosing this",
|
||||
Aliases: []string{"n"},
|
||||
Destination: &c.BucketName,
|
||||
},
|
||||
&cli.GenericFlag{
|
||||
Name: "org-id",
|
||||
Usage: "The ID of the organization",
|
||||
EnvVars: []string{"INFLUX_ORG_ID"},
|
||||
Value: &c.OrgID,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "org",
|
||||
Usage: "The name of the organization",
|
||||
Aliases: []string{"o"},
|
||||
EnvVars: []string{"INFLUX_ORG"},
|
||||
Destination: &c.OrgName,
|
||||
},
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user