Skip to content

Commit

Permalink
Add db-client-go for Cassandra
Browse files Browse the repository at this point in the history
Signed-off-by: Sabbir <[email protected]>
  • Loading branch information
sabbir-hossain70 committed Aug 14, 2024
1 parent f17f15a commit 6ec9c9e
Show file tree
Hide file tree
Showing 219 changed files with 27,422 additions and 146 deletions.
9 changes: 9 additions & 0 deletions cassandra/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package cassandra

import (
"github.com/gocql/gocql"
)

type Client struct {
*gocql.Session
}
107 changes: 107 additions & 0 deletions cassandra/kubedb_client_builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package cassandra

import (
"context"
"fmt"
"github.com/gocql/gocql"
api "kubedb.dev/apimachinery/apis/kubedb/v1alpha2"
"sigs.k8s.io/controller-runtime/pkg/client"
"strconv"
)

type KubeDBClientBuilder struct {
kc client.Client
db *api.Cassandra
url string
podName string
port *int
ctx context.Context
}

func NewKubeDBClientBuilder(kc client.Client, db *api.Cassandra) *KubeDBClientBuilder {
return &KubeDBClientBuilder{
kc: kc,
db: db,
}
}

func (o *KubeDBClientBuilder) WithURL(url string) *KubeDBClientBuilder {
o.url = url
return o
}

func (o *KubeDBClientBuilder) WithPort(port *int) *KubeDBClientBuilder {
o.port = port
return o
}

func (o *KubeDBClientBuilder) WithContext(ctx context.Context) *KubeDBClientBuilder {
o.ctx = ctx
return o
}
func (o *KubeDBClientBuilder) GetCassandraClient() (*Client, error) {
host := "127.0.0.1"
port := "9042"
username := "cluster1-superuser"
password := "X-XLZ0jxouJbAbg0KAsPW0a1E0GofwK12cWOA9WyHYCjdede2UBImQ"

cluster := gocql.NewCluster(host)
p, err := strconv.Atoi(port)
if err != nil {
return nil, fmt.Errorf("invalid port: %v", err)
}
cluster.Port = p
cluster.Keyspace = "system"
//cluster.Consistency = gocql.Any //ANY ConsistencyLevel is only supported for writes
cluster.Consistency = gocql.Quorum
cluster.Authenticator = gocql.PasswordAuthenticator{
Username: username,
Password: password,
}
session, err := cluster.CreateSession()
if err != nil {
return nil, fmt.Errorf("unable to connect to Cassandra cluster: %v", err)
}

return &Client{session}, nil
}

// CreateKeyspace creates a keyspace
func (c *Client) CreateKeyspace() error {
return c.Query(`CREATE KEYSPACE IF NOT EXISTS mykeyspace WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '2'}`).Exec()
}

// CreateTable creates a table
func (c *Client) CreateTable() error {
return c.Query(`CREATE TABLE IF NOT EXISTS mykeyspace.users (
id UUID PRIMARY KEY,
name TEXT,
age INT,
email TEXT
)`).Exec()
}

// InsertUser inserts a user into the table
func (c *Client) InsertUser(id gocql.UUID, name string, age int, email string) error {
return c.Query(`INSERT INTO mykeyspace.users (id, name, age, email) VALUES (?, ?, ?, ?)`,
id, name, age, email).Exec()
}
func (c *Client) DeleteUser(id gocql.UUID) error {
return c.Query(`DELETE FROM mykeyspace.users WHERE id = ?`, id).Exec()
}

// QueryUser queries a user by ID
func (c *Client) QueryUser(id gocql.UUID) (string, int, string, error) {
var name string
var age int
var email string

iter := c.Query(`SELECT name, age, email FROM mykeyspace.users WHERE id = ?`, id).Iter()
if iter.Scan(&name, &age, &email) {
if err := iter.Close(); err != nil {
return "", 0, "", fmt.Errorf("unable to query data: %v", err)
}
return name, age, email, nil
}
return "", 0, "", fmt.Errorf("no data found")
}
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/go-logr/logr v1.4.2
github.com/go-resty/resty/v2 v2.11.0
github.com/go-sql-driver/mysql v1.8.1
github.com/gocql/gocql v1.6.0
github.com/grafadruid/go-druid v0.0.6
github.com/hashicorp/go-retryablehttp v0.7.7
github.com/lib/pq v1.10.7
Expand All @@ -32,7 +33,7 @@ require (
k8s.io/klog/v2 v2.130.1
kmodules.xyz/client-go v0.30.9
kmodules.xyz/custom-resources v0.30.0
kubedb.dev/apimachinery v0.47.0-rc.2
kubedb.dev/apimachinery v0.47.0-rc.2.0.20240814122107-b1472e3500d3
sigs.k8s.io/controller-runtime v0.18.4
xorm.io/xorm v1.3.6
)
Expand Down Expand Up @@ -73,6 +74,7 @@ require (
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
Expand Down
15 changes: 12 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@ github.com/aws/aws-sdk-go-v2/service/sts v1.19.0/go.mod h1:BgQOMsg8av8jset59jely
github.com/aws/smithy-go v1.13.5/go.mod h1:Tg+OJXh4MB2R/uN61Ko2f6hTZwB/ZYGOtib8J3gBHzA=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bitly/go-hostpool v0.0.0-20171023180738-a3a6125de932 h1:mXoPYz/Ul5HYEDvkta6I8/rnYM5gSdSV2tJ6XbZuEtY=
github.com/bitly/go-hostpool v0.0.0-20171023180738-a3a6125de932/go.mod h1:NOuUCSz6Q9T7+igc/hlvDOUdtWKryOrtFyIVABv/p7k=
github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM=
github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ=
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 h1:DDGfHa7BWjL4YnC6+E63dPcxHo2sUxDIu8g3QgEJdRY=
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4=
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
Expand Down Expand Up @@ -145,6 +149,8 @@ github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZ
github.com/goccy/go-json v0.8.1/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/goccy/go-json v0.9.11 h1:/pAaQDLHEoCq/5FFmSKBswWmK6H0e8g4159Kc/X/nqk=
github.com/goccy/go-json v0.9.11/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/gocql/gocql v1.6.0 h1:IdFdOTbnpbd0pDhl4REKQDM+Q0SzKXQ1Yh+YZZ8T/qU=
github.com/gocql/gocql v1.6.0/go.mod h1:3gM2c4D3AnkISwBxGnMMsS8Oy4y2lhbPRsH4xnJrHG8=
github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
Expand All @@ -171,6 +177,7 @@ github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/gnostic-models v0.6.8 h1:yo/ABAfM5IMRsS1VnXjTBvUb61tFIHozhlYvRgGre9I=
Expand Down Expand Up @@ -203,6 +210,8 @@ github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+
github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
github.com/grafadruid/go-druid v0.0.6 h1:Nt9jQrhrtHi1BJICN9aDJgYDmBmc10pJYpQiuwAsxa4=
github.com/grafadruid/go-druid v0.0.6/go.mod h1:KY3a6MrVMKkXgMTwBS9Nrhm1E8OWyR4gd0WzUi8d/zM=
github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed h1:5upAirOpQc1Q53c0bnx2ufif5kANL7bfZWcc6VJWJd8=
github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed/go.mod h1:tMWxXQ9wFIaZeTI9F+hmhFiGpFmhOHzyShyFUhRm0H4=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
Expand Down Expand Up @@ -793,9 +802,9 @@ kmodules.xyz/monitoring-agent-api v0.29.0 h1:gpFl6OZrlMLb/ySMHdREI9EwGtnJ91oZBn9
kmodules.xyz/monitoring-agent-api v0.29.0/go.mod h1:iNbvaMTgVFOI5q2LJtGK91j4Dmjv4ZRiRdasGmWLKQI=
kmodules.xyz/offshoot-api v0.30.0 h1:dq9F93pu4Q8rL9oTcCk+vGGy8vpS7RNt0GSwx7Bvhec=
kmodules.xyz/offshoot-api v0.30.0/go.mod h1:o9VoA3ImZMDBp3lpLb8+kc2d/KBxioRwCpaKDfLIyDw=
kubedb.dev/apimachinery v0.47.0-rc.2 h1:pdtbBiLM0VQw8HoJ3Z2tp4PZX5y66CIYBRCIn7inADI=
kubedb.dev/apimachinery v0.47.0-rc.2/go.mod h1:XkzWrijuH4skCU2ru+Ye9O0KSWQ7Nn2mRjP3+F3StLk=
kubeops.dev/petset v0.0.6 h1:0IbvxD9fadZfH+3iMZWzN6ZHsO0vX458JlioamwyPKQ=
kubedb.dev/apimachinery v0.47.0-rc.2.0.20240814122107-b1472e3500d3 h1:x+ep22DMGmkSg2UdraSnt9pVVUjHqW9TuPwPw35QI+w=
kubedb.dev/apimachinery v0.47.0-rc.2.0.20240814122107-b1472e3500d3/go.mod h1:XkzWrijuH4skCU2ru+Ye9O0KSWQ7Nn2mRjP3+F3StLk=
kubeops.dev/petset v0.0.6 h1:pmDrb29zrG1TLMV3mEi18VTP7hKcgDbiP1+MPXc5M+M=
kubeops.dev/petset v0.0.6/go.mod h1:A15vh0r979NsvL65DTIZKWsa/NoX9VapHBAEw1ZsdYI=
lukechampine.com/uint128 v1.1.1/go.mod h1:c4eWIwlEGaxC/+H1VguhU4PHXNWDCDMUlWdIWl2j1gk=
lukechampine.com/uint128 v1.2.0 h1:mBi/5l91vocEN8otkC5bDLhi2KdCticRiwbdB0O+rjI=
Expand Down
5 changes: 5 additions & 0 deletions vendor/github.com/gocql/gocql/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

143 changes: 143 additions & 0 deletions vendor/github.com/gocql/gocql/AUTHORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6ec9c9e

Please sign in to comment.