-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Sabbir <[email protected]>
- Loading branch information
1 parent
f17f15a
commit 6ec9c9e
Showing
219 changed files
with
27,422 additions
and
146 deletions.
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
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 | ||
} |
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,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") | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.