-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add bucket schema management commands
- Loading branch information
1 parent
571b1de
commit 178c072
Showing
30 changed files
with
2,720 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -196,6 +196,7 @@ var app = cli.App{ | |
newSetupCmd(), | ||
newWriteCmd(), | ||
newBucketCmd(), | ||
newMeasurementSchemaCmd(), | ||
}, | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,247 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
"github.com/influxdata/influx-cli/v2/internal" | ||
"github.com/influxdata/influx-cli/v2/internal/cmd/measurement_schema" | ||
"github.com/influxdata/influx-cli/v2/internal/schema" | ||
"github.com/influxdata/influx-cli/v2/pkg/cli/middleware" | ||
"github.com/influxdata/influx-cli/v2/pkg/influxid" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
func withMSClient() cli.BeforeFunc { | ||
return middleware.WithBeforeFns( | ||
withCli(), | ||
withApi(), | ||
func(ctx *cli.Context) error { | ||
c := getCLI(ctx) | ||
client := getAPI(ctx) | ||
ctx.App.Metadata["measurement_schema"] = measurement_schema.Client{ | ||
BucketApi: client.BucketsApi, | ||
BucketSchemasApi: client.BucketSchemasApi, | ||
CLI: c, | ||
} | ||
return nil | ||
}) | ||
} | ||
|
||
func getMSClient(ctx *cli.Context) measurement_schema.Client { | ||
i, ok := ctx.App.Metadata["measurement_schema"].(measurement_schema.Client) | ||
if !ok { | ||
panic("missing measurement schema client") | ||
} | ||
return i | ||
} | ||
|
||
func newMeasurementSchemaCmd() *cli.Command { | ||
return &cli.Command{ | ||
Name: "bucket-schema", | ||
Usage: "Bucket schema management commands", | ||
Subcommands: []*cli.Command{ | ||
newMeasurementSchemaCreateCmd(), | ||
newMeasurementSchemaUpdateCmd(), | ||
newMeasurementSchemaListCmd(), | ||
}, | ||
} | ||
} | ||
|
||
func newMeasurementSchemaCreateCmd() *cli.Command { | ||
var params struct { | ||
internal.OrgBucketParams | ||
Name string | ||
ColumnsFile string | ||
ColumnsFormat schema.Format | ||
ExtendedOutput bool | ||
} | ||
return &cli.Command{ | ||
Name: "create", | ||
Usage: "Create a measurement schema for a bucket", | ||
Before: withMSClient(), | ||
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 { | ||
var ( | ||
r io.Reader | ||
name string | ||
) | ||
|
||
arg := ctx.Args().First() | ||
if arg == "-" || params.ColumnsFile == "" { | ||
// stdio | ||
r = ctx.App.Reader | ||
name = "stdio" | ||
} else { | ||
data, err := os.ReadFile(params.ColumnsFile) | ||
if err != nil { | ||
return fmt.Errorf("unable to read file %q: %w", params.ColumnsFile, err) | ||
} | ||
r = bytes.NewReader(data) | ||
name = params.ColumnsFile | ||
} | ||
|
||
reader, err := params.ColumnsFormat.FormatFn(name) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
cols, err := reader(r) | ||
if err != nil { | ||
return fmt.Errorf("unable to decode file %q: %w", name, err) | ||
} | ||
|
||
return getMSClient(ctx).Create(ctx.Context, measurement_schema.CreateParams{ | ||
OrgBucketParams: params.OrgBucketParams, | ||
Name: params.Name, | ||
Columns: cols, | ||
ExtendedOutput: params.ExtendedOutput, | ||
}) | ||
}, | ||
} | ||
} | ||
|
||
func newMeasurementSchemaUpdateCmd() *cli.Command { | ||
var params struct { | ||
internal.OrgBucketParams | ||
ID influxid.ID | ||
Name string | ||
ColumnsFile string | ||
ColumnsFormat schema.Format | ||
ExtendedOutput bool | ||
} | ||
return &cli.Command{ | ||
Name: "update", | ||
Usage: "Update a measurement schema for a bucket", | ||
Before: withMSClient(), | ||
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 { | ||
var ( | ||
r io.Reader | ||
name string | ||
) | ||
|
||
arg := ctx.Args().First() | ||
if arg == "-" || params.ColumnsFile == "" { | ||
// stdio | ||
r = ctx.App.Reader | ||
name = "stdio" | ||
} else { | ||
data, err := os.ReadFile(params.ColumnsFile) | ||
if err != nil { | ||
return fmt.Errorf("unable to read file %q: %w", params.ColumnsFile, err) | ||
} | ||
r = bytes.NewReader(data) | ||
name = params.ColumnsFile | ||
} | ||
|
||
reader, err := params.ColumnsFormat.FormatFn(name) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
cols, err := reader(r) | ||
if err != nil { | ||
return fmt.Errorf("unable to decode file %q: %w", name, err) | ||
} | ||
|
||
return getMSClient(ctx).Update(ctx.Context, measurement_schema.UpdateParams{ | ||
OrgBucketParams: params.OrgBucketParams, | ||
ID: params.ID.String(), | ||
Name: params.Name, | ||
Columns: cols, | ||
ExtendedOutput: params.ExtendedOutput, | ||
}) | ||
}, | ||
} | ||
} | ||
|
||
func newMeasurementSchemaListCmd() *cli.Command { | ||
var params measurement_schema.ListParams | ||
return &cli.Command{ | ||
Name: "list", | ||
Usage: "List schemas for a bucket", | ||
Before: withMSClient(), | ||
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 getMSClient(ctx).List(ctx.Context, params) | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
name,type,data_type | ||
time,timestamp, | ||
host,tag, | ||
usage_user,field,float |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.