-
Notifications
You must be signed in to change notification settings - Fork 29
CF-1870 : Manage SQL Catalog databases through CLI #3287
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
0e30b78
96a12c9
33e3b89
36c006d
d86fb7e
2c09071
4e19e4f
f85825b
02dfaf2
3271eec
8f3a1e1
25d8693
c95ac55
021cf01
977c3c9
3086734
d4f7d85
030c098
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| package flink | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "gopkg.in/yaml.v3" | ||
|
|
||
| cmfsdk "github.com/confluentinc/cmf-sdk-go/v1" | ||
|
|
||
| pcmd "github.com/confluentinc/cli/v4/pkg/cmd" | ||
| "github.com/confluentinc/cli/v4/pkg/errors" | ||
| "github.com/confluentinc/cli/v4/pkg/output" | ||
| ) | ||
|
|
||
| type databaseOut struct { | ||
| CreationTime string `human:"Creation Time" serialized:"creation_time"` | ||
| Name string `human:"Name" serialized:"name"` | ||
| Catalog string `human:"Catalog" serialized:"catalog"` | ||
| } | ||
|
|
||
| func (c *command) newCatalogDatabaseCommand() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "database", | ||
| Short: "Manage Flink databases in Confluent Platform.", | ||
| Annotations: map[string]string{pcmd.RunRequirement: pcmd.RequireCloudLogout}, | ||
| } | ||
|
|
||
|
paras-negi-flink marked this conversation as resolved.
|
||
| cmd.AddCommand(c.newCatalogDatabaseCreateCommand()) | ||
| cmd.AddCommand(c.newCatalogDatabaseDeleteCommand()) | ||
| cmd.AddCommand(c.newCatalogDatabaseDescribeCommand()) | ||
| cmd.AddCommand(c.newCatalogDatabaseListCommand()) | ||
| cmd.AddCommand(c.newCatalogDatabaseUpdateCommand()) | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func printDatabaseOutput(cmd *cobra.Command, sdkDatabase cmfsdk.KafkaDatabase, catalogName string) error { | ||
| if output.GetFormat(cmd) == output.Human { | ||
| table := output.NewTable(cmd) | ||
| var creationTime string | ||
| if sdkDatabase.GetMetadata().CreationTimestamp != nil { | ||
| creationTime = *sdkDatabase.GetMetadata().CreationTimestamp | ||
| } | ||
| table.Add(&databaseOut{ | ||
| CreationTime: creationTime, | ||
| Name: sdkDatabase.GetMetadata().Name, | ||
| Catalog: catalogName, | ||
| }) | ||
|
Comment on lines
+48
to
+52
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will it be a concern if human mode has such a difference compared to json or yml mode through
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment is not responded yet, the concern here is, is it ok for customers to see different set of fields in json, yaml and human mode?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Intentional and matches every CMF on-prem command in - |
||
| return table.Print() | ||
| } | ||
|
|
||
| localDatabase := convertSdkDatabaseToLocalDatabase(sdkDatabase) | ||
| return output.SerializedOutput(cmd, localDatabase) | ||
| } | ||
|
|
||
| func readDatabaseResourceFile(resourceFilePath string) (cmfsdk.KafkaDatabase, error) { | ||
| data, err := os.ReadFile(resourceFilePath) | ||
| if err != nil { | ||
| return cmfsdk.KafkaDatabase{}, fmt.Errorf("failed to read file: %w", err) | ||
| } | ||
|
|
||
| var genericData map[string]interface{} | ||
| ext := filepath.Ext(resourceFilePath) | ||
| switch ext { | ||
| case ".json": | ||
| err = json.Unmarshal(data, &genericData) | ||
| case ".yaml", ".yml": | ||
| err = yaml.Unmarshal(data, &genericData) | ||
| default: | ||
| return cmfsdk.KafkaDatabase{}, errors.NewErrorWithSuggestions(fmt.Sprintf("unsupported file format: %s", ext), "Supported file formats are .json, .yaml, and .yml.") | ||
| } | ||
| if err != nil { | ||
| return cmfsdk.KafkaDatabase{}, fmt.Errorf("failed to parse input file: %w", err) | ||
| } | ||
|
|
||
| jsonBytes, err := json.Marshal(genericData) | ||
| if err != nil { | ||
| return cmfsdk.KafkaDatabase{}, fmt.Errorf("failed to marshal intermediate data: %w", err) | ||
| } | ||
|
|
||
| var sdkDatabase cmfsdk.KafkaDatabase | ||
| if err = json.Unmarshal(jsonBytes, &sdkDatabase); err != nil { | ||
| return cmfsdk.KafkaDatabase{}, fmt.Errorf("failed to bind data to KafkaDatabase model: %w", err) | ||
| } | ||
|
|
||
| return sdkDatabase, nil | ||
| } | ||
|
|
||
| func convertSdkDatabaseToLocalDatabase(sdkDatabase cmfsdk.KafkaDatabase) LocalKafkaDatabase { | ||
| return LocalKafkaDatabase{ | ||
| ApiVersion: sdkDatabase.ApiVersion, | ||
| Kind: sdkDatabase.Kind, | ||
| Metadata: LocalDatabaseMetadata{ | ||
| Name: sdkDatabase.Metadata.Name, | ||
| CreationTimestamp: sdkDatabase.Metadata.CreationTimestamp, | ||
| UpdateTimestamp: sdkDatabase.Metadata.UpdateTimestamp, | ||
| Uid: sdkDatabase.Metadata.Uid, | ||
| Labels: sdkDatabase.Metadata.Labels, | ||
| Annotations: sdkDatabase.Metadata.Annotations, | ||
| }, | ||
| Spec: LocalKafkaDatabaseSpec{ | ||
| KafkaCluster: LocalKafkaDatabaseSpecKafkaCluster{ | ||
| ConnectionConfig: sdkDatabase.Spec.KafkaCluster.ConnectionConfig, | ||
| ConnectionSecretId: sdkDatabase.Spec.KafkaCluster.ConnectionSecretId, | ||
| }, | ||
| DdlEnvironments: sdkDatabase.Spec.DdlEnvironments, | ||
| }, | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package flink | ||
|
|
||
| import ( | ||
| "github.com/spf13/cobra" | ||
|
|
||
| pcmd "github.com/confluentinc/cli/v4/pkg/cmd" | ||
| ) | ||
|
|
||
| func (c *command) newCatalogDatabaseCreateCommand() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "create <resourceFilePath>", | ||
| Short: "Create a Flink database.", | ||
|
channingdong marked this conversation as resolved.
|
||
| Long: "Create a Flink database in a catalog in Confluent Platform.", | ||
| Args: cobra.ExactArgs(1), | ||
| RunE: c.catalogDatabaseCreate, | ||
| } | ||
|
|
||
| cmd.Flags().String("catalog", "", "Name of the catalog.") | ||
| cobra.CheckErr(cmd.MarkFlagRequired("catalog")) | ||
| addCmfFlagSet(cmd) | ||
| pcmd.AddOutputFlag(cmd) | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func (c *command) catalogDatabaseCreate(cmd *cobra.Command, args []string) error { | ||
| resourceFilePath := args[0] | ||
|
|
||
| catalogName, err := cmd.Flags().GetString("catalog") | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| client, err := c.GetCmfClient(cmd) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| sdkDatabase, err := readDatabaseResourceFile(resourceFilePath) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| sdkOutputDatabase, err := client.CreateDatabase(c.createContext(), catalogName, sdkDatabase) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return printDatabaseOutput(cmd, sdkOutputDatabase, catalogName) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package flink | ||
|
|
||
| import ( | ||
| "github.com/spf13/cobra" | ||
|
|
||
| pcmd "github.com/confluentinc/cli/v4/pkg/cmd" | ||
| "github.com/confluentinc/cli/v4/pkg/deletion" | ||
| "github.com/confluentinc/cli/v4/pkg/errors" | ||
| "github.com/confluentinc/cli/v4/pkg/resource" | ||
| ) | ||
|
|
||
| func (c *command) newCatalogDatabaseDeleteCommand() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "delete <name>", | ||
| Short: "Delete a Flink database in Confluent Platform.", | ||
| Args: cobra.ExactArgs(1), | ||
| RunE: c.catalogDatabaseDelete, | ||
| } | ||
|
|
||
| cmd.Flags().String("catalog", "", "Name of the catalog.") | ||
| cobra.CheckErr(cmd.MarkFlagRequired("catalog")) | ||
| addCmfFlagSet(cmd) | ||
| pcmd.AddForceFlag(cmd) | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func (c *command) catalogDatabaseDelete(cmd *cobra.Command, args []string) error { | ||
| catalogName, err := cmd.Flags().GetString("catalog") | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| client, err := c.GetCmfClient(cmd) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| existenceFunc := func(name string) bool { | ||
| _, err := client.DescribeDatabase(c.createContext(), catalogName, name) | ||
| return err == nil | ||
| } | ||
|
|
||
| if err := deletion.ValidateAndConfirm(cmd, args, existenceFunc, resource.FlinkDatabase); err != nil { | ||
| // We are validating only the existence of the resources (there is no prefix validation). | ||
| // Thus, we can add some extra context for the error. | ||
| suggestions := "List available Flink databases with `confluent flink catalog database list`." | ||
|
paras-negi-flink marked this conversation as resolved.
channingdong marked this conversation as resolved.
|
||
| suggestions += "\nCheck that CMF is running and accessible." | ||
| return errors.NewErrorWithSuggestions(err.Error(), suggestions) | ||
| } | ||
|
paras-negi-flink marked this conversation as resolved.
|
||
|
|
||
| deleteFunc := func(name string) error { | ||
| return client.DeleteDatabase(c.createContext(), catalogName, name) | ||
| } | ||
|
|
||
| _, err = deletion.Delete(cmd, args, deleteFunc, resource.FlinkDatabase) | ||
| return err | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package flink | ||
|
|
||
| import ( | ||
| "github.com/spf13/cobra" | ||
|
|
||
| pcmd "github.com/confluentinc/cli/v4/pkg/cmd" | ||
| ) | ||
|
|
||
| func (c *command) newCatalogDatabaseDescribeCommand() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "describe <name>", | ||
| Short: "Describe a Flink database in Confluent Platform.", | ||
| Args: cobra.ExactArgs(1), | ||
| RunE: c.catalogDatabaseDescribe, | ||
| } | ||
|
|
||
| cmd.Flags().String("catalog", "", "Name of the catalog.") | ||
| cobra.CheckErr(cmd.MarkFlagRequired("catalog")) | ||
| addCmfFlagSet(cmd) | ||
| pcmd.AddOutputFlag(cmd) | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func (c *command) catalogDatabaseDescribe(cmd *cobra.Command, args []string) error { | ||
| name := args[0] | ||
|
|
||
| catalogName, err := cmd.Flags().GetString("catalog") | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| client, err := c.GetCmfClient(cmd) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| sdkOutputDatabase, err := client.DescribeDatabase(c.createContext(), catalogName, name) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return printDatabaseOutput(cmd, sdkOutputDatabase, catalogName) | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.