Skip to content

Commit

Permalink
Add MongoDB data cli (#716)
Browse files Browse the repository at this point in the history
Signed-off-by: Arnob Kumar Saha <[email protected]>
  • Loading branch information
ArnobKumarSaha authored Aug 18, 2023
1 parent 95ef134 commit 6adaa37
Show file tree
Hide file tree
Showing 11 changed files with 483 additions and 43 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module kubedb.dev/cli
go 1.18

require (
github.com/Masterminds/semver/v3 v3.2.1
github.com/fatih/camelcase v1.0.0
github.com/go-sql-driver/mysql v1.6.0
github.com/spf13/cobra v1.7.0
Expand All @@ -25,7 +26,7 @@ require (
kmodules.xyz/monitoring-agent-api v0.25.1
kubedb.dev/apimachinery v0.34.1-0.20230814034200-4ce21bc51a16
kubedb.dev/db-client-go v0.0.8-0.20230818101900-6ddd035705ef
stash.appscode.dev/apimachinery v0.30.1-0.20230814025143-fcb8a9106d3c
stash.appscode.dev/apimachinery v0.31.0
)

require github.com/onsi/gomega v1.24.2 // indirect
Expand All @@ -40,7 +41,6 @@ require (
github.com/Azure/go-autorest/logger v0.2.1 // indirect
github.com/Azure/go-autorest/tracing v0.6.0 // indirect
github.com/MakeNowJust/heredoc v1.0.0 // indirect
github.com/Masterminds/semver/v3 v3.2.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cert-manager/cert-manager v1.9.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1389,5 +1389,5 @@ sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=
sigs.k8s.io/yaml v1.3.0 h1:a2VclLzOGrwOHDiV8EfBGhvjHvP46CtW5j6POvhYGGo=
sigs.k8s.io/yaml v1.3.0/go.mod h1:GeOyir5tyXNByN85N/dRIT9es5UQNerPYEKK56eTBm8=
stash.appscode.dev/apimachinery v0.30.1-0.20230814025143-fcb8a9106d3c h1:/3plffjueo5ffL+XhHAtluJ9TfP+K9elbY1tyIxNeKY=
stash.appscode.dev/apimachinery v0.30.1-0.20230814025143-fcb8a9106d3c/go.mod h1:IDbssRbYLSnMwZAQOGX4Vam+hl43FofP8BuXMLXVaPQ=
stash.appscode.dev/apimachinery v0.31.0 h1:AXsuz/4K1ltW6oCVsqOS0EoYksVZTn60LXbK7V4IL90=
stash.appscode.dev/apimachinery v0.31.0/go.mod h1:IDbssRbYLSnMwZAQOGX4Vam+hl43FofP8BuXMLXVaPQ=
24 changes: 12 additions & 12 deletions pkg/cmds/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@ func NewCmdData(f cmdutil.Factory) *cobra.Command {
DisableAutoGenTag: true,
}

// kubectl dba data insert -n demo rd-demo --rows=100
// kubectl dba data verify -n demo rd-demo --rows=100

cmd.AddCommand(InsertDataCMD(f))
cmd.AddCommand(VerifyDataCMD(f))
cmd.AddCommand(DropDataCMD(f))
Expand Down Expand Up @@ -103,11 +100,12 @@ func InsertDataCMD(f cmdutil.Factory) *cobra.Command {
DisableAutoGenTag: true,
}

cmd.AddCommand(data.InsertRedisDataCMD(f))
cmd.AddCommand(data.InsertElasticsearchDataCMD(f))
cmd.AddCommand(data.InsertPostgresDataCMD(f))
cmd.AddCommand(data.InsertMySQLDataCMD(f))
cmd.AddCommand(data.InsertMariaDBDataCMD(f))
cmd.AddCommand(data.InsertMongoDBDataCMD(f))
cmd.AddCommand(data.InsertMySQLDataCMD(f))
cmd.AddCommand(data.InsertPostgresDataCMD(f))
cmd.AddCommand(data.InsertRedisDataCMD(f))

return cmd
}
Expand Down Expand Up @@ -143,11 +141,12 @@ func VerifyDataCMD(f cmdutil.Factory) *cobra.Command {
DisableAutoGenTag: true,
}

cmd.AddCommand(data.VerifyRedisDataCMD(f))
cmd.AddCommand(data.VerifyElasticsearchDataCMD(f))
cmd.AddCommand(data.VerifyPostgresDataCMD(f))
cmd.AddCommand(data.VerifyMySQLDataCMD(f))
cmd.AddCommand(data.VerifyMariaDBDataCMD(f))
cmd.AddCommand(data.VerifyMongoDBDataCMD(f))
cmd.AddCommand(data.VerifyMySQLDataCMD(f))
cmd.AddCommand(data.VerifyPostgresDataCMD(f))
cmd.AddCommand(data.VerifyRedisDataCMD(f))

return cmd
}
Expand Down Expand Up @@ -183,11 +182,12 @@ func DropDataCMD(f cmdutil.Factory) *cobra.Command {
DisableAutoGenTag: true,
}

cmd.AddCommand(data.DropRedisDataCMD(f))
cmd.AddCommand(data.DropElasticsearchDataCMD(f))
cmd.AddCommand(data.DropPostgresDataCMD(f))
cmd.AddCommand(data.DropMySQLDataCMD(f))
cmd.AddCommand(data.DropMariaDBDataCMD(f))
cmd.AddCommand(data.DropMongoDBDataCMD(f))
cmd.AddCommand(data.DropMySQLDataCMD(f))
cmd.AddCommand(data.DropPostgresDataCMD(f))
cmd.AddCommand(data.DropRedisDataCMD(f))

return cmd
}
27 changes: 27 additions & 0 deletions pkg/data/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Copyright AppsCode Inc. and Contributors
Licensed under the AppsCode Community License 1.0.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://github.com/appscode/licenses/raw/1.0.0/AppsCode-Community-1.0.0.md
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package data

const (
caFile = "/tmp/ca.crt"
certFile = "/tmp/client.crt"
keyFile = "/tmp/client.key"
)

const (
actor = "kubedb-cli"
)
17 changes: 12 additions & 5 deletions pkg/data/elasticsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import (
"k8s.io/klog/v2"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"kmodules.xyz/client-go/tools/portforward"
//"sigs.k8s.io/controller-runtime/pkg/manager"
)

const (
Expand Down Expand Up @@ -91,14 +90,18 @@ func InsertElasticsearchDataCMD(f cmdutil.Factory) *cobra.Command {
log.Fatalln(err)
}

if rows <= 0 {
log.Fatal("rows need to be greater than 0")
}

err = opts.insertDataInDatabase(rows)
if err != nil {
log.Fatal(err)
}
},
}

esInsertCmd.Flags().IntVarP(&rows, "rows", "r", 10, "rows in ")
esInsertCmd.Flags().IntVarP(&rows, "rows", "r", 100, "number of rows to insert")

return esInsertCmd
}
Expand All @@ -115,7 +118,7 @@ func VerifyElasticsearchDataCMD(f cmdutil.Factory) *cobra.Command {
"es",
},
Short: "Verify rows in a elasticsearch database",
Long: `Use this cmd to verify data in a elasticsearc object`,
Long: `Use this cmd to verify data in a elasticsearch object`,
Example: `kubectl dba verify -n demo es es-quickstart --rows 1000`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
Expand Down Expand Up @@ -144,14 +147,18 @@ func VerifyElasticsearchDataCMD(f cmdutil.Factory) *cobra.Command {
log.Fatalln(err)
}

if rows <= 0 {
log.Fatal("rows need to be greater than 0")
}

err = opts.verifyElasticsearchData(rows)
if err != nil {
log.Fatal(err)
}
},
}

esVerifyCmd.Flags().IntVarP(&rows, "rows", "r", 10, "rows in ")
esVerifyCmd.Flags().IntVarP(&rows, "rows", "r", 100, "number of rows to verify")

return esVerifyCmd
}
Expand Down Expand Up @@ -269,7 +276,7 @@ func (opts *elasticsearchOpts) insertDataInDatabase(rows int) error {
"name": name,
}
if err := opts.esClient.PutData(idx, id, body); err != nil {
fmt.Printf("Failed to put data in the index %s for elasticsearch %s/%s", idx, opts.db.Namespace, opts.db.Name)
fmt.Printf("Failed to insert data in the index %s for elasticsearch %s/%s", idx, opts.db.Namespace, opts.db.Name)
return err
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/data/mariadb.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func InsertMariaDBDataCMD(f cmdutil.Factory) *cobra.Command {
},
}

mdInsertCmd.Flags().IntVarP(&rows, "rows", "r", 10, "rows in ")
mdInsertCmd.Flags().IntVarP(&rows, "rows", "r", 100, "number of rows to insert")

return mdInsertCmd
}
Expand Down Expand Up @@ -173,7 +173,7 @@ func VerifyMariaDBDataCMD(f cmdutil.Factory) *cobra.Command {
},
}

mdVerifyCmd.Flags().IntVarP(&rows, "rows", "r", 10, "rows in ")
mdVerifyCmd.Flags().IntVarP(&rows, "rows", "r", 100, "number of rows to verify")

return mdVerifyCmd
}
Expand Down
Loading

0 comments on commit 6adaa37

Please sign in to comment.