-
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
8f47085
commit e821040
Showing
22 changed files
with
484 additions
and
96 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 |
---|---|---|
|
@@ -2,12 +2,10 @@ package cassandra | |
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"k8s.io/klog/v2" | ||
health "kmodules.xyz/client-go/tools/healthchecker" | ||
"time" | ||
|
||
"github.com/gocql/gocql" | ||
"k8s.io/klog/v2" | ||
) | ||
|
||
type Client struct { | ||
|
@@ -16,78 +14,73 @@ type Client struct { | |
|
||
// 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() | ||
return c.Query(`CREATE KEYSPACE IF NOT EXISTS kubedb_keyspace 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 | ||
return c.Query(`CREATE TABLE IF NOT EXISTS kubedb_keyspace.healthcheck_table ( | ||
name TEXT PRIMARY KEY, | ||
product 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() | ||
} | ||
// UpdateData updates a record in the table | ||
func (c *Client) UpdateData(name string, product string) error { | ||
currentTime := time.Now().Format("2006-01-02 15:04:05") | ||
updatedProduct := fmt.Sprintf("%s - %s", product, currentTime) | ||
|
||
func (c *Client) DeleteUser(id gocql.UUID) error { | ||
return c.Query(`DELETE FROM mykeyspace.users WHERE id = ?`, id).Exec() | ||
return c.Query(`UPDATE kubedb_keyspace.healthcheck_table SET product = ? where name = ? `, | ||
updatedProduct, name).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 | ||
// queries a Data by ID | ||
func (c *Client) QueryData(name string) error { | ||
var product string | ||
|
||
iter := c.Query(`SELECT name, age, email FROM mykeyspace.users WHERE id = ?`, id).Iter() | ||
if iter.Scan(&name, &age, &email) { | ||
iter := c.Query(`SELECT product FROM kubedb_keyspace.healthcheck_table WHERE name = ?`, name).Iter() | ||
if iter.Scan(&product) { | ||
if err := iter.Close(); err != nil { | ||
return "", 0, "", fmt.Errorf("unable to query data: %v", err) | ||
return fmt.Errorf("unable to query data: %v", err) | ||
} | ||
return name, age, email, nil | ||
return nil | ||
} | ||
return "", 0, "", fmt.Errorf("no data found") | ||
return fmt.Errorf("no data found") | ||
} | ||
|
||
func (c *Client) CheckDbReadWrite() error { | ||
if err := c.CreateKeyspace(); err != nil { | ||
log.Fatal("Unable to create keyspace:", err) | ||
klog.Error("Unable to create keyspace:", err) | ||
return err | ||
} | ||
if err := c.CreateTable(); err != nil { | ||
log.Fatal("Unable to create table:", err) | ||
klog.Error("Unable to create table:", err) | ||
return err | ||
} | ||
id := gocql.TimeUUID() | ||
if err := c.InsertUser(id, "John Doe", 30, "[email protected]"); err != nil { | ||
log.Fatal("Unable to insert data:", err) | ||
if err := c.UpdateData("Appscode", "KubeDB"); err != nil { | ||
klog.Error("Unable to update data:", err) | ||
return err | ||
} | ||
|
||
name, age, email, err := c.QueryUser(id) | ||
err := c.QueryData("Appscode") | ||
if err != nil { | ||
klog.Error("Unable to query data:", err) | ||
return err | ||
} | ||
klog.Infoln("DB Read Write Successful") | ||
fmt.Printf("Name: %s, Age: %d, Email: %s\n", name, age, email) | ||
err = c.DeleteUser(id) | ||
return err | ||
return nil | ||
} | ||
|
||
func (c *Client) PingCassandra() error { | ||
query := c.Query("SELECT now() FROM system.local") | ||
if err := query.Exec(); err != nil { | ||
klog.Error("Unable to ping cassandra:", err) | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (c *Client) CloseCassandraClient(hcf *health.HealthCard) { | ||
func (c *Client) CloseCassandraClient() { | ||
if c != nil { | ||
c.Close() | ||
} | ||
hcf.ClientClosed() | ||
} |
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
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
33 changes: 31 additions & 2 deletions
33
vendor/kubedb.dev/apimachinery/apis/catalog/v1alpha1/openapi_generated.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
17 changes: 17 additions & 0 deletions
17
vendor/kubedb.dev/apimachinery/apis/catalog/v1alpha1/zz_generated.deepcopy.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.