Skip to content

Commit

Permalink
feat: Add bucket schema management commands (#52)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
stuartcarnie committed May 5, 2021
1 parent 4a1e622 commit 7eca7c0
Show file tree
Hide file tree
Showing 39 changed files with 4,014 additions and 11 deletions.
11 changes: 10 additions & 1 deletion cmd/influx/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -91,6 +94,12 @@ func newBucketCreateCmd() *cli.Command {
EnvVars: []string{"INFLUX_ORG"},
Destination: &params.OrgName,
},
&cli.GenericFlag{
Name: "schema-type",
Usage: "The schema type (implicit, explicit)",
DefaultText: "implicit",
Value: &params.SchemaType,
},
),
Action: func(ctx *cli.Context) error {
clients := getBucketsClient(ctx)
Expand Down
189 changes: 189 additions & 0 deletions cmd/influx/bucket_schema.go
Original file line number Diff line number Diff line change
@@ -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(&params.OrgBucketParams),
&cli.StringFlag{
Name: "name",
Usage: "Name of the measurement",
Destination: &params.Name,
},
&cli.StringFlag{
Name: "columns-file",
Usage: "A path referring to list of column definitions",
Destination: &params.ColumnsFile,
},
&cli.GenericFlag{
Name: "columns-format",
Usage: "The format of the columns file. \"auto\" will attempt to guess the format.",
DefaultText: "auto",
Value: &params.ColumnsFormat,
},
&cli.BoolFlag{
Name: "extended-output",
Usage: "Print column information for each measurement",
Aliases: []string{"x"},
Destination: &params.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(&params.OrgBucketParams),
&cli.GenericFlag{
Name: "id",
Usage: "ID of the measurement",
Value: &params.ID,
},
&cli.StringFlag{
Name: "name",
Usage: "Name of the measurement",
Destination: &params.Name,
},
&cli.StringFlag{
Name: "columns-file",
Usage: "A path referring to list of column definitions",
Destination: &params.ColumnsFile,
},
&cli.GenericFlag{
Name: "columns-format",
Usage: "The format of the columns file. \"auto\" will attempt to guess the format.",
DefaultText: "auto",
Value: &params.ColumnsFormat,
},
&cli.BoolFlag{
Name: "extended-output",
Usage: "Print column information for each measurement",
Aliases: []string{"x"},
Destination: &params.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(&params.OrgBucketParams),
&cli.StringFlag{
Name: "name",
Usage: "Name of single measurement to find",
Destination: &params.Name,
},
&cli.BoolFlag{
Name: "extended-output",
Usage: "Print column information for each measurement",
Aliases: []string{"x"},
Destination: &params.ExtendedOutput,
},
)...,
),
Action: func(ctx *cli.Context) error {
return getBucketSchemaClient(ctx).List(ctx.Context, params)
},
}
}
1 change: 1 addition & 0 deletions cmd/influx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ var app = cli.App{
newWriteCmd(),
newBucketCmd(),
newCompletionCmd(),
newBucketSchemaCmd(),
},
}

Expand Down
36 changes: 36 additions & 0 deletions cmd/influx/params.go
Original file line number Diff line number Diff line change
@@ -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,
},
}
}
2 changes: 1 addition & 1 deletion etc/generate-openapi.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ declare -r API_DIR="${ROOT_DIR}/internal/api"

declare -r GENERATED_PATTERN='^// Code generated .* DO NOT EDIT\.$'
declare -r GENERATOR_DOCKER_IMG=openapitools/openapi-generator-cli:v5.1.0
declare -r OPENAPI_COMMIT=e41d5e327a67e472a46cd6edfe673496e1b066dd
declare -r OPENAPI_COMMIT=af4e3efa496c8adcd2838145652fdd8d63a90702

# Clean up all the generated files in the target directory.
rm $(grep -Elr "${GENERATED_PATTERN}" "${API_DIR}")
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ go 1.16
require (
github.com/AlecAivazis/survey/v2 v2.2.9
github.com/BurntSushi/toml v0.3.1
github.com/MakeNowJust/heredoc/v2 v2.0.1
github.com/daixiang0/gci v0.2.8
github.com/fujiwara/shapeio v1.0.0
github.com/gocarina/gocsv v0.0.0-20210408192840-02d7211d929d
github.com/golang/mock v1.5.0
github.com/google/go-cmp v0.5.5
github.com/kr/pretty v0.1.0 // indirect
github.com/stretchr/testify v1.7.0
github.com/urfave/cli/v2 v2.3.0
Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/AlecAivazis/survey/v2 v2.2.9 h1:LWvJtUswz/W9/zVVXELrmlvdwWcKE60ZAw0FW
github.com/AlecAivazis/survey/v2 v2.2.9/go.mod h1:9DYvHgXtiXm6nCn+jXnOXLKbH+Yo9u8fAS/SduGdoPk=
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/MakeNowJust/heredoc/v2 v2.0.1 h1:rlCHh70XXXv7toz95ajQWOWQnN4WNLt0TdpZYIR/J6A=
github.com/MakeNowJust/heredoc/v2 v2.0.1/go.mod h1:6/2Abh5s+hc3g9nbWLe9ObDIOhaRrqsyY9MWy+4JdRM=
github.com/Netflix/go-expect v0.0.0-20180615182759-c93bf25de8e8 h1:xzYJEypr/85nBpB11F9br+3HUrpgb+fcm5iADzXXYEw=
github.com/Netflix/go-expect v0.0.0-20180615182759-c93bf25de8e8/go.mod h1:oX5x61PbNXchhh0oikYAH+4Pcfw5LKv21+Jnpr6r6Pc=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
Expand All @@ -15,6 +17,12 @@ github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/fujiwara/shapeio v1.0.0 h1:xG5D9oNqCSUUbryZ/jQV3cqe1v2suEjwPIcEg1gKM8M=
github.com/fujiwara/shapeio v1.0.0/go.mod h1:LmEmu6L/8jetyj1oewewFb7bZCNRwE7wLCUNzDLaLVA=
github.com/gocarina/gocsv v0.0.0-20210408192840-02d7211d929d h1:r3mStZSyjKhEcgbJ5xtv7kT5PZw/tDiFBTMgQx2qsXE=
github.com/gocarina/gocsv v0.0.0-20210408192840-02d7211d929d/go.mod h1:5YoVOkjYAQumqlV356Hj3xeYh4BdZuLE0/nRkf2NKkI=
github.com/golang/mock v1.5.0 h1:jlYHihg//f7RRwuPfptm04yp4s7O6Kw8EZiVYIGcH0g=
github.com/golang/mock v1.5.0/go.mod h1:CWnOUgYIOo4TcNZ0wHX3YZCqsaM1I1Jvs6v3mP3KVu8=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/hinshun/vt10x v0.0.0-20180616224451-1954e6464174 h1:WlZsjVhE8Af9IcZDGgJGQpNflI3+MJSBhsgT5PCtzBQ=
github.com/hinshun/vt10x v0.0.0-20180616224451-1954e6464174/go.mod h1:DqJ97dSdRW1W22yXSB90986pcOyQ7r45iio1KN2ez1A=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
Expand All @@ -39,6 +47,7 @@ github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.1/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
Expand Down Expand Up @@ -77,6 +86,7 @@ golang.org/x/tools v0.1.0 h1:po9/4sTYwZU9lPhi1tOrb4hCv3qrhiQ77LZfGa2OjwY=
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
Loading

0 comments on commit 7eca7c0

Please sign in to comment.